blob: 2ebf8673f8e95382c8742f30ccb22afc41c77508 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05004 *
5 * Written by Masami Hiramatsu <mhiramat@redhat.com>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05006 */
7
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03008#include <inttypes.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05009#include <sys/utsname.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <errno.h>
14#include <stdio.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050018#include <stdarg.h>
19#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090020#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050021
Masami Hiramatsu31facc52010-03-16 18:05:30 -040022#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050023#include "event.h"
Arnaldo Carvalho de Melo40f3b2d2019-01-22 11:24:34 -020024#include "namespaces.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050025#include "strlist.h"
Arnaldo Carvalho de Melo8ec20b12017-04-18 10:57:25 -030026#include "strfilter.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050027#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050028#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050029#include "color.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010030#include "map.h"
Arnaldo Carvalho de Melo41f30912019-01-27 13:44:29 +010031#include "map_groups.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040032#include "symbol.h"
33#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020034#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030035#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040037#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090038#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053039#include "session.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030040#include "string2.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050041
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030042#include "sane_ctype.h"
43
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050044#define PERFPROBE_GROUP "probe"
45
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040046bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090047struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040048
Masami Hiramatsu146a1432010-04-12 13:17:42 -040049#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050050
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090051int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050052{
53 int ret;
54 va_list ap;
55 va_start(ap, format);
56 ret = vsnprintf(str, size, format, ap);
57 va_end(ap);
58 if (ret >= (int)size)
59 ret = -E2BIG;
60 return ret;
61}
62
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000063static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040064
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090065/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090066int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040067{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040068 int ret;
69
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040070 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090071 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090072 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040073 if (ret < 0) {
74 pr_debug("Failed to init symbol map.\n");
75 goto out;
76 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000078 if (host_machine || user_only) /* already initialized */
79 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030080
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000081 if (symbol_conf.vmlinux_name)
82 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
83
84 host_machine = machine__new_host();
85 if (!host_machine) {
86 pr_debug("machine__new_host() failed.\n");
87 symbol__exit();
88 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090089 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040090out:
91 if (ret < 0)
92 pr_warning("Failed to init vmlinux path.\n");
93 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040094}
95
Namhyung Kim9bae1e82015-09-10 11:27:05 +090096void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000097{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -030098 machine__delete(host_machine);
99 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000100 symbol__exit();
101}
102
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000103static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
104{
105 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
106 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300107 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000108
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300109 if (map__load(map) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000110 return NULL;
111
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300112 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000113 if (!kmap)
114 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000115 return kmap->ref_reloc_sym;
116}
117
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900118static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
119 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000120{
121 struct ref_reloc_sym *reloc_sym;
122 struct symbol *sym;
123 struct map *map;
124
125 /* ref_reloc_sym is just a label. Need a special fix*/
126 reloc_sym = kernel_get_ref_reloc_sym();
127 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900128 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000129 else {
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -0300130 sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900131 if (!sym)
132 return -ENOENT;
133 *addr = map->unmap_ip(map, sym->start) -
134 ((reloc) ? 0 : map->reloc) -
135 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000136 }
137 return 0;
138}
139
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900140static struct map *kernel_get_module_map(const char *module)
141{
Arnaldo Carvalho de Melo68a74182018-04-24 17:06:25 -0300142 struct maps *maps = machine__kernel_maps(host_machine);
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300143 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900144
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900145 /* A file path -- this is an offline module */
146 if (module && strchr(module, '/'))
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900147 return dso__new_map(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900148
Adrian Huntereaeffeb2019-03-04 15:13:21 +0200149 if (!module) {
150 pos = machine__kernel_map(host_machine);
151 return map__get(pos);
152 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900153
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300154 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300155 /* short_name is "[module]" */
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900156 if (strncmp(pos->dso->short_name + 1, module,
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300157 pos->dso->short_name_len - 2) == 0 &&
158 module[pos->dso->short_name_len - 2] == '\0') {
Arnaldo Carvalho de Melof622df52018-05-24 11:17:34 -0300159 return map__get(pos);
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900160 }
161 }
162 return NULL;
163}
164
Krister Johansen544abd42017-07-05 18:48:10 -0700165struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900166{
167 /* Init maps of given executable or kernel */
Krister Johansen544abd42017-07-05 18:48:10 -0700168 if (user) {
169 struct map *map;
170
171 map = dso__new_map(target);
172 if (map && map->dso)
173 map->dso->nsinfo = nsinfo__get(nsi);
174 return map;
175 } else {
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900176 return kernel_get_module_map(target);
Krister Johansen544abd42017-07-05 18:48:10 -0700177 }
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900178}
179
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000180static int convert_exec_to_group(const char *exec, char **result)
181{
182 char *ptr1, *ptr2, *exec_copy;
183 char buf[64];
184 int ret;
185
186 exec_copy = strdup(exec);
187 if (!exec_copy)
188 return -ENOMEM;
189
190 ptr1 = basename(exec_copy);
191 if (!ptr1) {
192 ret = -EINVAL;
193 goto out;
194 }
195
Colin Ian Kingead1a572016-10-03 11:34:31 +0100196 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
Masami Hiramatsu35726d32016-09-24 00:35:16 +0900197 if (!isalnum(*ptr2) && *ptr2 != '_') {
198 *ptr2 = '\0';
199 break;
200 }
201 }
202
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000203 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
204 if (ret < 0)
205 goto out;
206
207 *result = strdup(buf);
208 ret = *result ? 0 : -ENOMEM;
209
210out:
211 free(exec_copy);
212 return ret;
213}
214
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900215static void clear_perf_probe_point(struct perf_probe_point *pp)
216{
217 free(pp->file);
218 free(pp->function);
219 free(pp->lazy_line);
220}
221
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000222static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
223{
224 int i;
225
226 for (i = 0; i < ntevs; i++)
227 clear_probe_trace_event(tevs + i);
228}
229
Masami Hiramatsub0312202015-06-16 20:50:55 +0900230static bool kprobe_blacklist__listed(unsigned long address);
231static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
232{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900233 u64 etext_addr = 0;
234 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000235
Masami Hiramatsub0312202015-06-16 20:50:55 +0900236 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900237 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
238 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000239
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900240 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900241 pr_warning("%s is out of .text, skip it.\n", symbol);
242 else if (kprobe_blacklist__listed(address))
243 pr_warning("%s is blacklisted function, skip it.\n", symbol);
244 else
245 return false;
246
247 return true;
248}
249
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530250/*
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530251 * @module can be module name of module file path. In case of path,
252 * inspect elf and find out what is actual module name.
253 * Caller has to free mod_name after using it.
254 */
255static char *find_module_name(const char *module)
256{
257 int fd;
258 Elf *elf;
259 GElf_Ehdr ehdr;
260 GElf_Shdr shdr;
261 Elf_Data *data;
262 Elf_Scn *sec;
263 char *mod_name = NULL;
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900264 int name_offset;
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530265
266 fd = open(module, O_RDONLY);
267 if (fd < 0)
268 return NULL;
269
270 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
271 if (elf == NULL)
272 goto elf_err;
273
274 if (gelf_getehdr(elf, &ehdr) == NULL)
275 goto ret_err;
276
277 sec = elf_section_by_name(elf, &ehdr, &shdr,
278 ".gnu.linkonce.this_module", NULL);
279 if (!sec)
280 goto ret_err;
281
282 data = elf_getdata(sec, NULL);
283 if (!data || !data->d_buf)
284 goto ret_err;
285
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900286 /*
287 * NOTE:
288 * '.gnu.linkonce.this_module' section of kernel module elf directly
289 * maps to 'struct module' from linux/module.h. This section contains
290 * actual module name which will be used by kernel after loading it.
291 * But, we cannot use 'struct module' here since linux/module.h is not
292 * exposed to user-space. Offset of 'name' has remained same from long
293 * time, so hardcoding it here.
294 */
295 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
296 name_offset = 12;
297 else /* expect ELFCLASS64 by default */
298 name_offset = 24;
299
300 mod_name = strdup((char *)data->d_buf + name_offset);
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530301
302ret_err:
303 elf_end(elf);
304elf_err:
305 close(fd);
306 return mod_name;
307}
308
Ingo Molnar89fe8082013-09-30 12:07:11 +0200309#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000310
311static int kernel_get_module_dso(const char *module, struct dso **pdso)
312{
313 struct dso *dso;
314 struct map *map;
315 const char *vmlinux_name;
316 int ret = 0;
317
318 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300319 char module_name[128];
320
321 snprintf(module_name, sizeof(module_name), "[%s]", module);
Arnaldo Carvalho de Melo83cf7742018-04-24 12:16:09 -0300322 map = map_groups__find_by_name(&host_machine->kmaps, module_name);
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300323 if (map) {
324 dso = map->dso;
325 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000326 }
327 pr_debug("Failed to find module %s.\n", module);
328 return -ENOENT;
329 }
330
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300331 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000332 dso = map->dso;
333
334 vmlinux_name = symbol_conf.vmlinux_name;
335 dso->load_errno = 0;
336 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300337 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000338 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300339 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000340found:
341 *pdso = dso;
342 return ret;
343}
344
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900345/*
346 * Some binaries like glibc have special symbols which are on the symbol
347 * table, but not in the debuginfo. If we can find the address of the
348 * symbol from map, we can translate the address back to the probe point.
349 */
350static int find_alternative_probe_point(struct debuginfo *dinfo,
351 struct perf_probe_point *pp,
352 struct perf_probe_point *result,
Krister Johansen544abd42017-07-05 18:48:10 -0700353 const char *target, struct nsinfo *nsi,
354 bool uprobes)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900355{
356 struct map *map = NULL;
357 struct symbol *sym;
358 u64 address = 0;
359 int ret = -ENOENT;
360
361 /* This can work only for function-name based one */
362 if (!pp->function || pp->file)
363 return -ENOTSUP;
364
Krister Johansen544abd42017-07-05 18:48:10 -0700365 map = get_target_map(target, nsi, uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900366 if (!map)
367 return -EINVAL;
368
369 /* Find the address of given function */
370 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900371 if (uprobes)
372 address = sym->start;
373 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900374 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900375 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900376 }
377 if (!address) {
378 ret = -ENOENT;
379 goto out;
380 }
Wang Nanf6c15622015-04-08 02:14:34 +0000381 pr_debug("Symbol %s address found : %" PRIx64 "\n",
382 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900383
384 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
385 result);
386 if (ret <= 0)
387 ret = (!ret) ? -ENOENT : ret;
388 else {
389 result->offset += pp->offset;
390 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800391 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900392 ret = 0;
393 }
394
395out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +0900396 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900397 return ret;
398
399}
400
401static int get_alternative_probe_event(struct debuginfo *dinfo,
402 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900403 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900404{
405 int ret;
406
407 memcpy(tmp, &pev->point, sizeof(*tmp));
408 memset(&pev->point, 0, sizeof(pev->point));
Krister Johansen544abd42017-07-05 18:48:10 -0700409 ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
410 pev->nsi, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900411 if (ret < 0)
412 memcpy(&pev->point, tmp, sizeof(*tmp));
413
414 return ret;
415}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000416
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900417static int get_alternative_line_range(struct debuginfo *dinfo,
418 struct line_range *lr,
419 const char *target, bool user)
420{
David Ahern6d4a4892015-03-11 10:36:20 -0400421 struct perf_probe_point pp = { .function = lr->function,
422 .file = lr->file,
423 .line = lr->start };
424 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900425 int ret, len = 0;
426
David Ahern6d4a4892015-03-11 10:36:20 -0400427 memset(&result, 0, sizeof(result));
428
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900429 if (lr->end != INT_MAX)
430 len = lr->end - lr->start;
431 ret = find_alternative_probe_point(dinfo, &pp, &result,
Krister Johansen544abd42017-07-05 18:48:10 -0700432 target, NULL, user);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900433 if (!ret) {
434 lr->function = result.function;
435 lr->file = result.file;
436 lr->start = result.line;
437 if (lr->end != INT_MAX)
438 lr->end = lr->start + len;
439 clear_perf_probe_point(&pp);
440 }
441 return ret;
442}
443
Masami Hiramatsuff741782011-06-27 16:27:39 +0900444/* Open new debuginfo of given module */
Krister Johansen544abd42017-07-05 18:48:10 -0700445static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
446 bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900447{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000448 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900449 char reason[STRERR_BUFSIZE];
450 struct debuginfo *ret = NULL;
451 struct dso *dso = NULL;
Krister Johansen544abd42017-07-05 18:48:10 -0700452 struct nscookie nsc;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900453 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900454
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000455 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900456 err = kernel_get_module_dso(module, &dso);
457 if (err < 0) {
458 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300459 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900460 strcpy(reason, "(unknown)");
461 } else
462 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Arnaldo Carvalho de Melo4d6101f2019-02-28 11:22:45 -0300463 if (!silent) {
464 if (module)
465 pr_err("Module %s is not loaded, please specify its full path name.\n", module);
466 else
467 pr_err("Failed to find the path for the kernel: %s\n", reason);
468 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900469 return NULL;
470 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900471 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900472 }
Krister Johansen544abd42017-07-05 18:48:10 -0700473 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000474 ret = debuginfo__new(path);
475 if (!ret && !silent) {
476 pr_warning("The %s file has no debug information.\n", path);
477 if (!module || !strtailcmp(path, ".ko"))
478 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
479 else
480 pr_warning("Rebuild with -g, ");
481 pr_warning("or install an appropriate debuginfo package.\n");
482 }
Krister Johansen544abd42017-07-05 18:48:10 -0700483 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000484 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400485}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300486
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900487/* For caching the last debuginfo */
488static struct debuginfo *debuginfo_cache;
489static char *debuginfo_cache_path;
490
491static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
492{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900493 const char *path = module;
494
495 /* If the module is NULL, it should be the kernel. */
496 if (!module)
497 path = "kernel";
498
499 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900500 goto out;
501
502 /* Copy module path */
503 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900504 debuginfo_cache_path = strdup(path);
505 if (!debuginfo_cache_path) {
506 debuginfo__delete(debuginfo_cache);
507 debuginfo_cache = NULL;
508 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900509 }
510
Krister Johansen544abd42017-07-05 18:48:10 -0700511 debuginfo_cache = open_debuginfo(module, NULL, silent);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900512 if (!debuginfo_cache)
513 zfree(&debuginfo_cache_path);
514out:
515 return debuginfo_cache;
516}
517
518static void debuginfo_cache__exit(void)
519{
520 debuginfo__delete(debuginfo_cache);
521 debuginfo_cache = NULL;
522 zfree(&debuginfo_cache_path);
523}
524
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000525
Krister Johansen544abd42017-07-05 18:48:10 -0700526static int get_text_start_address(const char *exec, unsigned long *address,
527 struct nsinfo *nsi)
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000528{
529 Elf *elf;
530 GElf_Ehdr ehdr;
531 GElf_Shdr shdr;
532 int fd, ret = -ENOENT;
Krister Johansen544abd42017-07-05 18:48:10 -0700533 struct nscookie nsc;
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000534
Krister Johansen544abd42017-07-05 18:48:10 -0700535 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000536 fd = open(exec, O_RDONLY);
Krister Johansen544abd42017-07-05 18:48:10 -0700537 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000538 if (fd < 0)
539 return -errno;
540
541 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900542 if (elf == NULL) {
543 ret = -EINVAL;
544 goto out_close;
545 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000546
547 if (gelf_getehdr(elf, &ehdr) == NULL)
548 goto out;
549
550 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
551 goto out;
552
553 *address = shdr.sh_addr - shdr.sh_offset;
554 ret = 0;
555out:
556 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900557out_close:
558 close(fd);
559
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000560 return ret;
561}
562
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000563/*
564 * Convert trace point to probe point with debuginfo
565 */
566static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
567 struct perf_probe_point *pp,
568 bool is_kprobe)
569{
570 struct debuginfo *dinfo = NULL;
571 unsigned long stext = 0;
572 u64 addr = tp->address;
573 int ret = -ENOENT;
574
575 /* convert the address to dwarf address */
576 if (!is_kprobe) {
577 if (!addr) {
578 ret = -EINVAL;
579 goto error;
580 }
Krister Johansen544abd42017-07-05 18:48:10 -0700581 ret = get_text_start_address(tp->module, &stext, NULL);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000582 if (ret < 0)
583 goto error;
584 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000585 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900586 /* If the module is given, this returns relative address */
587 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
588 false, !!tp->module);
589 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000590 goto error;
591 addr += tp->offset;
592 }
593
594 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
595 tp->module ? : "kernel");
596
Namhyung Kimbb963e12017-02-17 17:17:38 +0900597 dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900598 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000599 ret = debuginfo__find_probe_point(dinfo,
600 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900601 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000602 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000603
604 if (ret > 0) {
605 pp->retprobe = tp->retprobe;
606 return 0;
607 }
608error:
609 pr_debug("Failed to find corresponding probes from debuginfo.\n");
610 return ret ? : -ENOENT;
611}
612
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900613/* Adjust symbol name and address */
614static int post_process_probe_trace_point(struct probe_trace_point *tp,
615 struct map *map, unsigned long offs)
616{
617 struct symbol *sym;
Björn Töpel7598f8b2017-06-21 18:41:34 +0200618 u64 addr = tp->address - offs;
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900619
620 sym = map__find_symbol(map, addr);
621 if (!sym)
622 return -ENOENT;
623
624 if (strcmp(sym->name, tp->symbol)) {
625 /* If we have no realname, use symbol for it */
626 if (!tp->realname)
627 tp->realname = tp->symbol;
628 else
629 free(tp->symbol);
630 tp->symbol = strdup(sym->name);
631 if (!tp->symbol)
632 return -ENOMEM;
633 }
634 tp->offset = addr - sym->start;
635 tp->address -= offs;
636
637 return 0;
638}
639
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900640/*
641 * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
642 * and generate new symbols with suffixes such as .constprop.N or .isra.N
643 * etc. Since those symbols are not recorded in DWARF, we have to find
644 * correct generated symbols from offline ELF binary.
645 * For online kernel or uprobes we don't need this because those are
646 * rebased on _text, or already a section relative address.
647 */
648static int
649post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
650 int ntevs, const char *pathname)
651{
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900652 struct map *map;
653 unsigned long stext = 0;
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900654 int i, ret = 0;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900655
656 /* Prepare a map for offline binary */
657 map = dso__new_map(pathname);
Krister Johansen544abd42017-07-05 18:48:10 -0700658 if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900659 pr_warning("Failed to get ELF symbols for %s\n", pathname);
660 return -EINVAL;
661 }
662
663 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900664 ret = post_process_probe_trace_point(&tevs[i].point,
665 map, stext);
666 if (ret < 0)
667 break;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900668 }
669 map__put(map);
670
Masami Hiramatsu3e96dac2017-01-11 15:00:47 +0900671 return ret;
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900672}
673
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000674static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
Krister Johansen544abd42017-07-05 18:48:10 -0700675 int ntevs, const char *exec,
676 struct nsinfo *nsi)
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000677{
678 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000679 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000680
681 if (!exec)
682 return 0;
683
Krister Johansen544abd42017-07-05 18:48:10 -0700684 ret = get_text_start_address(exec, &stext, nsi);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000685 if (ret < 0)
686 return ret;
687
688 for (i = 0; i < ntevs && ret >= 0; i++) {
Ingo Molnaradba1632018-12-03 11:22:00 +0100689 /* point.address is the address of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000690 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000691 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000692 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000693 ret = -ENOMEM;
694 break;
695 }
696 tevs[i].uprobes = true;
697 }
698
699 return ret;
700}
701
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900702static int
703post_process_module_probe_trace_events(struct probe_trace_event *tevs,
704 int ntevs, const char *module,
705 struct debuginfo *dinfo)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900706{
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900707 Dwarf_Addr text_offs = 0;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900708 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530709 char *mod_name = NULL;
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900710 struct map *map;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900711
712 if (!module)
713 return 0;
714
Krister Johansen544abd42017-07-05 18:48:10 -0700715 map = get_target_map(module, NULL, false);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900716 if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
717 pr_warning("Failed to get ELF symbols for %s\n", module);
718 return -EINVAL;
719 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900720
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900721 mod_name = find_module_name(module);
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900722 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900723 ret = post_process_probe_trace_point(&tevs[i].point,
724 map, (unsigned long)text_offs);
725 if (ret < 0)
726 break;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530727 tevs[i].point.module =
728 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900729 if (!tevs[i].point.module) {
730 ret = -ENOMEM;
731 break;
732 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900733 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900734
Ravi Bangoria63a29612016-04-26 19:55:41 +0530735 free(mod_name);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900736 map__put(map);
737
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900738 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900739}
740
Ravi Bangoriad8204562016-08-09 01:23:24 -0500741static int
742post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
743 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000744{
745 struct ref_reloc_sym *reloc_sym;
746 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900747 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000748
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900749 /* Skip post process if the target is an offline kernel */
750 if (symbol_conf.ignore_vmlinux_buildid)
Masami Hiramatsu8a937a22017-01-04 12:30:19 +0900751 return post_process_offline_probe_trace_events(tevs, ntevs,
752 symbol_conf.vmlinux_name);
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900753
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000754 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000755 if (!reloc_sym) {
756 pr_warning("Relocated base symbol is not found!\n");
757 return -EINVAL;
758 }
759
760 for (i = 0; i < ntevs; i++) {
Naveen N. Rao7ab31d92017-03-08 13:56:09 +0530761 if (!tevs[i].point.address)
762 continue;
763 if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
Masami Hiramatsub0312202015-06-16 20:50:55 +0900764 continue;
765 /* If we found a wrong one, mark it by NULL symbol */
766 if (kprobe_warn_out_range(tevs[i].point.symbol,
767 tevs[i].point.address)) {
768 tmp = NULL;
769 skipped++;
770 } else {
771 tmp = strdup(reloc_sym->name);
772 if (!tmp)
773 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000774 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900775 /* If we have no realname, use symbol for it */
776 if (!tevs[i].point.realname)
777 tevs[i].point.realname = tevs[i].point.symbol;
778 else
779 free(tevs[i].point.symbol);
780 tevs[i].point.symbol = tmp;
781 tevs[i].point.offset = tevs[i].point.address -
782 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000783 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900784 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000785}
786
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500787void __weak
788arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
789 int ntevs __maybe_unused)
790{
791}
792
Ravi Bangoriad8204562016-08-09 01:23:24 -0500793/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500794static int post_process_probe_trace_events(struct perf_probe_event *pev,
795 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500796 int ntevs, const char *module,
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900797 bool uprobe, struct debuginfo *dinfo)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500798{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500799 int ret;
800
Ravi Bangoriad8204562016-08-09 01:23:24 -0500801 if (uprobe)
Krister Johansen544abd42017-07-05 18:48:10 -0700802 ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
803 pev->nsi);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500804 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500805 /* Currently ref_reloc_sym based probe is not for drivers */
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900806 ret = post_process_module_probe_trace_events(tevs, ntevs,
807 module, dinfo);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500808 else
809 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500810
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500811 if (ret >= 0)
812 arch__post_process_probe_trace_events(pev, ntevs);
813
814 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500815}
816
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300817/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530818static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900819 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300820{
821 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900822 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530823 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900824 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300825
Krister Johansen544abd42017-07-05 18:48:10 -0700826 dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900827 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000828 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900829 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900830 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831 return 0;
832 }
833
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000834 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900835 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900836 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900837
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900838 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900839 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900840 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900841 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900842 /*
843 * Write back to the original probe_event for
844 * setting appropriate (user given) event name
845 */
846 clear_perf_probe_point(&pev->point);
847 memcpy(&pev->point, &tmp, sizeof(tmp));
848 }
849 }
850
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400851 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000852 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500853 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900854 pev->target, pev->uprobes, dinfo);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900855 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900856 pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000857 clear_probe_trace_events(*tevs, ntevs);
858 zfree(tevs);
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900859 ntevs = 0;
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000860 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400861 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300862
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900863 debuginfo__delete(dinfo);
864
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400865 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900866 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400867 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900868 return -ENOENT;
Masami Hiramatsu613f0502017-01-11 15:01:57 +0900869 } else if (ntevs < 0) {
870 /* Error path : ntevs < 0 */
871 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000872 if (ntevs == -EBADF)
873 pr_warning("Warning: No dwarf info found in the vmlinux - "
874 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400875 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900876 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400877 return 0;
878 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300879 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400880 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300881}
882
883#define LINEBUF_SIZE 256
884#define NR_ADDITIONAL_LINES 2
885
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100886static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300887{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000888 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100889 const char *color = show_num ? "" : PERF_COLOR_BLUE;
890 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300891
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100892 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300893 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
894 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100895 if (skip)
896 continue;
897 if (!prefix) {
898 prefix = show_num ? "%7d " : " ";
899 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300900 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100901 color_fprintf(stdout, color, "%s", buf);
902
903 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400904
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100905 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300906error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100907 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000908 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300909 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100910 return -1;
911 }
912 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300913}
914
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100915static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
916{
917 int rv = __show_one_line(fp, l, skip, show_num);
918 if (rv == 0) {
919 pr_warning("Source file is shorter than expected.\n");
920 rv = -1;
921 }
922 return rv;
923}
924
925#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
926#define show_one_line(f,l) _show_one_line(f,l,false,false)
927#define skip_one_line(f,l) _show_one_line(f,l,true,false)
928#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
929
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300930/*
931 * Show line-range always requires debuginfo to find source file and
932 * line number.
933 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900934static int __show_line_range(struct line_range *lr, const char *module,
935 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300936{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400937 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000938 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900939 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300940 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900941 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900942 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000943 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300944
945 /* Search a line range */
Krister Johansen544abd42017-07-05 18:48:10 -0700946 dinfo = open_debuginfo(module, NULL, false);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000947 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900948 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400949
Masami Hiramatsuff741782011-06-27 16:27:39 +0900950 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900951 if (!ret) { /* Not found, retry with an alternative */
952 ret = get_alternative_line_range(dinfo, lr, module, user);
953 if (!ret)
954 ret = debuginfo__find_line_range(dinfo, lr);
955 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900956 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000957 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400958 pr_warning("Specified source line is not found.\n");
959 return -ENOENT;
960 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000961 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400962 return ret;
963 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300964
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900965 /* Convert source file path */
966 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900967 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800968
969 /* Free old path when new path is assigned */
970 if (tmp != lr->path)
971 free(tmp);
972
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900973 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000974 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900975 return ret;
976 }
977
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300978 setup_pager();
979
980 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900981 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300982 lr->start - lr->offset);
983 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100984 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985
986 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400987 if (fp == NULL) {
988 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300989 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400990 return -errno;
991 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300992 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100993 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100994 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100995 if (ret < 0)
996 goto end;
997 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300998
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300999 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001000 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001001 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001002 if (ret < 0)
1003 goto end;
1004 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001005 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001006 if (ret < 0)
1007 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001008 }
1009
1010 if (lr->end == INT_MAX)
1011 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +01001012 while (l <= lr->end) {
1013 ret = show_one_line_or_eof(fp, l++ - lr->offset);
1014 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +01001015 break;
1016 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001017end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001018 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001019 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001020}
1021
Krister Johansen544abd42017-07-05 18:48:10 -07001022int show_line_range(struct line_range *lr, const char *module,
1023 struct nsinfo *nsi, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001024{
1025 int ret;
Krister Johansen544abd42017-07-05 18:48:10 -07001026 struct nscookie nsc;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001027
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001028 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001029 if (ret < 0)
1030 return ret;
Krister Johansen544abd42017-07-05 18:48:10 -07001031 nsinfo__mountns_enter(nsi, &nsc);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +09001032 ret = __show_line_range(lr, module, user);
Krister Johansen544abd42017-07-05 18:48:10 -07001033 nsinfo__mountns_exit(&nsc);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001034 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001035
1036 return ret;
1037}
1038
Masami Hiramatsuff741782011-06-27 16:27:39 +09001039static int show_available_vars_at(struct debuginfo *dinfo,
1040 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001041 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001042{
1043 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001044 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001045 struct str_node *node;
1046 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001047 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001048 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001049
1050 buf = synthesize_perf_probe_point(&pev->point);
1051 if (!buf)
1052 return -EINVAL;
1053 pr_debug("Searching variables at %s\n", buf);
1054
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001055 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001056 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +09001057 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001058 if (!ret) {
1059 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001060 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001061 /* Release the old probe_point */
1062 clear_perf_probe_point(&tmp);
1063 }
1064 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001065 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001066 if (ret == 0 || ret == -ENOENT) {
1067 pr_err("Failed to find the address of %s\n", buf);
1068 ret = -ENOENT;
1069 } else
1070 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001071 goto end;
1072 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +00001073
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001074 /* Some variables are found */
1075 fprintf(stdout, "Available variables at %s\n", buf);
1076 for (i = 0; i < ret; i++) {
1077 vl = &vls[i];
1078 /*
1079 * A probe point might be converted to
1080 * several trace points.
1081 */
1082 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1083 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001084 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001085 nvars = 0;
1086 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001087 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001088 var = strchr(node->s, '\t') + 1;
1089 if (strfilter__compare(_filter, var)) {
1090 fprintf(stdout, "\t\t%s\n", node->s);
1091 nvars++;
1092 }
1093 }
1094 strlist__delete(vl->vars);
1095 }
1096 if (nvars == 0)
1097 fprintf(stdout, "\t\t(No matched variables)\n");
1098 }
1099 free(vls);
1100end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001101 free(buf);
1102 return ret;
1103}
1104
1105/* Show available variables on given probe point */
1106int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001107 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001108{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001109 int i, ret = 0;
1110 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001111
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001112 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001113 if (ret < 0)
1114 return ret;
1115
Krister Johansen544abd42017-07-05 18:48:10 -07001116 dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001117 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001118 ret = -ENOENT;
1119 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001120 }
1121
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001122 setup_pager();
1123
Masami Hiramatsuff741782011-06-27 16:27:39 +09001124 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001125 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001126
1127 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001128out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001129 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001130 return ret;
1131}
1132
Ingo Molnar89fe8082013-09-30 12:07:11 +02001133#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001134
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001135static void debuginfo_cache__exit(void)
1136{
1137}
1138
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001139static int
1140find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1141 struct perf_probe_point *pp __maybe_unused,
1142 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001143{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001144 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001145}
1146
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301147static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001148 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001149{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001150 if (perf_probe_event_need_dwarf(pev)) {
1151 pr_warning("Debuginfo-analysis is not supported.\n");
1152 return -ENOSYS;
1153 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301154
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001155 return 0;
1156}
1157
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001158int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001159 const char *module __maybe_unused,
Krister Johansen544abd42017-07-05 18:48:10 -07001160 struct nsinfo *nsi __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001161 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001162{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001163 pr_warning("Debuginfo-analysis is not supported.\n");
1164 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001165}
1166
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001167int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001168 int npevs __maybe_unused,
1169 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001170{
1171 pr_warning("Debuginfo-analysis is not supported.\n");
1172 return -ENOSYS;
1173}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001174#endif
1175
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001176void line_range__clear(struct line_range *lr)
1177{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001178 free(lr->function);
1179 free(lr->file);
1180 free(lr->path);
1181 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001182 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001183 memset(lr, 0, sizeof(*lr));
1184}
1185
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001186int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001187{
1188 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001189 lr->line_list = intlist__new(NULL);
1190 if (!lr->line_list)
1191 return -ENOMEM;
1192 else
1193 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001194}
1195
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001196static int parse_line_num(char **ptr, int *val, const char *what)
1197{
1198 const char *start = *ptr;
1199
1200 errno = 0;
1201 *val = strtol(*ptr, ptr, 0);
1202 if (errno || *ptr == start) {
1203 semantic_error("'%s' is not a valid number.\n", what);
1204 return -EINVAL;
1205 }
1206 return 0;
1207}
1208
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001209/* Check the name is good for event, group or function */
1210static bool is_c_func_name(const char *name)
1211{
1212 if (!isalpha(*name) && *name != '_')
1213 return false;
1214 while (*++name != '\0') {
1215 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1216 return false;
1217 }
1218 return true;
1219}
1220
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001221/*
1222 * Stuff 'lr' according to the line range described by 'arg'.
1223 * The line range syntax is described by:
1224 *
1225 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001226 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001227 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001229{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001230 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001231 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001232
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001233 if (!name)
1234 return -ENOMEM;
1235
1236 lr->start = 0;
1237 lr->end = INT_MAX;
1238
1239 range = strchr(name, ':');
1240 if (range) {
1241 *range++ = '\0';
1242
1243 err = parse_line_num(&range, &lr->start, "start line");
1244 if (err)
1245 goto err;
1246
1247 if (*range == '+' || *range == '-') {
1248 const char c = *range++;
1249
1250 err = parse_line_num(&range, &lr->end, "end line");
1251 if (err)
1252 goto err;
1253
1254 if (c == '+') {
1255 lr->end += lr->start;
1256 /*
1257 * Adjust the number of lines here.
1258 * If the number of lines == 1, the
1259 * the end of line should be equal to
1260 * the start of line.
1261 */
1262 lr->end--;
1263 }
1264 }
1265
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001266 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001267
1268 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001269 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001270 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001271 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001272 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001274 if (*range != '\0') {
1275 semantic_error("Tailing with invalid str '%s'.\n", range);
1276 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001277 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001278 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001279
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001280 file = strchr(name, '@');
1281 if (file) {
1282 *file = '\0';
1283 lr->file = strdup(++file);
1284 if (lr->file == NULL) {
1285 err = -ENOMEM;
1286 goto err;
1287 }
1288 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001289 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001290 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001291 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001292 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001293 else { /* Invalid name */
1294 semantic_error("'%s' is not a valid function name.\n", name);
1295 err = -EINVAL;
1296 goto err;
1297 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001298
1299 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001300err:
1301 free(name);
1302 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001303}
1304
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001305static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1306{
1307 char *ptr;
1308
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001309 ptr = strpbrk_esc(*arg, ":");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001310 if (ptr) {
1311 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001312 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001313 goto ng_name;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001314 pev->group = strdup_esc(*arg);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001315 if (!pev->group)
1316 return -ENOMEM;
1317 *arg = ptr + 1;
1318 } else
1319 pev->group = NULL;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001320
1321 pev->event = strdup_esc(*arg);
1322 if (pev->event == NULL)
1323 return -ENOMEM;
1324
1325 if (!pev->sdt && !is_c_func_name(pev->event)) {
1326 zfree(&pev->event);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001327ng_name:
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001328 zfree(&pev->group);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001329 semantic_error("%s is bad for event name -it must "
1330 "follow C symbol-naming rule.\n", *arg);
1331 return -EINVAL;
1332 }
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001333 return 0;
1334}
1335
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001336/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001337static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001338{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001339 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001340 char *ptr, *tmp;
1341 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301342 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001343 int ret;
1344
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001345 /*
1346 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001347 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1348 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001349 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001350 */
Wang Nane59d29e2015-04-28 08:46:09 +00001351 if (!arg)
1352 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001353
Ravi Bangoriaaf9100a2017-03-14 20:36:52 +05301354 if (is_sdt_event(arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001355 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001356 if (arg[0] == '%')
1357 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001358 }
1359
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001360 ptr = strpbrk_esc(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001361 if (pev->sdt) {
1362 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001363 if (*ptr != '@') {
1364 semantic_error("%s must be an SDT name.\n",
1365 arg);
1366 return -EINVAL;
1367 }
1368 /* This must be a target file name or build id */
1369 tmp = build_id_cache__complement(ptr + 1);
1370 if (tmp) {
1371 pev->target = build_id_cache__origname(tmp);
1372 free(tmp);
1373 } else
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001374 pev->target = strdup_esc(ptr + 1);
Masami Hiramatsua5981802016-07-12 19:05:37 +09001375 if (!pev->target)
1376 return -ENOMEM;
1377 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001378 }
1379 ret = parse_perf_probe_event_name(&arg, pev);
1380 if (ret == 0) {
1381 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1382 ret = -errno;
1383 }
1384 return ret;
1385 }
1386
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001387 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001388 *ptr = '\0';
1389 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001390 ret = parse_perf_probe_event_name(&arg, pev);
1391 if (ret < 0)
1392 return ret;
1393
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001394 arg = tmp;
1395 }
1396
Naveen N. Rao3099c022015-04-28 17:35:34 +05301397 /*
1398 * Check arg is function or file name and copy it.
1399 *
1400 * We consider arg to be a file spec if and only if it satisfies
1401 * all of the below criteria::
1402 * - it does not include any of "+@%",
1403 * - it includes one of ":;", and
1404 * - it has a period '.' in the name.
1405 *
1406 * Otherwise, we consider arg to be a function specification.
1407 */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001408 if (!strpbrk_esc(arg, "+@%")) {
1409 ptr = strpbrk_esc(arg, ";:");
Naveen N. Rao3099c022015-04-28 17:35:34 +05301410 /* This is a file spec if it includes a '.' before ; or : */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001411 if (ptr && memchr(arg, '.', ptr - arg))
Naveen N. Rao3099c022015-04-28 17:35:34 +05301412 file_spec = true;
1413 }
1414
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001415 ptr = strpbrk_esc(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001416 if (ptr) {
1417 nc = *ptr;
1418 *ptr++ = '\0';
1419 }
1420
Wang Nan6c6e0242015-08-26 10:57:44 +00001421 if (arg[0] == '\0')
1422 tmp = NULL;
1423 else {
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001424 tmp = strdup_esc(arg);
Wang Nan6c6e0242015-08-26 10:57:44 +00001425 if (tmp == NULL)
1426 return -ENOMEM;
1427 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001428
Naveen N. Rao3099c022015-04-28 17:35:34 +05301429 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001430 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001431 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001432 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001433
Wang Nanda15bd92015-08-26 10:57:45 +00001434 /*
1435 * Keep pp->function even if this is absolute address,
1436 * so it can mark whether abs_address is valid.
1437 * Which make 'perf probe lib.bin 0x0' possible.
1438 *
1439 * Note that checking length of tmp is not needed
1440 * because when we access tmp[1] we know tmp[0] is '0',
1441 * so tmp[1] should always valid (but could be '\0').
1442 */
1443 if (tmp && !strncmp(tmp, "0x", 2)) {
1444 pp->abs_address = strtoul(pp->function, &tmp, 0);
1445 if (*tmp != '\0') {
1446 semantic_error("Invalid absolute address.\n");
1447 return -EINVAL;
1448 }
1449 }
1450 }
1451
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001452 /* Parse other options */
1453 while (ptr) {
1454 arg = ptr;
1455 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001456 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001457 pp->lazy_line = strdup(arg); /* let leave escapes */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001458 if (pp->lazy_line == NULL)
1459 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001460 break;
1461 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001462 ptr = strpbrk_esc(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001463 if (ptr) {
1464 nc = *ptr;
1465 *ptr++ = '\0';
1466 }
1467 switch (c) {
1468 case ':': /* Line number */
1469 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001470 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001471 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001472 " in line number.\n");
1473 return -EINVAL;
1474 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001475 break;
1476 case '+': /* Byte offset from a symbol */
1477 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001479 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001480 " in offset.\n");
1481 return -EINVAL;
1482 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001483 break;
1484 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001485 if (pp->file) {
1486 semantic_error("SRC@SRC is not allowed.\n");
1487 return -EINVAL;
1488 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09001489 pp->file = strdup_esc(arg);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001490 if (pp->file == NULL)
1491 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001492 break;
1493 case '%': /* Probe places */
1494 if (strcmp(arg, "return") == 0) {
1495 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001496 } else { /* Others not supported yet */
1497 semantic_error("%%%s is not supported.\n", arg);
1498 return -ENOTSUP;
1499 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001500 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001501 default: /* Buggy case */
1502 pr_err("This program has a bug at %s:%d.\n",
1503 __FILE__, __LINE__);
1504 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001505 break;
1506 }
1507 }
1508
1509 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001511 semantic_error("Lazy pattern can't be used with"
1512 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001513 return -EINVAL;
1514 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001515
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001516 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001517 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001518 return -EINVAL;
1519 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001520
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001521 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001522 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001523 return -EINVAL;
1524 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001525
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001526 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001527 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001528 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001529 return -EINVAL;
1530 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001531
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001532 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001533 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001534 return -EINVAL;
1535 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001536
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001537 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001538 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001539 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001540 return -EINVAL;
1541 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001542
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001543 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001544 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1545 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001546 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001547}
1548
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001549/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001551{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001552 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001553 struct perf_probe_arg_field **fieldp;
1554
1555 pr_debug("parsing arg: %s into ", str);
1556
Masami Hiramatsu48481932010-04-12 13:16:53 -04001557 tmp = strchr(str, '=');
1558 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001559 arg->name = strndup(str, tmp - str);
1560 if (arg->name == NULL)
1561 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001562 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001563 str = tmp + 1;
1564 }
1565
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001566 tmp = strchr(str, ':');
1567 if (tmp) { /* Type setting */
1568 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001569 arg->type = strdup(tmp + 1);
1570 if (arg->type == NULL)
1571 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001572 pr_debug("type:%s ", arg->type);
1573 }
1574
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001575 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001576 if (!is_c_varname(str) || !tmp) {
1577 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001578 arg->var = strdup(str);
1579 if (arg->var == NULL)
1580 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001581 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001583 }
1584
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001585 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001586 arg->var = strndup(str, tmp - str);
1587 if (arg->var == NULL)
1588 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001589 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001590 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001591 fieldp = &arg->field;
1592
1593 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001594 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1595 if (*fieldp == NULL)
1596 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001597 if (*tmp == '[') { /* Array */
1598 str = tmp;
1599 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001600 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001601 if (*tmp != ']' || tmp == str + 1) {
1602 semantic_error("Array index must be a"
1603 " number.\n");
1604 return -EINVAL;
1605 }
1606 tmp++;
1607 if (*tmp == '\0')
1608 tmp = NULL;
1609 } else { /* Structure */
1610 if (*tmp == '.') {
1611 str = tmp + 1;
1612 (*fieldp)->ref = false;
1613 } else if (tmp[1] == '>') {
1614 str = tmp + 2;
1615 (*fieldp)->ref = true;
1616 } else {
1617 semantic_error("Argument parse error: %s\n",
1618 str);
1619 return -EINVAL;
1620 }
1621 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001622 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001623 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001624 (*fieldp)->name = strndup(str, tmp - str);
1625 if ((*fieldp)->name == NULL)
1626 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001627 if (*str != '[')
1628 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001629 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1630 fieldp = &(*fieldp)->next;
1631 }
1632 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001633 (*fieldp)->name = strdup(str);
1634 if ((*fieldp)->name == NULL)
1635 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001636 if (*str != '[')
1637 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001638 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001639
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001640 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001641 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001642 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001643 if (arg->name == NULL)
1644 return -ENOMEM;
1645 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001646 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001647}
1648
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001649/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001650int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001651{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001652 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001653 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001654
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001655 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001656 if (!argv) {
1657 pr_debug("Failed to split arguments.\n");
1658 return -ENOMEM;
1659 }
1660 if (argc - 1 > MAX_PROBE_ARGS) {
1661 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1662 ret = -ERANGE;
1663 goto out;
1664 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001665 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001666 ret = parse_perf_probe_point(argv[0], pev);
1667 if (ret < 0)
1668 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001669
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001670 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001671 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001672 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1673 if (pev->args == NULL) {
1674 ret = -ENOMEM;
1675 goto out;
1676 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001677 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1678 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1679 if (ret >= 0 &&
1680 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001681 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001682 " kretprobe.\n");
1683 ret = -EINVAL;
1684 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001685 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001686out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001687 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001688
1689 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001690}
1691
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301692/* Returns true if *any* ARG is either C variable, $params or $vars. */
1693bool perf_probe_with_var(struct perf_probe_event *pev)
1694{
1695 int i = 0;
1696
1697 for (i = 0; i < pev->nargs; i++)
1698 if (is_c_varname(pev->args[i].var) ||
1699 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1700 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1701 return true;
1702 return false;
1703}
1704
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001705/* Return true if this perf_probe_event requires debuginfo */
1706bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001707{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001708 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1709 return true;
1710
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301711 if (perf_probe_with_var(pev))
1712 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001713
1714 return false;
1715}
1716
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301717/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001718int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301720 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001721 char pr;
1722 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001723 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001724 int ret, i, argc;
1725 char **argv;
1726
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301727 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001728 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001729 if (!argv) {
1730 pr_debug("Failed to split arguments.\n");
1731 return -ENOMEM;
1732 }
1733 if (argc < 2) {
1734 semantic_error("Too few probe arguments.\n");
1735 ret = -ERANGE;
1736 goto out;
1737 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001738
1739 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001740 argv0_str = strdup(argv[0]);
1741 if (argv0_str == NULL) {
1742 ret = -ENOMEM;
1743 goto out;
1744 }
1745 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1746 fmt2_str = strtok_r(NULL, "/", &fmt);
1747 fmt3_str = strtok_r(NULL, " \t", &fmt);
1748 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1749 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001750 semantic_error("Failed to parse event name: %s\n", argv[0]);
1751 ret = -EINVAL;
1752 goto out;
1753 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001754 pr = fmt1_str[0];
1755 tev->group = strdup(fmt2_str);
1756 tev->event = strdup(fmt3_str);
1757 if (tev->group == NULL || tev->event == NULL) {
1758 ret = -ENOMEM;
1759 goto out;
1760 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001761 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001762
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001763 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001764
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001765 /* Scan module name(if there), function name and offset */
1766 p = strchr(argv[1], ':');
1767 if (p) {
1768 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001769 if (!tp->module) {
1770 ret = -ENOMEM;
1771 goto out;
1772 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001773 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001774 p++;
1775 } else
1776 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001777 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001778 /* only the address started with 0x */
1779 if (fmt1_str[0] == '0') {
1780 /*
1781 * Fix a special case:
1782 * if address == 0, kernel reports something like:
1783 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1784 * Newer kernel may fix that, but we want to
1785 * support old kernel also.
1786 */
1787 if (strcmp(fmt1_str, "0x") == 0) {
1788 if (!argv[2] || strcmp(argv[2], "(null)")) {
1789 ret = -EINVAL;
1790 goto out;
1791 }
1792 tp->address = 0;
1793
1794 free(argv[2]);
1795 for (i = 2; argv[i + 1] != NULL; i++)
1796 argv[i] = argv[i + 1];
1797
1798 argv[i] = NULL;
1799 argc -= 1;
1800 } else
1801 tp->address = strtoul(fmt1_str, NULL, 0);
1802 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001803 /* Only the symbol-based probe has offset */
1804 tp->symbol = strdup(fmt1_str);
1805 if (tp->symbol == NULL) {
1806 ret = -ENOMEM;
1807 goto out;
1808 }
1809 fmt2_str = strtok_r(NULL, "", &fmt);
1810 if (fmt2_str == NULL)
1811 tp->offset = 0;
1812 else
1813 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001814 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001815
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05301816 if (tev->uprobes) {
1817 fmt2_str = strchr(p, '(');
1818 if (fmt2_str)
1819 tp->ref_ctr_offset = strtoul(fmt2_str + 1, NULL, 0);
1820 }
1821
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001822 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301823 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001824 if (tev->args == NULL) {
1825 ret = -ENOMEM;
1826 goto out;
1827 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001828 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001829 p = strchr(argv[i + 2], '=');
1830 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001831 *p++ = '\0';
1832 else
1833 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001834 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001835 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 tev->args[i].value = strdup(p);
1837 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1838 ret = -ENOMEM;
1839 goto out;
1840 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001841 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001842 ret = 0;
1843out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001844 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001845 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001846 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001847}
1848
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001849/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001850char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001851{
1852 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001853 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001854 char *ret = NULL;
1855 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001856
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001857 if (strbuf_init(&buf, 64) < 0)
1858 return NULL;
1859
Masami Hiramatsu48481932010-04-12 13:16:53 -04001860 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001861 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001862 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001863 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1864 if (err)
1865 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001866
1867 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001868 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001869 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001870 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001871 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1872 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001873 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001874 if (err)
1875 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001876 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001877
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001878 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001879 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1880 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001881
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001882 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001883out:
1884 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001885 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001886}
1887
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001889char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001890{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001891 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001892 char *tmp, *ret = NULL;
1893 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001894
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001895 if (strbuf_init(&buf, 64) < 0)
1896 return NULL;
1897
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001898 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001899 if (strbuf_addstr(&buf, pp->function) < 0)
1900 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001901 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001902 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001903 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001904 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001905 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001906 err = strbuf_addstr(&buf, "%return");
1907 if (err)
1908 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001909 }
1910 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001911 tmp = pp->file;
1912 len = strlen(tmp);
1913 if (len > 30) {
1914 tmp = strchr(pp->file + len - 30, '/');
1915 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1916 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001917 err = strbuf_addf(&buf, "@%s", tmp);
1918 if (!err && !pp->function && pp->line)
1919 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001920 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001921 if (!err)
1922 ret = strbuf_detach(&buf, NULL);
1923out:
1924 strbuf_release(&buf);
1925 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001926}
1927
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001928char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1929{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001930 struct strbuf buf;
1931 char *tmp, *ret = NULL;
1932 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001933
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001934 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001935 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001936 if (pev->event)
1937 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1938 pev->event) < 0)
1939 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001940
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001941 tmp = synthesize_perf_probe_point(&pev->point);
1942 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1943 goto out;
1944 free(tmp);
1945
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001946 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001947 tmp = synthesize_perf_probe_arg(pev->args + i);
1948 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1949 goto out;
1950 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001951 }
1952
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001953 ret = strbuf_detach(&buf, NULL);
1954out:
1955 strbuf_release(&buf);
1956 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001957}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001958
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301959static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001960 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001961{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001962 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001963 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301964 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001965 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001966 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001967 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001968 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001969 err = strbuf_addf(buf, "%+ld(", ref->offset);
1970 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001971}
1972
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301973static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001974 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001975{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301976 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001977 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001978
1979 /* Argument name or separator */
1980 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001981 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001982 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001983 err = strbuf_addch(buf, ' ');
1984 if (err)
1985 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001986
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001987 /* Special case: @XXX */
1988 if (arg->value[0] == '@' && arg->ref)
1989 ref = ref->next;
1990
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001991 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001992 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001993 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001994 if (depth < 0)
1995 return depth;
1996 }
1997
1998 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001999 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002000 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04002001 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002002 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002003
2004 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002005 while (!err && depth--)
2006 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002007
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002008 /* Print argument type */
2009 if (!err && arg->type)
2010 err = strbuf_addf(buf, ":%s", arg->type);
2011
2012 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002013}
2014
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302015static int
2016synthesize_uprobe_trace_def(struct probe_trace_event *tev, struct strbuf *buf)
2017{
2018 struct probe_trace_point *tp = &tev->point;
2019 int err;
2020
2021 err = strbuf_addf(buf, "%s:0x%lx", tp->module, tp->address);
2022
2023 if (err >= 0 && tp->ref_ctr_offset) {
2024 if (!uprobe_ref_ctr_is_supported())
2025 return -1;
2026 err = strbuf_addf(buf, "(0x%lx)", tp->ref_ctr_offset);
2027 }
2028 return err >= 0 ? 0 : -1;
2029}
2030
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302031char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002032{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302033 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002034 struct strbuf buf;
2035 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002036 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002037
Wang Nanda15bd92015-08-26 10:57:45 +00002038 /* Uprobes must have tp->module */
2039 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002040 return NULL;
2041
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002042 if (strbuf_init(&buf, 32) < 0)
2043 return NULL;
2044
2045 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
2046 tev->group, tev->event) < 0)
2047 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00002048 /*
2049 * If tp->address == 0, then this point must be a
2050 * absolute address uprobe.
2051 * try_to_find_absolute_address() should have made
2052 * tp->symbol to "0x0".
2053 */
2054 if (tev->uprobes && !tp->address) {
2055 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
2056 goto error;
2057 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002058
2059 /* Use the tp->address for uprobes */
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302060 if (tev->uprobes) {
2061 err = synthesize_uprobe_trace_def(tev, &buf);
2062 } else if (!strncmp(tp->symbol, "0x", 2)) {
Wang Nanda15bd92015-08-26 10:57:45 +00002063 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002064 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
2065 tp->module ? ":" : "", tp->address);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302066 } else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002067 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
2068 tp->module ? ":" : "", tp->symbol, tp->offset);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302069 }
2070
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002071 if (err)
2072 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302073
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002074 for (i = 0; i < tev->nargs; i++)
2075 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002076 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002077
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002078 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002079error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002080 strbuf_release(&buf);
2081 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002082}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002083
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002084static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
2085 struct perf_probe_point *pp,
2086 bool is_kprobe)
2087{
2088 struct symbol *sym = NULL;
Arnaldo Carvalho de Melo8a2efd62017-02-14 15:28:41 -03002089 struct map *map = NULL;
Wang Nane4863672015-08-25 13:27:35 +00002090 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002091 int ret = -ENOENT;
2092
2093 if (!is_kprobe) {
2094 map = dso__new_map(tp->module);
2095 if (!map)
2096 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002097 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002098 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002099 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002100 if (kernel_get_symbol_address_by_name(tp->symbol,
2101 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002102 goto out;
2103 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002104 if (addr) {
2105 addr += tp->offset;
Arnaldo Carvalho de Melo107cad92018-04-30 12:20:54 -03002106 sym = machine__find_kernel_symbol(host_machine, addr, &map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002107 }
2108 }
Wang Nan98d3b252015-11-05 13:19:25 +00002109
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002110 if (!sym)
2111 goto out;
2112
2113 pp->retprobe = tp->retprobe;
2114 pp->offset = addr - map->unmap_ip(map, sym->start);
2115 pp->function = strdup(sym->name);
2116 ret = pp->function ? 0 : -ENOMEM;
2117
2118out:
2119 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002120 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002121 }
2122
2123 return ret;
2124}
2125
2126static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002127 struct perf_probe_point *pp,
2128 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002129{
2130 char buf[128];
2131 int ret;
2132
2133 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2134 if (!ret)
2135 return 0;
2136 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2137 if (!ret)
2138 return 0;
2139
2140 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2141
2142 if (tp->symbol) {
2143 pp->function = strdup(tp->symbol);
2144 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002145 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002146 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2147 if (ret < 0)
2148 return ret;
2149 pp->function = strdup(buf);
2150 pp->offset = 0;
2151 }
2152 if (pp->function == NULL)
2153 return -ENOMEM;
2154
2155 pp->retprobe = tp->retprobe;
2156
2157 return 0;
2158}
2159
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302160static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302161 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002162{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002163 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002164 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002165
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002166 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002167 pev->event = strdup(tev->event);
2168 pev->group = strdup(tev->group);
2169 if (pev->event == NULL || pev->group == NULL)
2170 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002171
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002172 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002173 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002174 if (ret < 0)
2175 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002176
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002177 /* Convert trace_arg to probe_arg */
2178 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002179 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2180 if (pev->args == NULL)
2181 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002182 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002183 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002184 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002186 if ((ret = strbuf_init(&buf, 32)) < 0)
2187 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002188 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2189 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002190 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002191 if (pev->args[i].name == NULL && ret >= 0)
2192 ret = -ENOMEM;
2193 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002194error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002195 if (ret < 0)
2196 clear_perf_probe_event(pev);
2197
2198 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002199}
2200
2201void clear_perf_probe_event(struct perf_probe_event *pev)
2202{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002203 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002204 int i;
2205
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002206 free(pev->event);
2207 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002208 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002209 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002210
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002211 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002212 free(pev->args[i].name);
2213 free(pev->args[i].var);
2214 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002215 field = pev->args[i].field;
2216 while (field) {
2217 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002218 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002219 free(field);
2220 field = next;
2221 }
2222 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002223 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002224 memset(pev, 0, sizeof(*pev));
2225}
2226
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002227#define strdup_or_goto(str, label) \
2228({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2229
2230static int perf_probe_point__copy(struct perf_probe_point *dst,
2231 struct perf_probe_point *src)
2232{
2233 dst->file = strdup_or_goto(src->file, out_err);
2234 dst->function = strdup_or_goto(src->function, out_err);
2235 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2236 dst->line = src->line;
2237 dst->retprobe = src->retprobe;
2238 dst->offset = src->offset;
2239 return 0;
2240
2241out_err:
2242 clear_perf_probe_point(dst);
2243 return -ENOMEM;
2244}
2245
2246static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2247 struct perf_probe_arg *src)
2248{
2249 struct perf_probe_arg_field *field, **ppfield;
2250
2251 dst->name = strdup_or_goto(src->name, out_err);
2252 dst->var = strdup_or_goto(src->var, out_err);
2253 dst->type = strdup_or_goto(src->type, out_err);
2254
2255 field = src->field;
2256 ppfield = &(dst->field);
2257 while (field) {
2258 *ppfield = zalloc(sizeof(*field));
2259 if (!*ppfield)
2260 goto out_err;
2261 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2262 (*ppfield)->index = field->index;
2263 (*ppfield)->ref = field->ref;
2264 field = field->next;
2265 ppfield = &((*ppfield)->next);
2266 }
2267 return 0;
2268out_err:
2269 return -ENOMEM;
2270}
2271
2272int perf_probe_event__copy(struct perf_probe_event *dst,
2273 struct perf_probe_event *src)
2274{
2275 int i;
2276
2277 dst->event = strdup_or_goto(src->event, out_err);
2278 dst->group = strdup_or_goto(src->group, out_err);
2279 dst->target = strdup_or_goto(src->target, out_err);
2280 dst->uprobes = src->uprobes;
2281
2282 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2283 goto out_err;
2284
2285 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2286 if (!dst->args)
2287 goto out_err;
2288 dst->nargs = src->nargs;
2289
2290 for (i = 0; i < src->nargs; i++)
2291 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2292 goto out_err;
2293 return 0;
2294
2295out_err:
2296 clear_perf_probe_event(dst);
2297 return -ENOMEM;
2298}
2299
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002300void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002301{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302302 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002303 int i;
2304
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002305 free(tev->event);
2306 free(tev->group);
2307 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002308 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002309 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002310 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002311 free(tev->args[i].name);
2312 free(tev->args[i].value);
2313 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002314 ref = tev->args[i].ref;
2315 while (ref) {
2316 next = ref->next;
2317 free(ref);
2318 ref = next;
2319 }
2320 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002321 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002322 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002323}
2324
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002325struct kprobe_blacklist_node {
2326 struct list_head list;
2327 unsigned long start;
2328 unsigned long end;
2329 char *symbol;
2330};
2331
2332static void kprobe_blacklist__delete(struct list_head *blacklist)
2333{
2334 struct kprobe_blacklist_node *node;
2335
2336 while (!list_empty(blacklist)) {
2337 node = list_first_entry(blacklist,
2338 struct kprobe_blacklist_node, list);
2339 list_del(&node->list);
2340 free(node->symbol);
2341 free(node);
2342 }
2343}
2344
2345static int kprobe_blacklist__load(struct list_head *blacklist)
2346{
2347 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002348 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002349 char buf[PATH_MAX], *p;
2350 FILE *fp;
2351 int ret;
2352
2353 if (__debugfs == NULL)
2354 return -ENOTSUP;
2355
2356 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2357 if (ret < 0)
2358 return ret;
2359
2360 fp = fopen(buf, "r");
2361 if (!fp)
2362 return -errno;
2363
2364 ret = 0;
2365 while (fgets(buf, PATH_MAX, fp)) {
2366 node = zalloc(sizeof(*node));
2367 if (!node) {
2368 ret = -ENOMEM;
2369 break;
2370 }
2371 INIT_LIST_HEAD(&node->list);
2372 list_add_tail(&node->list, blacklist);
2373 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2374 ret = -EINVAL;
2375 break;
2376 }
2377 p = strchr(buf, '\t');
2378 if (p) {
2379 p++;
2380 if (p[strlen(p) - 1] == '\n')
2381 p[strlen(p) - 1] = '\0';
2382 } else
2383 p = (char *)"unknown";
2384 node->symbol = strdup(p);
2385 if (!node->symbol) {
2386 ret = -ENOMEM;
2387 break;
2388 }
2389 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2390 node->start, node->end, node->symbol);
2391 ret++;
2392 }
2393 if (ret < 0)
2394 kprobe_blacklist__delete(blacklist);
2395 fclose(fp);
2396
2397 return ret;
2398}
2399
2400static struct kprobe_blacklist_node *
2401kprobe_blacklist__find_by_address(struct list_head *blacklist,
2402 unsigned long address)
2403{
2404 struct kprobe_blacklist_node *node;
2405
2406 list_for_each_entry(node, blacklist, list) {
Li Bin2c294612017-08-29 20:57:23 +08002407 if (node->start <= address && address < node->end)
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002408 return node;
2409 }
2410
2411 return NULL;
2412}
2413
Masami Hiramatsub0312202015-06-16 20:50:55 +09002414static LIST_HEAD(kprobe_blacklist);
2415
2416static void kprobe_blacklist__init(void)
2417{
2418 if (!list_empty(&kprobe_blacklist))
2419 return;
2420
2421 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2422 pr_debug("No kprobe blacklist support, ignored\n");
2423}
2424
2425static void kprobe_blacklist__release(void)
2426{
2427 kprobe_blacklist__delete(&kprobe_blacklist);
2428}
2429
2430static bool kprobe_blacklist__listed(unsigned long address)
2431{
2432 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2433}
2434
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002435static int perf_probe_event__sprintf(const char *group, const char *event,
2436 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002437 const char *module,
2438 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002439{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002440 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002441 char *buf;
2442
2443 if (asprintf(&buf, "%s:%s", group, event) < 0)
2444 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002445 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002446 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002447 if (ret)
2448 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002449
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002450 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002451 buf = synthesize_perf_probe_point(&pev->point);
2452 if (!buf)
2453 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002454 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002455 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002456
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002457 if (!ret && module)
2458 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002459
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002460 if (!ret && pev->nargs > 0) {
2461 ret = strbuf_add(result, " with", 5);
2462 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002463 buf = synthesize_perf_probe_arg(&pev->args[i]);
2464 if (!buf)
2465 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002466 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002467 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002468 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002469 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002470 if (!ret)
2471 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002472
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002473 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002474}
2475
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002476/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002477int show_perf_probe_event(const char *group, const char *event,
2478 struct perf_probe_event *pev,
2479 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002480{
2481 struct strbuf buf = STRBUF_INIT;
2482 int ret;
2483
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002484 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002485 if (ret >= 0) {
2486 if (use_stdout)
2487 printf("%s\n", buf.buf);
2488 else
2489 pr_info("%s\n", buf.buf);
2490 }
2491 strbuf_release(&buf);
2492
2493 return ret;
2494}
2495
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002496static bool filter_probe_trace_event(struct probe_trace_event *tev,
2497 struct strfilter *filter)
2498{
2499 char tmp[128];
2500
2501 /* At first, check the event name itself */
2502 if (strfilter__compare(filter, tev->event))
2503 return true;
2504
2505 /* Next, check the combination of name and group */
2506 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2507 return false;
2508 return strfilter__compare(filter, tmp);
2509}
2510
2511static int __show_perf_probe_events(int fd, bool is_kprobe,
2512 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002513{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302514 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302515 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002516 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002517 struct strlist *rawlist;
2518 struct str_node *ent;
2519
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002520 memset(&tev, 0, sizeof(tev));
2521 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002522
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002523 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002524 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002525 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002526
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002527 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302528 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002529 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002530 if (!filter_probe_trace_event(&tev, filter))
2531 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302532 ret = convert_to_perf_probe_event(&tev, &pev,
2533 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002534 if (ret < 0)
2535 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002536 ret = show_perf_probe_event(pev.group, pev.event,
2537 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002538 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002539 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002540next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002541 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302542 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002543 if (ret < 0)
2544 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002545 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002546 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002547 /* Cleanup cached debuginfo if needed */
2548 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002549
2550 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002551}
2552
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302553/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002554int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302555{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002556 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302557
2558 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302559
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002560 if (probe_conf.cache)
2561 return probe_cache__show_all_caches(filter);
2562
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002563 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302564 if (ret < 0)
2565 return ret;
2566
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002567 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2568 if (ret < 0)
2569 return ret;
2570
2571 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002572 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002573 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002574 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002575 if (kp_fd > 0)
2576 close(kp_fd);
2577 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002578 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002579 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302580
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002581 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002582}
2583
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002584static int get_new_event_name(char *buf, size_t len, const char *base,
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002585 struct strlist *namelist, bool ret_event,
2586 bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002587{
2588 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002589 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002590
Naveen N. Rao3099c022015-04-28 17:35:34 +05302591 if (*base == '.')
2592 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002593 nbase = strdup(base);
2594 if (!nbase)
2595 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302596
Masami Hiramatsua3110cd2017-12-11 15:19:25 -03002597 /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
2598 p = strpbrk(nbase, ".@");
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002599 if (p && p != nbase)
2600 *p = '\0';
2601
2602 /* Try no suffix number */
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002603 ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002604 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002605 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002606 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002607 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002608 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002609 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002610
Masami Hiramatsud761b082009-12-15 10:32:25 -05002611 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002612 pr_warning("Error: event \"%s\" already exists.\n"
2613 " Hint: Remove existing event by 'perf probe -d'\n"
2614 " or force duplicates by 'perf probe -f'\n"
2615 " or set 'force=yes' in BPF source.\n",
2616 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002617 ret = -EEXIST;
2618 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002619 }
2620
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002621 /* Try to add suffix */
2622 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002623 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002624 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002625 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002626 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002627 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002628 if (!strlist__has_entry(namelist, buf))
2629 break;
2630 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002631 if (i == MAX_EVENT_INDEX) {
2632 pr_warning("Too many events are on the same function.\n");
2633 ret = -ERANGE;
2634 }
2635
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002636out:
2637 free(nbase);
Masami Hiramatsu9f5c6d82017-12-09 01:26:46 +09002638
2639 /* Final validation */
2640 if (ret >= 0 && !is_c_func_name(buf)) {
2641 pr_warning("Internal error: \"%s\" is an invalid event name.\n",
2642 buf);
2643 ret = -EINVAL;
2644 }
2645
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002646 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002647}
2648
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002649/* Warn if the current kernel's uprobe implementation is old */
2650static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2651{
2652 int i;
2653 char *buf = synthesize_probe_trace_command(tev);
Ravi Bangoria5a5e3d32018-08-20 10:12:50 +05302654 struct probe_trace_point *tp = &tev->point;
2655
2656 if (tp->ref_ctr_offset && !uprobe_ref_ctr_is_supported()) {
2657 pr_warning("A semaphore is associated with %s:%s and "
2658 "seems your kernel doesn't support it.\n",
2659 tev->group, tev->event);
2660 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002661
2662 /* Old uprobe event doesn't support memory dereference */
2663 if (!tev->uprobes || tev->nargs == 0 || !buf)
2664 goto out;
2665
2666 for (i = 0; i < tev->nargs; i++)
2667 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2668 pr_warning("Please upgrade your kernel to at least "
2669 "3.14 to have access to feature %s\n",
2670 tev->args[i].value);
2671 break;
2672 }
2673out:
2674 free(buf);
2675}
2676
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002677/* Set new name from original perf_probe_event and namelist */
2678static int probe_trace_event__set_name(struct probe_trace_event *tev,
2679 struct perf_probe_event *pev,
2680 struct strlist *namelist,
2681 bool allow_suffix)
2682{
2683 const char *event, *group;
2684 char buf[64];
2685 int ret;
2686
Masami Hiramatsubc062232016-07-01 17:03:12 +09002687 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002688 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002689 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002690 else if (tev->event)
2691 event = tev->event;
2692 else {
2693 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002694 if (pev->point.function &&
2695 (strncmp(pev->point.function, "0x", 2) != 0) &&
2696 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002697 event = pev->point.function;
2698 else
2699 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002700 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002701 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002702 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002703 else if (tev->group)
2704 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002705 else
2706 group = PERFPROBE_GROUP;
2707
2708 /* Get an unused new event name */
Masami Hiramatsue63c6252017-12-09 01:27:44 +09002709 ret = get_new_event_name(buf, 64, event, namelist,
2710 tev->point.retprobe, allow_suffix);
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002711 if (ret < 0)
2712 return ret;
2713
2714 event = buf;
2715
2716 tev->event = strdup(event);
2717 tev->group = strdup(group);
2718 if (tev->event == NULL || tev->group == NULL)
2719 return -ENOMEM;
2720
2721 /* Add added event name to namelist */
2722 strlist__add(namelist, event);
2723 return 0;
2724}
2725
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002726static int __open_probe_file_and_namelist(bool uprobe,
2727 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002728{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002729 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002730
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002731 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002732 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002733 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002734
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002735 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002736 *namelist = probe_file__get_namelist(fd);
2737 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002738 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002739 close(fd);
2740 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002741 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002742 return fd;
2743}
2744
2745static int __add_probe_trace_events(struct perf_probe_event *pev,
2746 struct probe_trace_event *tevs,
2747 int ntevs, bool allow_suffix)
2748{
2749 int i, fd[2] = {-1, -1}, up, ret;
2750 struct probe_trace_event *tev = NULL;
2751 struct probe_cache *cache = NULL;
2752 struct strlist *namelist[2] = {NULL, NULL};
Krister Johansen544abd42017-07-05 18:48:10 -07002753 struct nscookie nsc;
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002754
2755 up = pev->uprobes ? 1 : 0;
2756 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2757 if (fd[up] < 0)
2758 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002759
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002760 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002761 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002762 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002763 up = tev->uprobes ? 1 : 0;
2764 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2765 fd[up] = __open_probe_file_and_namelist(up,
2766 &namelist[up]);
2767 if (fd[up] < 0)
2768 goto close_out;
2769 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002770 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002771 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002772 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002773
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002774 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002775 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002776 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002777 if (ret < 0)
2778 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002779
Krister Johansen544abd42017-07-05 18:48:10 -07002780 nsinfo__mountns_enter(pev->nsi, &nsc);
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002781 ret = probe_file__add_event(fd[up], tev);
Krister Johansen544abd42017-07-05 18:48:10 -07002782 nsinfo__mountns_exit(&nsc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002783 if (ret < 0)
2784 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002785
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002786 /*
2787 * Probes after the first probe which comes from same
2788 * user input are always allowed to add suffix, because
2789 * there might be several addresses corresponding to
2790 * one code line.
2791 */
2792 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002793 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002794 if (ret == -EINVAL && pev->uprobes)
2795 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002796 if (ret == 0 && probe_conf.cache) {
Krister Johansenf045b8c2017-07-05 18:48:11 -07002797 cache = probe_cache__new(pev->target, pev->nsi);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002798 if (!cache ||
2799 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2800 probe_cache__commit(cache) < 0)
2801 pr_warning("Failed to add event to probe cache\n");
2802 probe_cache__delete(cache);
2803 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002804
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002805close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002806 for (up = 0; up < 2; up++) {
2807 strlist__delete(namelist[up]);
2808 if (fd[up] >= 0)
2809 close(fd[up]);
2810 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002811 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002812}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002813
Wang Nan6bb536c2015-05-29 09:45:47 +00002814static int find_probe_functions(struct map *map, char *name,
2815 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002816{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002817 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002818 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002819 struct rb_node *tmp;
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002820 const char *norm, *ver;
2821 char *buf = NULL;
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002822 bool cut_version = true;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002823
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002824 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002825 return 0;
2826
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002827 /* If user gives a version, don't cut off the version from symbols */
2828 if (strchr(name, '@'))
2829 cut_version = false;
2830
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002831 map__for_each_symbol(map, sym, tmp) {
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002832 norm = arch__normalize_symbol_name(sym->name);
2833 if (!norm)
2834 continue;
2835
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002836 if (cut_version) {
2837 /* We don't care about default symbol or not */
2838 ver = strchr(norm, '@');
2839 if (ver) {
2840 buf = strndup(norm, ver - norm);
2841 if (!buf)
2842 return -ENOMEM;
2843 norm = buf;
2844 }
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002845 }
Masami Hiramatsuc588d152017-12-13 00:05:12 +09002846
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002847 if (strglobmatch(norm, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002848 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002849 if (syms && found < probe_conf.max_probes)
2850 syms[found - 1] = sym;
2851 }
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002852 if (buf)
2853 zfree(&buf);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002854 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002855
2856 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002857}
2858
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302859void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2860 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302861 struct map *map __maybe_unused,
2862 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302863
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002864/*
2865 * Find probe function addresses from map.
2866 * Return an error or the number of found probe_trace_event
2867 */
2868static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002869 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002870{
2871 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002872 struct ref_reloc_sym *reloc_sym = NULL;
2873 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002874 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002875 struct probe_trace_event *tev;
2876 struct perf_probe_point *pp = &pev->point;
2877 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002878 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002879 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302880 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002881
Krister Johansen544abd42017-07-05 18:48:10 -07002882 map = get_target_map(pev->target, pev->nsi, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002883 if (!map) {
2884 ret = -EINVAL;
2885 goto out;
2886 }
2887
Wang Nan6bb536c2015-05-29 09:45:47 +00002888 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2889 if (!syms) {
2890 ret = -ENOMEM;
2891 goto out;
2892 }
2893
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002894 /*
2895 * Load matched symbols: Since the different local symbols may have
2896 * same name but different addresses, this lists all the symbols.
2897 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002898 num_matched_functions = find_probe_functions(map, pp->function, syms);
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +09002899 if (num_matched_functions <= 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002900 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002901 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002902 ret = -ENOENT;
2903 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002904 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002905 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002906 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002907 ret = -E2BIG;
2908 goto out;
2909 }
2910
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002911 /* Note that the symbols in the kmodule are not relocated */
Naveen N. Rao7ab31d92017-03-08 13:56:09 +05302912 if (!pev->uprobes && !pev->target &&
2913 (!pp->retprobe || kretprobe_offset_is_supported())) {
He Kuang0560a0c2015-03-20 09:56:56 +08002914 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002915 if (!reloc_sym) {
2916 pr_warning("Relocated base symbol is not found!\n");
2917 ret = -EINVAL;
2918 goto out;
2919 }
2920 }
2921
2922 /* Setup result trace-probe-events */
2923 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2924 if (!*tevs) {
2925 ret = -ENOMEM;
2926 goto out;
2927 }
2928
2929 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002930
Wang Nan6bb536c2015-05-29 09:45:47 +00002931 for (j = 0; j < num_matched_functions; j++) {
2932 sym = syms[j];
2933
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002934 tev = (*tevs) + ret;
2935 tp = &tev->point;
2936 if (ret == num_matched_functions) {
2937 pr_warning("Too many symbols are listed. Skip it.\n");
2938 break;
2939 }
2940 ret++;
2941
2942 if (pp->offset > sym->end - sym->start) {
2943 pr_warning("Offset %ld is bigger than the size of %s\n",
2944 pp->offset, sym->name);
2945 ret = -ENOENT;
2946 goto err_out;
2947 }
2948 /* Add one probe point */
2949 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002950
2951 /* Check the kprobe (not in module) is within .text */
2952 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002953 kprobe_warn_out_range(sym->name, tp->address)) {
2954 tp->symbol = NULL; /* Skip it */
2955 skipped++;
2956 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002957 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2958 tp->offset = tp->address - reloc_sym->addr;
2959 } else {
2960 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2961 tp->offset = pp->offset;
2962 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002963 tp->realname = strdup_or_goto(sym->name, nomem_out);
2964
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002965 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302966 if (pev->target) {
2967 if (pev->uprobes) {
2968 tev->point.module = strdup_or_goto(pev->target,
2969 nomem_out);
2970 } else {
2971 mod_name = find_module_name(pev->target);
2972 tev->point.module =
2973 strdup(mod_name ? mod_name : pev->target);
2974 free(mod_name);
2975 if (!tev->point.module)
2976 goto nomem_out;
2977 }
2978 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002979 tev->uprobes = pev->uprobes;
2980 tev->nargs = pev->nargs;
2981 if (tev->nargs) {
2982 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2983 tev->nargs);
2984 if (tev->args == NULL)
2985 goto nomem_out;
2986 }
2987 for (i = 0; i < tev->nargs; i++) {
2988 if (pev->args[i].name)
2989 tev->args[i].name =
2990 strdup_or_goto(pev->args[i].name,
2991 nomem_out);
2992
2993 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2994 nomem_out);
2995 if (pev->args[i].type)
2996 tev->args[i].type =
2997 strdup_or_goto(pev->args[i].type,
2998 nomem_out);
2999 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05303000 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003001 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003002 if (ret == skipped) {
3003 ret = -ENOENT;
3004 goto err_out;
3005 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003006
3007out:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003008 map__put(map);
Wang Nan6bb536c2015-05-29 09:45:47 +00003009 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003010 return ret;
3011
3012nomem_out:
3013 ret = -ENOMEM;
3014err_out:
3015 clear_probe_trace_events(*tevs, num_matched_functions);
3016 zfree(tevs);
3017 goto out;
3018}
3019
Wang Nanda15bd92015-08-26 10:57:45 +00003020static int try_to_find_absolute_address(struct perf_probe_event *pev,
3021 struct probe_trace_event **tevs)
3022{
3023 struct perf_probe_point *pp = &pev->point;
3024 struct probe_trace_event *tev;
3025 struct probe_trace_point *tp;
3026 int i, err;
3027
3028 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
3029 return -EINVAL;
3030 if (perf_probe_event_need_dwarf(pev))
3031 return -EINVAL;
3032
3033 /*
3034 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
3035 * absolute address.
3036 *
3037 * Only one tev can be generated by this.
3038 */
3039 *tevs = zalloc(sizeof(*tev));
3040 if (!*tevs)
3041 return -ENOMEM;
3042
3043 tev = *tevs;
3044 tp = &tev->point;
3045
3046 /*
3047 * Don't use tp->offset, use address directly, because
3048 * in synthesize_probe_trace_command() address cannot be
3049 * zero.
3050 */
3051 tp->address = pev->point.abs_address;
3052 tp->retprobe = pp->retprobe;
3053 tev->uprobes = pev->uprobes;
3054
3055 err = -ENOMEM;
3056 /*
3057 * Give it a '0x' leading symbol name.
3058 * In __add_probe_trace_events, a NULL symbol is interpreted as
Ingo Molnaradba1632018-12-03 11:22:00 +01003059 * invalid.
Wang Nanda15bd92015-08-26 10:57:45 +00003060 */
3061 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
3062 goto errout;
3063
3064 /* For kprobe, check range */
3065 if ((!tev->uprobes) &&
3066 (kprobe_warn_out_range(tev->point.symbol,
3067 tev->point.address))) {
3068 err = -EACCES;
3069 goto errout;
3070 }
3071
3072 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
3073 goto errout;
3074
3075 if (pev->target) {
3076 tp->module = strdup(pev->target);
3077 if (!tp->module)
3078 goto errout;
3079 }
3080
3081 if (tev->group) {
3082 tev->group = strdup(pev->group);
3083 if (!tev->group)
3084 goto errout;
3085 }
3086
3087 if (pev->event) {
3088 tev->event = strdup(pev->event);
3089 if (!tev->event)
3090 goto errout;
3091 }
3092
3093 tev->nargs = pev->nargs;
3094 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Markus Elfringd1d0e292017-01-23 15:10:19 +01003095 if (!tev->args)
Wang Nanda15bd92015-08-26 10:57:45 +00003096 goto errout;
Markus Elfringd1d0e292017-01-23 15:10:19 +01003097
Wang Nanda15bd92015-08-26 10:57:45 +00003098 for (i = 0; i < tev->nargs; i++)
3099 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
3100
3101 return 1;
3102
3103errout:
Markus Elfring42e233c2017-01-23 14:54:26 +01003104 clear_probe_trace_events(*tevs, 1);
3105 *tevs = NULL;
Wang Nanda15bd92015-08-26 10:57:45 +00003106 return err;
3107}
3108
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003109/* Concatinate two arrays */
3110static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
3111{
3112 void *ret;
3113
3114 ret = malloc(sz_a + sz_b);
3115 if (ret) {
3116 memcpy(ret, a, sz_a);
3117 memcpy(ret + sz_a, b, sz_b);
3118 }
3119 return ret;
3120}
3121
3122static int
3123concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
3124 struct probe_trace_event **tevs2, int ntevs2)
3125{
3126 struct probe_trace_event *new_tevs;
3127 int ret = 0;
3128
Ravi Bangoriaf0a30dc2017-03-08 12:29:07 +05303129 if (*ntevs == 0) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003130 *tevs = *tevs2;
3131 *ntevs = ntevs2;
3132 *tevs2 = NULL;
3133 return 0;
3134 }
3135
3136 if (*ntevs + ntevs2 > probe_conf.max_probes)
3137 ret = -E2BIG;
3138 else {
3139 /* Concatinate the array of probe_trace_event */
3140 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3141 *tevs2, ntevs2 * sizeof(**tevs2));
3142 if (!new_tevs)
3143 ret = -ENOMEM;
3144 else {
3145 free(*tevs);
3146 *tevs = new_tevs;
3147 *ntevs += ntevs2;
3148 }
3149 }
3150 if (ret < 0)
3151 clear_probe_trace_events(*tevs2, ntevs2);
3152 zfree(tevs2);
3153
3154 return ret;
3155}
3156
3157/*
3158 * Try to find probe_trace_event from given probe caches. Return the number
3159 * of cached events found, if an error occurs return the error.
3160 */
3161static int find_cached_events(struct perf_probe_event *pev,
3162 struct probe_trace_event **tevs,
3163 const char *target)
3164{
3165 struct probe_cache *cache;
3166 struct probe_cache_entry *entry;
3167 struct probe_trace_event *tmp_tevs = NULL;
3168 int ntevs = 0;
3169 int ret = 0;
3170
Krister Johansenf045b8c2017-07-05 18:48:11 -07003171 cache = probe_cache__new(target, pev->nsi);
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003172 /* Return 0 ("not found") if the target has no probe cache. */
3173 if (!cache)
3174 return 0;
3175
3176 for_each_probe_cache_entry(entry, cache) {
3177 /* Skip the cache entry which has no name */
3178 if (!entry->pev.event || !entry->pev.group)
3179 continue;
3180 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3181 strglobmatch(entry->pev.event, pev->event)) {
3182 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3183 if (ret > 0)
3184 ret = concat_probe_trace_events(tevs, &ntevs,
3185 &tmp_tevs, ret);
3186 if (ret < 0)
3187 break;
3188 }
3189 }
3190 probe_cache__delete(cache);
3191 if (ret < 0) {
3192 clear_probe_trace_events(*tevs, ntevs);
3193 zfree(tevs);
3194 } else {
3195 ret = ntevs;
3196 if (ntevs > 0 && target && target[0] == '/')
3197 pev->uprobes = true;
3198 }
3199
3200 return ret;
3201}
3202
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003203/* Try to find probe_trace_event from all probe caches */
3204static int find_cached_events_all(struct perf_probe_event *pev,
3205 struct probe_trace_event **tevs)
3206{
3207 struct probe_trace_event *tmp_tevs = NULL;
3208 struct strlist *bidlist;
3209 struct str_node *nd;
3210 char *pathname;
3211 int ntevs = 0;
3212 int ret;
3213
3214 /* Get the buildid list of all valid caches */
3215 bidlist = build_id_cache__list_all(true);
3216 if (!bidlist) {
3217 ret = -errno;
3218 pr_debug("Failed to get buildids: %d\n", ret);
3219 return ret;
3220 }
3221
3222 ret = 0;
3223 strlist__for_each_entry(nd, bidlist) {
3224 pathname = build_id_cache__origname(nd->s);
3225 ret = find_cached_events(pev, &tmp_tevs, pathname);
3226 /* In the case of cnt == 0, we just skip it */
3227 if (ret > 0)
3228 ret = concat_probe_trace_events(tevs, &ntevs,
3229 &tmp_tevs, ret);
3230 free(pathname);
3231 if (ret < 0)
3232 break;
3233 }
3234 strlist__delete(bidlist);
3235
3236 if (ret < 0) {
3237 clear_probe_trace_events(*tevs, ntevs);
3238 zfree(tevs);
3239 } else
3240 ret = ntevs;
3241
3242 return ret;
3243}
3244
Masami Hiramatsubc062232016-07-01 17:03:12 +09003245static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3246 struct probe_trace_event **tevs)
3247{
3248 struct probe_cache *cache;
3249 struct probe_cache_entry *entry;
3250 struct probe_trace_event *tev;
3251 struct str_node *node;
3252 int ret, i;
3253
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003254 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003255 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003256 if (!pev->target)
3257 return find_cached_events_all(pev, tevs);
3258 else
3259 return find_cached_events(pev, tevs, pev->target);
3260 }
Krister Johansenf045b8c2017-07-05 18:48:11 -07003261 cache = probe_cache__new(pev->target, pev->nsi);
Masami Hiramatsubc062232016-07-01 17:03:12 +09003262 if (!cache)
3263 return 0;
3264
3265 entry = probe_cache__find(cache, pev);
3266 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003267 /* SDT must be in the cache */
3268 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003269 goto out;
3270 }
3271
3272 ret = strlist__nr_entries(entry->tevlist);
3273 if (ret > probe_conf.max_probes) {
3274 pr_debug("Too many entries matched in the cache of %s\n",
3275 pev->target ? : "kernel");
3276 ret = -E2BIG;
3277 goto out;
3278 }
3279
3280 *tevs = zalloc(ret * sizeof(*tev));
3281 if (!*tevs) {
3282 ret = -ENOMEM;
3283 goto out;
3284 }
3285
3286 i = 0;
3287 strlist__for_each_entry(node, entry->tevlist) {
3288 tev = &(*tevs)[i++];
3289 ret = parse_probe_trace_command(node->s, tev);
3290 if (ret < 0)
3291 goto out;
3292 /* Set the uprobes attribute as same as original */
3293 tev->uprobes = pev->uprobes;
3294 }
3295 ret = i;
3296
3297out:
3298 probe_cache__delete(cache);
3299 return ret;
3300}
3301
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303302static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003303 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003304{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003305 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003306
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003307 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003308 /* Set group name if not given */
3309 if (!pev->uprobes) {
3310 pev->group = strdup(PERFPROBE_GROUP);
3311 ret = pev->group ? 0 : -ENOMEM;
3312 } else
3313 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003314 if (ret != 0) {
3315 pr_warning("Failed to make a group name.\n");
3316 return ret;
3317 }
3318 }
3319
Wang Nanda15bd92015-08-26 10:57:45 +00003320 ret = try_to_find_absolute_address(pev, tevs);
3321 if (ret > 0)
3322 return ret;
3323
Masami Hiramatsubc062232016-07-01 17:03:12 +09003324 /* At first, we need to lookup cache entry */
3325 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003326 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3327 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003328
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003329 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003330 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003331 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003332 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003333
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003334 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003335}
3336
Wang Nan12fae5e2015-09-04 21:16:00 +09003337int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003338{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003339 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003340
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003341 /* Loop 1: convert all events */
3342 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003343 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003344 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003345 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003346 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003347 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003348 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003349 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003350 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003351 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003352 /* This just release blacklist only if allocated */
3353 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003354
Namhyung Kim844dffa2015-09-04 21:15:59 +09003355 return 0;
3356}
3357
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003358static int show_probe_trace_event(struct probe_trace_event *tev)
3359{
3360 char *buf = synthesize_probe_trace_command(tev);
3361
3362 if (!buf) {
3363 pr_debug("Failed to synthesize probe trace event.\n");
3364 return -EINVAL;
3365 }
3366
3367 /* Showing definition always go stdout */
3368 printf("%s\n", buf);
3369 free(buf);
3370
3371 return 0;
3372}
3373
3374int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3375{
3376 struct strlist *namelist = strlist__new(NULL, NULL);
3377 struct probe_trace_event *tev;
3378 struct perf_probe_event *pev;
3379 int i, j, ret = 0;
3380
3381 if (!namelist)
3382 return -ENOMEM;
3383
3384 for (j = 0; j < npevs && !ret; j++) {
3385 pev = &pevs[j];
3386 for (i = 0; i < pev->ntevs && !ret; i++) {
3387 tev = &pev->tevs[i];
3388 /* Skip if the symbol is out of .text or blacklisted */
3389 if (!tev->point.symbol && !pev->uprobes)
3390 continue;
3391
3392 /* Set new name for tev (and update namelist) */
3393 ret = probe_trace_event__set_name(tev, pev,
3394 namelist, true);
3395 if (!ret)
3396 ret = show_probe_trace_event(tev);
3397 }
3398 }
3399 strlist__delete(namelist);
3400
3401 return ret;
3402}
3403
Wang Nan12fae5e2015-09-04 21:16:00 +09003404int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003405{
3406 int i, ret = 0;
3407
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003408 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003409 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003410 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3411 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003412 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003413 if (ret < 0)
3414 break;
3415 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003416 return ret;
3417}
3418
Wang Nan12fae5e2015-09-04 21:16:00 +09003419void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003420{
3421 int i, j;
Krister Johansen544abd42017-07-05 18:48:10 -07003422 struct perf_probe_event *pev;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003423
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003424 /* Loop 3: cleanup and free trace events */
3425 for (i = 0; i < npevs; i++) {
Krister Johansen544abd42017-07-05 18:48:10 -07003426 pev = &pevs[i];
Wang Nan12fae5e2015-09-04 21:16:00 +09003427 for (j = 0; j < pevs[i].ntevs; j++)
3428 clear_probe_trace_event(&pevs[i].tevs[j]);
3429 zfree(&pevs[i].tevs);
3430 pevs[i].ntevs = 0;
Krister Johansen544abd42017-07-05 18:48:10 -07003431 nsinfo__zput(pev->nsi);
Namhyung Kima43aac22015-09-10 11:27:04 +09003432 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003433 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003434}
3435
3436int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3437{
3438 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003439
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003440 ret = init_probe_symbol_maps(pevs->uprobes);
3441 if (ret < 0)
3442 return ret;
3443
Wang Nan12fae5e2015-09-04 21:16:00 +09003444 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003445 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003446 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003447
Wang Nan12fae5e2015-09-04 21:16:00 +09003448 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003449
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003450 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003451 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003452}
3453
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003454int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003455{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003456 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003457 char *str = strfilter__string(filter);
3458
3459 if (!str)
3460 return -EINVAL;
3461
Masami Hiramatsufa282442009-12-08 17:03:23 -05003462 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003463 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3464 if (ret < 0)
3465 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303466
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003467 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003468 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303469 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003470
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003471 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003472 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003473 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003474 goto error;
3475 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003476 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303477
3478error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003479 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303480 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003481 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303482 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003483out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003484 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003485
3486 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003487}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303488
Krister Johansen544abd42017-07-05 18:48:10 -07003489int show_available_funcs(const char *target, struct nsinfo *nsi,
3490 struct strfilter *_filter, bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003491{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003492 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003493 struct map *map;
3494 int ret;
3495
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003496 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003497 if (ret < 0)
3498 return ret;
3499
3500 /* Get a symbol map */
Krister Johansen544abd42017-07-05 18:48:10 -07003501 map = get_target_map(target, nsi, user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003502 if (!map) {
3503 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003504 return -EINVAL;
3505 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003506
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003507 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003508 if (ret) {
3509 if (ret == -2) {
3510 char *str = strfilter__string(_filter);
3511 pr_err("Failed to find symbols matched to \"%s\"\n",
3512 str);
3513 free(str);
3514 } else
3515 pr_err("Failed to load symbols in %s\n",
3516 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003517 goto end;
3518 }
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03003519 if (!dso__sorted_by_name(map->dso))
3520 dso__sort_by_name(map->dso);
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003521
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003522 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303523 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003524
Davidlohr Bueso7137ff52018-12-06 11:18:17 -08003525 for (nd = rb_first_cached(&map->dso->symbol_names); nd;
3526 nd = rb_next(nd)) {
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003527 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3528
3529 if (strfilter__compare(_filter, pos->sym.name))
3530 printf("%s\n", pos->sym.name);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03003531 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003532end:
Masami Hiramatsueebc5092017-01-04 12:29:05 +09003533 map__put(map);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003534 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303535
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003536 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303537}
3538
Wang Nanda15bd92015-08-26 10:57:45 +00003539int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3540 struct perf_probe_arg *pvar)
3541{
3542 tvar->value = strdup(pvar->var);
3543 if (tvar->value == NULL)
3544 return -ENOMEM;
3545 if (pvar->type) {
3546 tvar->type = strdup(pvar->type);
3547 if (tvar->type == NULL)
3548 return -ENOMEM;
3549 }
3550 if (pvar->name) {
3551 tvar->name = strdup(pvar->name);
3552 if (tvar->name == NULL)
3553 return -ENOMEM;
3554 } else
3555 tvar->name = NULL;
3556 return 0;
3557}