blob: f12081e48a3263b73cbc1fb3357887a2332aa48b [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020043#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090047#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090054struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090058int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059{
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68}
69
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000070static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040071
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090072/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090073int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040074{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040075 int ret;
76
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090078 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090079 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 if (ret < 0) {
81 pr_debug("Failed to init symbol map.\n");
82 goto out;
83 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000085 if (host_machine || user_only) /* already initialized */
86 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030087
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000088 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90
91 host_machine = machine__new_host();
92 if (!host_machine) {
93 pr_debug("machine__new_host() failed.\n");
94 symbol__exit();
95 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090096 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040097out:
98 if (ret < 0)
99 pr_warning("Failed to init vmlinux path.\n");
100 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400101}
102
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900103void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000104{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300105 machine__delete(host_machine);
106 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107 symbol__exit();
108}
109
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110static struct symbol *__find_kernel_function_by_name(const char *name,
111 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400112{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000113 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900114 NULL);
115}
116
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000117static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
118{
119 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
120}
121
122static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
123{
124 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
125 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300126 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000127
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300128 if (map__load(map, NULL) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000129 return NULL;
130
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300131 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000132 if (!kmap)
133 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000134 return kmap->ref_reloc_sym;
135}
136
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900137static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
138 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000139{
140 struct ref_reloc_sym *reloc_sym;
141 struct symbol *sym;
142 struct map *map;
143
144 /* ref_reloc_sym is just a label. Need a special fix*/
145 reloc_sym = kernel_get_ref_reloc_sym();
146 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900147 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000148 else {
149 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900150 if (!sym)
151 return -ENOENT;
152 *addr = map->unmap_ip(map, sym->start) -
153 ((reloc) ? 0 : map->reloc) -
154 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000155 }
156 return 0;
157}
158
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900159static struct map *kernel_get_module_map(const char *module)
160{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000161 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300162 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300163 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900164
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900165 /* A file path -- this is an offline module */
166 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300167 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900168
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900169 if (!module)
170 module = "kernel";
171
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300172 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900173 if (strncmp(pos->dso->short_name + 1, module,
174 pos->dso->short_name_len - 2) == 0) {
175 return pos;
176 }
177 }
178 return NULL;
179}
180
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900181static struct map *get_target_map(const char *target, bool user)
182{
183 /* Init maps of given executable or kernel */
184 if (user)
185 return dso__new_map(target);
186 else
187 return kernel_get_module_map(target);
188}
189
190static void put_target_map(struct map *map, bool user)
191{
192 if (map && user) {
193 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300194 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900195 }
196}
197
198
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000199static int convert_exec_to_group(const char *exec, char **result)
200{
201 char *ptr1, *ptr2, *exec_copy;
202 char buf[64];
203 int ret;
204
205 exec_copy = strdup(exec);
206 if (!exec_copy)
207 return -ENOMEM;
208
209 ptr1 = basename(exec_copy);
210 if (!ptr1) {
211 ret = -EINVAL;
212 goto out;
213 }
214
215 ptr2 = strpbrk(ptr1, "-._");
216 if (ptr2)
217 *ptr2 = '\0';
218 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
219 if (ret < 0)
220 goto out;
221
222 *result = strdup(buf);
223 ret = *result ? 0 : -ENOMEM;
224
225out:
226 free(exec_copy);
227 return ret;
228}
229
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900230static void clear_perf_probe_point(struct perf_probe_point *pp)
231{
232 free(pp->file);
233 free(pp->function);
234 free(pp->lazy_line);
235}
236
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000237static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
238{
239 int i;
240
241 for (i = 0; i < ntevs; i++)
242 clear_probe_trace_event(tevs + i);
243}
244
Masami Hiramatsub0312202015-06-16 20:50:55 +0900245static bool kprobe_blacklist__listed(unsigned long address);
246static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
247{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900248 u64 etext_addr = 0;
249 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000250
Masami Hiramatsub0312202015-06-16 20:50:55 +0900251 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900252 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
253 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000254
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900255 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900256 pr_warning("%s is out of .text, skip it.\n", symbol);
257 else if (kprobe_blacklist__listed(address))
258 pr_warning("%s is blacklisted function, skip it.\n", symbol);
259 else
260 return false;
261
262 return true;
263}
264
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530265/*
266 * NOTE:
267 * '.gnu.linkonce.this_module' section of kernel module elf directly
268 * maps to 'struct module' from linux/module.h. This section contains
269 * actual module name which will be used by kernel after loading it.
270 * But, we cannot use 'struct module' here since linux/module.h is not
271 * exposed to user-space. Offset of 'name' has remained same from long
272 * time, so hardcoding it here.
273 */
274#ifdef __LP64__
275#define MOD_NAME_OFFSET 24
276#else
277#define MOD_NAME_OFFSET 12
278#endif
279
280/*
281 * @module can be module name of module file path. In case of path,
282 * inspect elf and find out what is actual module name.
283 * Caller has to free mod_name after using it.
284 */
285static char *find_module_name(const char *module)
286{
287 int fd;
288 Elf *elf;
289 GElf_Ehdr ehdr;
290 GElf_Shdr shdr;
291 Elf_Data *data;
292 Elf_Scn *sec;
293 char *mod_name = NULL;
294
295 fd = open(module, O_RDONLY);
296 if (fd < 0)
297 return NULL;
298
299 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
300 if (elf == NULL)
301 goto elf_err;
302
303 if (gelf_getehdr(elf, &ehdr) == NULL)
304 goto ret_err;
305
306 sec = elf_section_by_name(elf, &ehdr, &shdr,
307 ".gnu.linkonce.this_module", NULL);
308 if (!sec)
309 goto ret_err;
310
311 data = elf_getdata(sec, NULL);
312 if (!data || !data->d_buf)
313 goto ret_err;
314
315 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
316
317ret_err:
318 elf_end(elf);
319elf_err:
320 close(fd);
321 return mod_name;
322}
323
Ingo Molnar89fe8082013-09-30 12:07:11 +0200324#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000325
326static int kernel_get_module_dso(const char *module, struct dso **pdso)
327{
328 struct dso *dso;
329 struct map *map;
330 const char *vmlinux_name;
331 int ret = 0;
332
333 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300334 char module_name[128];
335
336 snprintf(module_name, sizeof(module_name), "[%s]", module);
337 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
338 if (map) {
339 dso = map->dso;
340 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000341 }
342 pr_debug("Failed to find module %s.\n", module);
343 return -ENOENT;
344 }
345
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300346 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000347 dso = map->dso;
348
349 vmlinux_name = symbol_conf.vmlinux_name;
350 dso->load_errno = 0;
351 if (vmlinux_name)
352 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
353 else
354 ret = dso__load_vmlinux_path(dso, map, NULL);
355found:
356 *pdso = dso;
357 return ret;
358}
359
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900360/*
361 * Some binaries like glibc have special symbols which are on the symbol
362 * table, but not in the debuginfo. If we can find the address of the
363 * symbol from map, we can translate the address back to the probe point.
364 */
365static int find_alternative_probe_point(struct debuginfo *dinfo,
366 struct perf_probe_point *pp,
367 struct perf_probe_point *result,
368 const char *target, bool uprobes)
369{
370 struct map *map = NULL;
371 struct symbol *sym;
372 u64 address = 0;
373 int ret = -ENOENT;
374
375 /* This can work only for function-name based one */
376 if (!pp->function || pp->file)
377 return -ENOTSUP;
378
379 map = get_target_map(target, uprobes);
380 if (!map)
381 return -EINVAL;
382
383 /* Find the address of given function */
384 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900385 if (uprobes)
386 address = sym->start;
387 else
388 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900389 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900390 }
391 if (!address) {
392 ret = -ENOENT;
393 goto out;
394 }
Wang Nanf6c15622015-04-08 02:14:34 +0000395 pr_debug("Symbol %s address found : %" PRIx64 "\n",
396 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900397
398 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
399 result);
400 if (ret <= 0)
401 ret = (!ret) ? -ENOENT : ret;
402 else {
403 result->offset += pp->offset;
404 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800405 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900406 ret = 0;
407 }
408
409out:
410 put_target_map(map, uprobes);
411 return ret;
412
413}
414
415static int get_alternative_probe_event(struct debuginfo *dinfo,
416 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900417 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900418{
419 int ret;
420
421 memcpy(tmp, &pev->point, sizeof(*tmp));
422 memset(&pev->point, 0, sizeof(pev->point));
423 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900424 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900425 if (ret < 0)
426 memcpy(&pev->point, tmp, sizeof(*tmp));
427
428 return ret;
429}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000430
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900431static int get_alternative_line_range(struct debuginfo *dinfo,
432 struct line_range *lr,
433 const char *target, bool user)
434{
David Ahern6d4a4892015-03-11 10:36:20 -0400435 struct perf_probe_point pp = { .function = lr->function,
436 .file = lr->file,
437 .line = lr->start };
438 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900439 int ret, len = 0;
440
David Ahern6d4a4892015-03-11 10:36:20 -0400441 memset(&result, 0, sizeof(result));
442
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900443 if (lr->end != INT_MAX)
444 len = lr->end - lr->start;
445 ret = find_alternative_probe_point(dinfo, &pp, &result,
446 target, user);
447 if (!ret) {
448 lr->function = result.function;
449 lr->file = result.file;
450 lr->start = result.line;
451 if (lr->end != INT_MAX)
452 lr->end = lr->start + len;
453 clear_perf_probe_point(&pp);
454 }
455 return ret;
456}
457
Masami Hiramatsuff741782011-06-27 16:27:39 +0900458/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000459static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900460{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000461 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900462 char reason[STRERR_BUFSIZE];
463 struct debuginfo *ret = NULL;
464 struct dso *dso = NULL;
465 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900466
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000467 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900468 err = kernel_get_module_dso(module, &dso);
469 if (err < 0) {
470 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300471 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900472 strcpy(reason, "(unknown)");
473 } else
474 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000475 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900476 pr_err("Failed to find the path for %s: %s\n",
477 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900478 return NULL;
479 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900480 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900481 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000482 ret = debuginfo__new(path);
483 if (!ret && !silent) {
484 pr_warning("The %s file has no debug information.\n", path);
485 if (!module || !strtailcmp(path, ".ko"))
486 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
487 else
488 pr_warning("Rebuild with -g, ");
489 pr_warning("or install an appropriate debuginfo package.\n");
490 }
491 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400492}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300493
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900494/* For caching the last debuginfo */
495static struct debuginfo *debuginfo_cache;
496static char *debuginfo_cache_path;
497
498static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
499{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900500 const char *path = module;
501
502 /* If the module is NULL, it should be the kernel. */
503 if (!module)
504 path = "kernel";
505
506 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900507 goto out;
508
509 /* Copy module path */
510 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900511 debuginfo_cache_path = strdup(path);
512 if (!debuginfo_cache_path) {
513 debuginfo__delete(debuginfo_cache);
514 debuginfo_cache = NULL;
515 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900516 }
517
518 debuginfo_cache = open_debuginfo(module, silent);
519 if (!debuginfo_cache)
520 zfree(&debuginfo_cache_path);
521out:
522 return debuginfo_cache;
523}
524
525static void debuginfo_cache__exit(void)
526{
527 debuginfo__delete(debuginfo_cache);
528 debuginfo_cache = NULL;
529 zfree(&debuginfo_cache_path);
530}
531
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000532
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000533static int get_text_start_address(const char *exec, unsigned long *address)
534{
535 Elf *elf;
536 GElf_Ehdr ehdr;
537 GElf_Shdr shdr;
538 int fd, ret = -ENOENT;
539
540 fd = open(exec, O_RDONLY);
541 if (fd < 0)
542 return -errno;
543
544 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900545 if (elf == NULL) {
546 ret = -EINVAL;
547 goto out_close;
548 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000549
550 if (gelf_getehdr(elf, &ehdr) == NULL)
551 goto out;
552
553 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
554 goto out;
555
556 *address = shdr.sh_addr - shdr.sh_offset;
557 ret = 0;
558out:
559 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900560out_close:
561 close(fd);
562
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000563 return ret;
564}
565
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000566/*
567 * Convert trace point to probe point with debuginfo
568 */
569static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
570 struct perf_probe_point *pp,
571 bool is_kprobe)
572{
573 struct debuginfo *dinfo = NULL;
574 unsigned long stext = 0;
575 u64 addr = tp->address;
576 int ret = -ENOENT;
577
578 /* convert the address to dwarf address */
579 if (!is_kprobe) {
580 if (!addr) {
581 ret = -EINVAL;
582 goto error;
583 }
584 ret = get_text_start_address(tp->module, &stext);
585 if (ret < 0)
586 goto error;
587 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000588 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900589 /* If the module is given, this returns relative address */
590 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
591 false, !!tp->module);
592 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000593 goto error;
594 addr += tp->offset;
595 }
596
597 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
598 tp->module ? : "kernel");
599
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900600 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
601 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000602 ret = debuginfo__find_probe_point(dinfo,
603 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900604 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000605 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000606
607 if (ret > 0) {
608 pp->retprobe = tp->retprobe;
609 return 0;
610 }
611error:
612 pr_debug("Failed to find corresponding probes from debuginfo.\n");
613 return ret ? : -ENOENT;
614}
615
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000616static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
617 int ntevs, const char *exec)
618{
619 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000620 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000621
622 if (!exec)
623 return 0;
624
625 ret = get_text_start_address(exec, &stext);
626 if (ret < 0)
627 return ret;
628
629 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000630 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000631 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000632 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000633 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000634 ret = -ENOMEM;
635 break;
636 }
637 tevs[i].uprobes = true;
638 }
639
640 return ret;
641}
642
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900643static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
644 int ntevs, const char *module)
645{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900646 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530647 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900648
649 if (!module)
650 return 0;
651
Ravi Bangoria63a29612016-04-26 19:55:41 +0530652 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900653
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900654 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530655 tevs[i].point.module =
656 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900657 if (!tevs[i].point.module) {
658 ret = -ENOMEM;
659 break;
660 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900661 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900662
Ravi Bangoria63a29612016-04-26 19:55:41 +0530663 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900664 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900665}
666
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000667/* Post processing the probe events */
668static int post_process_probe_trace_events(struct probe_trace_event *tevs,
669 int ntevs, const char *module,
670 bool uprobe)
671{
672 struct ref_reloc_sym *reloc_sym;
673 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900674 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000675
676 if (uprobe)
677 return add_exec_to_probe_trace_events(tevs, ntevs, module);
678
679 /* Note that currently ref_reloc_sym based probe is not for drivers */
680 if (module)
681 return add_module_to_probe_trace_events(tevs, ntevs, module);
682
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000683 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000684 if (!reloc_sym) {
685 pr_warning("Relocated base symbol is not found!\n");
686 return -EINVAL;
687 }
688
689 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900690 if (!tevs[i].point.address || tevs[i].point.retprobe)
691 continue;
692 /* If we found a wrong one, mark it by NULL symbol */
693 if (kprobe_warn_out_range(tevs[i].point.symbol,
694 tevs[i].point.address)) {
695 tmp = NULL;
696 skipped++;
697 } else {
698 tmp = strdup(reloc_sym->name);
699 if (!tmp)
700 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000701 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900702 /* If we have no realname, use symbol for it */
703 if (!tevs[i].point.realname)
704 tevs[i].point.realname = tevs[i].point.symbol;
705 else
706 free(tevs[i].point.symbol);
707 tevs[i].point.symbol = tmp;
708 tevs[i].point.offset = tevs[i].point.address -
709 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000710 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900711 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000712}
713
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300714/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530715static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900716 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300717{
718 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900719 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530720 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900721 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300722
Masami Hiramatsu44225522015-05-08 10:03:28 +0900723 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900724 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000725 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900726 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900727 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300728 return 0;
729 }
730
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000731 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900732 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900733 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900734
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900735 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900736 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900737 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900738 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900739 /*
740 * Write back to the original probe_event for
741 * setting appropriate (user given) event name
742 */
743 clear_perf_probe_point(&pev->point);
744 memcpy(&pev->point, &tmp, sizeof(tmp));
745 }
746 }
747
Masami Hiramatsuff741782011-06-27 16:27:39 +0900748 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300749
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400750 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000751 pr_debug("Found %d probe_trace_events.\n", ntevs);
752 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900753 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900754 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000755 clear_probe_trace_events(*tevs, ntevs);
756 zfree(tevs);
757 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900758 if (ret != ntevs)
759 return ret < 0 ? ret : ntevs;
760 ntevs = 0;
761 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400762 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300763
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400764 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900765 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400766 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900767 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400768 }
769 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400770 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000771 if (ntevs < 0) {
772 if (ntevs == -EBADF)
773 pr_warning("Warning: No dwarf info found in the vmlinux - "
774 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400775 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900776 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400777 return 0;
778 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300779 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400780 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300781}
782
783#define LINEBUF_SIZE 256
784#define NR_ADDITIONAL_LINES 2
785
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100786static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300787{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000788 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100789 const char *color = show_num ? "" : PERF_COLOR_BLUE;
790 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300791
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100792 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300793 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
794 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100795 if (skip)
796 continue;
797 if (!prefix) {
798 prefix = show_num ? "%7d " : " ";
799 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100801 color_fprintf(stdout, color, "%s", buf);
802
803 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400804
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100805 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100807 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000808 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300809 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100810 return -1;
811 }
812 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300813}
814
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100815static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
816{
817 int rv = __show_one_line(fp, l, skip, show_num);
818 if (rv == 0) {
819 pr_warning("Source file is shorter than expected.\n");
820 rv = -1;
821 }
822 return rv;
823}
824
825#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
826#define show_one_line(f,l) _show_one_line(f,l,false,false)
827#define skip_one_line(f,l) _show_one_line(f,l,true,false)
828#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
829
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300830/*
831 * Show line-range always requires debuginfo to find source file and
832 * line number.
833 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900834static int __show_line_range(struct line_range *lr, const char *module,
835 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300836{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400837 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000838 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900839 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300840 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900841 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900842 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000843 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300844
845 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000846 dinfo = open_debuginfo(module, false);
847 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900848 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400849
Masami Hiramatsuff741782011-06-27 16:27:39 +0900850 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900851 if (!ret) { /* Not found, retry with an alternative */
852 ret = get_alternative_line_range(dinfo, lr, module, user);
853 if (!ret)
854 ret = debuginfo__find_line_range(dinfo, lr);
855 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900856 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000857 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400858 pr_warning("Specified source line is not found.\n");
859 return -ENOENT;
860 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000861 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400862 return ret;
863 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300864
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900865 /* Convert source file path */
866 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900867 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800868
869 /* Free old path when new path is assigned */
870 if (tmp != lr->path)
871 free(tmp);
872
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900873 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000874 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900875 return ret;
876 }
877
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300878 setup_pager();
879
880 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900881 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300882 lr->start - lr->offset);
883 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100884 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300885
886 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400887 if (fp == NULL) {
888 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300889 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400890 return -errno;
891 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300892 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100893 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100894 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100895 if (ret < 0)
896 goto end;
897 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300898
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300899 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000900 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100901 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100902 if (ret < 0)
903 goto end;
904 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100905 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400906 if (ret < 0)
907 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300908 }
909
910 if (lr->end == INT_MAX)
911 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100912 while (l <= lr->end) {
913 ret = show_one_line_or_eof(fp, l++ - lr->offset);
914 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100915 break;
916 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400917end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300918 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400919 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300920}
921
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000922int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000923{
924 int ret;
925
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900926 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000927 if (ret < 0)
928 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900929 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900930 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000931
932 return ret;
933}
934
Masami Hiramatsuff741782011-06-27 16:27:39 +0900935static int show_available_vars_at(struct debuginfo *dinfo,
936 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900937 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900938{
939 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900940 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900941 struct str_node *node;
942 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900943 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900944 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900945
946 buf = synthesize_perf_probe_point(&pev->point);
947 if (!buf)
948 return -EINVAL;
949 pr_debug("Searching variables at %s\n", buf);
950
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900951 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900952 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900953 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900954 if (!ret) {
955 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900956 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900957 /* Release the old probe_point */
958 clear_perf_probe_point(&tmp);
959 }
960 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900961 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000962 if (ret == 0 || ret == -ENOENT) {
963 pr_err("Failed to find the address of %s\n", buf);
964 ret = -ENOENT;
965 } else
966 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900967 goto end;
968 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000969
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900970 /* Some variables are found */
971 fprintf(stdout, "Available variables at %s\n", buf);
972 for (i = 0; i < ret; i++) {
973 vl = &vls[i];
974 /*
975 * A probe point might be converted to
976 * several trace points.
977 */
978 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
979 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300980 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900981 nvars = 0;
982 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -0300983 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900984 var = strchr(node->s, '\t') + 1;
985 if (strfilter__compare(_filter, var)) {
986 fprintf(stdout, "\t\t%s\n", node->s);
987 nvars++;
988 }
989 }
990 strlist__delete(vl->vars);
991 }
992 if (nvars == 0)
993 fprintf(stdout, "\t\t(No matched variables)\n");
994 }
995 free(vls);
996end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900997 free(buf);
998 return ret;
999}
1000
1001/* Show available variables on given probe point */
1002int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001003 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001004{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001005 int i, ret = 0;
1006 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001007
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001008 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001009 if (ret < 0)
1010 return ret;
1011
Masami Hiramatsu44225522015-05-08 10:03:28 +09001012 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001013 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001014 ret = -ENOENT;
1015 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001016 }
1017
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001018 setup_pager();
1019
Masami Hiramatsuff741782011-06-27 16:27:39 +09001020 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001021 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001022
1023 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001024out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001025 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001026 return ret;
1027}
1028
Ingo Molnar89fe8082013-09-30 12:07:11 +02001029#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001030
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001031static void debuginfo_cache__exit(void)
1032{
1033}
1034
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001035static int
1036find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1037 struct perf_probe_point *pp __maybe_unused,
1038 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001039{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001040 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001041}
1042
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301043static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001044 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001045{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001046 if (perf_probe_event_need_dwarf(pev)) {
1047 pr_warning("Debuginfo-analysis is not supported.\n");
1048 return -ENOSYS;
1049 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301050
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001051 return 0;
1052}
1053
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001054int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001055 const char *module __maybe_unused,
1056 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001057{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001058 pr_warning("Debuginfo-analysis is not supported.\n");
1059 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001060}
1061
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001062int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001063 int npevs __maybe_unused,
1064 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001065{
1066 pr_warning("Debuginfo-analysis is not supported.\n");
1067 return -ENOSYS;
1068}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001069#endif
1070
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001071void line_range__clear(struct line_range *lr)
1072{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001073 free(lr->function);
1074 free(lr->file);
1075 free(lr->path);
1076 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001077 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001078 memset(lr, 0, sizeof(*lr));
1079}
1080
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001081int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001082{
1083 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001084 lr->line_list = intlist__new(NULL);
1085 if (!lr->line_list)
1086 return -ENOMEM;
1087 else
1088 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001089}
1090
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001091static int parse_line_num(char **ptr, int *val, const char *what)
1092{
1093 const char *start = *ptr;
1094
1095 errno = 0;
1096 *val = strtol(*ptr, ptr, 0);
1097 if (errno || *ptr == start) {
1098 semantic_error("'%s' is not a valid number.\n", what);
1099 return -EINVAL;
1100 }
1101 return 0;
1102}
1103
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001104/* Check the name is good for event, group or function */
1105static bool is_c_func_name(const char *name)
1106{
1107 if (!isalpha(*name) && *name != '_')
1108 return false;
1109 while (*++name != '\0') {
1110 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1111 return false;
1112 }
1113 return true;
1114}
1115
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001116/*
1117 * Stuff 'lr' according to the line range described by 'arg'.
1118 * The line range syntax is described by:
1119 *
1120 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001121 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001122 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001123int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001124{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001125 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001126 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001127
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001128 if (!name)
1129 return -ENOMEM;
1130
1131 lr->start = 0;
1132 lr->end = INT_MAX;
1133
1134 range = strchr(name, ':');
1135 if (range) {
1136 *range++ = '\0';
1137
1138 err = parse_line_num(&range, &lr->start, "start line");
1139 if (err)
1140 goto err;
1141
1142 if (*range == '+' || *range == '-') {
1143 const char c = *range++;
1144
1145 err = parse_line_num(&range, &lr->end, "end line");
1146 if (err)
1147 goto err;
1148
1149 if (c == '+') {
1150 lr->end += lr->start;
1151 /*
1152 * Adjust the number of lines here.
1153 * If the number of lines == 1, the
1154 * the end of line should be equal to
1155 * the start of line.
1156 */
1157 lr->end--;
1158 }
1159 }
1160
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001161 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001162
1163 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001164 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001165 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001166 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001167 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001168 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001169 if (*range != '\0') {
1170 semantic_error("Tailing with invalid str '%s'.\n", range);
1171 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001173 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001174
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001175 file = strchr(name, '@');
1176 if (file) {
1177 *file = '\0';
1178 lr->file = strdup(++file);
1179 if (lr->file == NULL) {
1180 err = -ENOMEM;
1181 goto err;
1182 }
1183 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001184 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001185 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001186 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001187 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001188 else { /* Invalid name */
1189 semantic_error("'%s' is not a valid function name.\n", name);
1190 err = -EINVAL;
1191 goto err;
1192 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193
1194 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001195err:
1196 free(name);
1197 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001198}
1199
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001200static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1201{
1202 char *ptr;
1203
1204 ptr = strchr(*arg, ':');
1205 if (ptr) {
1206 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001207 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001208 goto ng_name;
1209 pev->group = strdup(*arg);
1210 if (!pev->group)
1211 return -ENOMEM;
1212 *arg = ptr + 1;
1213 } else
1214 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001215 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001216ng_name:
1217 semantic_error("%s is bad for event name -it must "
1218 "follow C symbol-naming rule.\n", *arg);
1219 return -EINVAL;
1220 }
1221 pev->event = strdup(*arg);
1222 if (pev->event == NULL)
1223 return -ENOMEM;
1224
1225 return 0;
1226}
1227
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001228/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001230{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001231 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001232 char *ptr, *tmp;
1233 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301234 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001235 int ret;
1236
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001237 /*
1238 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001239 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1240 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001241 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001242 */
Wang Nane59d29e2015-04-28 08:46:09 +00001243 if (!arg)
1244 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001245
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001246 if (arg[0] == '%') {
1247 pev->sdt = true;
1248 arg++;
1249 }
1250
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001251 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001252 if (pev->sdt) {
1253 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001254 if (*ptr != '@') {
1255 semantic_error("%s must be an SDT name.\n",
1256 arg);
1257 return -EINVAL;
1258 }
1259 /* This must be a target file name or build id */
1260 tmp = build_id_cache__complement(ptr + 1);
1261 if (tmp) {
1262 pev->target = build_id_cache__origname(tmp);
1263 free(tmp);
1264 } else
1265 pev->target = strdup(ptr + 1);
1266 if (!pev->target)
1267 return -ENOMEM;
1268 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001269 }
1270 ret = parse_perf_probe_event_name(&arg, pev);
1271 if (ret == 0) {
1272 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1273 ret = -errno;
1274 }
1275 return ret;
1276 }
1277
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001278 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001279 *ptr = '\0';
1280 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001281 ret = parse_perf_probe_event_name(&arg, pev);
1282 if (ret < 0)
1283 return ret;
1284
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001285 arg = tmp;
1286 }
1287
Naveen N. Rao3099c022015-04-28 17:35:34 +05301288 /*
1289 * Check arg is function or file name and copy it.
1290 *
1291 * We consider arg to be a file spec if and only if it satisfies
1292 * all of the below criteria::
1293 * - it does not include any of "+@%",
1294 * - it includes one of ":;", and
1295 * - it has a period '.' in the name.
1296 *
1297 * Otherwise, we consider arg to be a function specification.
1298 */
1299 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1300 /* This is a file spec if it includes a '.' before ; or : */
1301 if (memchr(arg, '.', ptr - arg))
1302 file_spec = true;
1303 }
1304
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001305 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001306 if (ptr) {
1307 nc = *ptr;
1308 *ptr++ = '\0';
1309 }
1310
Wang Nan6c6e0242015-08-26 10:57:44 +00001311 if (arg[0] == '\0')
1312 tmp = NULL;
1313 else {
1314 tmp = strdup(arg);
1315 if (tmp == NULL)
1316 return -ENOMEM;
1317 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001318
Naveen N. Rao3099c022015-04-28 17:35:34 +05301319 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001320 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001321 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001322 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001323
Wang Nanda15bd92015-08-26 10:57:45 +00001324 /*
1325 * Keep pp->function even if this is absolute address,
1326 * so it can mark whether abs_address is valid.
1327 * Which make 'perf probe lib.bin 0x0' possible.
1328 *
1329 * Note that checking length of tmp is not needed
1330 * because when we access tmp[1] we know tmp[0] is '0',
1331 * so tmp[1] should always valid (but could be '\0').
1332 */
1333 if (tmp && !strncmp(tmp, "0x", 2)) {
1334 pp->abs_address = strtoul(pp->function, &tmp, 0);
1335 if (*tmp != '\0') {
1336 semantic_error("Invalid absolute address.\n");
1337 return -EINVAL;
1338 }
1339 }
1340 }
1341
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001342 /* Parse other options */
1343 while (ptr) {
1344 arg = ptr;
1345 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001346 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001347 pp->lazy_line = strdup(arg);
1348 if (pp->lazy_line == NULL)
1349 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001350 break;
1351 }
1352 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001353 if (ptr) {
1354 nc = *ptr;
1355 *ptr++ = '\0';
1356 }
1357 switch (c) {
1358 case ':': /* Line number */
1359 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001361 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001362 " in line number.\n");
1363 return -EINVAL;
1364 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001365 break;
1366 case '+': /* Byte offset from a symbol */
1367 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001368 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001369 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001370 " in offset.\n");
1371 return -EINVAL;
1372 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001373 break;
1374 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001375 if (pp->file) {
1376 semantic_error("SRC@SRC is not allowed.\n");
1377 return -EINVAL;
1378 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001379 pp->file = strdup(arg);
1380 if (pp->file == NULL)
1381 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001382 break;
1383 case '%': /* Probe places */
1384 if (strcmp(arg, "return") == 0) {
1385 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001386 } else { /* Others not supported yet */
1387 semantic_error("%%%s is not supported.\n", arg);
1388 return -ENOTSUP;
1389 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001390 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001391 default: /* Buggy case */
1392 pr_err("This program has a bug at %s:%d.\n",
1393 __FILE__, __LINE__);
1394 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001395 break;
1396 }
1397 }
1398
1399 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001400 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001401 semantic_error("Lazy pattern can't be used with"
1402 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 return -EINVAL;
1404 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001405
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001406 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001407 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 return -EINVAL;
1409 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001410
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001411 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001412 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001413 return -EINVAL;
1414 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001415
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001416 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001417 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001418 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001419 return -EINVAL;
1420 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001421
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001422 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001423 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001424 return -EINVAL;
1425 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001426
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001427 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001428 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001429 return -EINVAL;
1430 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001431
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001432 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001433 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001434 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001435 return -EINVAL;
1436 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001437
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001438 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001439 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1440 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001441 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001442}
1443
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001444/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001446{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001447 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001448 struct perf_probe_arg_field **fieldp;
1449
1450 pr_debug("parsing arg: %s into ", str);
1451
Masami Hiramatsu48481932010-04-12 13:16:53 -04001452 tmp = strchr(str, '=');
1453 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001454 arg->name = strndup(str, tmp - str);
1455 if (arg->name == NULL)
1456 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001457 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001458 str = tmp + 1;
1459 }
1460
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001461 tmp = strchr(str, ':');
1462 if (tmp) { /* Type setting */
1463 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001464 arg->type = strdup(tmp + 1);
1465 if (arg->type == NULL)
1466 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001467 pr_debug("type:%s ", arg->type);
1468 }
1469
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001470 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001471 if (!is_c_varname(str) || !tmp) {
1472 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001473 arg->var = strdup(str);
1474 if (arg->var == NULL)
1475 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001476 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001477 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001478 }
1479
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001480 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001481 arg->var = strndup(str, tmp - str);
1482 if (arg->var == NULL)
1483 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001484 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001485 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001486 fieldp = &arg->field;
1487
1488 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001489 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1490 if (*fieldp == NULL)
1491 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001492 if (*tmp == '[') { /* Array */
1493 str = tmp;
1494 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001495 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001496 if (*tmp != ']' || tmp == str + 1) {
1497 semantic_error("Array index must be a"
1498 " number.\n");
1499 return -EINVAL;
1500 }
1501 tmp++;
1502 if (*tmp == '\0')
1503 tmp = NULL;
1504 } else { /* Structure */
1505 if (*tmp == '.') {
1506 str = tmp + 1;
1507 (*fieldp)->ref = false;
1508 } else if (tmp[1] == '>') {
1509 str = tmp + 2;
1510 (*fieldp)->ref = true;
1511 } else {
1512 semantic_error("Argument parse error: %s\n",
1513 str);
1514 return -EINVAL;
1515 }
1516 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001517 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001518 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001519 (*fieldp)->name = strndup(str, tmp - str);
1520 if ((*fieldp)->name == NULL)
1521 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001522 if (*str != '[')
1523 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001524 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1525 fieldp = &(*fieldp)->next;
1526 }
1527 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001528 (*fieldp)->name = strdup(str);
1529 if ((*fieldp)->name == NULL)
1530 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001531 if (*str != '[')
1532 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001533 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001534
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001535 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001536 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001537 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001538 if (arg->name == NULL)
1539 return -ENOMEM;
1540 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001541 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001542}
1543
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001544/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001545int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001546{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001547 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001548 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001549
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001550 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001551 if (!argv) {
1552 pr_debug("Failed to split arguments.\n");
1553 return -ENOMEM;
1554 }
1555 if (argc - 1 > MAX_PROBE_ARGS) {
1556 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1557 ret = -ERANGE;
1558 goto out;
1559 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001560 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001561 ret = parse_perf_probe_point(argv[0], pev);
1562 if (ret < 0)
1563 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001564
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001565 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001566 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001567 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1568 if (pev->args == NULL) {
1569 ret = -ENOMEM;
1570 goto out;
1571 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001572 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1573 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1574 if (ret >= 0 &&
1575 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001576 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001577 " kretprobe.\n");
1578 ret = -EINVAL;
1579 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001580 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001581out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001582 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001583
1584 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001585}
1586
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001587/* Return true if this perf_probe_event requires debuginfo */
1588bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001589{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001590 int i;
1591
1592 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1593 return true;
1594
1595 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsuf6eb0512016-07-12 19:04:34 +09001596 if (is_c_varname(pev->args[i].var) ||
1597 !strcmp(pev->args[i].var, "$params") ||
1598 !strcmp(pev->args[i].var, "$vars"))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001599 return true;
1600
1601 return false;
1602}
1603
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301604/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001605int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001606{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301607 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001608 char pr;
1609 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001610 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001611 int ret, i, argc;
1612 char **argv;
1613
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301614 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001615 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001616 if (!argv) {
1617 pr_debug("Failed to split arguments.\n");
1618 return -ENOMEM;
1619 }
1620 if (argc < 2) {
1621 semantic_error("Too few probe arguments.\n");
1622 ret = -ERANGE;
1623 goto out;
1624 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001625
1626 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001627 argv0_str = strdup(argv[0]);
1628 if (argv0_str == NULL) {
1629 ret = -ENOMEM;
1630 goto out;
1631 }
1632 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1633 fmt2_str = strtok_r(NULL, "/", &fmt);
1634 fmt3_str = strtok_r(NULL, " \t", &fmt);
1635 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1636 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001637 semantic_error("Failed to parse event name: %s\n", argv[0]);
1638 ret = -EINVAL;
1639 goto out;
1640 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001641 pr = fmt1_str[0];
1642 tev->group = strdup(fmt2_str);
1643 tev->event = strdup(fmt3_str);
1644 if (tev->group == NULL || tev->event == NULL) {
1645 ret = -ENOMEM;
1646 goto out;
1647 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001648 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001649
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001650 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001651
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001652 /* Scan module name(if there), function name and offset */
1653 p = strchr(argv[1], ':');
1654 if (p) {
1655 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001656 if (!tp->module) {
1657 ret = -ENOMEM;
1658 goto out;
1659 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001660 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001661 p++;
1662 } else
1663 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001664 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001665 /* only the address started with 0x */
1666 if (fmt1_str[0] == '0') {
1667 /*
1668 * Fix a special case:
1669 * if address == 0, kernel reports something like:
1670 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1671 * Newer kernel may fix that, but we want to
1672 * support old kernel also.
1673 */
1674 if (strcmp(fmt1_str, "0x") == 0) {
1675 if (!argv[2] || strcmp(argv[2], "(null)")) {
1676 ret = -EINVAL;
1677 goto out;
1678 }
1679 tp->address = 0;
1680
1681 free(argv[2]);
1682 for (i = 2; argv[i + 1] != NULL; i++)
1683 argv[i] = argv[i + 1];
1684
1685 argv[i] = NULL;
1686 argc -= 1;
1687 } else
1688 tp->address = strtoul(fmt1_str, NULL, 0);
1689 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001690 /* Only the symbol-based probe has offset */
1691 tp->symbol = strdup(fmt1_str);
1692 if (tp->symbol == NULL) {
1693 ret = -ENOMEM;
1694 goto out;
1695 }
1696 fmt2_str = strtok_r(NULL, "", &fmt);
1697 if (fmt2_str == NULL)
1698 tp->offset = 0;
1699 else
1700 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001701 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001702
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001703 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301704 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001705 if (tev->args == NULL) {
1706 ret = -ENOMEM;
1707 goto out;
1708 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001709 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001710 p = strchr(argv[i + 2], '=');
1711 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001712 *p++ = '\0';
1713 else
1714 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001715 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001717 tev->args[i].value = strdup(p);
1718 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1719 ret = -ENOMEM;
1720 goto out;
1721 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001722 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001723 ret = 0;
1724out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001725 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001726 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001727 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001728}
1729
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001730/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001731char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001732{
1733 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001734 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001735 char *ret = NULL;
1736 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001737
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001738 if (strbuf_init(&buf, 64) < 0)
1739 return NULL;
1740
Masami Hiramatsu48481932010-04-12 13:16:53 -04001741 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001742 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001743 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001744 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1745 if (err)
1746 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001747
1748 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001749 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001750 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001751 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001752 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1753 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001754 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001755 if (err)
1756 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001757 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001758
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001759 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001760 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1761 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001762
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001763 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001764out:
1765 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001766 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001767}
1768
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001769/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001770char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001771{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001772 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001773 char *tmp, *ret = NULL;
1774 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001775
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001776 if (strbuf_init(&buf, 64) < 0)
1777 return NULL;
1778
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001779 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001780 if (strbuf_addstr(&buf, pp->function) < 0)
1781 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001782 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001783 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001784 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001785 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001786 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001787 err = strbuf_addstr(&buf, "%return");
1788 if (err)
1789 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001790 }
1791 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001792 tmp = pp->file;
1793 len = strlen(tmp);
1794 if (len > 30) {
1795 tmp = strchr(pp->file + len - 30, '/');
1796 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1797 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001798 err = strbuf_addf(&buf, "@%s", tmp);
1799 if (!err && !pp->function && pp->line)
1800 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001801 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001802 if (!err)
1803 ret = strbuf_detach(&buf, NULL);
1804out:
1805 strbuf_release(&buf);
1806 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001807}
1808
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001809char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1810{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001811 struct strbuf buf;
1812 char *tmp, *ret = NULL;
1813 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001814
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001815 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001816 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001817 if (pev->event)
1818 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1819 pev->event) < 0)
1820 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001821
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001822 tmp = synthesize_perf_probe_point(&pev->point);
1823 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1824 goto out;
1825 free(tmp);
1826
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001827 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001828 tmp = synthesize_perf_probe_arg(pev->args + i);
1829 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1830 goto out;
1831 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001832 }
1833
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001834 ret = strbuf_detach(&buf, NULL);
1835out:
1836 strbuf_release(&buf);
1837 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001838}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001839
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301840static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001841 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001842{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001843 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001844 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301845 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001846 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001847 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001848 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001849 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001850 err = strbuf_addf(buf, "%+ld(", ref->offset);
1851 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001852}
1853
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301854static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001855 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001856{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301857 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001858 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859
1860 /* Argument name or separator */
1861 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001862 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001863 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001864 err = strbuf_addch(buf, ' ');
1865 if (err)
1866 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001867
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001868 /* Special case: @XXX */
1869 if (arg->value[0] == '@' && arg->ref)
1870 ref = ref->next;
1871
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001872 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001873 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001874 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001875 if (depth < 0)
1876 return depth;
1877 }
1878
1879 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001880 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001881 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001882 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001883 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884
1885 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001886 while (!err && depth--)
1887 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001889 /* Print argument type */
1890 if (!err && arg->type)
1891 err = strbuf_addf(buf, ":%s", arg->type);
1892
1893 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001894}
1895
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301896char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301898 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001899 struct strbuf buf;
1900 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001901 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001902
Wang Nanda15bd92015-08-26 10:57:45 +00001903 /* Uprobes must have tp->module */
1904 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001905 return NULL;
1906
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001907 if (strbuf_init(&buf, 32) < 0)
1908 return NULL;
1909
1910 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1911 tev->group, tev->event) < 0)
1912 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001913 /*
1914 * If tp->address == 0, then this point must be a
1915 * absolute address uprobe.
1916 * try_to_find_absolute_address() should have made
1917 * tp->symbol to "0x0".
1918 */
1919 if (tev->uprobes && !tp->address) {
1920 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1921 goto error;
1922 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001923
1924 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301925 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001926 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001927 else if (!strncmp(tp->symbol, "0x", 2))
1928 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001929 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1930 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301931 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001932 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1933 tp->module ? ":" : "", tp->symbol, tp->offset);
1934 if (err)
1935 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301936
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001937 for (i = 0; i < tev->nargs; i++)
1938 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001939 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001940
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001941 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001942error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001943 strbuf_release(&buf);
1944 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001945}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001946
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001947static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1948 struct perf_probe_point *pp,
1949 bool is_kprobe)
1950{
1951 struct symbol *sym = NULL;
1952 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001953 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001954 int ret = -ENOENT;
1955
1956 if (!is_kprobe) {
1957 map = dso__new_map(tp->module);
1958 if (!map)
1959 goto out;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001960 sym = map__find_symbol(map, addr, NULL);
1961 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001962 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09001963 if (kernel_get_symbol_address_by_name(tp->symbol,
1964 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09001965 goto out;
1966 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001967 if (addr) {
1968 addr += tp->offset;
1969 sym = __find_kernel_function(addr, &map);
1970 }
1971 }
Wang Nan98d3b252015-11-05 13:19:25 +00001972
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001973 if (!sym)
1974 goto out;
1975
1976 pp->retprobe = tp->retprobe;
1977 pp->offset = addr - map->unmap_ip(map, sym->start);
1978 pp->function = strdup(sym->name);
1979 ret = pp->function ? 0 : -ENOMEM;
1980
1981out:
1982 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001983 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001984 }
1985
1986 return ret;
1987}
1988
1989static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00001990 struct perf_probe_point *pp,
1991 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001992{
1993 char buf[128];
1994 int ret;
1995
1996 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1997 if (!ret)
1998 return 0;
1999 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2000 if (!ret)
2001 return 0;
2002
2003 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2004
2005 if (tp->symbol) {
2006 pp->function = strdup(tp->symbol);
2007 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002008 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002009 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2010 if (ret < 0)
2011 return ret;
2012 pp->function = strdup(buf);
2013 pp->offset = 0;
2014 }
2015 if (pp->function == NULL)
2016 return -ENOMEM;
2017
2018 pp->retprobe = tp->retprobe;
2019
2020 return 0;
2021}
2022
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302023static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302024 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002025{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002026 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002027 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002028
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002029 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002030 pev->event = strdup(tev->event);
2031 pev->group = strdup(tev->group);
2032 if (pev->event == NULL || pev->group == NULL)
2033 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002034
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002035 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002036 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002037 if (ret < 0)
2038 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002039
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002040 /* Convert trace_arg to probe_arg */
2041 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002042 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2043 if (pev->args == NULL)
2044 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002045 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002046 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002047 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002048 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002049 if ((ret = strbuf_init(&buf, 32)) < 0)
2050 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002051 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2052 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002053 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002054 if (pev->args[i].name == NULL && ret >= 0)
2055 ret = -ENOMEM;
2056 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002057error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002058 if (ret < 0)
2059 clear_perf_probe_event(pev);
2060
2061 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002062}
2063
2064void clear_perf_probe_event(struct perf_probe_event *pev)
2065{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002066 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002067 int i;
2068
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002069 free(pev->event);
2070 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002071 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002072 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002073
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002074 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002075 free(pev->args[i].name);
2076 free(pev->args[i].var);
2077 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002078 field = pev->args[i].field;
2079 while (field) {
2080 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002081 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002082 free(field);
2083 field = next;
2084 }
2085 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002086 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002087 memset(pev, 0, sizeof(*pev));
2088}
2089
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002090#define strdup_or_goto(str, label) \
2091({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2092
2093static int perf_probe_point__copy(struct perf_probe_point *dst,
2094 struct perf_probe_point *src)
2095{
2096 dst->file = strdup_or_goto(src->file, out_err);
2097 dst->function = strdup_or_goto(src->function, out_err);
2098 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2099 dst->line = src->line;
2100 dst->retprobe = src->retprobe;
2101 dst->offset = src->offset;
2102 return 0;
2103
2104out_err:
2105 clear_perf_probe_point(dst);
2106 return -ENOMEM;
2107}
2108
2109static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2110 struct perf_probe_arg *src)
2111{
2112 struct perf_probe_arg_field *field, **ppfield;
2113
2114 dst->name = strdup_or_goto(src->name, out_err);
2115 dst->var = strdup_or_goto(src->var, out_err);
2116 dst->type = strdup_or_goto(src->type, out_err);
2117
2118 field = src->field;
2119 ppfield = &(dst->field);
2120 while (field) {
2121 *ppfield = zalloc(sizeof(*field));
2122 if (!*ppfield)
2123 goto out_err;
2124 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2125 (*ppfield)->index = field->index;
2126 (*ppfield)->ref = field->ref;
2127 field = field->next;
2128 ppfield = &((*ppfield)->next);
2129 }
2130 return 0;
2131out_err:
2132 return -ENOMEM;
2133}
2134
2135int perf_probe_event__copy(struct perf_probe_event *dst,
2136 struct perf_probe_event *src)
2137{
2138 int i;
2139
2140 dst->event = strdup_or_goto(src->event, out_err);
2141 dst->group = strdup_or_goto(src->group, out_err);
2142 dst->target = strdup_or_goto(src->target, out_err);
2143 dst->uprobes = src->uprobes;
2144
2145 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2146 goto out_err;
2147
2148 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2149 if (!dst->args)
2150 goto out_err;
2151 dst->nargs = src->nargs;
2152
2153 for (i = 0; i < src->nargs; i++)
2154 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2155 goto out_err;
2156 return 0;
2157
2158out_err:
2159 clear_perf_probe_event(dst);
2160 return -ENOMEM;
2161}
2162
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002163void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002164{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302165 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002166 int i;
2167
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002168 free(tev->event);
2169 free(tev->group);
2170 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002171 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002172 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002173 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002174 free(tev->args[i].name);
2175 free(tev->args[i].value);
2176 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002177 ref = tev->args[i].ref;
2178 while (ref) {
2179 next = ref->next;
2180 free(ref);
2181 ref = next;
2182 }
2183 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002184 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002186}
2187
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002188struct kprobe_blacklist_node {
2189 struct list_head list;
2190 unsigned long start;
2191 unsigned long end;
2192 char *symbol;
2193};
2194
2195static void kprobe_blacklist__delete(struct list_head *blacklist)
2196{
2197 struct kprobe_blacklist_node *node;
2198
2199 while (!list_empty(blacklist)) {
2200 node = list_first_entry(blacklist,
2201 struct kprobe_blacklist_node, list);
2202 list_del(&node->list);
2203 free(node->symbol);
2204 free(node);
2205 }
2206}
2207
2208static int kprobe_blacklist__load(struct list_head *blacklist)
2209{
2210 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002211 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002212 char buf[PATH_MAX], *p;
2213 FILE *fp;
2214 int ret;
2215
2216 if (__debugfs == NULL)
2217 return -ENOTSUP;
2218
2219 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2220 if (ret < 0)
2221 return ret;
2222
2223 fp = fopen(buf, "r");
2224 if (!fp)
2225 return -errno;
2226
2227 ret = 0;
2228 while (fgets(buf, PATH_MAX, fp)) {
2229 node = zalloc(sizeof(*node));
2230 if (!node) {
2231 ret = -ENOMEM;
2232 break;
2233 }
2234 INIT_LIST_HEAD(&node->list);
2235 list_add_tail(&node->list, blacklist);
2236 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2237 ret = -EINVAL;
2238 break;
2239 }
2240 p = strchr(buf, '\t');
2241 if (p) {
2242 p++;
2243 if (p[strlen(p) - 1] == '\n')
2244 p[strlen(p) - 1] = '\0';
2245 } else
2246 p = (char *)"unknown";
2247 node->symbol = strdup(p);
2248 if (!node->symbol) {
2249 ret = -ENOMEM;
2250 break;
2251 }
2252 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2253 node->start, node->end, node->symbol);
2254 ret++;
2255 }
2256 if (ret < 0)
2257 kprobe_blacklist__delete(blacklist);
2258 fclose(fp);
2259
2260 return ret;
2261}
2262
2263static struct kprobe_blacklist_node *
2264kprobe_blacklist__find_by_address(struct list_head *blacklist,
2265 unsigned long address)
2266{
2267 struct kprobe_blacklist_node *node;
2268
2269 list_for_each_entry(node, blacklist, list) {
2270 if (node->start <= address && address <= node->end)
2271 return node;
2272 }
2273
2274 return NULL;
2275}
2276
Masami Hiramatsub0312202015-06-16 20:50:55 +09002277static LIST_HEAD(kprobe_blacklist);
2278
2279static void kprobe_blacklist__init(void)
2280{
2281 if (!list_empty(&kprobe_blacklist))
2282 return;
2283
2284 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2285 pr_debug("No kprobe blacklist support, ignored\n");
2286}
2287
2288static void kprobe_blacklist__release(void)
2289{
2290 kprobe_blacklist__delete(&kprobe_blacklist);
2291}
2292
2293static bool kprobe_blacklist__listed(unsigned long address)
2294{
2295 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2296}
2297
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002298static int perf_probe_event__sprintf(const char *group, const char *event,
2299 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002300 const char *module,
2301 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002302{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002303 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002304 char *buf;
2305
2306 if (asprintf(&buf, "%s:%s", group, event) < 0)
2307 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002308 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002309 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002310 if (ret)
2311 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002312
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002313 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002314 buf = synthesize_perf_probe_point(&pev->point);
2315 if (!buf)
2316 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002317 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002318 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002319
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002320 if (!ret && module)
2321 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002322
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002323 if (!ret && pev->nargs > 0) {
2324 ret = strbuf_add(result, " with", 5);
2325 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002326 buf = synthesize_perf_probe_arg(&pev->args[i]);
2327 if (!buf)
2328 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002329 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002330 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002331 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002332 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002333 if (!ret)
2334 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002335
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002336 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002337}
2338
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002339/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002340int show_perf_probe_event(const char *group, const char *event,
2341 struct perf_probe_event *pev,
2342 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002343{
2344 struct strbuf buf = STRBUF_INIT;
2345 int ret;
2346
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002347 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002348 if (ret >= 0) {
2349 if (use_stdout)
2350 printf("%s\n", buf.buf);
2351 else
2352 pr_info("%s\n", buf.buf);
2353 }
2354 strbuf_release(&buf);
2355
2356 return ret;
2357}
2358
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002359static bool filter_probe_trace_event(struct probe_trace_event *tev,
2360 struct strfilter *filter)
2361{
2362 char tmp[128];
2363
2364 /* At first, check the event name itself */
2365 if (strfilter__compare(filter, tev->event))
2366 return true;
2367
2368 /* Next, check the combination of name and group */
2369 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2370 return false;
2371 return strfilter__compare(filter, tmp);
2372}
2373
2374static int __show_perf_probe_events(int fd, bool is_kprobe,
2375 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002376{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302377 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302378 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002379 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002380 struct strlist *rawlist;
2381 struct str_node *ent;
2382
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002383 memset(&tev, 0, sizeof(tev));
2384 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002385
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002386 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002387 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002388 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002389
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002390 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302391 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002392 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002393 if (!filter_probe_trace_event(&tev, filter))
2394 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302395 ret = convert_to_perf_probe_event(&tev, &pev,
2396 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002397 if (ret < 0)
2398 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002399 ret = show_perf_probe_event(pev.group, pev.event,
2400 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002401 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002403next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002404 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302405 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002406 if (ret < 0)
2407 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002408 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002409 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002410 /* Cleanup cached debuginfo if needed */
2411 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002412
2413 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002414}
2415
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302416/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002417int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302418{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002419 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302420
2421 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302422
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002423 if (probe_conf.cache)
2424 return probe_cache__show_all_caches(filter);
2425
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002426 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302427 if (ret < 0)
2428 return ret;
2429
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002430 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2431 if (ret < 0)
2432 return ret;
2433
2434 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002435 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002436 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002437 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002438 if (kp_fd > 0)
2439 close(kp_fd);
2440 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002441 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002442 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302443
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002444 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002445}
2446
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447static int get_new_event_name(char *buf, size_t len, const char *base,
2448 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002449{
2450 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002451 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002452
Naveen N. Rao3099c022015-04-28 17:35:34 +05302453 if (*base == '.')
2454 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002455 nbase = strdup(base);
2456 if (!nbase)
2457 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302458
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002459 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2460 p = strchr(nbase, '.');
2461 if (p && p != nbase)
2462 *p = '\0';
2463
2464 /* Try no suffix number */
2465 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002466 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002467 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002468 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002469 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002470 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002471 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002472
Masami Hiramatsud761b082009-12-15 10:32:25 -05002473 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002474 pr_warning("Error: event \"%s\" already exists.\n"
2475 " Hint: Remove existing event by 'perf probe -d'\n"
2476 " or force duplicates by 'perf probe -f'\n"
2477 " or set 'force=yes' in BPF source.\n",
2478 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002479 ret = -EEXIST;
2480 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002481 }
2482
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002483 /* Try to add suffix */
2484 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002485 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002486 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002487 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002488 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002489 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002490 if (!strlist__has_entry(namelist, buf))
2491 break;
2492 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002493 if (i == MAX_EVENT_INDEX) {
2494 pr_warning("Too many events are on the same function.\n");
2495 ret = -ERANGE;
2496 }
2497
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002498out:
2499 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002500 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002501}
2502
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002503/* Warn if the current kernel's uprobe implementation is old */
2504static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2505{
2506 int i;
2507 char *buf = synthesize_probe_trace_command(tev);
2508
2509 /* Old uprobe event doesn't support memory dereference */
2510 if (!tev->uprobes || tev->nargs == 0 || !buf)
2511 goto out;
2512
2513 for (i = 0; i < tev->nargs; i++)
2514 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2515 pr_warning("Please upgrade your kernel to at least "
2516 "3.14 to have access to feature %s\n",
2517 tev->args[i].value);
2518 break;
2519 }
2520out:
2521 free(buf);
2522}
2523
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002524/* Set new name from original perf_probe_event and namelist */
2525static int probe_trace_event__set_name(struct probe_trace_event *tev,
2526 struct perf_probe_event *pev,
2527 struct strlist *namelist,
2528 bool allow_suffix)
2529{
2530 const char *event, *group;
2531 char buf[64];
2532 int ret;
2533
Masami Hiramatsubc062232016-07-01 17:03:12 +09002534 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002535 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002536 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002537 else if (tev->event)
2538 event = tev->event;
2539 else {
2540 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002541 if (pev->point.function &&
2542 (strncmp(pev->point.function, "0x", 2) != 0) &&
2543 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002544 event = pev->point.function;
2545 else
2546 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002547 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002548 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002549 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002550 else if (tev->group)
2551 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002552 else
2553 group = PERFPROBE_GROUP;
2554
2555 /* Get an unused new event name */
2556 ret = get_new_event_name(buf, 64, event,
2557 namelist, allow_suffix);
2558 if (ret < 0)
2559 return ret;
2560
2561 event = buf;
2562
2563 tev->event = strdup(event);
2564 tev->group = strdup(group);
2565 if (tev->event == NULL || tev->group == NULL)
2566 return -ENOMEM;
2567
2568 /* Add added event name to namelist */
2569 strlist__add(namelist, event);
2570 return 0;
2571}
2572
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002573static int __open_probe_file_and_namelist(bool uprobe,
2574 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002575{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002576 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002577
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002578 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002579 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002580 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002581
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002582 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002583 *namelist = probe_file__get_namelist(fd);
2584 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002585 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002586 close(fd);
2587 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002588 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002589 return fd;
2590}
2591
2592static int __add_probe_trace_events(struct perf_probe_event *pev,
2593 struct probe_trace_event *tevs,
2594 int ntevs, bool allow_suffix)
2595{
2596 int i, fd[2] = {-1, -1}, up, ret;
2597 struct probe_trace_event *tev = NULL;
2598 struct probe_cache *cache = NULL;
2599 struct strlist *namelist[2] = {NULL, NULL};
2600
2601 up = pev->uprobes ? 1 : 0;
2602 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2603 if (fd[up] < 0)
2604 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002605
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002606 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002607 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002608 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002609 up = tev->uprobes ? 1 : 0;
2610 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2611 fd[up] = __open_probe_file_and_namelist(up,
2612 &namelist[up]);
2613 if (fd[up] < 0)
2614 goto close_out;
2615 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002616 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002617 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002618 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002619
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002620 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002621 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002622 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002623 if (ret < 0)
2624 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002625
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002626 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002627 if (ret < 0)
2628 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002629
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002630 /*
2631 * Probes after the first probe which comes from same
2632 * user input are always allowed to add suffix, because
2633 * there might be several addresses corresponding to
2634 * one code line.
2635 */
2636 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002637 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002638 if (ret == -EINVAL && pev->uprobes)
2639 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002640 if (ret == 0 && probe_conf.cache) {
2641 cache = probe_cache__new(pev->target);
2642 if (!cache ||
2643 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2644 probe_cache__commit(cache) < 0)
2645 pr_warning("Failed to add event to probe cache\n");
2646 probe_cache__delete(cache);
2647 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002648
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002649close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002650 for (up = 0; up < 2; up++) {
2651 strlist__delete(namelist[up]);
2652 if (fd[up] >= 0)
2653 close(fd[up]);
2654 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002655 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002656}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002657
Wang Nan6bb536c2015-05-29 09:45:47 +00002658static int find_probe_functions(struct map *map, char *name,
2659 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002660{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002661 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002662 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002663 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002664
Wang Nan75e4a2a2015-05-15 12:14:44 +00002665 if (map__load(map, NULL) < 0)
2666 return 0;
2667
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002668 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002669 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002670 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002671 if (syms && found < probe_conf.max_probes)
2672 syms[found - 1] = sym;
2673 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002674 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002675
2676 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002677}
2678
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302679void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2680 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302681 struct map *map __maybe_unused,
2682 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302683
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002684/*
2685 * Find probe function addresses from map.
2686 * Return an error or the number of found probe_trace_event
2687 */
2688static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002689 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002690{
2691 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002692 struct ref_reloc_sym *reloc_sym = NULL;
2693 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002694 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002695 struct probe_trace_event *tev;
2696 struct perf_probe_point *pp = &pev->point;
2697 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002698 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002699 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302700 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002701
Masami Hiramatsu44225522015-05-08 10:03:28 +09002702 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002703 if (!map) {
2704 ret = -EINVAL;
2705 goto out;
2706 }
2707
Wang Nan6bb536c2015-05-29 09:45:47 +00002708 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2709 if (!syms) {
2710 ret = -ENOMEM;
2711 goto out;
2712 }
2713
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002714 /*
2715 * Load matched symbols: Since the different local symbols may have
2716 * same name but different addresses, this lists all the symbols.
2717 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002718 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002719 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002720 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002721 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002722 ret = -ENOENT;
2723 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002724 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002725 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002726 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002727 ret = -E2BIG;
2728 goto out;
2729 }
2730
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002731 /* Note that the symbols in the kmodule are not relocated */
2732 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002733 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002734 if (!reloc_sym) {
2735 pr_warning("Relocated base symbol is not found!\n");
2736 ret = -EINVAL;
2737 goto out;
2738 }
2739 }
2740
2741 /* Setup result trace-probe-events */
2742 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2743 if (!*tevs) {
2744 ret = -ENOMEM;
2745 goto out;
2746 }
2747
2748 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002749
Wang Nan6bb536c2015-05-29 09:45:47 +00002750 for (j = 0; j < num_matched_functions; j++) {
2751 sym = syms[j];
2752
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002753 tev = (*tevs) + ret;
2754 tp = &tev->point;
2755 if (ret == num_matched_functions) {
2756 pr_warning("Too many symbols are listed. Skip it.\n");
2757 break;
2758 }
2759 ret++;
2760
2761 if (pp->offset > sym->end - sym->start) {
2762 pr_warning("Offset %ld is bigger than the size of %s\n",
2763 pp->offset, sym->name);
2764 ret = -ENOENT;
2765 goto err_out;
2766 }
2767 /* Add one probe point */
2768 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002769
2770 /* Check the kprobe (not in module) is within .text */
2771 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002772 kprobe_warn_out_range(sym->name, tp->address)) {
2773 tp->symbol = NULL; /* Skip it */
2774 skipped++;
2775 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002776 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2777 tp->offset = tp->address - reloc_sym->addr;
2778 } else {
2779 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2780 tp->offset = pp->offset;
2781 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002782 tp->realname = strdup_or_goto(sym->name, nomem_out);
2783
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002784 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302785 if (pev->target) {
2786 if (pev->uprobes) {
2787 tev->point.module = strdup_or_goto(pev->target,
2788 nomem_out);
2789 } else {
2790 mod_name = find_module_name(pev->target);
2791 tev->point.module =
2792 strdup(mod_name ? mod_name : pev->target);
2793 free(mod_name);
2794 if (!tev->point.module)
2795 goto nomem_out;
2796 }
2797 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002798 tev->uprobes = pev->uprobes;
2799 tev->nargs = pev->nargs;
2800 if (tev->nargs) {
2801 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2802 tev->nargs);
2803 if (tev->args == NULL)
2804 goto nomem_out;
2805 }
2806 for (i = 0; i < tev->nargs; i++) {
2807 if (pev->args[i].name)
2808 tev->args[i].name =
2809 strdup_or_goto(pev->args[i].name,
2810 nomem_out);
2811
2812 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2813 nomem_out);
2814 if (pev->args[i].type)
2815 tev->args[i].type =
2816 strdup_or_goto(pev->args[i].type,
2817 nomem_out);
2818 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302819 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002820 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002821 if (ret == skipped) {
2822 ret = -ENOENT;
2823 goto err_out;
2824 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002825
2826out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002827 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002828 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002829 return ret;
2830
2831nomem_out:
2832 ret = -ENOMEM;
2833err_out:
2834 clear_probe_trace_events(*tevs, num_matched_functions);
2835 zfree(tevs);
2836 goto out;
2837}
2838
Wang Nanda15bd92015-08-26 10:57:45 +00002839static int try_to_find_absolute_address(struct perf_probe_event *pev,
2840 struct probe_trace_event **tevs)
2841{
2842 struct perf_probe_point *pp = &pev->point;
2843 struct probe_trace_event *tev;
2844 struct probe_trace_point *tp;
2845 int i, err;
2846
2847 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2848 return -EINVAL;
2849 if (perf_probe_event_need_dwarf(pev))
2850 return -EINVAL;
2851
2852 /*
2853 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2854 * absolute address.
2855 *
2856 * Only one tev can be generated by this.
2857 */
2858 *tevs = zalloc(sizeof(*tev));
2859 if (!*tevs)
2860 return -ENOMEM;
2861
2862 tev = *tevs;
2863 tp = &tev->point;
2864
2865 /*
2866 * Don't use tp->offset, use address directly, because
2867 * in synthesize_probe_trace_command() address cannot be
2868 * zero.
2869 */
2870 tp->address = pev->point.abs_address;
2871 tp->retprobe = pp->retprobe;
2872 tev->uprobes = pev->uprobes;
2873
2874 err = -ENOMEM;
2875 /*
2876 * Give it a '0x' leading symbol name.
2877 * In __add_probe_trace_events, a NULL symbol is interpreted as
2878 * invalud.
2879 */
2880 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2881 goto errout;
2882
2883 /* For kprobe, check range */
2884 if ((!tev->uprobes) &&
2885 (kprobe_warn_out_range(tev->point.symbol,
2886 tev->point.address))) {
2887 err = -EACCES;
2888 goto errout;
2889 }
2890
2891 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2892 goto errout;
2893
2894 if (pev->target) {
2895 tp->module = strdup(pev->target);
2896 if (!tp->module)
2897 goto errout;
2898 }
2899
2900 if (tev->group) {
2901 tev->group = strdup(pev->group);
2902 if (!tev->group)
2903 goto errout;
2904 }
2905
2906 if (pev->event) {
2907 tev->event = strdup(pev->event);
2908 if (!tev->event)
2909 goto errout;
2910 }
2911
2912 tev->nargs = pev->nargs;
2913 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2914 if (!tev->args) {
2915 err = -ENOMEM;
2916 goto errout;
2917 }
2918 for (i = 0; i < tev->nargs; i++)
2919 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2920
2921 return 1;
2922
2923errout:
2924 if (*tevs) {
2925 clear_probe_trace_events(*tevs, 1);
2926 *tevs = NULL;
2927 }
2928 return err;
2929}
2930
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302931bool __weak arch__prefers_symtab(void) { return false; }
2932
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002933/* Concatinate two arrays */
2934static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2935{
2936 void *ret;
2937
2938 ret = malloc(sz_a + sz_b);
2939 if (ret) {
2940 memcpy(ret, a, sz_a);
2941 memcpy(ret + sz_a, b, sz_b);
2942 }
2943 return ret;
2944}
2945
2946static int
2947concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2948 struct probe_trace_event **tevs2, int ntevs2)
2949{
2950 struct probe_trace_event *new_tevs;
2951 int ret = 0;
2952
2953 if (ntevs == 0) {
2954 *tevs = *tevs2;
2955 *ntevs = ntevs2;
2956 *tevs2 = NULL;
2957 return 0;
2958 }
2959
2960 if (*ntevs + ntevs2 > probe_conf.max_probes)
2961 ret = -E2BIG;
2962 else {
2963 /* Concatinate the array of probe_trace_event */
2964 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
2965 *tevs2, ntevs2 * sizeof(**tevs2));
2966 if (!new_tevs)
2967 ret = -ENOMEM;
2968 else {
2969 free(*tevs);
2970 *tevs = new_tevs;
2971 *ntevs += ntevs2;
2972 }
2973 }
2974 if (ret < 0)
2975 clear_probe_trace_events(*tevs2, ntevs2);
2976 zfree(tevs2);
2977
2978 return ret;
2979}
2980
2981/*
2982 * Try to find probe_trace_event from given probe caches. Return the number
2983 * of cached events found, if an error occurs return the error.
2984 */
2985static int find_cached_events(struct perf_probe_event *pev,
2986 struct probe_trace_event **tevs,
2987 const char *target)
2988{
2989 struct probe_cache *cache;
2990 struct probe_cache_entry *entry;
2991 struct probe_trace_event *tmp_tevs = NULL;
2992 int ntevs = 0;
2993 int ret = 0;
2994
2995 cache = probe_cache__new(target);
2996 /* Return 0 ("not found") if the target has no probe cache. */
2997 if (!cache)
2998 return 0;
2999
3000 for_each_probe_cache_entry(entry, cache) {
3001 /* Skip the cache entry which has no name */
3002 if (!entry->pev.event || !entry->pev.group)
3003 continue;
3004 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3005 strglobmatch(entry->pev.event, pev->event)) {
3006 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3007 if (ret > 0)
3008 ret = concat_probe_trace_events(tevs, &ntevs,
3009 &tmp_tevs, ret);
3010 if (ret < 0)
3011 break;
3012 }
3013 }
3014 probe_cache__delete(cache);
3015 if (ret < 0) {
3016 clear_probe_trace_events(*tevs, ntevs);
3017 zfree(tevs);
3018 } else {
3019 ret = ntevs;
3020 if (ntevs > 0 && target && target[0] == '/')
3021 pev->uprobes = true;
3022 }
3023
3024 return ret;
3025}
3026
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003027/* Try to find probe_trace_event from all probe caches */
3028static int find_cached_events_all(struct perf_probe_event *pev,
3029 struct probe_trace_event **tevs)
3030{
3031 struct probe_trace_event *tmp_tevs = NULL;
3032 struct strlist *bidlist;
3033 struct str_node *nd;
3034 char *pathname;
3035 int ntevs = 0;
3036 int ret;
3037
3038 /* Get the buildid list of all valid caches */
3039 bidlist = build_id_cache__list_all(true);
3040 if (!bidlist) {
3041 ret = -errno;
3042 pr_debug("Failed to get buildids: %d\n", ret);
3043 return ret;
3044 }
3045
3046 ret = 0;
3047 strlist__for_each_entry(nd, bidlist) {
3048 pathname = build_id_cache__origname(nd->s);
3049 ret = find_cached_events(pev, &tmp_tevs, pathname);
3050 /* In the case of cnt == 0, we just skip it */
3051 if (ret > 0)
3052 ret = concat_probe_trace_events(tevs, &ntevs,
3053 &tmp_tevs, ret);
3054 free(pathname);
3055 if (ret < 0)
3056 break;
3057 }
3058 strlist__delete(bidlist);
3059
3060 if (ret < 0) {
3061 clear_probe_trace_events(*tevs, ntevs);
3062 zfree(tevs);
3063 } else
3064 ret = ntevs;
3065
3066 return ret;
3067}
3068
Masami Hiramatsubc062232016-07-01 17:03:12 +09003069static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3070 struct probe_trace_event **tevs)
3071{
3072 struct probe_cache *cache;
3073 struct probe_cache_entry *entry;
3074 struct probe_trace_event *tev;
3075 struct str_node *node;
3076 int ret, i;
3077
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003078 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003079 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003080 if (!pev->target)
3081 return find_cached_events_all(pev, tevs);
3082 else
3083 return find_cached_events(pev, tevs, pev->target);
3084 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003085 cache = probe_cache__new(pev->target);
3086 if (!cache)
3087 return 0;
3088
3089 entry = probe_cache__find(cache, pev);
3090 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003091 /* SDT must be in the cache */
3092 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003093 goto out;
3094 }
3095
3096 ret = strlist__nr_entries(entry->tevlist);
3097 if (ret > probe_conf.max_probes) {
3098 pr_debug("Too many entries matched in the cache of %s\n",
3099 pev->target ? : "kernel");
3100 ret = -E2BIG;
3101 goto out;
3102 }
3103
3104 *tevs = zalloc(ret * sizeof(*tev));
3105 if (!*tevs) {
3106 ret = -ENOMEM;
3107 goto out;
3108 }
3109
3110 i = 0;
3111 strlist__for_each_entry(node, entry->tevlist) {
3112 tev = &(*tevs)[i++];
3113 ret = parse_probe_trace_command(node->s, tev);
3114 if (ret < 0)
3115 goto out;
3116 /* Set the uprobes attribute as same as original */
3117 tev->uprobes = pev->uprobes;
3118 }
3119 ret = i;
3120
3121out:
3122 probe_cache__delete(cache);
3123 return ret;
3124}
3125
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303126static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003127 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003128{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003129 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003130
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003131 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003132 /* Set group name if not given */
3133 if (!pev->uprobes) {
3134 pev->group = strdup(PERFPROBE_GROUP);
3135 ret = pev->group ? 0 : -ENOMEM;
3136 } else
3137 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003138 if (ret != 0) {
3139 pr_warning("Failed to make a group name.\n");
3140 return ret;
3141 }
3142 }
3143
Wang Nanda15bd92015-08-26 10:57:45 +00003144 ret = try_to_find_absolute_address(pev, tevs);
3145 if (ret > 0)
3146 return ret;
3147
Masami Hiramatsubc062232016-07-01 17:03:12 +09003148 /* At first, we need to lookup cache entry */
3149 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003150 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3151 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003152
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05303153 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003154 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05303155 if (ret > 0)
3156 return ret; /* Found in symbol table */
3157 }
3158
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003159 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003160 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003161 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003162 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003163
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003164 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003165}
3166
Wang Nan12fae5e2015-09-04 21:16:00 +09003167int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003168{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003169 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003170
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003171 /* Loop 1: convert all events */
3172 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003173 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003174 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003175 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003176 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003177 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003178 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003179 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003180 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003181 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003182 /* This just release blacklist only if allocated */
3183 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003184
Namhyung Kim844dffa2015-09-04 21:15:59 +09003185 return 0;
3186}
3187
Wang Nan12fae5e2015-09-04 21:16:00 +09003188int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003189{
3190 int i, ret = 0;
3191
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003192 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003193 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003194 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3195 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003196 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003197 if (ret < 0)
3198 break;
3199 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003200 return ret;
3201}
3202
Wang Nan12fae5e2015-09-04 21:16:00 +09003203void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003204{
3205 int i, j;
3206
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003207 /* Loop 3: cleanup and free trace events */
3208 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003209 for (j = 0; j < pevs[i].ntevs; j++)
3210 clear_probe_trace_event(&pevs[i].tevs[j]);
3211 zfree(&pevs[i].tevs);
3212 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003213 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003214 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003215}
3216
3217int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3218{
3219 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003220
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003221 ret = init_probe_symbol_maps(pevs->uprobes);
3222 if (ret < 0)
3223 return ret;
3224
Wang Nan12fae5e2015-09-04 21:16:00 +09003225 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003226 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003227 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003228
Wang Nan12fae5e2015-09-04 21:16:00 +09003229 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003230
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003231 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003232 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003233}
3234
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003235int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003236{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003237 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003238 char *str = strfilter__string(filter);
3239
3240 if (!str)
3241 return -EINVAL;
3242
Masami Hiramatsufa282442009-12-08 17:03:23 -05003243 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003244 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3245 if (ret < 0)
3246 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303247
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003248 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003249 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303250 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003251
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003252 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003253 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003254 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003255 goto error;
3256 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003257 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303258
3259error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003260 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303261 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003262 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303263 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003264out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003265 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003266
3267 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003268}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303269
Masami Hiramatsu3c422582011-01-20 23:15:45 +09003270/* TODO: don't use a global variable for filter ... */
3271static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003272
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003273/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09003274 * If a symbol corresponds to a function with global binding and
3275 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003276 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003277static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09003278 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003279{
Namhyung Kime578da32015-03-06 16:31:29 +09003280 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09003281 return 0;
3282 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003283}
3284
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003285int show_available_funcs(const char *target, struct strfilter *_filter,
3286 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003287{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003288 struct map *map;
3289 int ret;
3290
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003291 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003292 if (ret < 0)
3293 return ret;
3294
3295 /* Get a symbol map */
3296 if (user)
3297 map = dso__new_map(target);
3298 else
3299 map = kernel_get_module_map(target);
3300 if (!map) {
3301 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003302 return -EINVAL;
3303 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003304
3305 /* Load symbols with given filter */
3306 available_func_filter = _filter;
3307 if (map__load(map, filter_available_functions)) {
3308 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
3309 goto end;
3310 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003311 if (!dso__sorted_by_name(map->dso, map->type))
3312 dso__sort_by_name(map->dso, map->type);
3313
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003314 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303315 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003316 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
3317end:
3318 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003319 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003320 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003321 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303322
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003323 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303324}
3325
Wang Nanda15bd92015-08-26 10:57:45 +00003326int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3327 struct perf_probe_arg *pvar)
3328{
3329 tvar->value = strdup(pvar->var);
3330 if (tvar->value == NULL)
3331 return -ENOMEM;
3332 if (pvar->type) {
3333 tvar->type = strdup(pvar->type);
3334 if (tvar->type == NULL)
3335 return -ENOMEM;
3336 }
3337 if (pvar->name) {
3338 tvar->name = strdup(pvar->name);
3339 if (tvar->name == NULL)
3340 return -ENOMEM;
3341 } else
3342 tvar->name = NULL;
3343 return 0;
3344}