blob: 198e09ff611e48cecce9ae3795f0a7c8a63c2c27 [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
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030022#include <inttypes.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050023#include <sys/utsname.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050032#include <stdarg.h>
33#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090034#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050035
Masami Hiramatsu31facc52010-03-16 18:05:30 -040036#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050037#include "event.h"
Arnaldo Carvalho de Melo40f3b2d2019-01-22 11:24:34 -020038#include "namespaces.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050039#include "strlist.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030040#include "strfilter.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050041#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050042#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050043#include "color.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010044#include "map.h"
Arnaldo Carvalho de Melo41f30912019-01-27 13:44:29 +010045#include "map_groups.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040046#include "symbol.h"
47#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020048#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030049#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040051#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090052#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053053#include "session.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030054#include "string2.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050055
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030056#include "sane_ctype.h"
57
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050058#define PERFPROBE_GROUP "probe"
59
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040060bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090061struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040062
Masami Hiramatsu146a1432010-04-12 13:17:42 -040063#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050064
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090065int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050066{
67 int ret;
68 va_list ap;
69 va_start(ap, format);
70 ret = vsnprintf(str, size, format, ap);
71 va_end(ap);
72 if (ret >= (int)size)
73 ret = -E2BIG;
74 return ret;
75}
76
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000077static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040078
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090079/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090080int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040081{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040082 int ret;
83
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090085 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090086 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040087 if (ret < 0) {
88 pr_debug("Failed to init symbol map.\n");
89 goto out;
90 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040091
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000092 if (host_machine || user_only) /* already initialized */
93 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030094
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000095 if (symbol_conf.vmlinux_name)
96 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
97
98 host_machine = machine__new_host();
99 if (!host_machine) {
100 pr_debug("machine__new_host() failed.\n");
101 symbol__exit();
102 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900103 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400104out:
105 if (ret < 0)
106 pr_warning("Failed to init vmlinux path.\n");
107 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400108}
109
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900110void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000111{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300112 machine__delete(host_machine);
113 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000114 symbol__exit();
115}
116
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000117static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
118{
119 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
120 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300121 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000122
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300123 if (map__load(map) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000124 return NULL;
125
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300126 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000127 if (!kmap)
128 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000129 return kmap->ref_reloc_sym;
130}
131
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900132static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
133 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000134{
135 struct ref_reloc_sym *reloc_sym;
136 struct symbol *sym;
137 struct map *map;
138
139 /* ref_reloc_sym is just a label. Need a special fix*/
140 reloc_sym = kernel_get_ref_reloc_sym();
141 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900142 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000143 else {
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -0300144 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900145 if (!sym)
146 return -ENOENT;
147 *addr = map->unmap_ip(map, sym->start) -
148 ((reloc) ? 0 : map->reloc) -
149 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000150 }
151 return 0;
152}
153
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900154static struct map *kernel_get_module_map(const char *module)
155{
Arnaldo Carvalho de Melo68a74182018-04-24 17:06:25 -0300156 struct maps *maps = machine__kernel_maps(host_machine);
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300157 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900158
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900159 /* A file path -- this is an offline module */
160 if (module && strchr(module, '/'))
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900161 return dso__new_map(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900162
Adrian Huntereaeffeb2019-03-04 15:13:21 +0200163 if (!module) {
164 pos = machine__kernel_map(host_machine);
165 return map__get(pos);
166 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900167
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300168 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300169 /* short_name is "[module]" */
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900170 if (strncmp(pos->dso->short_name + 1, module,
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300171 pos->dso->short_name_len - 2) == 0 &&
172 module[pos->dso->short_name_len - 2] == '\0') {
Arnaldo Carvalho de Melof622df52018-05-24 11:17:34 -0300173 return map__get(pos);
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900174 }
175 }
176 return NULL;
177}
178
Krister Johansen544abd42017-07-05 18:48:10 -0700179struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900180{
181 /* Init maps of given executable or kernel */
Krister Johansen544abd42017-07-05 18:48:10 -0700182 if (user) {
183 struct map *map;
184
185 map = dso__new_map(target);
186 if (map && map->dso)
187 map->dso->nsinfo = nsinfo__get(nsi);
188 return map;
189 } else {
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900190 return kernel_get_module_map(target);
Krister Johansen544abd42017-07-05 18:48:10 -0700191 }
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900192}
193
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000194static int convert_exec_to_group(const char *exec, char **result)
195{
196 char *ptr1, *ptr2, *exec_copy;
197 char buf[64];
198 int ret;
199
200 exec_copy = strdup(exec);
201 if (!exec_copy)
202 return -ENOMEM;
203
204 ptr1 = basename(exec_copy);
205 if (!ptr1) {
206 ret = -EINVAL;
207 goto out;
208 }
209
Colin Ian Kingead1a572016-10-03 11:34:31 +0100210 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
Masami Hiramatsu35726d32016-09-24 00:35:16 +0900211 if (!isalnum(*ptr2) && *ptr2 != '_') {
212 *ptr2 = '\0';
213 break;
214 }
215 }
216
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000217 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
218 if (ret < 0)
219 goto out;
220
221 *result = strdup(buf);
222 ret = *result ? 0 : -ENOMEM;
223
224out:
225 free(exec_copy);
226 return ret;
227}
228
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900229static void clear_perf_probe_point(struct perf_probe_point *pp)
230{
231 free(pp->file);
232 free(pp->function);
233 free(pp->lazy_line);
234}
235
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000236static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
237{
238 int i;
239
240 for (i = 0; i < ntevs; i++)
241 clear_probe_trace_event(tevs + i);
242}
243
Masami Hiramatsub0312202015-06-16 20:50:55 +0900244static bool kprobe_blacklist__listed(unsigned long address);
245static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
246{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900247 u64 etext_addr = 0;
248 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000249
Masami Hiramatsub0312202015-06-16 20:50:55 +0900250 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900251 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
252 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000253
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900254 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900255 pr_warning("%s is out of .text, skip it.\n", symbol);
256 else if (kprobe_blacklist__listed(address))
257 pr_warning("%s is blacklisted function, skip it.\n", symbol);
258 else
259 return false;
260
261 return true;
262}
263
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530264/*
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530265 * @module can be module name of module file path. In case of path,
266 * inspect elf and find out what is actual module name.
267 * Caller has to free mod_name after using it.
268 */
269static char *find_module_name(const char *module)
270{
271 int fd;
272 Elf *elf;
273 GElf_Ehdr ehdr;
274 GElf_Shdr shdr;
275 Elf_Data *data;
276 Elf_Scn *sec;
277 char *mod_name = NULL;
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900278 int name_offset;
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530279
280 fd = open(module, O_RDONLY);
281 if (fd < 0)
282 return NULL;
283
284 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
285 if (elf == NULL)
286 goto elf_err;
287
288 if (gelf_getehdr(elf, &ehdr) == NULL)
289 goto ret_err;
290
291 sec = elf_section_by_name(elf, &ehdr, &shdr,
292 ".gnu.linkonce.this_module", NULL);
293 if (!sec)
294 goto ret_err;
295
296 data = elf_getdata(sec, NULL);
297 if (!data || !data->d_buf)
298 goto ret_err;
299
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900300 /*
301 * NOTE:
302 * '.gnu.linkonce.this_module' section of kernel module elf directly
303 * maps to 'struct module' from linux/module.h. This section contains
304 * actual module name which will be used by kernel after loading it.
305 * But, we cannot use 'struct module' here since linux/module.h is not
306 * exposed to user-space. Offset of 'name' has remained same from long
307 * time, so hardcoding it here.
308 */
309 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
310 name_offset = 12;
311 else /* expect ELFCLASS64 by default */
312 name_offset = 24;
313
314 mod_name = strdup((char *)data->d_buf + name_offset);
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530315
316ret_err:
317 elf_end(elf);
318elf_err:
319 close(fd);
320 return mod_name;
321}
322
Ingo Molnar89fe8082013-09-30 12:07:11 +0200323#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000324
325static int kernel_get_module_dso(const char *module, struct dso **pdso)
326{
327 struct dso *dso;
328 struct map *map;
329 const char *vmlinux_name;
330 int ret = 0;
331
332 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300333 char module_name[128];
334
335 snprintf(module_name, sizeof(module_name), "[%s]", module);
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300336 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300337 if (map) {
338 dso = map->dso;
339 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000340 }
341 pr_debug("Failed to find module %s.\n", module);
342 return -ENOENT;
343 }
344
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300345 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000346 dso = map->dso;
347
348 vmlinux_name = symbol_conf.vmlinux_name;
349 dso->load_errno = 0;
350 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300351 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000352 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300353 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000354found:
355 *pdso = dso;
356 return ret;
357}
358
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900359/*
360 * Some binaries like glibc have special symbols which are on the symbol
361 * table, but not in the debuginfo. If we can find the address of the
362 * symbol from map, we can translate the address back to the probe point.
363 */
364static int find_alternative_probe_point(struct debuginfo *dinfo,
365 struct perf_probe_point *pp,
366 struct perf_probe_point *result,
Krister Johansen544abd42017-07-05 18:48:10 -0700367 const char *target, struct nsinfo *nsi,
368 bool uprobes)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900369{
370 struct map *map = NULL;
371 struct symbol *sym;
372 u64 address = 0;
373 int ret = -ENOENT;
374
375 /* This can work only for function-name based one */
376 if (!pp->function || pp->file)
377 return -ENOTSUP;
378
Krister Johansen544abd42017-07-05 18:48:10 -0700379 map = get_target_map(target, nsi, uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900380 if (!map)
381 return -EINVAL;
382
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900385 if (uprobes)
386 address = sym->start;
387 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900388 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900389 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900390 }
391 if (!address) {
392 ret = -ENOENT;
393 goto out;
394 }
Wang Nanf6c15622015-04-08 02:14:34 +0000395 pr_debug("Symbol %s address found : %" PRIx64 "\n",
396 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900397
398 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
399 result);
400 if (ret <= 0)
401 ret = (!ret) ? -ENOENT : ret;
402 else {
403 result->offset += pp->offset;
404 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800405 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900406 ret = 0;
407 }
408
409out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900410 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900411 return ret;
412
413}
414
415static int get_alternative_probe_event(struct debuginfo *dinfo,
416 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900417 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900418{
419 int ret;
420
421 memcpy(tmp, &pev->point, sizeof(*tmp));
422 memset(&pev->point, 0, sizeof(pev->point));
Krister Johansen544abd42017-07-05 18:48:10 -0700423 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
424 pev->nsi, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900425 if (ret < 0)
426 memcpy(&pev->point, tmp, sizeof(*tmp));
427
428 return ret;
429}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000430
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900431static int get_alternative_line_range(struct debuginfo *dinfo,
432 struct line_range *lr,
433 const char *target, bool user)
434{
David Ahern6d4a4892015-03-11 10:36:20 -0400435 struct perf_probe_point pp = { .function = lr->function,
436 .file = lr->file,
437 .line = lr->start };
438 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900439 int ret, len = 0;
440
David Ahern6d4a4892015-03-11 10:36:20 -0400441 memset(&result, 0, sizeof(result));
442
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900443 if (lr->end != INT_MAX)
444 len = lr->end - lr->start;
445 ret = find_alternative_probe_point(dinfo, &pp, &result,
Krister Johansen544abd42017-07-05 18:48:10 -0700446 target, NULL, user);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900447 if (!ret) {
448 lr->function = result.function;
449 lr->file = result.file;
450 lr->start = result.line;
451 if (lr->end != INT_MAX)
452 lr->end = lr->start + len;
453 clear_perf_probe_point(&pp);
454 }
455 return ret;
456}
457
Masami Hiramatsuff741782011-06-27 16:27:39 +0900458/* Open new debuginfo of given module */
Krister Johansen544abd42017-07-05 18:48:10 -0700459static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
460 bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900461{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000462 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900463 char reason[STRERR_BUFSIZE];
464 struct debuginfo *ret = NULL;
465 struct dso *dso = NULL;
Krister Johansen544abd42017-07-05 18:48:10 -0700466 struct nscookie nsc;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900467 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900468
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000469 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900470 err = kernel_get_module_dso(module, &dso);
471 if (err < 0) {
472 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300473 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900474 strcpy(reason, "(unknown)");
475 } else
476 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Arnaldo Carvalho de Melo4d6101f2019-02-28 11:22:45 -0300477 if (!silent) {
478 if (module)
479 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
480 else
481 pr_err("Failed to find the path for the kernel: %s\n", reason);
482 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900483 return NULL;
484 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900485 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900486 }
Krister Johansen544abd42017-07-05 18:48:10 -0700487 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000488 ret = debuginfo__new(path);
489 if (!ret && !silent) {
490 pr_warning("The %s file has no debug information.\n", path);
491 if (!module || !strtailcmp(path, ".ko"))
492 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
493 else
494 pr_warning("Rebuild with -g, ");
495 pr_warning("or install an appropriate debuginfo package.\n");
496 }
Krister Johansen544abd42017-07-05 18:48:10 -0700497 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000498 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400499}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300500
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900501/* For caching the last debuginfo */
502static struct debuginfo *debuginfo_cache;
503static char *debuginfo_cache_path;
504
505static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
506{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900507 const char *path = module;
508
509 /* If the module is NULL, it should be the kernel. */
510 if (!module)
511 path = "kernel";
512
513 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900514 goto out;
515
516 /* Copy module path */
517 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900518 debuginfo_cache_path = strdup(path);
519 if (!debuginfo_cache_path) {
520 debuginfo__delete(debuginfo_cache);
521 debuginfo_cache = NULL;
522 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900523 }
524
Krister Johansen544abd42017-07-05 18:48:10 -0700525 debuginfo_cache = open_debuginfo(module, NULL, silent);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900526 if (!debuginfo_cache)
527 zfree(&debuginfo_cache_path);
528out:
529 return debuginfo_cache;
530}
531
532static void debuginfo_cache__exit(void)
533{
534 debuginfo__delete(debuginfo_cache);
535 debuginfo_cache = NULL;
536 zfree(&debuginfo_cache_path);
537}
538
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000539
Krister Johansen544abd42017-07-05 18:48:10 -0700540static int get_text_start_address(const char *exec, unsigned long *address,
541 struct nsinfo *nsi)
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000542{
543 Elf *elf;
544 GElf_Ehdr ehdr;
545 GElf_Shdr shdr;
546 int fd, ret = -ENOENT;
Krister Johansen544abd42017-07-05 18:48:10 -0700547 struct nscookie nsc;
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000548
Krister Johansen544abd42017-07-05 18:48:10 -0700549 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000550 fd = open(exec, O_RDONLY);
Krister Johansen544abd42017-07-05 18:48:10 -0700551 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000552 if (fd < 0)
553 return -errno;
554
555 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900556 if (elf == NULL) {
557 ret = -EINVAL;
558 goto out_close;
559 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000560
561 if (gelf_getehdr(elf, &ehdr) == NULL)
562 goto out;
563
564 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
565 goto out;
566
567 *address = shdr.sh_addr - shdr.sh_offset;
568 ret = 0;
569out:
570 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900571out_close:
572 close(fd);
573
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000574 return ret;
575}
576
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000577/*
578 * Convert trace point to probe point with debuginfo
579 */
580static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
581 struct perf_probe_point *pp,
582 bool is_kprobe)
583{
584 struct debuginfo *dinfo = NULL;
585 unsigned long stext = 0;
586 u64 addr = tp->address;
587 int ret = -ENOENT;
588
589 /* convert the address to dwarf address */
590 if (!is_kprobe) {
591 if (!addr) {
592 ret = -EINVAL;
593 goto error;
594 }
Krister Johansen544abd42017-07-05 18:48:10 -0700595 ret = get_text_start_address(tp->module, &stext, NULL);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000596 if (ret < 0)
597 goto error;
598 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000599 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900600 /* If the module is given, this returns relative address */
601 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
602 false, !!tp->module);
603 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000604 goto error;
605 addr += tp->offset;
606 }
607
608 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
609 tp->module ? : "kernel");
610
Namhyung Kimbb963e12017-02-17 17:17:38 +0900611 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900612 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000613 ret = debuginfo__find_probe_point(dinfo,
614 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900615 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000616 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000617
618 if (ret > 0) {
619 pp->retprobe = tp->retprobe;
620 return 0;
621 }
622error:
623 pr_debug("Failed to find corresponding probes from debuginfo.\n");
624 return ret ? : -ENOENT;
625}
626
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900627/* Adjust symbol name and address */
628static int post_process_probe_trace_point(struct probe_trace_point *tp,
629 struct map *map, unsigned long offs)
630{
631 struct symbol *sym;
Björn Töpel7598f8b2017-06-21 18:41:34 +0200632 u64 addr = tp->address - offs;
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900633
634 sym = map__find_symbol(map, addr);
635 if (!sym)
636 return -ENOENT;
637
638 if (strcmp(sym->name, tp->symbol)) {
639 /* If we have no realname, use symbol for it */
640 if (!tp->realname)
641 tp->realname = tp->symbol;
642 else
643 free(tp->symbol);
644 tp->symbol = strdup(sym->name);
645 if (!tp->symbol)
646 return -ENOMEM;
647 }
648 tp->offset = addr - sym->start;
649 tp->address -= offs;
650
651 return 0;
652}
653
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900654/*
655 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
656 * and generate new symbols with suffixes such as .constprop.N or .isra.N
657 * etc. Since those symbols are not recorded in DWARF, we have to find
658 * correct generated symbols from offline ELF binary.
659 * For online kernel or uprobes we don't need this because those are
660 * rebased on _text, or already a section relative address.
661 */
662static int
663post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
664 int ntevs, const char *pathname)
665{
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900666 struct map *map;
667 unsigned long stext = 0;
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900668 int i, ret = 0;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900669
670 /* Prepare a map for offline binary */
671 map = dso__new_map(pathname);
Krister Johansen544abd42017-07-05 18:48:10 -0700672 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900673 pr_warning("Failed to get ELF symbols for %s\n", pathname);
674 return -EINVAL;
675 }
676
677 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900678 ret = post_process_probe_trace_point(&tevs[i].point,
679 map, stext);
680 if (ret < 0)
681 break;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900682 }
683 map__put(map);
684
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900685 return ret;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900686}
687
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000688static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
Krister Johansen544abd42017-07-05 18:48:10 -0700689 int ntevs, const char *exec,
690 struct nsinfo *nsi)
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000691{
692 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000693 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000694
695 if (!exec)
696 return 0;
697
Krister Johansen544abd42017-07-05 18:48:10 -0700698 ret = get_text_start_address(exec, &stext, nsi);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000699 if (ret < 0)
700 return ret;
701
702 for (i = 0; i < ntevs && ret >= 0; i++) {
Ingo Molnaradba1632018-12-03 11:22:00 +0100703 /* point.address is the address of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000704 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000705 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000706 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000707 ret = -ENOMEM;
708 break;
709 }
710 tevs[i].uprobes = true;
711 }
712
713 return ret;
714}
715
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900716static int
717post_process_module_probe_trace_events(struct probe_trace_event *tevs,
718 int ntevs, const char *module,
719 struct debuginfo *dinfo)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900720{
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900721 Dwarf_Addr text_offs = 0;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900722 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530723 char *mod_name = NULL;
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900724 struct map *map;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900725
726 if (!module)
727 return 0;
728
Krister Johansen544abd42017-07-05 18:48:10 -0700729 map = get_target_map(module, NULL, false);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900730 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
731 pr_warning("Failed to get ELF symbols for %s\n", module);
732 return -EINVAL;
733 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900734
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900735 mod_name = find_module_name(module);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900736 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900737 ret = post_process_probe_trace_point(&tevs[i].point,
738 map, (unsigned long)text_offs);
739 if (ret < 0)
740 break;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530741 tevs[i].point.module =
742 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900743 if (!tevs[i].point.module) {
744 ret = -ENOMEM;
745 break;
746 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900747 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900748
Ravi Bangoria63a29612016-04-26 19:55:41 +0530749 free(mod_name);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900750 map__put(map);
751
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900752 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900753}
754
Ravi Bangoriad8204562016-08-09 01:23:24 -0500755static int
756post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
757 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000758{
759 struct ref_reloc_sym *reloc_sym;
760 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900761 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000762
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900763 /* Skip post process if the target is an offline kernel */
764 if (symbol_conf.ignore_vmlinux_buildid)
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900765 return post_process_offline_probe_trace_events(tevs, ntevs,
766 symbol_conf.vmlinux_name);
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900767
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000768 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000769 if (!reloc_sym) {
770 pr_warning("Relocated base symbol is not found!\n");
771 return -EINVAL;
772 }
773
774 for (i = 0; i < ntevs; i++) {
Naveen N. Rao7ab31d92017-03-08 13:56:09 +0530775 if (!tevs[i].point.address)
776 continue;
777 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
Masami Hiramatsub0312202015-06-16 20:50:55 +0900778 continue;
779 /* If we found a wrong one, mark it by NULL symbol */
780 if (kprobe_warn_out_range(tevs[i].point.symbol,
781 tevs[i].point.address)) {
782 tmp = NULL;
783 skipped++;
784 } else {
785 tmp = strdup(reloc_sym->name);
786 if (!tmp)
787 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000788 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900789 /* If we have no realname, use symbol for it */
790 if (!tevs[i].point.realname)
791 tevs[i].point.realname = tevs[i].point.symbol;
792 else
793 free(tevs[i].point.symbol);
794 tevs[i].point.symbol = tmp;
795 tevs[i].point.offset = tevs[i].point.address -
796 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000797 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900798 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000799}
800
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500801void __weak
802arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
803 int ntevs __maybe_unused)
804{
805}
806
Ravi Bangoriad8204562016-08-09 01:23:24 -0500807/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500808static int post_process_probe_trace_events(struct perf_probe_event *pev,
809 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500810 int ntevs, const char *module,
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900811 bool uprobe, struct debuginfo *dinfo)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500812{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500813 int ret;
814
Ravi Bangoriad8204562016-08-09 01:23:24 -0500815 if (uprobe)
Krister Johansen544abd42017-07-05 18:48:10 -0700816 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
817 pev->nsi);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500818 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500819 /* Currently ref_reloc_sym based probe is not for drivers */
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900820 ret = post_process_module_probe_trace_events(tevs, ntevs,
821 module, dinfo);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500822 else
823 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500824
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500825 if (ret >= 0)
826 arch__post_process_probe_trace_events(pev, ntevs);
827
828 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500829}
830
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530832static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900833 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300834{
835 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900836 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530837 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900838 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300839
Krister Johansen544abd42017-07-05 18:48:10 -0700840 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900841 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000842 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900843 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900844 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300845 return 0;
846 }
847
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000848 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900849 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900850 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900851
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900852 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900853 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900854 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900855 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900856 /*
857 * Write back to the original probe_event for
858 * setting appropriate (user given) event name
859 */
860 clear_perf_probe_point(&pev->point);
861 memcpy(&pev->point, &tmp, sizeof(tmp));
862 }
863 }
864
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000866 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500867 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900868 pev->target, pev->uprobes, dinfo);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900869 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900870 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000871 clear_probe_trace_events(*tevs, ntevs);
872 zfree(tevs);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900873 ntevs = 0;
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000874 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400875 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300876
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900877 debuginfo__delete(dinfo);
878
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400879 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900880 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400881 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900882 return -ENOENT;
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900883 } else if (ntevs < 0) {
884 /* Error path : ntevs < 0 */
885 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000886 if (ntevs == -EBADF)
887 pr_warning("Warning: No dwarf info found in the vmlinux - "
888 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400889 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900890 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400891 return 0;
892 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300893 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400894 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300895}
896
897#define LINEBUF_SIZE 256
898#define NR_ADDITIONAL_LINES 2
899
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100900static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300901{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000902 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100903 const char *color = show_num ? "" : PERF_COLOR_BLUE;
904 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300905
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100906 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300907 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
908 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100909 if (skip)
910 continue;
911 if (!prefix) {
912 prefix = show_num ? "%7d " : " ";
913 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300914 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100915 color_fprintf(stdout, color, "%s", buf);
916
917 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400918
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100919 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300920error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100921 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000922 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300923 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100924 return -1;
925 }
926 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300927}
928
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100929static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
930{
931 int rv = __show_one_line(fp, l, skip, show_num);
932 if (rv == 0) {
933 pr_warning("Source file is shorter than expected.\n");
934 rv = -1;
935 }
936 return rv;
937}
938
939#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
940#define show_one_line(f,l) _show_one_line(f,l,false,false)
941#define skip_one_line(f,l) _show_one_line(f,l,true,false)
942#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
943
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300944/*
945 * Show line-range always requires debuginfo to find source file and
946 * line number.
947 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900948static int __show_line_range(struct line_range *lr, const char *module,
949 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300950{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400951 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000952 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900953 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300954 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900955 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900956 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000957 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300958
959 /* Search a line range */
Krister Johansen544abd42017-07-05 18:48:10 -0700960 dinfo = open_debuginfo(module, NULL, false);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000961 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900962 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400963
Masami Hiramatsuff741782011-06-27 16:27:39 +0900964 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900965 if (!ret) { /* Not found, retry with an alternative */
966 ret = get_alternative_line_range(dinfo, lr, module, user);
967 if (!ret)
968 ret = debuginfo__find_line_range(dinfo, lr);
969 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900970 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000971 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400972 pr_warning("Specified source line is not found.\n");
973 return -ENOENT;
974 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000975 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400976 return ret;
977 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300978
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900979 /* Convert source file path */
980 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900981 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800982
983 /* Free old path when new path is assigned */
984 if (tmp != lr->path)
985 free(tmp);
986
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900987 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000988 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900989 return ret;
990 }
991
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300992 setup_pager();
993
994 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900995 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300996 lr->start - lr->offset);
997 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100998 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300999
1000 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001001 if (fp == NULL) {
1002 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001003 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001004 return -errno;
1005 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001006 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001007 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001008 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001009 if (ret < 0)
1010 goto end;
1011 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001012
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -03001013 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001014 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001015 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001016 if (ret < 0)
1017 goto end;
1018 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001019 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001020 if (ret < 0)
1021 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001022 }
1023
1024 if (lr->end == INT_MAX)
1025 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001026 while (l <= lr->end) {
1027 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1028 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001029 break;
1030 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001031end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001032 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001033 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001034}
1035
Krister Johansen544abd42017-07-05 18:48:10 -07001036int show_line_range(struct line_range *lr, const char *module,
1037 struct nsinfo *nsi, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001038{
1039 int ret;
Krister Johansen544abd42017-07-05 18:48:10 -07001040 struct nscookie nsc;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001041
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001042 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001043 if (ret < 0)
1044 return ret;
Krister Johansen544abd42017-07-05 18:48:10 -07001045 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +09001046 ret = __show_line_range(lr, module, user);
Krister Johansen544abd42017-07-05 18:48:10 -07001047 nsinfo__mountns_exit(&nsc);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001048 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001049
1050 return ret;
1051}
1052
Masami Hiramatsuff741782011-06-27 16:27:39 +09001053static int show_available_vars_at(struct debuginfo *dinfo,
1054 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001055 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001056{
1057 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001058 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001059 struct str_node *node;
1060 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001061 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001062 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001063
1064 buf = synthesize_perf_probe_point(&pev->point);
1065 if (!buf)
1066 return -EINVAL;
1067 pr_debug("Searching variables at %s\n", buf);
1068
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001069 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001070 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +09001071 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001072 if (!ret) {
1073 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001074 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001075 /* Release the old probe_point */
1076 clear_perf_probe_point(&tmp);
1077 }
1078 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001079 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001080 if (ret == 0 || ret == -ENOENT) {
1081 pr_err("Failed to find the address of %s\n", buf);
1082 ret = -ENOENT;
1083 } else
1084 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001085 goto end;
1086 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001087
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001088 /* Some variables are found */
1089 fprintf(stdout, "Available variables at %s\n", buf);
1090 for (i = 0; i < ret; i++) {
1091 vl = &vls[i];
1092 /*
1093 * A probe point might be converted to
1094 * several trace points.
1095 */
1096 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1097 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001098 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001099 nvars = 0;
1100 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001101 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001102 var = strchr(node->s, '\t') + 1;
1103 if (strfilter__compare(_filter, var)) {
1104 fprintf(stdout, "\t\t%s\n", node->s);
1105 nvars++;
1106 }
1107 }
1108 strlist__delete(vl->vars);
1109 }
1110 if (nvars == 0)
1111 fprintf(stdout, "\t\t(No matched variables)\n");
1112 }
1113 free(vls);
1114end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001115 free(buf);
1116 return ret;
1117}
1118
1119/* Show available variables on given probe point */
1120int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001121 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001122{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001123 int i, ret = 0;
1124 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001125
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001126 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001127 if (ret < 0)
1128 return ret;
1129
Krister Johansen544abd42017-07-05 18:48:10 -07001130 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001131 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001132 ret = -ENOENT;
1133 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001134 }
1135
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001136 setup_pager();
1137
Masami Hiramatsuff741782011-06-27 16:27:39 +09001138 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001139 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001140
1141 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001142out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001143 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001144 return ret;
1145}
1146
Ingo Molnar89fe8082013-09-30 12:07:11 +02001147#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001148
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001149static void debuginfo_cache__exit(void)
1150{
1151}
1152
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001153static int
1154find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1155 struct perf_probe_point *pp __maybe_unused,
1156 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001157{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001158 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001159}
1160
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301161static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001162 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001163{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001164 if (perf_probe_event_need_dwarf(pev)) {
1165 pr_warning("Debuginfo-analysis is not supported.\n");
1166 return -ENOSYS;
1167 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301168
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001169 return 0;
1170}
1171
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001172int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001173 const char *module __maybe_unused,
Krister Johansen544abd42017-07-05 18:48:10 -07001174 struct nsinfo *nsi __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001175 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001176{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001177 pr_warning("Debuginfo-analysis is not supported.\n");
1178 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001179}
1180
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001181int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001182 int npevs __maybe_unused,
1183 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001184{
1185 pr_warning("Debuginfo-analysis is not supported.\n");
1186 return -ENOSYS;
1187}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001188#endif
1189
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001190void line_range__clear(struct line_range *lr)
1191{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001192 free(lr->function);
1193 free(lr->file);
1194 free(lr->path);
1195 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001196 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001197 memset(lr, 0, sizeof(*lr));
1198}
1199
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001200int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001201{
1202 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001203 lr->line_list = intlist__new(NULL);
1204 if (!lr->line_list)
1205 return -ENOMEM;
1206 else
1207 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001208}
1209
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001210static int parse_line_num(char **ptr, int *val, const char *what)
1211{
1212 const char *start = *ptr;
1213
1214 errno = 0;
1215 *val = strtol(*ptr, ptr, 0);
1216 if (errno || *ptr == start) {
1217 semantic_error("'%s' is not a valid number.\n", what);
1218 return -EINVAL;
1219 }
1220 return 0;
1221}
1222
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001223/* Check the name is good for event, group or function */
1224static bool is_c_func_name(const char *name)
1225{
1226 if (!isalpha(*name) && *name != '_')
1227 return false;
1228 while (*++name != '\0') {
1229 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1230 return false;
1231 }
1232 return true;
1233}
1234
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001235/*
1236 * Stuff 'lr' according to the line range described by 'arg'.
1237 * The line range syntax is described by:
1238 *
1239 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001240 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001241 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001243{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001244 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001245 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001246
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001247 if (!name)
1248 return -ENOMEM;
1249
1250 lr->start = 0;
1251 lr->end = INT_MAX;
1252
1253 range = strchr(name, ':');
1254 if (range) {
1255 *range++ = '\0';
1256
1257 err = parse_line_num(&range, &lr->start, "start line");
1258 if (err)
1259 goto err;
1260
1261 if (*range == '+' || *range == '-') {
1262 const char c = *range++;
1263
1264 err = parse_line_num(&range, &lr->end, "end line");
1265 if (err)
1266 goto err;
1267
1268 if (c == '+') {
1269 lr->end += lr->start;
1270 /*
1271 * Adjust the number of lines here.
1272 * If the number of lines == 1, the
1273 * the end of line should be equal to
1274 * the start of line.
1275 */
1276 lr->end--;
1277 }
1278 }
1279
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001280 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001281
1282 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001283 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001284 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001285 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001286 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001287 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001288 if (*range != '\0') {
1289 semantic_error("Tailing with invalid str '%s'.\n", range);
1290 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001292 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001293
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001294 file = strchr(name, '@');
1295 if (file) {
1296 *file = '\0';
1297 lr->file = strdup(++file);
1298 if (lr->file == NULL) {
1299 err = -ENOMEM;
1300 goto err;
1301 }
1302 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001303 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001304 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001305 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001306 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001307 else { /* Invalid name */
1308 semantic_error("'%s' is not a valid function name.\n", name);
1309 err = -EINVAL;
1310 goto err;
1311 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312
1313 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001314err:
1315 free(name);
1316 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001317}
1318
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001319static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1320{
1321 char *ptr;
1322
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001323 ptr = strpbrk_esc(*arg, ":");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001324 if (ptr) {
1325 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001326 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001327 goto ng_name;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001328 pev->group = strdup_esc(*arg);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001329 if (!pev->group)
1330 return -ENOMEM;
1331 *arg = ptr + 1;
1332 } else
1333 pev->group = NULL;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001334
1335 pev->event = strdup_esc(*arg);
1336 if (pev->event == NULL)
1337 return -ENOMEM;
1338
1339 if (!pev->sdt && !is_c_func_name(pev->event)) {
1340 zfree(&pev->event);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001341ng_name:
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001342 zfree(&pev->group);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001343 semantic_error("%s is bad for event name -it must "
1344 "follow C symbol-naming rule.\n", *arg);
1345 return -EINVAL;
1346 }
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001347 return 0;
1348}
1349
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001350/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001351static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001352{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001353 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001354 char *ptr, *tmp;
1355 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301356 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001357 int ret;
1358
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001359 /*
1360 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001361 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1362 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001363 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001364 */
Wang Nane59d29e2015-04-28 08:46:09 +00001365 if (!arg)
1366 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001367
Ravi Bangoriaaf9100a2017-03-14 20:36:52 +05301368 if (is_sdt_event(arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001369 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001370 if (arg[0] == '%')
1371 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001372 }
1373
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001374 ptr = strpbrk_esc(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001375 if (pev->sdt) {
1376 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001377 if (*ptr != '@') {
1378 semantic_error("%s must be an SDT name.\n",
1379 arg);
1380 return -EINVAL;
1381 }
1382 /* This must be a target file name or build id */
1383 tmp = build_id_cache__complement(ptr + 1);
1384 if (tmp) {
1385 pev->target = build_id_cache__origname(tmp);
1386 free(tmp);
1387 } else
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001388 pev->target = strdup_esc(ptr + 1);
Masami Hiramatsua5981802016-07-12 19:05:37 +09001389 if (!pev->target)
1390 return -ENOMEM;
1391 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001392 }
1393 ret = parse_perf_probe_event_name(&arg, pev);
1394 if (ret == 0) {
1395 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1396 ret = -errno;
1397 }
1398 return ret;
1399 }
1400
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001401 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001402 *ptr = '\0';
1403 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001404 ret = parse_perf_probe_event_name(&arg, pev);
1405 if (ret < 0)
1406 return ret;
1407
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001408 arg = tmp;
1409 }
1410
Naveen N. Rao3099c022015-04-28 17:35:34 +05301411 /*
1412 * Check arg is function or file name and copy it.
1413 *
1414 * We consider arg to be a file spec if and only if it satisfies
1415 * all of the below criteria::
1416 * - it does not include any of "+@%",
1417 * - it includes one of ":;", and
1418 * - it has a period '.' in the name.
1419 *
1420 * Otherwise, we consider arg to be a function specification.
1421 */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001422 if (!strpbrk_esc(arg, "+@%")) {
1423 ptr = strpbrk_esc(arg, ";:");
Naveen N. Rao3099c022015-04-28 17:35:34 +05301424 /* This is a file spec if it includes a '.' before ; or : */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001425 if (ptr && memchr(arg, '.', ptr - arg))
Naveen N. Rao3099c022015-04-28 17:35:34 +05301426 file_spec = true;
1427 }
1428
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001429 ptr = strpbrk_esc(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001430 if (ptr) {
1431 nc = *ptr;
1432 *ptr++ = '\0';
1433 }
1434
Wang Nan6c6e0242015-08-26 10:57:44 +00001435 if (arg[0] == '\0')
1436 tmp = NULL;
1437 else {
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001438 tmp = strdup_esc(arg);
Wang Nan6c6e0242015-08-26 10:57:44 +00001439 if (tmp == NULL)
1440 return -ENOMEM;
1441 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001442
Naveen N. Rao3099c022015-04-28 17:35:34 +05301443 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001444 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001445 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001446 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001447
Wang Nanda15bd92015-08-26 10:57:45 +00001448 /*
1449 * Keep pp->function even if this is absolute address,
1450 * so it can mark whether abs_address is valid.
1451 * Which make 'perf probe lib.bin 0x0' possible.
1452 *
1453 * Note that checking length of tmp is not needed
1454 * because when we access tmp[1] we know tmp[0] is '0',
1455 * so tmp[1] should always valid (but could be '\0').
1456 */
1457 if (tmp && !strncmp(tmp, "0x", 2)) {
1458 pp->abs_address = strtoul(pp->function, &tmp, 0);
1459 if (*tmp != '\0') {
1460 semantic_error("Invalid absolute address.\n");
1461 return -EINVAL;
1462 }
1463 }
1464 }
1465
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001466 /* Parse other options */
1467 while (ptr) {
1468 arg = ptr;
1469 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001470 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001471 pp->lazy_line = strdup(arg); /* let leave escapes */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001472 if (pp->lazy_line == NULL)
1473 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001474 break;
1475 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001476 ptr = strpbrk_esc(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001477 if (ptr) {
1478 nc = *ptr;
1479 *ptr++ = '\0';
1480 }
1481 switch (c) {
1482 case ':': /* Line number */
1483 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001484 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001485 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001486 " in line number.\n");
1487 return -EINVAL;
1488 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001489 break;
1490 case '+': /* Byte offset from a symbol */
1491 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001492 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001493 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001494 " in offset.\n");
1495 return -EINVAL;
1496 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001497 break;
1498 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001499 if (pp->file) {
1500 semantic_error("SRC@SRC is not allowed.\n");
1501 return -EINVAL;
1502 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001503 pp->file = strdup_esc(arg);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001504 if (pp->file == NULL)
1505 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001506 break;
1507 case '%': /* Probe places */
1508 if (strcmp(arg, "return") == 0) {
1509 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 } else { /* Others not supported yet */
1511 semantic_error("%%%s is not supported.\n", arg);
1512 return -ENOTSUP;
1513 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001514 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001515 default: /* Buggy case */
1516 pr_err("This program has a bug at %s:%d.\n",
1517 __FILE__, __LINE__);
1518 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001519 break;
1520 }
1521 }
1522
1523 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001524 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001525 semantic_error("Lazy pattern can't be used with"
1526 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001527 return -EINVAL;
1528 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001529
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001530 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001531 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001532 return -EINVAL;
1533 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001534
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001535 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001536 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001537 return -EINVAL;
1538 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001539
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001540 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001541 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001542 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001543 return -EINVAL;
1544 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001545
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001546 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001547 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548 return -EINVAL;
1549 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001550
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001551 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001552 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001553 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 return -EINVAL;
1555 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001556
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001557 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001558 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1559 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001560 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001561}
1562
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001563/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001564static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001565{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001566 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001567 struct perf_probe_arg_field **fieldp;
1568
1569 pr_debug("parsing arg: %s into ", str);
1570
Masami Hiramatsu48481932010-04-12 13:16:53 -04001571 tmp = strchr(str, '=');
1572 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001573 arg->name = strndup(str, tmp - str);
1574 if (arg->name == NULL)
1575 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001576 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001577 str = tmp + 1;
1578 }
1579
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001580 tmp = strchr(str, ':');
1581 if (tmp) { /* Type setting */
1582 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001583 arg->type = strdup(tmp + 1);
1584 if (arg->type == NULL)
1585 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001586 pr_debug("type:%s ", arg->type);
1587 }
1588
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001589 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001590 if (!is_c_varname(str) || !tmp) {
1591 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001592 arg->var = strdup(str);
1593 if (arg->var == NULL)
1594 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001595 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001596 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001597 }
1598
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001599 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001600 arg->var = strndup(str, tmp - str);
1601 if (arg->var == NULL)
1602 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001603 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001604 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001605 fieldp = &arg->field;
1606
1607 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001608 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1609 if (*fieldp == NULL)
1610 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001611 if (*tmp == '[') { /* Array */
1612 str = tmp;
1613 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001614 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001615 if (*tmp != ']' || tmp == str + 1) {
1616 semantic_error("Array index must be a"
1617 " number.\n");
1618 return -EINVAL;
1619 }
1620 tmp++;
1621 if (*tmp == '\0')
1622 tmp = NULL;
1623 } else { /* Structure */
1624 if (*tmp == '.') {
1625 str = tmp + 1;
1626 (*fieldp)->ref = false;
1627 } else if (tmp[1] == '>') {
1628 str = tmp + 2;
1629 (*fieldp)->ref = true;
1630 } else {
1631 semantic_error("Argument parse error: %s\n",
1632 str);
1633 return -EINVAL;
1634 }
1635 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001636 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001637 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001638 (*fieldp)->name = strndup(str, tmp - str);
1639 if ((*fieldp)->name == NULL)
1640 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001641 if (*str != '[')
1642 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001643 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1644 fieldp = &(*fieldp)->next;
1645 }
1646 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001647 (*fieldp)->name = strdup(str);
1648 if ((*fieldp)->name == NULL)
1649 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001650 if (*str != '[')
1651 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001652 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001653
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001654 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001655 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001656 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001657 if (arg->name == NULL)
1658 return -ENOMEM;
1659 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001660 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001661}
1662
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001663/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001664int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001665{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001666 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001667 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001668
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001669 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001670 if (!argv) {
1671 pr_debug("Failed to split arguments.\n");
1672 return -ENOMEM;
1673 }
1674 if (argc - 1 > MAX_PROBE_ARGS) {
1675 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1676 ret = -ERANGE;
1677 goto out;
1678 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001679 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001680 ret = parse_perf_probe_point(argv[0], pev);
1681 if (ret < 0)
1682 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001683
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001684 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001685 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001686 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1687 if (pev->args == NULL) {
1688 ret = -ENOMEM;
1689 goto out;
1690 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001691 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1692 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1693 if (ret >= 0 &&
1694 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001696 " kretprobe.\n");
1697 ret = -EINVAL;
1698 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001699 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001700out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001701 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001702
1703 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001704}
1705
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301706/* Returns true if *any* ARG is either C variable, $params or $vars. */
1707bool perf_probe_with_var(struct perf_probe_event *pev)
1708{
1709 int i = 0;
1710
1711 for (i = 0; i < pev->nargs; i++)
1712 if (is_c_varname(pev->args[i].var) ||
1713 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1714 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1715 return true;
1716 return false;
1717}
1718
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719/* Return true if this perf_probe_event requires debuginfo */
1720bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001721{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001722 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1723 return true;
1724
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301725 if (perf_probe_with_var(pev))
1726 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001727
1728 return false;
1729}
1730
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301731/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001732int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001733{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001735 char pr;
1736 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001737 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001738 int ret, i, argc;
1739 char **argv;
1740
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301741 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001742 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001743 if (!argv) {
1744 pr_debug("Failed to split arguments.\n");
1745 return -ENOMEM;
1746 }
1747 if (argc < 2) {
1748 semantic_error("Too few probe arguments.\n");
1749 ret = -ERANGE;
1750 goto out;
1751 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001752
1753 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001754 argv0_str = strdup(argv[0]);
1755 if (argv0_str == NULL) {
1756 ret = -ENOMEM;
1757 goto out;
1758 }
1759 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1760 fmt2_str = strtok_r(NULL, "/", &fmt);
1761 fmt3_str = strtok_r(NULL, " \t", &fmt);
1762 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1763 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001764 semantic_error("Failed to parse event name: %s\n", argv[0]);
1765 ret = -EINVAL;
1766 goto out;
1767 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001768 pr = fmt1_str[0];
1769 tev->group = strdup(fmt2_str);
1770 tev->event = strdup(fmt3_str);
1771 if (tev->group == NULL || tev->event == NULL) {
1772 ret = -ENOMEM;
1773 goto out;
1774 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001775 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001776
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001777 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001778
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001779 /* Scan module name(if there), function name and offset */
1780 p = strchr(argv[1], ':');
1781 if (p) {
1782 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001783 if (!tp->module) {
1784 ret = -ENOMEM;
1785 goto out;
1786 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001787 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001788 p++;
1789 } else
1790 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001791 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001792 /* only the address started with 0x */
1793 if (fmt1_str[0] == '0') {
1794 /*
1795 * Fix a special case:
1796 * if address == 0, kernel reports something like:
1797 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1798 * Newer kernel may fix that, but we want to
1799 * support old kernel also.
1800 */
1801 if (strcmp(fmt1_str, "0x") == 0) {
1802 if (!argv[2] || strcmp(argv[2], "(null)")) {
1803 ret = -EINVAL;
1804 goto out;
1805 }
1806 tp->address = 0;
1807
1808 free(argv[2]);
1809 for (i = 2; argv[i + 1] != NULL; i++)
1810 argv[i] = argv[i + 1];
1811
1812 argv[i] = NULL;
1813 argc -= 1;
1814 } else
1815 tp->address = strtoul(fmt1_str, NULL, 0);
1816 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001817 /* Only the symbol-based probe has offset */
1818 tp->symbol = strdup(fmt1_str);
1819 if (tp->symbol == NULL) {
1820 ret = -ENOMEM;
1821 goto out;
1822 }
1823 fmt2_str = strtok_r(NULL, "", &fmt);
1824 if (fmt2_str == NULL)
1825 tp->offset = 0;
1826 else
1827 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001828 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001829
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05301830 if (tev->uprobes) {
1831 fmt2_str = strchr(p, '(');
1832 if (fmt2_str)
1833 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1834 }
1835
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001836 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301837 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001838 if (tev->args == NULL) {
1839 ret = -ENOMEM;
1840 goto out;
1841 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001842 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001843 p = strchr(argv[i + 2], '=');
1844 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001845 *p++ = '\0';
1846 else
1847 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001848 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001849 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001850 tev->args[i].value = strdup(p);
1851 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1852 ret = -ENOMEM;
1853 goto out;
1854 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001855 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001856 ret = 0;
1857out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001858 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001859 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001860 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001861}
1862
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001863/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001864char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001865{
1866 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001867 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001868 char *ret = NULL;
1869 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001870
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001871 if (strbuf_init(&buf, 64) < 0)
1872 return NULL;
1873
Masami Hiramatsu48481932010-04-12 13:16:53 -04001874 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001875 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001876 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001877 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1878 if (err)
1879 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001880
1881 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001882 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001883 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001884 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001885 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1886 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001887 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001888 if (err)
1889 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001890 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001891
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001892 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001893 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1894 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001895
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001896 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001897out:
1898 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001899 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001900}
1901
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001902/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001903char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001904{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001905 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001906 char *tmp, *ret = NULL;
1907 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001908
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001909 if (strbuf_init(&buf, 64) < 0)
1910 return NULL;
1911
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001912 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001913 if (strbuf_addstr(&buf, pp->function) < 0)
1914 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001915 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001916 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001917 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001918 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001919 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001920 err = strbuf_addstr(&buf, "%return");
1921 if (err)
1922 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001923 }
1924 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001925 tmp = pp->file;
1926 len = strlen(tmp);
1927 if (len > 30) {
1928 tmp = strchr(pp->file + len - 30, '/');
1929 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1930 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001931 err = strbuf_addf(&buf, "@%s", tmp);
1932 if (!err && !pp->function && pp->line)
1933 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001934 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001935 if (!err)
1936 ret = strbuf_detach(&buf, NULL);
1937out:
1938 strbuf_release(&buf);
1939 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001940}
1941
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1943{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001944 struct strbuf buf;
1945 char *tmp, *ret = NULL;
1946 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001947
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001948 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001949 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001950 if (pev->event)
1951 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1952 pev->event) < 0)
1953 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001954
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001955 tmp = synthesize_perf_probe_point(&pev->point);
1956 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1957 goto out;
1958 free(tmp);
1959
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001960 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001961 tmp = synthesize_perf_probe_arg(pev->args + i);
1962 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1963 goto out;
1964 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001965 }
1966
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001967 ret = strbuf_detach(&buf, NULL);
1968out:
1969 strbuf_release(&buf);
1970 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001972
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301973static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001974 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001975{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001976 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001977 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301978 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001979 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001980 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001981 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001982 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001983 err = strbuf_addf(buf, "%+ld(", ref->offset);
1984 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001985}
1986
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301987static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001988 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001989{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301990 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001991 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001992
1993 /* Argument name or separator */
1994 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001995 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001996 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001997 err = strbuf_addch(buf, ' ');
1998 if (err)
1999 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002000
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002001 /* Special case: @XXX */
2002 if (arg->value[0] == '@' && arg->ref)
2003 ref = ref->next;
2004
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002005 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002006 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002007 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002008 if (depth < 0)
2009 return depth;
2010 }
2011
2012 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002013 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002014 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002015 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002016 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002017
2018 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002019 while (!err && depth--)
2020 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002021
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002022 /* Print argument type */
2023 if (!err && arg->type)
2024 err = strbuf_addf(buf, ":%s", arg->type);
2025
2026 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002027}
2028
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302029static int
2030synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2031{
2032 struct probe_trace_point *tp = &tev->point;
2033 int err;
2034
2035 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2036
2037 if (err >= 0 && tp->ref_ctr_offset) {
2038 if (!uprobe_ref_ctr_is_supported())
2039 return -1;
2040 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2041 }
2042 return err >= 0 ? 0 : -1;
2043}
2044
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302045char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002046{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302047 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002048 struct strbuf buf;
2049 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002050 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002051
Wang Nanda15bd92015-08-26 10:57:45 +00002052 /* Uprobes must have tp->module */
2053 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002054 return NULL;
2055
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002056 if (strbuf_init(&buf, 32) < 0)
2057 return NULL;
2058
2059 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2060 tev->group, tev->event) < 0)
2061 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00002062 /*
2063 * If tp->address == 0, then this point must be a
2064 * absolute address uprobe.
2065 * try_to_find_absolute_address() should have made
2066 * tp->symbol to "0x0".
2067 */
2068 if (tev->uprobes && !tp->address) {
2069 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2070 goto error;
2071 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002072
2073 /* Use the tp->address for uprobes */
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302074 if (tev->uprobes) {
2075 err = synthesize_uprobe_trace_def(tev, &buf);
2076 } else if (!strncmp(tp->symbol, "0x", 2)) {
Wang Nanda15bd92015-08-26 10:57:45 +00002077 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002078 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2079 tp->module ? ":" : "", tp->address);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302080 } else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002081 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2082 tp->module ? ":" : "", tp->symbol, tp->offset);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302083 }
2084
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002085 if (err)
2086 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302087
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002088 for (i = 0; i < tev->nargs; i++)
2089 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002090 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002091
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002092 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002093error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002094 strbuf_release(&buf);
2095 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002096}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002097
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002098static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2099 struct perf_probe_point *pp,
2100 bool is_kprobe)
2101{
2102 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo8a2efd62017-02-14 15:28:41 -03002103 struct map *map = NULL;
Wang Nane4863672015-08-25 13:27:35 +00002104 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002105 int ret = -ENOENT;
2106
2107 if (!is_kprobe) {
2108 map = dso__new_map(tp->module);
2109 if (!map)
2110 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002111 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002112 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002113 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002114 if (kernel_get_symbol_address_by_name(tp->symbol,
2115 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002116 goto out;
2117 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002118 if (addr) {
2119 addr += tp->offset;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002120 sym = machine__find_kernel_symbol(host_machine, addr, &map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002121 }
2122 }
Wang Nan98d3b252015-11-05 13:19:25 +00002123
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002124 if (!sym)
2125 goto out;
2126
2127 pp->retprobe = tp->retprobe;
2128 pp->offset = addr - map->unmap_ip(map, sym->start);
2129 pp->function = strdup(sym->name);
2130 ret = pp->function ? 0 : -ENOMEM;
2131
2132out:
2133 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002134 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002135 }
2136
2137 return ret;
2138}
2139
2140static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002141 struct perf_probe_point *pp,
2142 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002143{
2144 char buf[128];
2145 int ret;
2146
2147 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2148 if (!ret)
2149 return 0;
2150 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2151 if (!ret)
2152 return 0;
2153
2154 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2155
2156 if (tp->symbol) {
2157 pp->function = strdup(tp->symbol);
2158 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002159 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002160 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2161 if (ret < 0)
2162 return ret;
2163 pp->function = strdup(buf);
2164 pp->offset = 0;
2165 }
2166 if (pp->function == NULL)
2167 return -ENOMEM;
2168
2169 pp->retprobe = tp->retprobe;
2170
2171 return 0;
2172}
2173
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302174static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302175 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002176{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002177 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002178 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002179
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002180 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002181 pev->event = strdup(tev->event);
2182 pev->group = strdup(tev->group);
2183 if (pev->event == NULL || pev->group == NULL)
2184 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002185
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002186 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002187 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002188 if (ret < 0)
2189 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002190
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002191 /* Convert trace_arg to probe_arg */
2192 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002193 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2194 if (pev->args == NULL)
2195 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002196 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002197 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002198 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002199 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002200 if ((ret = strbuf_init(&buf, 32)) < 0)
2201 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002202 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2203 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002204 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002205 if (pev->args[i].name == NULL && ret >= 0)
2206 ret = -ENOMEM;
2207 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002208error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002209 if (ret < 0)
2210 clear_perf_probe_event(pev);
2211
2212 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002213}
2214
2215void clear_perf_probe_event(struct perf_probe_event *pev)
2216{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002217 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002218 int i;
2219
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002220 free(pev->event);
2221 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002222 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002223 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002224
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002225 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002226 free(pev->args[i].name);
2227 free(pev->args[i].var);
2228 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002229 field = pev->args[i].field;
2230 while (field) {
2231 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002232 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002233 free(field);
2234 field = next;
2235 }
2236 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002237 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002238 memset(pev, 0, sizeof(*pev));
2239}
2240
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002241#define strdup_or_goto(str, label) \
2242({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2243
2244static int perf_probe_point__copy(struct perf_probe_point *dst,
2245 struct perf_probe_point *src)
2246{
2247 dst->file = strdup_or_goto(src->file, out_err);
2248 dst->function = strdup_or_goto(src->function, out_err);
2249 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2250 dst->line = src->line;
2251 dst->retprobe = src->retprobe;
2252 dst->offset = src->offset;
2253 return 0;
2254
2255out_err:
2256 clear_perf_probe_point(dst);
2257 return -ENOMEM;
2258}
2259
2260static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2261 struct perf_probe_arg *src)
2262{
2263 struct perf_probe_arg_field *field, **ppfield;
2264
2265 dst->name = strdup_or_goto(src->name, out_err);
2266 dst->var = strdup_or_goto(src->var, out_err);
2267 dst->type = strdup_or_goto(src->type, out_err);
2268
2269 field = src->field;
2270 ppfield = &(dst->field);
2271 while (field) {
2272 *ppfield = zalloc(sizeof(*field));
2273 if (!*ppfield)
2274 goto out_err;
2275 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2276 (*ppfield)->index = field->index;
2277 (*ppfield)->ref = field->ref;
2278 field = field->next;
2279 ppfield = &((*ppfield)->next);
2280 }
2281 return 0;
2282out_err:
2283 return -ENOMEM;
2284}
2285
2286int perf_probe_event__copy(struct perf_probe_event *dst,
2287 struct perf_probe_event *src)
2288{
2289 int i;
2290
2291 dst->event = strdup_or_goto(src->event, out_err);
2292 dst->group = strdup_or_goto(src->group, out_err);
2293 dst->target = strdup_or_goto(src->target, out_err);
2294 dst->uprobes = src->uprobes;
2295
2296 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2297 goto out_err;
2298
2299 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2300 if (!dst->args)
2301 goto out_err;
2302 dst->nargs = src->nargs;
2303
2304 for (i = 0; i < src->nargs; i++)
2305 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2306 goto out_err;
2307 return 0;
2308
2309out_err:
2310 clear_perf_probe_event(dst);
2311 return -ENOMEM;
2312}
2313
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002314void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002315{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302316 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002317 int i;
2318
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002319 free(tev->event);
2320 free(tev->group);
2321 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002322 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002323 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002324 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002325 free(tev->args[i].name);
2326 free(tev->args[i].value);
2327 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002328 ref = tev->args[i].ref;
2329 while (ref) {
2330 next = ref->next;
2331 free(ref);
2332 ref = next;
2333 }
2334 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002335 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002336 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002337}
2338
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002339struct kprobe_blacklist_node {
2340 struct list_head list;
2341 unsigned long start;
2342 unsigned long end;
2343 char *symbol;
2344};
2345
2346static void kprobe_blacklist__delete(struct list_head *blacklist)
2347{
2348 struct kprobe_blacklist_node *node;
2349
2350 while (!list_empty(blacklist)) {
2351 node = list_first_entry(blacklist,
2352 struct kprobe_blacklist_node, list);
2353 list_del(&node->list);
2354 free(node->symbol);
2355 free(node);
2356 }
2357}
2358
2359static int kprobe_blacklist__load(struct list_head *blacklist)
2360{
2361 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002362 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002363 char buf[PATH_MAX], *p;
2364 FILE *fp;
2365 int ret;
2366
2367 if (__debugfs == NULL)
2368 return -ENOTSUP;
2369
2370 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2371 if (ret < 0)
2372 return ret;
2373
2374 fp = fopen(buf, "r");
2375 if (!fp)
2376 return -errno;
2377
2378 ret = 0;
2379 while (fgets(buf, PATH_MAX, fp)) {
2380 node = zalloc(sizeof(*node));
2381 if (!node) {
2382 ret = -ENOMEM;
2383 break;
2384 }
2385 INIT_LIST_HEAD(&node->list);
2386 list_add_tail(&node->list, blacklist);
2387 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2388 ret = -EINVAL;
2389 break;
2390 }
2391 p = strchr(buf, '\t');
2392 if (p) {
2393 p++;
2394 if (p[strlen(p) - 1] == '\n')
2395 p[strlen(p) - 1] = '\0';
2396 } else
2397 p = (char *)"unknown";
2398 node->symbol = strdup(p);
2399 if (!node->symbol) {
2400 ret = -ENOMEM;
2401 break;
2402 }
2403 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2404 node->start, node->end, node->symbol);
2405 ret++;
2406 }
2407 if (ret < 0)
2408 kprobe_blacklist__delete(blacklist);
2409 fclose(fp);
2410
2411 return ret;
2412}
2413
2414static struct kprobe_blacklist_node *
2415kprobe_blacklist__find_by_address(struct list_head *blacklist,
2416 unsigned long address)
2417{
2418 struct kprobe_blacklist_node *node;
2419
2420 list_for_each_entry(node, blacklist, list) {
Li Bin2c294612017-08-29 20:57:23 +08002421 if (node->start <= address && address < node->end)
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002422 return node;
2423 }
2424
2425 return NULL;
2426}
2427
Masami Hiramatsub0312202015-06-16 20:50:55 +09002428static LIST_HEAD(kprobe_blacklist);
2429
2430static void kprobe_blacklist__init(void)
2431{
2432 if (!list_empty(&kprobe_blacklist))
2433 return;
2434
2435 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2436 pr_debug("No kprobe blacklist support, ignored\n");
2437}
2438
2439static void kprobe_blacklist__release(void)
2440{
2441 kprobe_blacklist__delete(&kprobe_blacklist);
2442}
2443
2444static bool kprobe_blacklist__listed(unsigned long address)
2445{
2446 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2447}
2448
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002449static int perf_probe_event__sprintf(const char *group, const char *event,
2450 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002451 const char *module,
2452 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002453{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002454 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002455 char *buf;
2456
2457 if (asprintf(&buf, "%s:%s", group, event) < 0)
2458 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002459 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002460 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002461 if (ret)
2462 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002463
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002464 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002465 buf = synthesize_perf_probe_point(&pev->point);
2466 if (!buf)
2467 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002468 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002469 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002470
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002471 if (!ret && module)
2472 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002473
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002474 if (!ret && pev->nargs > 0) {
2475 ret = strbuf_add(result, " with", 5);
2476 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002477 buf = synthesize_perf_probe_arg(&pev->args[i]);
2478 if (!buf)
2479 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002480 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002481 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002482 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002483 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002484 if (!ret)
2485 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002486
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002487 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002488}
2489
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002490/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002491int show_perf_probe_event(const char *group, const char *event,
2492 struct perf_probe_event *pev,
2493 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002494{
2495 struct strbuf buf = STRBUF_INIT;
2496 int ret;
2497
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002498 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002499 if (ret >= 0) {
2500 if (use_stdout)
2501 printf("%s\n", buf.buf);
2502 else
2503 pr_info("%s\n", buf.buf);
2504 }
2505 strbuf_release(&buf);
2506
2507 return ret;
2508}
2509
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002510static bool filter_probe_trace_event(struct probe_trace_event *tev,
2511 struct strfilter *filter)
2512{
2513 char tmp[128];
2514
2515 /* At first, check the event name itself */
2516 if (strfilter__compare(filter, tev->event))
2517 return true;
2518
2519 /* Next, check the combination of name and group */
2520 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2521 return false;
2522 return strfilter__compare(filter, tmp);
2523}
2524
2525static int __show_perf_probe_events(int fd, bool is_kprobe,
2526 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002527{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302528 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302529 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002530 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002531 struct strlist *rawlist;
2532 struct str_node *ent;
2533
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002534 memset(&tev, 0, sizeof(tev));
2535 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002536
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002537 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002538 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002539 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002540
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002541 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302542 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002543 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002544 if (!filter_probe_trace_event(&tev, filter))
2545 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302546 ret = convert_to_perf_probe_event(&tev, &pev,
2547 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002548 if (ret < 0)
2549 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002550 ret = show_perf_probe_event(pev.group, pev.event,
2551 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002552 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002553 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002554next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002555 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302556 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002557 if (ret < 0)
2558 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002559 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002560 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002561 /* Cleanup cached debuginfo if needed */
2562 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002563
2564 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002565}
2566
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302567/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002568int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302569{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002570 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302571
2572 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302573
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002574 if (probe_conf.cache)
2575 return probe_cache__show_all_caches(filter);
2576
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002577 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302578 if (ret < 0)
2579 return ret;
2580
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002581 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2582 if (ret < 0)
2583 return ret;
2584
2585 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002586 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002587 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002588 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002589 if (kp_fd > 0)
2590 close(kp_fd);
2591 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002592 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002593 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302594
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002595 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002596}
2597
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002598static int get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002599 struct strlist *namelist, bool ret_event,
2600 bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002601{
2602 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002603 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002604
Naveen N. Rao3099c022015-04-28 17:35:34 +05302605 if (*base == '.')
2606 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002607 nbase = strdup(base);
2608 if (!nbase)
2609 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302610
Masami Hiramatsua3110cd2017-12-11 15:19:25 -03002611 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2612 p = strpbrk(nbase, ".@");
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002613 if (p && p != nbase)
2614 *p = '\0';
2615
2616 /* Try no suffix number */
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002617 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002618 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002619 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002620 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002621 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002622 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002623 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002624
Masami Hiramatsud761b082009-12-15 10:32:25 -05002625 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002626 pr_warning("Error: event \"%s\" already exists.\n"
2627 " Hint: Remove existing event by 'perf probe -d'\n"
2628 " or force duplicates by 'perf probe -f'\n"
2629 " or set 'force=yes' in BPF source.\n",
2630 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002631 ret = -EEXIST;
2632 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002633 }
2634
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002635 /* Try to add suffix */
2636 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002637 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002638 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002639 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002640 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002641 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002642 if (!strlist__has_entry(namelist, buf))
2643 break;
2644 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002645 if (i == MAX_EVENT_INDEX) {
2646 pr_warning("Too many events are on the same function.\n");
2647 ret = -ERANGE;
2648 }
2649
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002650out:
2651 free(nbase);
Masami Hiramatsu9f5c6d82017-12-09 01:26:46 +09002652
2653 /* Final validation */
2654 if (ret >= 0 && !is_c_func_name(buf)) {
2655 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2656 buf);
2657 ret = -EINVAL;
2658 }
2659
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002660 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002661}
2662
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002663/* Warn if the current kernel's uprobe implementation is old */
2664static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2665{
2666 int i;
2667 char *buf = synthesize_probe_trace_command(tev);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302668 struct probe_trace_point *tp = &tev->point;
2669
2670 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2671 pr_warning("A semaphore is associated with %s:%s and "
2672 "seems your kernel doesn't support it.\n",
2673 tev->group, tev->event);
2674 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002675
2676 /* Old uprobe event doesn't support memory dereference */
2677 if (!tev->uprobes || tev->nargs == 0 || !buf)
2678 goto out;
2679
2680 for (i = 0; i < tev->nargs; i++)
2681 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2682 pr_warning("Please upgrade your kernel to at least "
2683 "3.14 to have access to feature %s\n",
2684 tev->args[i].value);
2685 break;
2686 }
2687out:
2688 free(buf);
2689}
2690
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002691/* Set new name from original perf_probe_event and namelist */
2692static int probe_trace_event__set_name(struct probe_trace_event *tev,
2693 struct perf_probe_event *pev,
2694 struct strlist *namelist,
2695 bool allow_suffix)
2696{
2697 const char *event, *group;
2698 char buf[64];
2699 int ret;
2700
Masami Hiramatsubc062232016-07-01 17:03:12 +09002701 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002702 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002703 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002704 else if (tev->event)
2705 event = tev->event;
2706 else {
2707 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002708 if (pev->point.function &&
2709 (strncmp(pev->point.function, "0x", 2) != 0) &&
2710 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002711 event = pev->point.function;
2712 else
2713 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002714 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002715 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002716 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002717 else if (tev->group)
2718 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002719 else
2720 group = PERFPROBE_GROUP;
2721
2722 /* Get an unused new event name */
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002723 ret = get_new_event_name(buf, 64, event, namelist,
2724 tev->point.retprobe, allow_suffix);
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002725 if (ret < 0)
2726 return ret;
2727
2728 event = buf;
2729
2730 tev->event = strdup(event);
2731 tev->group = strdup(group);
2732 if (tev->event == NULL || tev->group == NULL)
2733 return -ENOMEM;
2734
2735 /* Add added event name to namelist */
2736 strlist__add(namelist, event);
2737 return 0;
2738}
2739
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002740static int __open_probe_file_and_namelist(bool uprobe,
2741 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002742{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002743 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002744
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002745 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002746 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002747 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002748
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002749 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002750 *namelist = probe_file__get_namelist(fd);
2751 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002752 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002753 close(fd);
2754 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002755 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002756 return fd;
2757}
2758
2759static int __add_probe_trace_events(struct perf_probe_event *pev,
2760 struct probe_trace_event *tevs,
2761 int ntevs, bool allow_suffix)
2762{
2763 int i, fd[2] = {-1, -1}, up, ret;
2764 struct probe_trace_event *tev = NULL;
2765 struct probe_cache *cache = NULL;
2766 struct strlist *namelist[2] = {NULL, NULL};
Krister Johansen544abd42017-07-05 18:48:10 -07002767 struct nscookie nsc;
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002768
2769 up = pev->uprobes ? 1 : 0;
2770 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2771 if (fd[up] < 0)
2772 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002773
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002774 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002775 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002776 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002777 up = tev->uprobes ? 1 : 0;
2778 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2779 fd[up] = __open_probe_file_and_namelist(up,
2780 &namelist[up]);
2781 if (fd[up] < 0)
2782 goto close_out;
2783 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002784 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002785 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002786 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002787
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002788 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002789 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002790 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002791 if (ret < 0)
2792 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002793
Krister Johansen544abd42017-07-05 18:48:10 -07002794 nsinfo__mountns_enter(pev->nsi, &nsc);
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002795 ret = probe_file__add_event(fd[up], tev);
Krister Johansen544abd42017-07-05 18:48:10 -07002796 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002797 if (ret < 0)
2798 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002799
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002800 /*
2801 * Probes after the first probe which comes from same
2802 * user input are always allowed to add suffix, because
2803 * there might be several addresses corresponding to
2804 * one code line.
2805 */
2806 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002807 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002808 if (ret == -EINVAL && pev->uprobes)
2809 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002810 if (ret == 0 && probe_conf.cache) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002811 cache = probe_cache__new(pev->target, pev->nsi);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002812 if (!cache ||
2813 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2814 probe_cache__commit(cache) < 0)
2815 pr_warning("Failed to add event to probe cache\n");
2816 probe_cache__delete(cache);
2817 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002818
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002819close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002820 for (up = 0; up < 2; up++) {
2821 strlist__delete(namelist[up]);
2822 if (fd[up] >= 0)
2823 close(fd[up]);
2824 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002825 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002826}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002827
Wang Nan6bb536c2015-05-29 09:45:47 +00002828static int find_probe_functions(struct map *map, char *name,
2829 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002830{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002831 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002832 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002833 struct rb_node *tmp;
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002834 const char *norm, *ver;
2835 char *buf = NULL;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002836 bool cut_version = true;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002837
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002838 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002839 return 0;
2840
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002841 /* If user gives a version, don't cut off the version from symbols */
2842 if (strchr(name, '@'))
2843 cut_version = false;
2844
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002845 map__for_each_symbol(map, sym, tmp) {
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002846 norm = arch__normalize_symbol_name(sym->name);
2847 if (!norm)
2848 continue;
2849
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002850 if (cut_version) {
2851 /* We don't care about default symbol or not */
2852 ver = strchr(norm, '@');
2853 if (ver) {
2854 buf = strndup(norm, ver - norm);
2855 if (!buf)
2856 return -ENOMEM;
2857 norm = buf;
2858 }
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002859 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002860
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002861 if (strglobmatch(norm, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002862 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002863 if (syms && found < probe_conf.max_probes)
2864 syms[found - 1] = sym;
2865 }
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002866 if (buf)
2867 zfree(&buf);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002868 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002869
2870 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002871}
2872
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302873void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2874 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302875 struct map *map __maybe_unused,
2876 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302877
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002878/*
2879 * Find probe function addresses from map.
2880 * Return an error or the number of found probe_trace_event
2881 */
2882static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002883 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002884{
2885 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002886 struct ref_reloc_sym *reloc_sym = NULL;
2887 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002888 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002889 struct probe_trace_event *tev;
2890 struct perf_probe_point *pp = &pev->point;
2891 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002892 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002893 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302894 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002895
Krister Johansen544abd42017-07-05 18:48:10 -07002896 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002897 if (!map) {
2898 ret = -EINVAL;
2899 goto out;
2900 }
2901
Wang Nan6bb536c2015-05-29 09:45:47 +00002902 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2903 if (!syms) {
2904 ret = -ENOMEM;
2905 goto out;
2906 }
2907
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002908 /*
2909 * Load matched symbols: Since the different local symbols may have
2910 * same name but different addresses, this lists all the symbols.
2911 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002912 num_matched_functions = find_probe_functions(map, pp->function, syms);
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002913 if (num_matched_functions <= 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002914 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002915 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002916 ret = -ENOENT;
2917 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002918 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002919 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002920 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002921 ret = -E2BIG;
2922 goto out;
2923 }
2924
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002925 /* Note that the symbols in the kmodule are not relocated */
Naveen N. Rao7ab31d92017-03-08 13:56:09 +05302926 if (!pev->uprobes && !pev->target &&
2927 (!pp->retprobe || kretprobe_offset_is_supported())) {
He Kuang0560a0c2015-03-20 09:56:56 +08002928 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002929 if (!reloc_sym) {
2930 pr_warning("Relocated base symbol is not found!\n");
2931 ret = -EINVAL;
2932 goto out;
2933 }
2934 }
2935
2936 /* Setup result trace-probe-events */
2937 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2938 if (!*tevs) {
2939 ret = -ENOMEM;
2940 goto out;
2941 }
2942
2943 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002944
Wang Nan6bb536c2015-05-29 09:45:47 +00002945 for (j = 0; j < num_matched_functions; j++) {
2946 sym = syms[j];
2947
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002948 tev = (*tevs) + ret;
2949 tp = &tev->point;
2950 if (ret == num_matched_functions) {
2951 pr_warning("Too many symbols are listed. Skip it.\n");
2952 break;
2953 }
2954 ret++;
2955
2956 if (pp->offset > sym->end - sym->start) {
2957 pr_warning("Offset %ld is bigger than the size of %s\n",
2958 pp->offset, sym->name);
2959 ret = -ENOENT;
2960 goto err_out;
2961 }
2962 /* Add one probe point */
2963 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002964
2965 /* Check the kprobe (not in module) is within .text */
2966 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002967 kprobe_warn_out_range(sym->name, tp->address)) {
2968 tp->symbol = NULL; /* Skip it */
2969 skipped++;
2970 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002971 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2972 tp->offset = tp->address - reloc_sym->addr;
2973 } else {
2974 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2975 tp->offset = pp->offset;
2976 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002977 tp->realname = strdup_or_goto(sym->name, nomem_out);
2978
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002979 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302980 if (pev->target) {
2981 if (pev->uprobes) {
2982 tev->point.module = strdup_or_goto(pev->target,
2983 nomem_out);
2984 } else {
2985 mod_name = find_module_name(pev->target);
2986 tev->point.module =
2987 strdup(mod_name ? mod_name : pev->target);
2988 free(mod_name);
2989 if (!tev->point.module)
2990 goto nomem_out;
2991 }
2992 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002993 tev->uprobes = pev->uprobes;
2994 tev->nargs = pev->nargs;
2995 if (tev->nargs) {
2996 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2997 tev->nargs);
2998 if (tev->args == NULL)
2999 goto nomem_out;
3000 }
3001 for (i = 0; i < tev->nargs; i++) {
3002 if (pev->args[i].name)
3003 tev->args[i].name =
3004 strdup_or_goto(pev->args[i].name,
3005 nomem_out);
3006
3007 tev->args[i].value = strdup_or_goto(pev->args[i].var,
3008 nomem_out);
3009 if (pev->args[i].type)
3010 tev->args[i].type =
3011 strdup_or_goto(pev->args[i].type,
3012 nomem_out);
3013 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05303014 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003015 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003016 if (ret == skipped) {
3017 ret = -ENOENT;
3018 goto err_out;
3019 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003020
3021out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003022 map__put(map);
Wang Nan6bb536c2015-05-29 09:45:47 +00003023 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003024 return ret;
3025
3026nomem_out:
3027 ret = -ENOMEM;
3028err_out:
3029 clear_probe_trace_events(*tevs, num_matched_functions);
3030 zfree(tevs);
3031 goto out;
3032}
3033
Wang Nanda15bd92015-08-26 10:57:45 +00003034static int try_to_find_absolute_address(struct perf_probe_event *pev,
3035 struct probe_trace_event **tevs)
3036{
3037 struct perf_probe_point *pp = &pev->point;
3038 struct probe_trace_event *tev;
3039 struct probe_trace_point *tp;
3040 int i, err;
3041
3042 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3043 return -EINVAL;
3044 if (perf_probe_event_need_dwarf(pev))
3045 return -EINVAL;
3046
3047 /*
3048 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3049 * absolute address.
3050 *
3051 * Only one tev can be generated by this.
3052 */
3053 *tevs = zalloc(sizeof(*tev));
3054 if (!*tevs)
3055 return -ENOMEM;
3056
3057 tev = *tevs;
3058 tp = &tev->point;
3059
3060 /*
3061 * Don't use tp->offset, use address directly, because
3062 * in synthesize_probe_trace_command() address cannot be
3063 * zero.
3064 */
3065 tp->address = pev->point.abs_address;
3066 tp->retprobe = pp->retprobe;
3067 tev->uprobes = pev->uprobes;
3068
3069 err = -ENOMEM;
3070 /*
3071 * Give it a '0x' leading symbol name.
3072 * In __add_probe_trace_events, a NULL symbol is interpreted as
Ingo Molnaradba1632018-12-03 11:22:00 +01003073 * invalid.
Wang Nanda15bd92015-08-26 10:57:45 +00003074 */
3075 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3076 goto errout;
3077
3078 /* For kprobe, check range */
3079 if ((!tev->uprobes) &&
3080 (kprobe_warn_out_range(tev->point.symbol,
3081 tev->point.address))) {
3082 err = -EACCES;
3083 goto errout;
3084 }
3085
3086 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3087 goto errout;
3088
3089 if (pev->target) {
3090 tp->module = strdup(pev->target);
3091 if (!tp->module)
3092 goto errout;
3093 }
3094
3095 if (tev->group) {
3096 tev->group = strdup(pev->group);
3097 if (!tev->group)
3098 goto errout;
3099 }
3100
3101 if (pev->event) {
3102 tev->event = strdup(pev->event);
3103 if (!tev->event)
3104 goto errout;
3105 }
3106
3107 tev->nargs = pev->nargs;
3108 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Markus Elfringd1d0e292017-01-23 15:10:19 +01003109 if (!tev->args)
Wang Nanda15bd92015-08-26 10:57:45 +00003110 goto errout;
Markus Elfringd1d0e292017-01-23 15:10:19 +01003111
Wang Nanda15bd92015-08-26 10:57:45 +00003112 for (i = 0; i < tev->nargs; i++)
3113 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3114
3115 return 1;
3116
3117errout:
Markus Elfring42e233c2017-01-23 14:54:26 +01003118 clear_probe_trace_events(*tevs, 1);
3119 *tevs = NULL;
Wang Nanda15bd92015-08-26 10:57:45 +00003120 return err;
3121}
3122
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003123/* Concatinate two arrays */
3124static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3125{
3126 void *ret;
3127
3128 ret = malloc(sz_a + sz_b);
3129 if (ret) {
3130 memcpy(ret, a, sz_a);
3131 memcpy(ret + sz_a, b, sz_b);
3132 }
3133 return ret;
3134}
3135
3136static int
3137concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3138 struct probe_trace_event **tevs2, int ntevs2)
3139{
3140 struct probe_trace_event *new_tevs;
3141 int ret = 0;
3142
Ravi Bangoriaf0a30dc2017-03-08 12:29:07 +05303143 if (*ntevs == 0) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003144 *tevs = *tevs2;
3145 *ntevs = ntevs2;
3146 *tevs2 = NULL;
3147 return 0;
3148 }
3149
3150 if (*ntevs + ntevs2 > probe_conf.max_probes)
3151 ret = -E2BIG;
3152 else {
3153 /* Concatinate the array of probe_trace_event */
3154 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3155 *tevs2, ntevs2 * sizeof(**tevs2));
3156 if (!new_tevs)
3157 ret = -ENOMEM;
3158 else {
3159 free(*tevs);
3160 *tevs = new_tevs;
3161 *ntevs += ntevs2;
3162 }
3163 }
3164 if (ret < 0)
3165 clear_probe_trace_events(*tevs2, ntevs2);
3166 zfree(tevs2);
3167
3168 return ret;
3169}
3170
3171/*
3172 * Try to find probe_trace_event from given probe caches. Return the number
3173 * of cached events found, if an error occurs return the error.
3174 */
3175static int find_cached_events(struct perf_probe_event *pev,
3176 struct probe_trace_event **tevs,
3177 const char *target)
3178{
3179 struct probe_cache *cache;
3180 struct probe_cache_entry *entry;
3181 struct probe_trace_event *tmp_tevs = NULL;
3182 int ntevs = 0;
3183 int ret = 0;
3184
Krister Johansenf045b8c2017-07-05 18:48:11 -07003185 cache = probe_cache__new(target, pev->nsi);
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003186 /* Return 0 ("not found") if the target has no probe cache. */
3187 if (!cache)
3188 return 0;
3189
3190 for_each_probe_cache_entry(entry, cache) {
3191 /* Skip the cache entry which has no name */
3192 if (!entry->pev.event || !entry->pev.group)
3193 continue;
3194 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3195 strglobmatch(entry->pev.event, pev->event)) {
3196 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3197 if (ret > 0)
3198 ret = concat_probe_trace_events(tevs, &ntevs,
3199 &tmp_tevs, ret);
3200 if (ret < 0)
3201 break;
3202 }
3203 }
3204 probe_cache__delete(cache);
3205 if (ret < 0) {
3206 clear_probe_trace_events(*tevs, ntevs);
3207 zfree(tevs);
3208 } else {
3209 ret = ntevs;
3210 if (ntevs > 0 && target && target[0] == '/')
3211 pev->uprobes = true;
3212 }
3213
3214 return ret;
3215}
3216
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003217/* Try to find probe_trace_event from all probe caches */
3218static int find_cached_events_all(struct perf_probe_event *pev,
3219 struct probe_trace_event **tevs)
3220{
3221 struct probe_trace_event *tmp_tevs = NULL;
3222 struct strlist *bidlist;
3223 struct str_node *nd;
3224 char *pathname;
3225 int ntevs = 0;
3226 int ret;
3227
3228 /* Get the buildid list of all valid caches */
3229 bidlist = build_id_cache__list_all(true);
3230 if (!bidlist) {
3231 ret = -errno;
3232 pr_debug("Failed to get buildids: %d\n", ret);
3233 return ret;
3234 }
3235
3236 ret = 0;
3237 strlist__for_each_entry(nd, bidlist) {
3238 pathname = build_id_cache__origname(nd->s);
3239 ret = find_cached_events(pev, &tmp_tevs, pathname);
3240 /* In the case of cnt == 0, we just skip it */
3241 if (ret > 0)
3242 ret = concat_probe_trace_events(tevs, &ntevs,
3243 &tmp_tevs, ret);
3244 free(pathname);
3245 if (ret < 0)
3246 break;
3247 }
3248 strlist__delete(bidlist);
3249
3250 if (ret < 0) {
3251 clear_probe_trace_events(*tevs, ntevs);
3252 zfree(tevs);
3253 } else
3254 ret = ntevs;
3255
3256 return ret;
3257}
3258
Masami Hiramatsubc062232016-07-01 17:03:12 +09003259static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3260 struct probe_trace_event **tevs)
3261{
3262 struct probe_cache *cache;
3263 struct probe_cache_entry *entry;
3264 struct probe_trace_event *tev;
3265 struct str_node *node;
3266 int ret, i;
3267
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003268 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003269 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003270 if (!pev->target)
3271 return find_cached_events_all(pev, tevs);
3272 else
3273 return find_cached_events(pev, tevs, pev->target);
3274 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07003275 cache = probe_cache__new(pev->target, pev->nsi);
Masami Hiramatsubc062232016-07-01 17:03:12 +09003276 if (!cache)
3277 return 0;
3278
3279 entry = probe_cache__find(cache, pev);
3280 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003281 /* SDT must be in the cache */
3282 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003283 goto out;
3284 }
3285
3286 ret = strlist__nr_entries(entry->tevlist);
3287 if (ret > probe_conf.max_probes) {
3288 pr_debug("Too many entries matched in the cache of %s\n",
3289 pev->target ? : "kernel");
3290 ret = -E2BIG;
3291 goto out;
3292 }
3293
3294 *tevs = zalloc(ret * sizeof(*tev));
3295 if (!*tevs) {
3296 ret = -ENOMEM;
3297 goto out;
3298 }
3299
3300 i = 0;
3301 strlist__for_each_entry(node, entry->tevlist) {
3302 tev = &(*tevs)[i++];
3303 ret = parse_probe_trace_command(node->s, tev);
3304 if (ret < 0)
3305 goto out;
3306 /* Set the uprobes attribute as same as original */
3307 tev->uprobes = pev->uprobes;
3308 }
3309 ret = i;
3310
3311out:
3312 probe_cache__delete(cache);
3313 return ret;
3314}
3315
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303316static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003317 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003318{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003319 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003320
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003321 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003322 /* Set group name if not given */
3323 if (!pev->uprobes) {
3324 pev->group = strdup(PERFPROBE_GROUP);
3325 ret = pev->group ? 0 : -ENOMEM;
3326 } else
3327 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003328 if (ret != 0) {
3329 pr_warning("Failed to make a group name.\n");
3330 return ret;
3331 }
3332 }
3333
Wang Nanda15bd92015-08-26 10:57:45 +00003334 ret = try_to_find_absolute_address(pev, tevs);
3335 if (ret > 0)
3336 return ret;
3337
Masami Hiramatsubc062232016-07-01 17:03:12 +09003338 /* At first, we need to lookup cache entry */
3339 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003340 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3341 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003342
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003343 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003344 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003345 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003346 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003347
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003348 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003349}
3350
Wang Nan12fae5e2015-09-04 21:16:00 +09003351int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003352{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003353 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003354
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003355 /* Loop 1: convert all events */
3356 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003357 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003358 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003359 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003360 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003361 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003362 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003363 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003364 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003365 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003366 /* This just release blacklist only if allocated */
3367 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003368
Namhyung Kim844dffa2015-09-04 21:15:59 +09003369 return 0;
3370}
3371
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003372static int show_probe_trace_event(struct probe_trace_event *tev)
3373{
3374 char *buf = synthesize_probe_trace_command(tev);
3375
3376 if (!buf) {
3377 pr_debug("Failed to synthesize probe trace event.\n");
3378 return -EINVAL;
3379 }
3380
3381 /* Showing definition always go stdout */
3382 printf("%s\n", buf);
3383 free(buf);
3384
3385 return 0;
3386}
3387
3388int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3389{
3390 struct strlist *namelist = strlist__new(NULL, NULL);
3391 struct probe_trace_event *tev;
3392 struct perf_probe_event *pev;
3393 int i, j, ret = 0;
3394
3395 if (!namelist)
3396 return -ENOMEM;
3397
3398 for (j = 0; j < npevs && !ret; j++) {
3399 pev = &pevs[j];
3400 for (i = 0; i < pev->ntevs && !ret; i++) {
3401 tev = &pev->tevs[i];
3402 /* Skip if the symbol is out of .text or blacklisted */
3403 if (!tev->point.symbol && !pev->uprobes)
3404 continue;
3405
3406 /* Set new name for tev (and update namelist) */
3407 ret = probe_trace_event__set_name(tev, pev,
3408 namelist, true);
3409 if (!ret)
3410 ret = show_probe_trace_event(tev);
3411 }
3412 }
3413 strlist__delete(namelist);
3414
3415 return ret;
3416}
3417
Wang Nan12fae5e2015-09-04 21:16:00 +09003418int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003419{
3420 int i, ret = 0;
3421
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003422 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003423 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003424 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3425 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003426 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003427 if (ret < 0)
3428 break;
3429 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003430 return ret;
3431}
3432
Wang Nan12fae5e2015-09-04 21:16:00 +09003433void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003434{
3435 int i, j;
Krister Johansen544abd42017-07-05 18:48:10 -07003436 struct perf_probe_event *pev;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003437
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003438 /* Loop 3: cleanup and free trace events */
3439 for (i = 0; i < npevs; i++) {
Krister Johansen544abd42017-07-05 18:48:10 -07003440 pev = &pevs[i];
Wang Nan12fae5e2015-09-04 21:16:00 +09003441 for (j = 0; j < pevs[i].ntevs; j++)
3442 clear_probe_trace_event(&pevs[i].tevs[j]);
3443 zfree(&pevs[i].tevs);
3444 pevs[i].ntevs = 0;
Krister Johansen544abd42017-07-05 18:48:10 -07003445 nsinfo__zput(pev->nsi);
Namhyung Kima43aac22015-09-10 11:27:04 +09003446 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003447 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003448}
3449
3450int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3451{
3452 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003453
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003454 ret = init_probe_symbol_maps(pevs->uprobes);
3455 if (ret < 0)
3456 return ret;
3457
Wang Nan12fae5e2015-09-04 21:16:00 +09003458 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003459 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003460 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003461
Wang Nan12fae5e2015-09-04 21:16:00 +09003462 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003463
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003464 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003465 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003466}
3467
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003468int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003469{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003470 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003471 char *str = strfilter__string(filter);
3472
3473 if (!str)
3474 return -EINVAL;
3475
Masami Hiramatsufa282442009-12-08 17:03:23 -05003476 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003477 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3478 if (ret < 0)
3479 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303480
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003481 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003482 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303483 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003484
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003485 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003486 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003487 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003488 goto error;
3489 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003490 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303491
3492error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003493 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303494 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003495 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303496 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003497out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003498 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003499
3500 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003501}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303502
Krister Johansen544abd42017-07-05 18:48:10 -07003503int show_available_funcs(const char *target, struct nsinfo *nsi,
3504 struct strfilter *_filter, bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003505{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003506 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003507 struct map *map;
3508 int ret;
3509
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003510 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003511 if (ret < 0)
3512 return ret;
3513
3514 /* Get a symbol map */
Krister Johansen544abd42017-07-05 18:48:10 -07003515 map = get_target_map(target, nsi, user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003516 if (!map) {
3517 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003518 return -EINVAL;
3519 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003520
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003521 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003522 if (ret) {
3523 if (ret == -2) {
3524 char *str = strfilter__string(_filter);
3525 pr_err("Failed to find symbols matched to \"%s\"\n",
3526 str);
3527 free(str);
3528 } else
3529 pr_err("Failed to load symbols in %s\n",
3530 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003531 goto end;
3532 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03003533 if (!dso__sorted_by_name(map->dso))
3534 dso__sort_by_name(map->dso);
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003535
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003536 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303537 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003538
Davidlohr Bueso7137ff52018-12-06 11:18:17 -08003539 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3540 nd = rb_next(nd)) {
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003541 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3542
3543 if (strfilter__compare(_filter, pos->sym.name))
3544 printf("%s\n", pos->sym.name);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03003545 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003546end:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003547 map__put(map);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003548 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303549
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003550 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303551}
3552
Wang Nanda15bd92015-08-26 10:57:45 +00003553int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3554 struct perf_probe_arg *pvar)
3555{
3556 tvar->value = strdup(pvar->var);
3557 if (tvar->value == NULL)
3558 return -ENOMEM;
3559 if (pvar->type) {
3560 tvar->type = strdup(pvar->type);
3561 if (tvar->type == NULL)
3562 return -ENOMEM;
3563 }
3564 if (pvar->name) {
3565 tvar->name = strdup(pvar->name);
3566 if (tvar->name == NULL)
3567 return -ENOMEM;
3568 } else
3569 tvar->name = NULL;
3570 return 0;
3571}