blob: 8f810961ec78b3448eb81573e9a3bb374762d48d [file] [log] [blame]
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001/*
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302 * probe-event.c : perf-probe definition to probe_events format converter
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05003 *
4 * Written by Masami Hiramatsu <mhiramat@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050022#include <sys/utsname.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <string.h>
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050031#include <stdarg.h>
32#include <limits.h>
Masami Hiramatsue80711c2011-01-13 21:46:11 +090033#include <elf.h>
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050034
Masami Hiramatsu31facc52010-03-16 18:05:30 -040035#include "util.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050036#include "event.h"
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050037#include "strlist.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050038#include "debug.h"
Masami Hiramatsu72041332010-01-05 17:47:10 -050039#include "cache.h"
Masami Hiramatsu631c9de2010-01-06 09:45:34 -050040#include "color.h"
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040041#include "symbol.h"
42#include "thread.h"
Jiri Olsa4605eab2015-09-02 09:56:43 +020043#include <api/fs/fs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030044#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050045#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040046#include "probe-finder.h"
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090047#include "probe-file.h"
Srikar Dronamraju225466f2012-04-16 17:39:09 +053048#include "session.h"
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050049
50#define MAX_CMDLEN 256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050051#define PERFPROBE_GROUP "probe"
52
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040053bool probe_event_dry_run; /* Dry run flag */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +090054struct probe_conf probe_conf;
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -040055
Masami Hiramatsu146a1432010-04-12 13:17:42 -040056#define semantic_error(msg ...) pr_err("Semantic error :" msg)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050057
Masami Hiramatsu92f6c722015-07-15 18:14:07 +090058int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050059{
60 int ret;
61 va_list ap;
62 va_start(ap, format);
63 ret = vsnprintf(str, size, format, ap);
64 va_end(ap);
65 if (ret >= (int)size)
66 ret = -E2BIG;
67 return ret;
68}
69
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000070static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040071
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090072/* Initialize symbol maps and path of vmlinux/modules */
Namhyung Kim9bae1e82015-09-10 11:27:05 +090073int init_probe_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040074{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040075 int ret;
76
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090078 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090079 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040080 if (ret < 0) {
81 pr_debug("Failed to init symbol map.\n");
82 goto out;
83 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040084
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000085 if (host_machine || user_only) /* already initialized */
86 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030087
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000088 if (symbol_conf.vmlinux_name)
89 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
90
91 host_machine = machine__new_host();
92 if (!host_machine) {
93 pr_debug("machine__new_host() failed.\n");
94 symbol__exit();
95 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090096 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -040097out:
98 if (ret < 0)
99 pr_warning("Failed to init vmlinux path.\n");
100 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400101}
102
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900103void exit_probe_symbol_maps(void)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000104{
Arnaldo Carvalho de Melo32ca6782016-06-22 10:19:11 -0300105 machine__delete(host_machine);
106 host_machine = NULL;
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000107 symbol__exit();
108}
109
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900110static struct symbol *__find_kernel_function_by_name(const char *name,
111 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400112{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300113 return machine__find_kernel_function_by_name(host_machine, name, mapp);
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900114}
115
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000116static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
117{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300118 return machine__find_kernel_function(host_machine, addr, mapp);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000119}
120
121static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
122{
123 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
124 struct kmap *kmap;
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300125 struct map *map = machine__kernel_map(host_machine);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000126
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300127 if (map__load(map) < 0)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000128 return NULL;
129
Arnaldo Carvalho de Melo77e65972015-09-30 11:08:58 -0300130 kmap = map__kmap(map);
Wang Nanba927322015-04-07 08:22:45 +0000131 if (!kmap)
132 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000133 return kmap->ref_reloc_sym;
134}
135
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900136static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
137 bool reloc, bool reladdr)
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000138{
139 struct ref_reloc_sym *reloc_sym;
140 struct symbol *sym;
141 struct map *map;
142
143 /* ref_reloc_sym is just a label. Need a special fix*/
144 reloc_sym = kernel_get_ref_reloc_sym();
145 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900146 *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000147 else {
148 sym = __find_kernel_function_by_name(name, &map);
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900149 if (!sym)
150 return -ENOENT;
151 *addr = map->unmap_ip(map, sym->start) -
152 ((reloc) ? 0 : map->reloc) -
153 ((reladdr) ? map->start : 0);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000154 }
155 return 0;
156}
157
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900158static struct map *kernel_get_module_map(const char *module)
159{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000160 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300161 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300162 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900164 /* A file path -- this is an offline module */
165 if (module && strchr(module, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300166 return machine__findnew_module_map(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900167
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168 if (!module)
169 module = "kernel";
170
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300171 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300172 /* short_name is "[module]" */
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900173 if (strncmp(pos->dso->short_name + 1, module,
Konstantin Khlebnikovcb3f3372016-08-05 15:22:36 +0300174 pos->dso->short_name_len - 2) == 0 &&
175 module[pos->dso->short_name_len - 2] == '\0') {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900176 return pos;
177 }
178 }
179 return NULL;
180}
181
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500182struct map *get_target_map(const char *target, bool user)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900183{
184 /* Init maps of given executable or kernel */
185 if (user)
186 return dso__new_map(target);
187 else
188 return kernel_get_module_map(target);
189}
190
191static void put_target_map(struct map *map, bool user)
192{
193 if (map && user) {
194 /* Only the user map needs to be released */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300195 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900196 }
197}
198
199
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000200static int convert_exec_to_group(const char *exec, char **result)
201{
202 char *ptr1, *ptr2, *exec_copy;
203 char buf[64];
204 int ret;
205
206 exec_copy = strdup(exec);
207 if (!exec_copy)
208 return -ENOMEM;
209
210 ptr1 = basename(exec_copy);
211 if (!ptr1) {
212 ret = -EINVAL;
213 goto out;
214 }
215
Colin Ian Kingead1a572016-10-03 11:34:31 +0100216 for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
Masami Hiramatsu35726d32016-09-24 00:35:16 +0900217 if (!isalnum(*ptr2) && *ptr2 != '_') {
218 *ptr2 = '\0';
219 break;
220 }
221 }
222
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000223 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
224 if (ret < 0)
225 goto out;
226
227 *result = strdup(buf);
228 ret = *result ? 0 : -ENOMEM;
229
230out:
231 free(exec_copy);
232 return ret;
233}
234
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900235static void clear_perf_probe_point(struct perf_probe_point *pp)
236{
237 free(pp->file);
238 free(pp->function);
239 free(pp->lazy_line);
240}
241
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000242static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
243{
244 int i;
245
246 for (i = 0; i < ntevs; i++)
247 clear_probe_trace_event(tevs + i);
248}
249
Masami Hiramatsub0312202015-06-16 20:50:55 +0900250static bool kprobe_blacklist__listed(unsigned long address);
251static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
252{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900253 u64 etext_addr = 0;
254 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000255
Masami Hiramatsub0312202015-06-16 20:50:55 +0900256 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900257 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
258 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000259
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900260 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900261 pr_warning("%s is out of .text, skip it.\n", symbol);
262 else if (kprobe_blacklist__listed(address))
263 pr_warning("%s is blacklisted function, skip it.\n", symbol);
264 else
265 return false;
266
267 return true;
268}
269
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530270/*
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530271 * @module can be module name of module file path. In case of path,
272 * inspect elf and find out what is actual module name.
273 * Caller has to free mod_name after using it.
274 */
275static char *find_module_name(const char *module)
276{
277 int fd;
278 Elf *elf;
279 GElf_Ehdr ehdr;
280 GElf_Shdr shdr;
281 Elf_Data *data;
282 Elf_Scn *sec;
283 char *mod_name = NULL;
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900284 int name_offset;
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530285
286 fd = open(module, O_RDONLY);
287 if (fd < 0)
288 return NULL;
289
290 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
291 if (elf == NULL)
292 goto elf_err;
293
294 if (gelf_getehdr(elf, &ehdr) == NULL)
295 goto ret_err;
296
297 sec = elf_section_by_name(elf, &ehdr, &shdr,
298 ".gnu.linkonce.this_module", NULL);
299 if (!sec)
300 goto ret_err;
301
302 data = elf_getdata(sec, NULL);
303 if (!data || !data->d_buf)
304 goto ret_err;
305
Masami Hiramatsu1f2ed152017-01-03 00:20:49 +0900306 /*
307 * NOTE:
308 * '.gnu.linkonce.this_module' section of kernel module elf directly
309 * maps to 'struct module' from linux/module.h. This section contains
310 * actual module name which will be used by kernel after loading it.
311 * But, we cannot use 'struct module' here since linux/module.h is not
312 * exposed to user-space. Offset of 'name' has remained same from long
313 * time, so hardcoding it here.
314 */
315 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
316 name_offset = 12;
317 else /* expect ELFCLASS64 by default */
318 name_offset = 24;
319
320 mod_name = strdup((char *)data->d_buf + name_offset);
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530321
322ret_err:
323 elf_end(elf);
324elf_err:
325 close(fd);
326 return mod_name;
327}
328
Ingo Molnar89fe8082013-09-30 12:07:11 +0200329#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000330
331static int kernel_get_module_dso(const char *module, struct dso **pdso)
332{
333 struct dso *dso;
334 struct map *map;
335 const char *vmlinux_name;
336 int ret = 0;
337
338 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300339 char module_name[128];
340
341 snprintf(module_name, sizeof(module_name), "[%s]", module);
342 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
343 if (map) {
344 dso = map->dso;
345 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000346 }
347 pr_debug("Failed to find module %s.\n", module);
348 return -ENOENT;
349 }
350
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300351 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000352 dso = map->dso;
353
354 vmlinux_name = symbol_conf.vmlinux_name;
355 dso->load_errno = 0;
356 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300357 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000358 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300359 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000360found:
361 *pdso = dso;
362 return ret;
363}
364
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900365/*
366 * Some binaries like glibc have special symbols which are on the symbol
367 * table, but not in the debuginfo. If we can find the address of the
368 * symbol from map, we can translate the address back to the probe point.
369 */
370static int find_alternative_probe_point(struct debuginfo *dinfo,
371 struct perf_probe_point *pp,
372 struct perf_probe_point *result,
373 const char *target, bool uprobes)
374{
375 struct map *map = NULL;
376 struct symbol *sym;
377 u64 address = 0;
378 int ret = -ENOENT;
379
380 /* This can work only for function-name based one */
381 if (!pp->function || pp->file)
382 return -ENOTSUP;
383
384 map = get_target_map(target, uprobes);
385 if (!map)
386 return -EINVAL;
387
388 /* Find the address of given function */
389 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900390 if (uprobes)
391 address = sym->start;
392 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900393 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900394 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900395 }
396 if (!address) {
397 ret = -ENOENT;
398 goto out;
399 }
Wang Nanf6c15622015-04-08 02:14:34 +0000400 pr_debug("Symbol %s address found : %" PRIx64 "\n",
401 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900402
403 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
404 result);
405 if (ret <= 0)
406 ret = (!ret) ? -ENOENT : ret;
407 else {
408 result->offset += pp->offset;
409 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800410 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900411 ret = 0;
412 }
413
414out:
415 put_target_map(map, uprobes);
416 return ret;
417
418}
419
420static int get_alternative_probe_event(struct debuginfo *dinfo,
421 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900422 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900423{
424 int ret;
425
426 memcpy(tmp, &pev->point, sizeof(*tmp));
427 memset(&pev->point, 0, sizeof(pev->point));
428 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900429 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900430 if (ret < 0)
431 memcpy(&pev->point, tmp, sizeof(*tmp));
432
433 return ret;
434}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000435
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900436static int get_alternative_line_range(struct debuginfo *dinfo,
437 struct line_range *lr,
438 const char *target, bool user)
439{
David Ahern6d4a4892015-03-11 10:36:20 -0400440 struct perf_probe_point pp = { .function = lr->function,
441 .file = lr->file,
442 .line = lr->start };
443 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900444 int ret, len = 0;
445
David Ahern6d4a4892015-03-11 10:36:20 -0400446 memset(&result, 0, sizeof(result));
447
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900448 if (lr->end != INT_MAX)
449 len = lr->end - lr->start;
450 ret = find_alternative_probe_point(dinfo, &pp, &result,
451 target, user);
452 if (!ret) {
453 lr->function = result.function;
454 lr->file = result.file;
455 lr->start = result.line;
456 if (lr->end != INT_MAX)
457 lr->end = lr->start + len;
458 clear_perf_probe_point(&pp);
459 }
460 return ret;
461}
462
Masami Hiramatsuff741782011-06-27 16:27:39 +0900463/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000464static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900465{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000466 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900467 char reason[STRERR_BUFSIZE];
468 struct debuginfo *ret = NULL;
469 struct dso *dso = NULL;
470 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900471
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000472 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900473 err = kernel_get_module_dso(module, &dso);
474 if (err < 0) {
475 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300476 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900477 strcpy(reason, "(unknown)");
478 } else
479 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000480 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900481 pr_err("Failed to find the path for %s: %s\n",
482 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900483 return NULL;
484 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900485 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900486 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000487 ret = debuginfo__new(path);
488 if (!ret && !silent) {
489 pr_warning("The %s file has no debug information.\n", path);
490 if (!module || !strtailcmp(path, ".ko"))
491 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
492 else
493 pr_warning("Rebuild with -g, ");
494 pr_warning("or install an appropriate debuginfo package.\n");
495 }
496 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400497}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300498
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900499/* For caching the last debuginfo */
500static struct debuginfo *debuginfo_cache;
501static char *debuginfo_cache_path;
502
503static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
504{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900505 const char *path = module;
506
507 /* If the module is NULL, it should be the kernel. */
508 if (!module)
509 path = "kernel";
510
511 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900512 goto out;
513
514 /* Copy module path */
515 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900516 debuginfo_cache_path = strdup(path);
517 if (!debuginfo_cache_path) {
518 debuginfo__delete(debuginfo_cache);
519 debuginfo_cache = NULL;
520 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900521 }
522
523 debuginfo_cache = open_debuginfo(module, silent);
524 if (!debuginfo_cache)
525 zfree(&debuginfo_cache_path);
526out:
527 return debuginfo_cache;
528}
529
530static void debuginfo_cache__exit(void)
531{
532 debuginfo__delete(debuginfo_cache);
533 debuginfo_cache = NULL;
534 zfree(&debuginfo_cache_path);
535}
536
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000537
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000538static int get_text_start_address(const char *exec, unsigned long *address)
539{
540 Elf *elf;
541 GElf_Ehdr ehdr;
542 GElf_Shdr shdr;
543 int fd, ret = -ENOENT;
544
545 fd = open(exec, O_RDONLY);
546 if (fd < 0)
547 return -errno;
548
549 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900550 if (elf == NULL) {
551 ret = -EINVAL;
552 goto out_close;
553 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000554
555 if (gelf_getehdr(elf, &ehdr) == NULL)
556 goto out;
557
558 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
559 goto out;
560
561 *address = shdr.sh_addr - shdr.sh_offset;
562 ret = 0;
563out:
564 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900565out_close:
566 close(fd);
567
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000568 return ret;
569}
570
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000571/*
572 * Convert trace point to probe point with debuginfo
573 */
574static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
575 struct perf_probe_point *pp,
576 bool is_kprobe)
577{
578 struct debuginfo *dinfo = NULL;
579 unsigned long stext = 0;
580 u64 addr = tp->address;
581 int ret = -ENOENT;
582
583 /* convert the address to dwarf address */
584 if (!is_kprobe) {
585 if (!addr) {
586 ret = -EINVAL;
587 goto error;
588 }
589 ret = get_text_start_address(tp->module, &stext);
590 if (ret < 0)
591 goto error;
592 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000593 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900594 /* If the module is given, this returns relative address */
595 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
596 false, !!tp->module);
597 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000598 goto error;
599 addr += tp->offset;
600 }
601
602 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
603 tp->module ? : "kernel");
604
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900605 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
606 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000607 ret = debuginfo__find_probe_point(dinfo,
608 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900609 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000610 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000611
612 if (ret > 0) {
613 pp->retprobe = tp->retprobe;
614 return 0;
615 }
616error:
617 pr_debug("Failed to find corresponding probes from debuginfo.\n");
618 return ret ? : -ENOENT;
619}
620
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000621static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
622 int ntevs, const char *exec)
623{
624 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000625 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000626
627 if (!exec)
628 return 0;
629
630 ret = get_text_start_address(exec, &stext);
631 if (ret < 0)
632 return ret;
633
634 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000635 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000636 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000637 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000638 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000639 ret = -ENOMEM;
640 break;
641 }
642 tevs[i].uprobes = true;
643 }
644
645 return ret;
646}
647
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900648static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
649 int ntevs, const char *module)
650{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900651 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530652 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900653
654 if (!module)
655 return 0;
656
Ravi Bangoria63a29612016-04-26 19:55:41 +0530657 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900658
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900659 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530660 tevs[i].point.module =
661 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900662 if (!tevs[i].point.module) {
663 ret = -ENOMEM;
664 break;
665 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900666 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900667
Ravi Bangoria63a29612016-04-26 19:55:41 +0530668 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900669 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900670}
671
Ravi Bangoriad8204562016-08-09 01:23:24 -0500672static int
673post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
674 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000675{
676 struct ref_reloc_sym *reloc_sym;
677 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900678 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000679
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900680 /* Skip post process if the target is an offline kernel */
681 if (symbol_conf.ignore_vmlinux_buildid)
682 return 0;
683
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000684 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000685 if (!reloc_sym) {
686 pr_warning("Relocated base symbol is not found!\n");
687 return -EINVAL;
688 }
689
690 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900691 if (!tevs[i].point.address || tevs[i].point.retprobe)
692 continue;
693 /* If we found a wrong one, mark it by NULL symbol */
694 if (kprobe_warn_out_range(tevs[i].point.symbol,
695 tevs[i].point.address)) {
696 tmp = NULL;
697 skipped++;
698 } else {
699 tmp = strdup(reloc_sym->name);
700 if (!tmp)
701 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000702 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900703 /* If we have no realname, use symbol for it */
704 if (!tevs[i].point.realname)
705 tevs[i].point.realname = tevs[i].point.symbol;
706 else
707 free(tevs[i].point.symbol);
708 tevs[i].point.symbol = tmp;
709 tevs[i].point.offset = tevs[i].point.address -
710 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000711 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900712 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000713}
714
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500715void __weak
716arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
717 int ntevs __maybe_unused)
718{
719}
720
Ravi Bangoriad8204562016-08-09 01:23:24 -0500721/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500722static int post_process_probe_trace_events(struct perf_probe_event *pev,
723 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500724 int ntevs, const char *module,
725 bool uprobe)
726{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500727 int ret;
728
Ravi Bangoriad8204562016-08-09 01:23:24 -0500729 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500730 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
731 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500732 /* Currently ref_reloc_sym based probe is not for drivers */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500733 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
734 else
735 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500736
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500737 if (ret >= 0)
738 arch__post_process_probe_trace_events(pev, ntevs);
739
740 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500741}
742
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300743/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530744static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900745 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300746{
747 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900748 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530749 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900750 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300751
Masami Hiramatsu44225522015-05-08 10:03:28 +0900752 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900753 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000754 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900755 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900756 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300757 return 0;
758 }
759
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000760 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900761 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900762 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900763
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900764 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900765 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900766 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900767 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900768 /*
769 * Write back to the original probe_event for
770 * setting appropriate (user given) event name
771 */
772 clear_perf_probe_point(&pev->point);
773 memcpy(&pev->point, &tmp, sizeof(tmp));
774 }
775 }
776
Masami Hiramatsuff741782011-06-27 16:27:39 +0900777 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300778
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400779 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000780 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500781 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900782 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900783 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000784 clear_probe_trace_events(*tevs, ntevs);
785 zfree(tevs);
786 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900787 if (ret != ntevs)
788 return ret < 0 ? ret : ntevs;
789 ntevs = 0;
790 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400791 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300792
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400793 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900794 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400795 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900796 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400797 }
798 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400799 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000800 if (ntevs < 0) {
801 if (ntevs == -EBADF)
802 pr_warning("Warning: No dwarf info found in the vmlinux - "
803 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400804 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900805 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400806 return 0;
807 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300808 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400809 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810}
811
812#define LINEBUF_SIZE 256
813#define NR_ADDITIONAL_LINES 2
814
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100815static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000817 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100818 const char *color = show_num ? "" : PERF_COLOR_BLUE;
819 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300820
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100821 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
823 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100824 if (skip)
825 continue;
826 if (!prefix) {
827 prefix = show_num ? "%7d " : " ";
828 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300829 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100830 color_fprintf(stdout, color, "%s", buf);
831
832 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400833
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100834 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300835error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100836 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000837 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300838 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100839 return -1;
840 }
841 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300842}
843
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100844static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
845{
846 int rv = __show_one_line(fp, l, skip, show_num);
847 if (rv == 0) {
848 pr_warning("Source file is shorter than expected.\n");
849 rv = -1;
850 }
851 return rv;
852}
853
854#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
855#define show_one_line(f,l) _show_one_line(f,l,false,false)
856#define skip_one_line(f,l) _show_one_line(f,l,true,false)
857#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
858
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300859/*
860 * Show line-range always requires debuginfo to find source file and
861 * line number.
862 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900863static int __show_line_range(struct line_range *lr, const char *module,
864 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300865{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400866 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000867 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900868 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300869 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900870 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900871 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000872 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300873
874 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000875 dinfo = open_debuginfo(module, false);
876 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900877 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400878
Masami Hiramatsuff741782011-06-27 16:27:39 +0900879 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900880 if (!ret) { /* Not found, retry with an alternative */
881 ret = get_alternative_line_range(dinfo, lr, module, user);
882 if (!ret)
883 ret = debuginfo__find_line_range(dinfo, lr);
884 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900885 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000886 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400887 pr_warning("Specified source line is not found.\n");
888 return -ENOENT;
889 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000890 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400891 return ret;
892 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300893
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900894 /* Convert source file path */
895 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900896 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800897
898 /* Free old path when new path is assigned */
899 if (tmp != lr->path)
900 free(tmp);
901
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900902 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000903 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900904 return ret;
905 }
906
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300907 setup_pager();
908
909 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900910 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300911 lr->start - lr->offset);
912 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100913 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300914
915 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400916 if (fp == NULL) {
917 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300918 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400919 return -errno;
920 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300921 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100922 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100923 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100924 if (ret < 0)
925 goto end;
926 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300927
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300928 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000929 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100930 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100931 if (ret < 0)
932 goto end;
933 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100934 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400935 if (ret < 0)
936 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300937 }
938
939 if (lr->end == INT_MAX)
940 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100941 while (l <= lr->end) {
942 ret = show_one_line_or_eof(fp, l++ - lr->offset);
943 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100944 break;
945 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400946end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300947 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400948 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300949}
950
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000951int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000952{
953 int ret;
954
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900955 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000956 if (ret < 0)
957 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900958 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900959 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000960
961 return ret;
962}
963
Masami Hiramatsuff741782011-06-27 16:27:39 +0900964static int show_available_vars_at(struct debuginfo *dinfo,
965 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900966 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900967{
968 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900969 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900970 struct str_node *node;
971 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900972 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900973 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900974
975 buf = synthesize_perf_probe_point(&pev->point);
976 if (!buf)
977 return -EINVAL;
978 pr_debug("Searching variables at %s\n", buf);
979
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900980 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900981 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900982 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900983 if (!ret) {
984 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900985 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900986 /* Release the old probe_point */
987 clear_perf_probe_point(&tmp);
988 }
989 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900990 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000991 if (ret == 0 || ret == -ENOENT) {
992 pr_err("Failed to find the address of %s\n", buf);
993 ret = -ENOENT;
994 } else
995 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900996 goto end;
997 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000998
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900999 /* Some variables are found */
1000 fprintf(stdout, "Available variables at %s\n", buf);
1001 for (i = 0; i < ret; i++) {
1002 vl = &vls[i];
1003 /*
1004 * A probe point might be converted to
1005 * several trace points.
1006 */
1007 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1008 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001009 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001010 nvars = 0;
1011 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001012 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001013 var = strchr(node->s, '\t') + 1;
1014 if (strfilter__compare(_filter, var)) {
1015 fprintf(stdout, "\t\t%s\n", node->s);
1016 nvars++;
1017 }
1018 }
1019 strlist__delete(vl->vars);
1020 }
1021 if (nvars == 0)
1022 fprintf(stdout, "\t\t(No matched variables)\n");
1023 }
1024 free(vls);
1025end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001026 free(buf);
1027 return ret;
1028}
1029
1030/* Show available variables on given probe point */
1031int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001032 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001033{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001034 int i, ret = 0;
1035 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001036
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001037 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001038 if (ret < 0)
1039 return ret;
1040
Masami Hiramatsu44225522015-05-08 10:03:28 +09001041 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001042 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001043 ret = -ENOENT;
1044 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001045 }
1046
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001047 setup_pager();
1048
Masami Hiramatsuff741782011-06-27 16:27:39 +09001049 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001050 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001051
1052 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001053out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001054 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001055 return ret;
1056}
1057
Ingo Molnar89fe8082013-09-30 12:07:11 +02001058#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001059
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001060static void debuginfo_cache__exit(void)
1061{
1062}
1063
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001064static int
1065find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1066 struct perf_probe_point *pp __maybe_unused,
1067 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001068{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001069 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001070}
1071
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301072static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001073 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001074{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001075 if (perf_probe_event_need_dwarf(pev)) {
1076 pr_warning("Debuginfo-analysis is not supported.\n");
1077 return -ENOSYS;
1078 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301079
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001080 return 0;
1081}
1082
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001083int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001084 const char *module __maybe_unused,
1085 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001086{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001087 pr_warning("Debuginfo-analysis is not supported.\n");
1088 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001089}
1090
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001091int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001092 int npevs __maybe_unused,
1093 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001094{
1095 pr_warning("Debuginfo-analysis is not supported.\n");
1096 return -ENOSYS;
1097}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001098#endif
1099
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001100void line_range__clear(struct line_range *lr)
1101{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001102 free(lr->function);
1103 free(lr->file);
1104 free(lr->path);
1105 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001106 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001107 memset(lr, 0, sizeof(*lr));
1108}
1109
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001110int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001111{
1112 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001113 lr->line_list = intlist__new(NULL);
1114 if (!lr->line_list)
1115 return -ENOMEM;
1116 else
1117 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001118}
1119
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001120static int parse_line_num(char **ptr, int *val, const char *what)
1121{
1122 const char *start = *ptr;
1123
1124 errno = 0;
1125 *val = strtol(*ptr, ptr, 0);
1126 if (errno || *ptr == start) {
1127 semantic_error("'%s' is not a valid number.\n", what);
1128 return -EINVAL;
1129 }
1130 return 0;
1131}
1132
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001133/* Check the name is good for event, group or function */
1134static bool is_c_func_name(const char *name)
1135{
1136 if (!isalpha(*name) && *name != '_')
1137 return false;
1138 while (*++name != '\0') {
1139 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1140 return false;
1141 }
1142 return true;
1143}
1144
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001145/*
1146 * Stuff 'lr' according to the line range described by 'arg'.
1147 * The line range syntax is described by:
1148 *
1149 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001150 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001151 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001152int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001153{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001154 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001155 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001156
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001157 if (!name)
1158 return -ENOMEM;
1159
1160 lr->start = 0;
1161 lr->end = INT_MAX;
1162
1163 range = strchr(name, ':');
1164 if (range) {
1165 *range++ = '\0';
1166
1167 err = parse_line_num(&range, &lr->start, "start line");
1168 if (err)
1169 goto err;
1170
1171 if (*range == '+' || *range == '-') {
1172 const char c = *range++;
1173
1174 err = parse_line_num(&range, &lr->end, "end line");
1175 if (err)
1176 goto err;
1177
1178 if (c == '+') {
1179 lr->end += lr->start;
1180 /*
1181 * Adjust the number of lines here.
1182 * If the number of lines == 1, the
1183 * the end of line should be equal to
1184 * the start of line.
1185 */
1186 lr->end--;
1187 }
1188 }
1189
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001190 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001191
1192 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001193 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001194 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001195 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001196 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001198 if (*range != '\0') {
1199 semantic_error("Tailing with invalid str '%s'.\n", range);
1200 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001202 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001203
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001204 file = strchr(name, '@');
1205 if (file) {
1206 *file = '\0';
1207 lr->file = strdup(++file);
1208 if (lr->file == NULL) {
1209 err = -ENOMEM;
1210 goto err;
1211 }
1212 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001213 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001214 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001215 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001216 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001217 else { /* Invalid name */
1218 semantic_error("'%s' is not a valid function name.\n", name);
1219 err = -EINVAL;
1220 goto err;
1221 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001222
1223 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001224err:
1225 free(name);
1226 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001227}
1228
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001229static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1230{
1231 char *ptr;
1232
1233 ptr = strchr(*arg, ':');
1234 if (ptr) {
1235 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001236 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001237 goto ng_name;
1238 pev->group = strdup(*arg);
1239 if (!pev->group)
1240 return -ENOMEM;
1241 *arg = ptr + 1;
1242 } else
1243 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001244 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001245ng_name:
1246 semantic_error("%s is bad for event name -it must "
1247 "follow C symbol-naming rule.\n", *arg);
1248 return -EINVAL;
1249 }
1250 pev->event = strdup(*arg);
1251 if (pev->event == NULL)
1252 return -ENOMEM;
1253
1254 return 0;
1255}
1256
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001259{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001260 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001261 char *ptr, *tmp;
1262 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301263 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001264 int ret;
1265
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001266 /*
1267 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001268 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1269 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001270 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001271 */
Wang Nane59d29e2015-04-28 08:46:09 +00001272 if (!arg)
1273 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001274
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001275 /*
1276 * If the probe point starts with '%',
1277 * or starts with "sdt_" and has a ':' but no '=',
1278 * then it should be a SDT/cached probe point.
1279 */
1280 if (arg[0] == '%' ||
1281 (!strncmp(arg, "sdt_", 4) &&
1282 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001283 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001284 if (arg[0] == '%')
1285 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001286 }
1287
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001288 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001289 if (pev->sdt) {
1290 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001291 if (*ptr != '@') {
1292 semantic_error("%s must be an SDT name.\n",
1293 arg);
1294 return -EINVAL;
1295 }
1296 /* This must be a target file name or build id */
1297 tmp = build_id_cache__complement(ptr + 1);
1298 if (tmp) {
1299 pev->target = build_id_cache__origname(tmp);
1300 free(tmp);
1301 } else
1302 pev->target = strdup(ptr + 1);
1303 if (!pev->target)
1304 return -ENOMEM;
1305 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001306 }
1307 ret = parse_perf_probe_event_name(&arg, pev);
1308 if (ret == 0) {
1309 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1310 ret = -errno;
1311 }
1312 return ret;
1313 }
1314
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001315 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001316 *ptr = '\0';
1317 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001318 ret = parse_perf_probe_event_name(&arg, pev);
1319 if (ret < 0)
1320 return ret;
1321
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001322 arg = tmp;
1323 }
1324
Naveen N. Rao3099c022015-04-28 17:35:34 +05301325 /*
1326 * Check arg is function or file name and copy it.
1327 *
1328 * We consider arg to be a file spec if and only if it satisfies
1329 * all of the below criteria::
1330 * - it does not include any of "+@%",
1331 * - it includes one of ":;", and
1332 * - it has a period '.' in the name.
1333 *
1334 * Otherwise, we consider arg to be a function specification.
1335 */
1336 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1337 /* This is a file spec if it includes a '.' before ; or : */
1338 if (memchr(arg, '.', ptr - arg))
1339 file_spec = true;
1340 }
1341
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001342 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001343 if (ptr) {
1344 nc = *ptr;
1345 *ptr++ = '\0';
1346 }
1347
Wang Nan6c6e0242015-08-26 10:57:44 +00001348 if (arg[0] == '\0')
1349 tmp = NULL;
1350 else {
1351 tmp = strdup(arg);
1352 if (tmp == NULL)
1353 return -ENOMEM;
1354 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001355
Naveen N. Rao3099c022015-04-28 17:35:34 +05301356 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001357 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001358 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001359 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001360
Wang Nanda15bd92015-08-26 10:57:45 +00001361 /*
1362 * Keep pp->function even if this is absolute address,
1363 * so it can mark whether abs_address is valid.
1364 * Which make 'perf probe lib.bin 0x0' possible.
1365 *
1366 * Note that checking length of tmp is not needed
1367 * because when we access tmp[1] we know tmp[0] is '0',
1368 * so tmp[1] should always valid (but could be '\0').
1369 */
1370 if (tmp && !strncmp(tmp, "0x", 2)) {
1371 pp->abs_address = strtoul(pp->function, &tmp, 0);
1372 if (*tmp != '\0') {
1373 semantic_error("Invalid absolute address.\n");
1374 return -EINVAL;
1375 }
1376 }
1377 }
1378
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001379 /* Parse other options */
1380 while (ptr) {
1381 arg = ptr;
1382 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001383 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001384 pp->lazy_line = strdup(arg);
1385 if (pp->lazy_line == NULL)
1386 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001387 break;
1388 }
1389 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001390 if (ptr) {
1391 nc = *ptr;
1392 *ptr++ = '\0';
1393 }
1394 switch (c) {
1395 case ':': /* Line number */
1396 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001397 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001398 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001399 " in line number.\n");
1400 return -EINVAL;
1401 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001402 break;
1403 case '+': /* Byte offset from a symbol */
1404 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001405 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001406 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001407 " in offset.\n");
1408 return -EINVAL;
1409 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001410 break;
1411 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412 if (pp->file) {
1413 semantic_error("SRC@SRC is not allowed.\n");
1414 return -EINVAL;
1415 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001416 pp->file = strdup(arg);
1417 if (pp->file == NULL)
1418 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001419 break;
1420 case '%': /* Probe places */
1421 if (strcmp(arg, "return") == 0) {
1422 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001423 } else { /* Others not supported yet */
1424 semantic_error("%%%s is not supported.\n", arg);
1425 return -ENOTSUP;
1426 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001427 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001428 default: /* Buggy case */
1429 pr_err("This program has a bug at %s:%d.\n",
1430 __FILE__, __LINE__);
1431 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001432 break;
1433 }
1434 }
1435
1436 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001437 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001438 semantic_error("Lazy pattern can't be used with"
1439 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001440 return -EINVAL;
1441 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001442
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001443 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001444 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001445 return -EINVAL;
1446 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001447
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001449 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450 return -EINVAL;
1451 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001452
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001453 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001454 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001455 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001456 return -EINVAL;
1457 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001458
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001459 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001460 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001461 return -EINVAL;
1462 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001463
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001464 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001465 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001466 return -EINVAL;
1467 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001468
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001469 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001470 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001471 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001472 return -EINVAL;
1473 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001474
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001475 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001476 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1477 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001479}
1480
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001481/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001483{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001484 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001485 struct perf_probe_arg_field **fieldp;
1486
1487 pr_debug("parsing arg: %s into ", str);
1488
Masami Hiramatsu48481932010-04-12 13:16:53 -04001489 tmp = strchr(str, '=');
1490 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001491 arg->name = strndup(str, tmp - str);
1492 if (arg->name == NULL)
1493 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001494 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001495 str = tmp + 1;
1496 }
1497
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001498 tmp = strchr(str, ':');
1499 if (tmp) { /* Type setting */
1500 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001501 arg->type = strdup(tmp + 1);
1502 if (arg->type == NULL)
1503 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001504 pr_debug("type:%s ", arg->type);
1505 }
1506
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001507 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001508 if (!is_c_varname(str) || !tmp) {
1509 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001510 arg->var = strdup(str);
1511 if (arg->var == NULL)
1512 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001513 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001514 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001515 }
1516
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001517 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001518 arg->var = strndup(str, tmp - str);
1519 if (arg->var == NULL)
1520 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001521 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001522 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001523 fieldp = &arg->field;
1524
1525 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001526 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1527 if (*fieldp == NULL)
1528 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001529 if (*tmp == '[') { /* Array */
1530 str = tmp;
1531 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001532 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001533 if (*tmp != ']' || tmp == str + 1) {
1534 semantic_error("Array index must be a"
1535 " number.\n");
1536 return -EINVAL;
1537 }
1538 tmp++;
1539 if (*tmp == '\0')
1540 tmp = NULL;
1541 } else { /* Structure */
1542 if (*tmp == '.') {
1543 str = tmp + 1;
1544 (*fieldp)->ref = false;
1545 } else if (tmp[1] == '>') {
1546 str = tmp + 2;
1547 (*fieldp)->ref = true;
1548 } else {
1549 semantic_error("Argument parse error: %s\n",
1550 str);
1551 return -EINVAL;
1552 }
1553 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001555 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001556 (*fieldp)->name = strndup(str, tmp - str);
1557 if ((*fieldp)->name == NULL)
1558 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001559 if (*str != '[')
1560 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001561 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1562 fieldp = &(*fieldp)->next;
1563 }
1564 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001565 (*fieldp)->name = strdup(str);
1566 if ((*fieldp)->name == NULL)
1567 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001568 if (*str != '[')
1569 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001570 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001571
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001572 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001573 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001574 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001575 if (arg->name == NULL)
1576 return -ENOMEM;
1577 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001578 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001579}
1580
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001581/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001582int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001583{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001584 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001585 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001586
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001587 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001588 if (!argv) {
1589 pr_debug("Failed to split arguments.\n");
1590 return -ENOMEM;
1591 }
1592 if (argc - 1 > MAX_PROBE_ARGS) {
1593 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1594 ret = -ERANGE;
1595 goto out;
1596 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001597 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001598 ret = parse_perf_probe_point(argv[0], pev);
1599 if (ret < 0)
1600 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001601
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001602 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001603 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001604 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1605 if (pev->args == NULL) {
1606 ret = -ENOMEM;
1607 goto out;
1608 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001609 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1610 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1611 if (ret >= 0 &&
1612 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001613 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001614 " kretprobe.\n");
1615 ret = -EINVAL;
1616 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001617 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001618out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001619 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001620
1621 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001622}
1623
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301624/* Returns true if *any* ARG is either C variable, $params or $vars. */
1625bool perf_probe_with_var(struct perf_probe_event *pev)
1626{
1627 int i = 0;
1628
1629 for (i = 0; i < pev->nargs; i++)
1630 if (is_c_varname(pev->args[i].var) ||
1631 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1632 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1633 return true;
1634 return false;
1635}
1636
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001637/* Return true if this perf_probe_event requires debuginfo */
1638bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001639{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001640 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1641 return true;
1642
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301643 if (perf_probe_with_var(pev))
1644 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001645
1646 return false;
1647}
1648
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301649/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001650int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001651{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301652 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001653 char pr;
1654 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001655 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001656 int ret, i, argc;
1657 char **argv;
1658
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301659 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001660 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001661 if (!argv) {
1662 pr_debug("Failed to split arguments.\n");
1663 return -ENOMEM;
1664 }
1665 if (argc < 2) {
1666 semantic_error("Too few probe arguments.\n");
1667 ret = -ERANGE;
1668 goto out;
1669 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001670
1671 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001672 argv0_str = strdup(argv[0]);
1673 if (argv0_str == NULL) {
1674 ret = -ENOMEM;
1675 goto out;
1676 }
1677 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1678 fmt2_str = strtok_r(NULL, "/", &fmt);
1679 fmt3_str = strtok_r(NULL, " \t", &fmt);
1680 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1681 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001682 semantic_error("Failed to parse event name: %s\n", argv[0]);
1683 ret = -EINVAL;
1684 goto out;
1685 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001686 pr = fmt1_str[0];
1687 tev->group = strdup(fmt2_str);
1688 tev->event = strdup(fmt3_str);
1689 if (tev->group == NULL || tev->event == NULL) {
1690 ret = -ENOMEM;
1691 goto out;
1692 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001693 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001694
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001695 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001696
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001697 /* Scan module name(if there), function name and offset */
1698 p = strchr(argv[1], ':');
1699 if (p) {
1700 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001701 if (!tp->module) {
1702 ret = -ENOMEM;
1703 goto out;
1704 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001705 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001706 p++;
1707 } else
1708 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001709 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001710 /* only the address started with 0x */
1711 if (fmt1_str[0] == '0') {
1712 /*
1713 * Fix a special case:
1714 * if address == 0, kernel reports something like:
1715 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1716 * Newer kernel may fix that, but we want to
1717 * support old kernel also.
1718 */
1719 if (strcmp(fmt1_str, "0x") == 0) {
1720 if (!argv[2] || strcmp(argv[2], "(null)")) {
1721 ret = -EINVAL;
1722 goto out;
1723 }
1724 tp->address = 0;
1725
1726 free(argv[2]);
1727 for (i = 2; argv[i + 1] != NULL; i++)
1728 argv[i] = argv[i + 1];
1729
1730 argv[i] = NULL;
1731 argc -= 1;
1732 } else
1733 tp->address = strtoul(fmt1_str, NULL, 0);
1734 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001735 /* Only the symbol-based probe has offset */
1736 tp->symbol = strdup(fmt1_str);
1737 if (tp->symbol == NULL) {
1738 ret = -ENOMEM;
1739 goto out;
1740 }
1741 fmt2_str = strtok_r(NULL, "", &fmt);
1742 if (fmt2_str == NULL)
1743 tp->offset = 0;
1744 else
1745 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001746 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001747
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001748 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301749 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001750 if (tev->args == NULL) {
1751 ret = -ENOMEM;
1752 goto out;
1753 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001754 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001755 p = strchr(argv[i + 2], '=');
1756 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001757 *p++ = '\0';
1758 else
1759 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001760 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001761 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001762 tev->args[i].value = strdup(p);
1763 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1764 ret = -ENOMEM;
1765 goto out;
1766 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001767 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001768 ret = 0;
1769out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001770 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001771 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001772 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001773}
1774
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001775/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001776char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001777{
1778 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001779 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001780 char *ret = NULL;
1781 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001782
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001783 if (strbuf_init(&buf, 64) < 0)
1784 return NULL;
1785
Masami Hiramatsu48481932010-04-12 13:16:53 -04001786 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001787 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001788 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001789 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1790 if (err)
1791 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001792
1793 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001794 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001795 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001796 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001797 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1798 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001799 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001800 if (err)
1801 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001802 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001803
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001804 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001805 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1806 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001807
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001808 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001809out:
1810 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001811 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001812}
1813
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001814/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001815char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001816{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001817 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001818 char *tmp, *ret = NULL;
1819 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001820
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001821 if (strbuf_init(&buf, 64) < 0)
1822 return NULL;
1823
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001824 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001825 if (strbuf_addstr(&buf, pp->function) < 0)
1826 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001827 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001828 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001829 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001830 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001831 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001832 err = strbuf_addstr(&buf, "%return");
1833 if (err)
1834 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001835 }
1836 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001837 tmp = pp->file;
1838 len = strlen(tmp);
1839 if (len > 30) {
1840 tmp = strchr(pp->file + len - 30, '/');
1841 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1842 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001843 err = strbuf_addf(&buf, "@%s", tmp);
1844 if (!err && !pp->function && pp->line)
1845 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001846 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001847 if (!err)
1848 ret = strbuf_detach(&buf, NULL);
1849out:
1850 strbuf_release(&buf);
1851 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001852}
1853
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001854char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1855{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001856 struct strbuf buf;
1857 char *tmp, *ret = NULL;
1858 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001860 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001862 if (pev->event)
1863 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1864 pev->event) < 0)
1865 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001866
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001867 tmp = synthesize_perf_probe_point(&pev->point);
1868 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1869 goto out;
1870 free(tmp);
1871
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001872 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001873 tmp = synthesize_perf_probe_arg(pev->args + i);
1874 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1875 goto out;
1876 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001877 }
1878
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001879 ret = strbuf_detach(&buf, NULL);
1880out:
1881 strbuf_release(&buf);
1882 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001884
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301885static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001886 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001887{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001888 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001889 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301890 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001891 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001892 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001893 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001894 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001895 err = strbuf_addf(buf, "%+ld(", ref->offset);
1896 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897}
1898
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301899static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001900 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001901{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301902 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001903 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904
1905 /* Argument name or separator */
1906 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001907 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001909 err = strbuf_addch(buf, ' ');
1910 if (err)
1911 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001913 /* Special case: @XXX */
1914 if (arg->value[0] == '@' && arg->ref)
1915 ref = ref->next;
1916
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001917 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001918 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001919 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001920 if (depth < 0)
1921 return depth;
1922 }
1923
1924 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001925 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001926 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001927 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001928 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929
1930 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001931 while (!err && depth--)
1932 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001933
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001934 /* Print argument type */
1935 if (!err && arg->type)
1936 err = strbuf_addf(buf, ":%s", arg->type);
1937
1938 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001939}
1940
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301941char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001942{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301943 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001944 struct strbuf buf;
1945 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001946 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001947
Wang Nanda15bd92015-08-26 10:57:45 +00001948 /* Uprobes must have tp->module */
1949 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001950 return NULL;
1951
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001952 if (strbuf_init(&buf, 32) < 0)
1953 return NULL;
1954
1955 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1956 tev->group, tev->event) < 0)
1957 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001958 /*
1959 * If tp->address == 0, then this point must be a
1960 * absolute address uprobe.
1961 * try_to_find_absolute_address() should have made
1962 * tp->symbol to "0x0".
1963 */
1964 if (tev->uprobes && !tp->address) {
1965 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1966 goto error;
1967 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001968
1969 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301970 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001971 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001972 else if (!strncmp(tp->symbol, "0x", 2))
1973 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001974 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1975 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301976 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001977 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1978 tp->module ? ":" : "", tp->symbol, tp->offset);
1979 if (err)
1980 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301981
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001982 for (i = 0; i < tev->nargs; i++)
1983 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001984 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001985
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001986 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001987error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001988 strbuf_release(&buf);
1989 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001990}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001991
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001992static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1993 struct perf_probe_point *pp,
1994 bool is_kprobe)
1995{
1996 struct symbol *sym = NULL;
1997 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001998 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001999 int ret = -ENOENT;
2000
2001 if (!is_kprobe) {
2002 map = dso__new_map(tp->module);
2003 if (!map)
2004 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002005 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002006 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002007 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002008 if (kernel_get_symbol_address_by_name(tp->symbol,
2009 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002010 goto out;
2011 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002012 if (addr) {
2013 addr += tp->offset;
2014 sym = __find_kernel_function(addr, &map);
2015 }
2016 }
Wang Nan98d3b252015-11-05 13:19:25 +00002017
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002018 if (!sym)
2019 goto out;
2020
2021 pp->retprobe = tp->retprobe;
2022 pp->offset = addr - map->unmap_ip(map, sym->start);
2023 pp->function = strdup(sym->name);
2024 ret = pp->function ? 0 : -ENOMEM;
2025
2026out:
2027 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002028 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002029 }
2030
2031 return ret;
2032}
2033
2034static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002035 struct perf_probe_point *pp,
2036 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002037{
2038 char buf[128];
2039 int ret;
2040
2041 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2042 if (!ret)
2043 return 0;
2044 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2045 if (!ret)
2046 return 0;
2047
2048 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2049
2050 if (tp->symbol) {
2051 pp->function = strdup(tp->symbol);
2052 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002053 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002054 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2055 if (ret < 0)
2056 return ret;
2057 pp->function = strdup(buf);
2058 pp->offset = 0;
2059 }
2060 if (pp->function == NULL)
2061 return -ENOMEM;
2062
2063 pp->retprobe = tp->retprobe;
2064
2065 return 0;
2066}
2067
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302068static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302069 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002070{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002071 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002072 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002073
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002074 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002075 pev->event = strdup(tev->event);
2076 pev->group = strdup(tev->group);
2077 if (pev->event == NULL || pev->group == NULL)
2078 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002079
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002080 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002081 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002082 if (ret < 0)
2083 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002084
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002085 /* Convert trace_arg to probe_arg */
2086 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002087 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2088 if (pev->args == NULL)
2089 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002090 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002091 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002092 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002093 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002094 if ((ret = strbuf_init(&buf, 32)) < 0)
2095 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002096 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2097 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002098 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002099 if (pev->args[i].name == NULL && ret >= 0)
2100 ret = -ENOMEM;
2101 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002102error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002103 if (ret < 0)
2104 clear_perf_probe_event(pev);
2105
2106 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002107}
2108
2109void clear_perf_probe_event(struct perf_probe_event *pev)
2110{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002111 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002112 int i;
2113
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002114 free(pev->event);
2115 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002116 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002117 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002118
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002119 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002120 free(pev->args[i].name);
2121 free(pev->args[i].var);
2122 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002123 field = pev->args[i].field;
2124 while (field) {
2125 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002126 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002127 free(field);
2128 field = next;
2129 }
2130 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002131 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002132 memset(pev, 0, sizeof(*pev));
2133}
2134
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002135#define strdup_or_goto(str, label) \
2136({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2137
2138static int perf_probe_point__copy(struct perf_probe_point *dst,
2139 struct perf_probe_point *src)
2140{
2141 dst->file = strdup_or_goto(src->file, out_err);
2142 dst->function = strdup_or_goto(src->function, out_err);
2143 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2144 dst->line = src->line;
2145 dst->retprobe = src->retprobe;
2146 dst->offset = src->offset;
2147 return 0;
2148
2149out_err:
2150 clear_perf_probe_point(dst);
2151 return -ENOMEM;
2152}
2153
2154static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2155 struct perf_probe_arg *src)
2156{
2157 struct perf_probe_arg_field *field, **ppfield;
2158
2159 dst->name = strdup_or_goto(src->name, out_err);
2160 dst->var = strdup_or_goto(src->var, out_err);
2161 dst->type = strdup_or_goto(src->type, out_err);
2162
2163 field = src->field;
2164 ppfield = &(dst->field);
2165 while (field) {
2166 *ppfield = zalloc(sizeof(*field));
2167 if (!*ppfield)
2168 goto out_err;
2169 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2170 (*ppfield)->index = field->index;
2171 (*ppfield)->ref = field->ref;
2172 field = field->next;
2173 ppfield = &((*ppfield)->next);
2174 }
2175 return 0;
2176out_err:
2177 return -ENOMEM;
2178}
2179
2180int perf_probe_event__copy(struct perf_probe_event *dst,
2181 struct perf_probe_event *src)
2182{
2183 int i;
2184
2185 dst->event = strdup_or_goto(src->event, out_err);
2186 dst->group = strdup_or_goto(src->group, out_err);
2187 dst->target = strdup_or_goto(src->target, out_err);
2188 dst->uprobes = src->uprobes;
2189
2190 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2191 goto out_err;
2192
2193 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2194 if (!dst->args)
2195 goto out_err;
2196 dst->nargs = src->nargs;
2197
2198 for (i = 0; i < src->nargs; i++)
2199 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2200 goto out_err;
2201 return 0;
2202
2203out_err:
2204 clear_perf_probe_event(dst);
2205 return -ENOMEM;
2206}
2207
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002208void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002209{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302210 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002211 int i;
2212
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002213 free(tev->event);
2214 free(tev->group);
2215 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002216 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002217 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002218 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002219 free(tev->args[i].name);
2220 free(tev->args[i].value);
2221 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002222 ref = tev->args[i].ref;
2223 while (ref) {
2224 next = ref->next;
2225 free(ref);
2226 ref = next;
2227 }
2228 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002229 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002230 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002231}
2232
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002233struct kprobe_blacklist_node {
2234 struct list_head list;
2235 unsigned long start;
2236 unsigned long end;
2237 char *symbol;
2238};
2239
2240static void kprobe_blacklist__delete(struct list_head *blacklist)
2241{
2242 struct kprobe_blacklist_node *node;
2243
2244 while (!list_empty(blacklist)) {
2245 node = list_first_entry(blacklist,
2246 struct kprobe_blacklist_node, list);
2247 list_del(&node->list);
2248 free(node->symbol);
2249 free(node);
2250 }
2251}
2252
2253static int kprobe_blacklist__load(struct list_head *blacklist)
2254{
2255 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002256 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002257 char buf[PATH_MAX], *p;
2258 FILE *fp;
2259 int ret;
2260
2261 if (__debugfs == NULL)
2262 return -ENOTSUP;
2263
2264 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2265 if (ret < 0)
2266 return ret;
2267
2268 fp = fopen(buf, "r");
2269 if (!fp)
2270 return -errno;
2271
2272 ret = 0;
2273 while (fgets(buf, PATH_MAX, fp)) {
2274 node = zalloc(sizeof(*node));
2275 if (!node) {
2276 ret = -ENOMEM;
2277 break;
2278 }
2279 INIT_LIST_HEAD(&node->list);
2280 list_add_tail(&node->list, blacklist);
2281 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2282 ret = -EINVAL;
2283 break;
2284 }
2285 p = strchr(buf, '\t');
2286 if (p) {
2287 p++;
2288 if (p[strlen(p) - 1] == '\n')
2289 p[strlen(p) - 1] = '\0';
2290 } else
2291 p = (char *)"unknown";
2292 node->symbol = strdup(p);
2293 if (!node->symbol) {
2294 ret = -ENOMEM;
2295 break;
2296 }
2297 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2298 node->start, node->end, node->symbol);
2299 ret++;
2300 }
2301 if (ret < 0)
2302 kprobe_blacklist__delete(blacklist);
2303 fclose(fp);
2304
2305 return ret;
2306}
2307
2308static struct kprobe_blacklist_node *
2309kprobe_blacklist__find_by_address(struct list_head *blacklist,
2310 unsigned long address)
2311{
2312 struct kprobe_blacklist_node *node;
2313
2314 list_for_each_entry(node, blacklist, list) {
2315 if (node->start <= address && address <= node->end)
2316 return node;
2317 }
2318
2319 return NULL;
2320}
2321
Masami Hiramatsub0312202015-06-16 20:50:55 +09002322static LIST_HEAD(kprobe_blacklist);
2323
2324static void kprobe_blacklist__init(void)
2325{
2326 if (!list_empty(&kprobe_blacklist))
2327 return;
2328
2329 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2330 pr_debug("No kprobe blacklist support, ignored\n");
2331}
2332
2333static void kprobe_blacklist__release(void)
2334{
2335 kprobe_blacklist__delete(&kprobe_blacklist);
2336}
2337
2338static bool kprobe_blacklist__listed(unsigned long address)
2339{
2340 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2341}
2342
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002343static int perf_probe_event__sprintf(const char *group, const char *event,
2344 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002345 const char *module,
2346 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002347{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002348 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002349 char *buf;
2350
2351 if (asprintf(&buf, "%s:%s", group, event) < 0)
2352 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002353 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002354 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002355 if (ret)
2356 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002357
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002358 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002359 buf = synthesize_perf_probe_point(&pev->point);
2360 if (!buf)
2361 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002362 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002363 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002364
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002365 if (!ret && module)
2366 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002367
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002368 if (!ret && pev->nargs > 0) {
2369 ret = strbuf_add(result, " with", 5);
2370 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002371 buf = synthesize_perf_probe_arg(&pev->args[i]);
2372 if (!buf)
2373 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002374 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002375 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002376 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002377 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002378 if (!ret)
2379 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002380
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002381 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002382}
2383
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002384/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002385int show_perf_probe_event(const char *group, const char *event,
2386 struct perf_probe_event *pev,
2387 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002388{
2389 struct strbuf buf = STRBUF_INIT;
2390 int ret;
2391
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002392 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002393 if (ret >= 0) {
2394 if (use_stdout)
2395 printf("%s\n", buf.buf);
2396 else
2397 pr_info("%s\n", buf.buf);
2398 }
2399 strbuf_release(&buf);
2400
2401 return ret;
2402}
2403
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002404static bool filter_probe_trace_event(struct probe_trace_event *tev,
2405 struct strfilter *filter)
2406{
2407 char tmp[128];
2408
2409 /* At first, check the event name itself */
2410 if (strfilter__compare(filter, tev->event))
2411 return true;
2412
2413 /* Next, check the combination of name and group */
2414 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2415 return false;
2416 return strfilter__compare(filter, tmp);
2417}
2418
2419static int __show_perf_probe_events(int fd, bool is_kprobe,
2420 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002421{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302422 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302423 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002424 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002425 struct strlist *rawlist;
2426 struct str_node *ent;
2427
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002428 memset(&tev, 0, sizeof(tev));
2429 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002430
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002431 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002432 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002433 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002434
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002435 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302436 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002437 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002438 if (!filter_probe_trace_event(&tev, filter))
2439 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302440 ret = convert_to_perf_probe_event(&tev, &pev,
2441 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002442 if (ret < 0)
2443 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002444 ret = show_perf_probe_event(pev.group, pev.event,
2445 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002446 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002448next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002449 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302450 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002451 if (ret < 0)
2452 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002453 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002454 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002455 /* Cleanup cached debuginfo if needed */
2456 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002457
2458 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002459}
2460
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302461/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002462int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302463{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002464 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302465
2466 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302467
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002468 if (probe_conf.cache)
2469 return probe_cache__show_all_caches(filter);
2470
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002471 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302472 if (ret < 0)
2473 return ret;
2474
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002475 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2476 if (ret < 0)
2477 return ret;
2478
2479 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002480 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002481 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002482 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002483 if (kp_fd > 0)
2484 close(kp_fd);
2485 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002486 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002487 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302488
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002489 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002490}
2491
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002492static int get_new_event_name(char *buf, size_t len, const char *base,
2493 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002494{
2495 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002496 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002497
Naveen N. Rao3099c022015-04-28 17:35:34 +05302498 if (*base == '.')
2499 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002500 nbase = strdup(base);
2501 if (!nbase)
2502 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302503
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002504 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2505 p = strchr(nbase, '.');
2506 if (p && p != nbase)
2507 *p = '\0';
2508
2509 /* Try no suffix number */
2510 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002511 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002512 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002513 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002514 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002515 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002516 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002517
Masami Hiramatsud761b082009-12-15 10:32:25 -05002518 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002519 pr_warning("Error: event \"%s\" already exists.\n"
2520 " Hint: Remove existing event by 'perf probe -d'\n"
2521 " or force duplicates by 'perf probe -f'\n"
2522 " or set 'force=yes' in BPF source.\n",
2523 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002524 ret = -EEXIST;
2525 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002526 }
2527
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002528 /* Try to add suffix */
2529 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002530 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002531 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002532 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002533 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002534 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002535 if (!strlist__has_entry(namelist, buf))
2536 break;
2537 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002538 if (i == MAX_EVENT_INDEX) {
2539 pr_warning("Too many events are on the same function.\n");
2540 ret = -ERANGE;
2541 }
2542
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002543out:
2544 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002545 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002546}
2547
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002548/* Warn if the current kernel's uprobe implementation is old */
2549static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2550{
2551 int i;
2552 char *buf = synthesize_probe_trace_command(tev);
2553
2554 /* Old uprobe event doesn't support memory dereference */
2555 if (!tev->uprobes || tev->nargs == 0 || !buf)
2556 goto out;
2557
2558 for (i = 0; i < tev->nargs; i++)
2559 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2560 pr_warning("Please upgrade your kernel to at least "
2561 "3.14 to have access to feature %s\n",
2562 tev->args[i].value);
2563 break;
2564 }
2565out:
2566 free(buf);
2567}
2568
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002569/* Set new name from original perf_probe_event and namelist */
2570static int probe_trace_event__set_name(struct probe_trace_event *tev,
2571 struct perf_probe_event *pev,
2572 struct strlist *namelist,
2573 bool allow_suffix)
2574{
2575 const char *event, *group;
2576 char buf[64];
2577 int ret;
2578
Masami Hiramatsubc062232016-07-01 17:03:12 +09002579 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002580 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002581 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002582 else if (tev->event)
2583 event = tev->event;
2584 else {
2585 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002586 if (pev->point.function &&
2587 (strncmp(pev->point.function, "0x", 2) != 0) &&
2588 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002589 event = pev->point.function;
2590 else
2591 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002592 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002593 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002594 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002595 else if (tev->group)
2596 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002597 else
2598 group = PERFPROBE_GROUP;
2599
2600 /* Get an unused new event name */
2601 ret = get_new_event_name(buf, 64, event,
2602 namelist, allow_suffix);
2603 if (ret < 0)
2604 return ret;
2605
2606 event = buf;
2607
2608 tev->event = strdup(event);
2609 tev->group = strdup(group);
2610 if (tev->event == NULL || tev->group == NULL)
2611 return -ENOMEM;
2612
2613 /* Add added event name to namelist */
2614 strlist__add(namelist, event);
2615 return 0;
2616}
2617
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002618static int __open_probe_file_and_namelist(bool uprobe,
2619 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002620{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002621 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002622
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002623 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002624 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002625 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002626
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002627 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002628 *namelist = probe_file__get_namelist(fd);
2629 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002630 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002631 close(fd);
2632 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002633 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002634 return fd;
2635}
2636
2637static int __add_probe_trace_events(struct perf_probe_event *pev,
2638 struct probe_trace_event *tevs,
2639 int ntevs, bool allow_suffix)
2640{
2641 int i, fd[2] = {-1, -1}, up, ret;
2642 struct probe_trace_event *tev = NULL;
2643 struct probe_cache *cache = NULL;
2644 struct strlist *namelist[2] = {NULL, NULL};
2645
2646 up = pev->uprobes ? 1 : 0;
2647 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2648 if (fd[up] < 0)
2649 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002650
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002651 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002652 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002653 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002654 up = tev->uprobes ? 1 : 0;
2655 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2656 fd[up] = __open_probe_file_and_namelist(up,
2657 &namelist[up]);
2658 if (fd[up] < 0)
2659 goto close_out;
2660 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002661 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002662 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002663 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002664
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002665 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002666 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002667 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002668 if (ret < 0)
2669 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002670
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002671 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002672 if (ret < 0)
2673 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002674
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002675 /*
2676 * Probes after the first probe which comes from same
2677 * user input are always allowed to add suffix, because
2678 * there might be several addresses corresponding to
2679 * one code line.
2680 */
2681 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002682 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002683 if (ret == -EINVAL && pev->uprobes)
2684 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002685 if (ret == 0 && probe_conf.cache) {
2686 cache = probe_cache__new(pev->target);
2687 if (!cache ||
2688 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2689 probe_cache__commit(cache) < 0)
2690 pr_warning("Failed to add event to probe cache\n");
2691 probe_cache__delete(cache);
2692 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002693
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002694close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002695 for (up = 0; up < 2; up++) {
2696 strlist__delete(namelist[up]);
2697 if (fd[up] >= 0)
2698 close(fd[up]);
2699 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002700 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002701}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002702
Wang Nan6bb536c2015-05-29 09:45:47 +00002703static int find_probe_functions(struct map *map, char *name,
2704 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002705{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002706 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002707 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002708 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002709
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002710 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002711 return 0;
2712
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002713 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002714 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002715 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002716 if (syms && found < probe_conf.max_probes)
2717 syms[found - 1] = sym;
2718 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002719 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002720
2721 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002722}
2723
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302724void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2725 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302726 struct map *map __maybe_unused,
2727 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302728
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002729/*
2730 * Find probe function addresses from map.
2731 * Return an error or the number of found probe_trace_event
2732 */
2733static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002734 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002735{
2736 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002737 struct ref_reloc_sym *reloc_sym = NULL;
2738 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002739 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002740 struct probe_trace_event *tev;
2741 struct perf_probe_point *pp = &pev->point;
2742 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002743 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002744 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302745 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002746
Masami Hiramatsu44225522015-05-08 10:03:28 +09002747 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002748 if (!map) {
2749 ret = -EINVAL;
2750 goto out;
2751 }
2752
Wang Nan6bb536c2015-05-29 09:45:47 +00002753 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2754 if (!syms) {
2755 ret = -ENOMEM;
2756 goto out;
2757 }
2758
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002759 /*
2760 * Load matched symbols: Since the different local symbols may have
2761 * same name but different addresses, this lists all the symbols.
2762 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002763 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002764 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002765 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002766 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002767 ret = -ENOENT;
2768 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002769 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002770 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002771 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002772 ret = -E2BIG;
2773 goto out;
2774 }
2775
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002776 /* Note that the symbols in the kmodule are not relocated */
2777 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002778 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002779 if (!reloc_sym) {
2780 pr_warning("Relocated base symbol is not found!\n");
2781 ret = -EINVAL;
2782 goto out;
2783 }
2784 }
2785
2786 /* Setup result trace-probe-events */
2787 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2788 if (!*tevs) {
2789 ret = -ENOMEM;
2790 goto out;
2791 }
2792
2793 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002794
Wang Nan6bb536c2015-05-29 09:45:47 +00002795 for (j = 0; j < num_matched_functions; j++) {
2796 sym = syms[j];
2797
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002798 tev = (*tevs) + ret;
2799 tp = &tev->point;
2800 if (ret == num_matched_functions) {
2801 pr_warning("Too many symbols are listed. Skip it.\n");
2802 break;
2803 }
2804 ret++;
2805
2806 if (pp->offset > sym->end - sym->start) {
2807 pr_warning("Offset %ld is bigger than the size of %s\n",
2808 pp->offset, sym->name);
2809 ret = -ENOENT;
2810 goto err_out;
2811 }
2812 /* Add one probe point */
2813 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002814
2815 /* Check the kprobe (not in module) is within .text */
2816 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002817 kprobe_warn_out_range(sym->name, tp->address)) {
2818 tp->symbol = NULL; /* Skip it */
2819 skipped++;
2820 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002821 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2822 tp->offset = tp->address - reloc_sym->addr;
2823 } else {
2824 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2825 tp->offset = pp->offset;
2826 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002827 tp->realname = strdup_or_goto(sym->name, nomem_out);
2828
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002829 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302830 if (pev->target) {
2831 if (pev->uprobes) {
2832 tev->point.module = strdup_or_goto(pev->target,
2833 nomem_out);
2834 } else {
2835 mod_name = find_module_name(pev->target);
2836 tev->point.module =
2837 strdup(mod_name ? mod_name : pev->target);
2838 free(mod_name);
2839 if (!tev->point.module)
2840 goto nomem_out;
2841 }
2842 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002843 tev->uprobes = pev->uprobes;
2844 tev->nargs = pev->nargs;
2845 if (tev->nargs) {
2846 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2847 tev->nargs);
2848 if (tev->args == NULL)
2849 goto nomem_out;
2850 }
2851 for (i = 0; i < tev->nargs; i++) {
2852 if (pev->args[i].name)
2853 tev->args[i].name =
2854 strdup_or_goto(pev->args[i].name,
2855 nomem_out);
2856
2857 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2858 nomem_out);
2859 if (pev->args[i].type)
2860 tev->args[i].type =
2861 strdup_or_goto(pev->args[i].type,
2862 nomem_out);
2863 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302864 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002865 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002866 if (ret == skipped) {
2867 ret = -ENOENT;
2868 goto err_out;
2869 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002870
2871out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002872 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002873 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002874 return ret;
2875
2876nomem_out:
2877 ret = -ENOMEM;
2878err_out:
2879 clear_probe_trace_events(*tevs, num_matched_functions);
2880 zfree(tevs);
2881 goto out;
2882}
2883
Wang Nanda15bd92015-08-26 10:57:45 +00002884static int try_to_find_absolute_address(struct perf_probe_event *pev,
2885 struct probe_trace_event **tevs)
2886{
2887 struct perf_probe_point *pp = &pev->point;
2888 struct probe_trace_event *tev;
2889 struct probe_trace_point *tp;
2890 int i, err;
2891
2892 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2893 return -EINVAL;
2894 if (perf_probe_event_need_dwarf(pev))
2895 return -EINVAL;
2896
2897 /*
2898 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2899 * absolute address.
2900 *
2901 * Only one tev can be generated by this.
2902 */
2903 *tevs = zalloc(sizeof(*tev));
2904 if (!*tevs)
2905 return -ENOMEM;
2906
2907 tev = *tevs;
2908 tp = &tev->point;
2909
2910 /*
2911 * Don't use tp->offset, use address directly, because
2912 * in synthesize_probe_trace_command() address cannot be
2913 * zero.
2914 */
2915 tp->address = pev->point.abs_address;
2916 tp->retprobe = pp->retprobe;
2917 tev->uprobes = pev->uprobes;
2918
2919 err = -ENOMEM;
2920 /*
2921 * Give it a '0x' leading symbol name.
2922 * In __add_probe_trace_events, a NULL symbol is interpreted as
2923 * invalud.
2924 */
2925 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2926 goto errout;
2927
2928 /* For kprobe, check range */
2929 if ((!tev->uprobes) &&
2930 (kprobe_warn_out_range(tev->point.symbol,
2931 tev->point.address))) {
2932 err = -EACCES;
2933 goto errout;
2934 }
2935
2936 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2937 goto errout;
2938
2939 if (pev->target) {
2940 tp->module = strdup(pev->target);
2941 if (!tp->module)
2942 goto errout;
2943 }
2944
2945 if (tev->group) {
2946 tev->group = strdup(pev->group);
2947 if (!tev->group)
2948 goto errout;
2949 }
2950
2951 if (pev->event) {
2952 tev->event = strdup(pev->event);
2953 if (!tev->event)
2954 goto errout;
2955 }
2956
2957 tev->nargs = pev->nargs;
2958 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2959 if (!tev->args) {
2960 err = -ENOMEM;
2961 goto errout;
2962 }
2963 for (i = 0; i < tev->nargs; i++)
2964 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2965
2966 return 1;
2967
2968errout:
2969 if (*tevs) {
2970 clear_probe_trace_events(*tevs, 1);
2971 *tevs = NULL;
2972 }
2973 return err;
2974}
2975
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002976/* Concatinate two arrays */
2977static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2978{
2979 void *ret;
2980
2981 ret = malloc(sz_a + sz_b);
2982 if (ret) {
2983 memcpy(ret, a, sz_a);
2984 memcpy(ret + sz_a, b, sz_b);
2985 }
2986 return ret;
2987}
2988
2989static int
2990concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2991 struct probe_trace_event **tevs2, int ntevs2)
2992{
2993 struct probe_trace_event *new_tevs;
2994 int ret = 0;
2995
2996 if (ntevs == 0) {
2997 *tevs = *tevs2;
2998 *ntevs = ntevs2;
2999 *tevs2 = NULL;
3000 return 0;
3001 }
3002
3003 if (*ntevs + ntevs2 > probe_conf.max_probes)
3004 ret = -E2BIG;
3005 else {
3006 /* Concatinate the array of probe_trace_event */
3007 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3008 *tevs2, ntevs2 * sizeof(**tevs2));
3009 if (!new_tevs)
3010 ret = -ENOMEM;
3011 else {
3012 free(*tevs);
3013 *tevs = new_tevs;
3014 *ntevs += ntevs2;
3015 }
3016 }
3017 if (ret < 0)
3018 clear_probe_trace_events(*tevs2, ntevs2);
3019 zfree(tevs2);
3020
3021 return ret;
3022}
3023
3024/*
3025 * Try to find probe_trace_event from given probe caches. Return the number
3026 * of cached events found, if an error occurs return the error.
3027 */
3028static int find_cached_events(struct perf_probe_event *pev,
3029 struct probe_trace_event **tevs,
3030 const char *target)
3031{
3032 struct probe_cache *cache;
3033 struct probe_cache_entry *entry;
3034 struct probe_trace_event *tmp_tevs = NULL;
3035 int ntevs = 0;
3036 int ret = 0;
3037
3038 cache = probe_cache__new(target);
3039 /* Return 0 ("not found") if the target has no probe cache. */
3040 if (!cache)
3041 return 0;
3042
3043 for_each_probe_cache_entry(entry, cache) {
3044 /* Skip the cache entry which has no name */
3045 if (!entry->pev.event || !entry->pev.group)
3046 continue;
3047 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3048 strglobmatch(entry->pev.event, pev->event)) {
3049 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3050 if (ret > 0)
3051 ret = concat_probe_trace_events(tevs, &ntevs,
3052 &tmp_tevs, ret);
3053 if (ret < 0)
3054 break;
3055 }
3056 }
3057 probe_cache__delete(cache);
3058 if (ret < 0) {
3059 clear_probe_trace_events(*tevs, ntevs);
3060 zfree(tevs);
3061 } else {
3062 ret = ntevs;
3063 if (ntevs > 0 && target && target[0] == '/')
3064 pev->uprobes = true;
3065 }
3066
3067 return ret;
3068}
3069
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003070/* Try to find probe_trace_event from all probe caches */
3071static int find_cached_events_all(struct perf_probe_event *pev,
3072 struct probe_trace_event **tevs)
3073{
3074 struct probe_trace_event *tmp_tevs = NULL;
3075 struct strlist *bidlist;
3076 struct str_node *nd;
3077 char *pathname;
3078 int ntevs = 0;
3079 int ret;
3080
3081 /* Get the buildid list of all valid caches */
3082 bidlist = build_id_cache__list_all(true);
3083 if (!bidlist) {
3084 ret = -errno;
3085 pr_debug("Failed to get buildids: %d\n", ret);
3086 return ret;
3087 }
3088
3089 ret = 0;
3090 strlist__for_each_entry(nd, bidlist) {
3091 pathname = build_id_cache__origname(nd->s);
3092 ret = find_cached_events(pev, &tmp_tevs, pathname);
3093 /* In the case of cnt == 0, we just skip it */
3094 if (ret > 0)
3095 ret = concat_probe_trace_events(tevs, &ntevs,
3096 &tmp_tevs, ret);
3097 free(pathname);
3098 if (ret < 0)
3099 break;
3100 }
3101 strlist__delete(bidlist);
3102
3103 if (ret < 0) {
3104 clear_probe_trace_events(*tevs, ntevs);
3105 zfree(tevs);
3106 } else
3107 ret = ntevs;
3108
3109 return ret;
3110}
3111
Masami Hiramatsubc062232016-07-01 17:03:12 +09003112static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3113 struct probe_trace_event **tevs)
3114{
3115 struct probe_cache *cache;
3116 struct probe_cache_entry *entry;
3117 struct probe_trace_event *tev;
3118 struct str_node *node;
3119 int ret, i;
3120
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003121 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003122 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003123 if (!pev->target)
3124 return find_cached_events_all(pev, tevs);
3125 else
3126 return find_cached_events(pev, tevs, pev->target);
3127 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003128 cache = probe_cache__new(pev->target);
3129 if (!cache)
3130 return 0;
3131
3132 entry = probe_cache__find(cache, pev);
3133 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003134 /* SDT must be in the cache */
3135 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003136 goto out;
3137 }
3138
3139 ret = strlist__nr_entries(entry->tevlist);
3140 if (ret > probe_conf.max_probes) {
3141 pr_debug("Too many entries matched in the cache of %s\n",
3142 pev->target ? : "kernel");
3143 ret = -E2BIG;
3144 goto out;
3145 }
3146
3147 *tevs = zalloc(ret * sizeof(*tev));
3148 if (!*tevs) {
3149 ret = -ENOMEM;
3150 goto out;
3151 }
3152
3153 i = 0;
3154 strlist__for_each_entry(node, entry->tevlist) {
3155 tev = &(*tevs)[i++];
3156 ret = parse_probe_trace_command(node->s, tev);
3157 if (ret < 0)
3158 goto out;
3159 /* Set the uprobes attribute as same as original */
3160 tev->uprobes = pev->uprobes;
3161 }
3162 ret = i;
3163
3164out:
3165 probe_cache__delete(cache);
3166 return ret;
3167}
3168
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303169static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003170 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003171{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003172 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003173
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003174 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003175 /* Set group name if not given */
3176 if (!pev->uprobes) {
3177 pev->group = strdup(PERFPROBE_GROUP);
3178 ret = pev->group ? 0 : -ENOMEM;
3179 } else
3180 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003181 if (ret != 0) {
3182 pr_warning("Failed to make a group name.\n");
3183 return ret;
3184 }
3185 }
3186
Wang Nanda15bd92015-08-26 10:57:45 +00003187 ret = try_to_find_absolute_address(pev, tevs);
3188 if (ret > 0)
3189 return ret;
3190
Masami Hiramatsubc062232016-07-01 17:03:12 +09003191 /* At first, we need to lookup cache entry */
3192 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003193 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3194 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003195
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003196 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003197 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003198 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003199 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003200
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003201 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003202}
3203
Wang Nan12fae5e2015-09-04 21:16:00 +09003204int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003205{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003206 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003207
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003208 /* Loop 1: convert all events */
3209 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003210 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003211 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003212 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003213 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003214 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003215 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003216 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003217 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003218 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003219 /* This just release blacklist only if allocated */
3220 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003221
Namhyung Kim844dffa2015-09-04 21:15:59 +09003222 return 0;
3223}
3224
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003225static int show_probe_trace_event(struct probe_trace_event *tev)
3226{
3227 char *buf = synthesize_probe_trace_command(tev);
3228
3229 if (!buf) {
3230 pr_debug("Failed to synthesize probe trace event.\n");
3231 return -EINVAL;
3232 }
3233
3234 /* Showing definition always go stdout */
3235 printf("%s\n", buf);
3236 free(buf);
3237
3238 return 0;
3239}
3240
3241int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3242{
3243 struct strlist *namelist = strlist__new(NULL, NULL);
3244 struct probe_trace_event *tev;
3245 struct perf_probe_event *pev;
3246 int i, j, ret = 0;
3247
3248 if (!namelist)
3249 return -ENOMEM;
3250
3251 for (j = 0; j < npevs && !ret; j++) {
3252 pev = &pevs[j];
3253 for (i = 0; i < pev->ntevs && !ret; i++) {
3254 tev = &pev->tevs[i];
3255 /* Skip if the symbol is out of .text or blacklisted */
3256 if (!tev->point.symbol && !pev->uprobes)
3257 continue;
3258
3259 /* Set new name for tev (and update namelist) */
3260 ret = probe_trace_event__set_name(tev, pev,
3261 namelist, true);
3262 if (!ret)
3263 ret = show_probe_trace_event(tev);
3264 }
3265 }
3266 strlist__delete(namelist);
3267
3268 return ret;
3269}
3270
Wang Nan12fae5e2015-09-04 21:16:00 +09003271int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003272{
3273 int i, ret = 0;
3274
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003275 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003276 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003277 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3278 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003279 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003280 if (ret < 0)
3281 break;
3282 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003283 return ret;
3284}
3285
Wang Nan12fae5e2015-09-04 21:16:00 +09003286void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003287{
3288 int i, j;
3289
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003290 /* Loop 3: cleanup and free trace events */
3291 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003292 for (j = 0; j < pevs[i].ntevs; j++)
3293 clear_probe_trace_event(&pevs[i].tevs[j]);
3294 zfree(&pevs[i].tevs);
3295 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003296 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003297 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003298}
3299
3300int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3301{
3302 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003303
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003304 ret = init_probe_symbol_maps(pevs->uprobes);
3305 if (ret < 0)
3306 return ret;
3307
Wang Nan12fae5e2015-09-04 21:16:00 +09003308 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003309 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003310 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003311
Wang Nan12fae5e2015-09-04 21:16:00 +09003312 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003313
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003314 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003315 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003316}
3317
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003318int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003319{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003320 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003321 char *str = strfilter__string(filter);
3322
3323 if (!str)
3324 return -EINVAL;
3325
Masami Hiramatsufa282442009-12-08 17:03:23 -05003326 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003327 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3328 if (ret < 0)
3329 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303330
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003331 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003332 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303333 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003334
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003335 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003336 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003337 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003338 goto error;
3339 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003340 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303341
3342error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003343 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303344 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003345 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303346 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003347out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003348 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003349
3350 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003351}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303352
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003353int show_available_funcs(const char *target, struct strfilter *_filter,
3354 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003355{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003356 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003357 struct map *map;
3358 int ret;
3359
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003360 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003361 if (ret < 0)
3362 return ret;
3363
3364 /* Get a symbol map */
3365 if (user)
3366 map = dso__new_map(target);
3367 else
3368 map = kernel_get_module_map(target);
3369 if (!map) {
3370 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003371 return -EINVAL;
3372 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003373
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003374 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003375 if (ret) {
3376 if (ret == -2) {
3377 char *str = strfilter__string(_filter);
3378 pr_err("Failed to find symbols matched to \"%s\"\n",
3379 str);
3380 free(str);
3381 } else
3382 pr_err("Failed to load symbols in %s\n",
3383 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003384 goto end;
3385 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003386 if (!dso__sorted_by_name(map->dso, map->type))
3387 dso__sort_by_name(map->dso, map->type);
3388
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003389 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303390 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003391
3392 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3393 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3394
3395 if (strfilter__compare(_filter, pos->sym.name))
3396 printf("%s\n", pos->sym.name);
3397 }
3398
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003399end:
3400 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003401 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003402 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003403 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303404
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003405 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303406}
3407
Wang Nanda15bd92015-08-26 10:57:45 +00003408int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3409 struct perf_probe_arg *pvar)
3410{
3411 tvar->value = strdup(pvar->var);
3412 if (tvar->value == NULL)
3413 return -ENOMEM;
3414 if (pvar->type) {
3415 tvar->type = strdup(pvar->type);
3416 if (tvar->type == NULL)
3417 return -ENOMEM;
3418 }
3419 if (pvar->name) {
3420 tvar->name = strdup(pvar->name);
3421 if (tvar->name == NULL)
3422 return -ENOMEM;
3423 } else
3424 tvar->name = NULL;
3425 return 0;
3426}