blob: bc60ce49720ba1fae3ee77adf51e9b33e2caa00f [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
216 ptr2 = strpbrk(ptr1, "-._");
217 if (ptr2)
218 *ptr2 = '\0';
219 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
220 if (ret < 0)
221 goto out;
222
223 *result = strdup(buf);
224 ret = *result ? 0 : -ENOMEM;
225
226out:
227 free(exec_copy);
228 return ret;
229}
230
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900231static void clear_perf_probe_point(struct perf_probe_point *pp)
232{
233 free(pp->file);
234 free(pp->function);
235 free(pp->lazy_line);
236}
237
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000238static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
239{
240 int i;
241
242 for (i = 0; i < ntevs; i++)
243 clear_probe_trace_event(tevs + i);
244}
245
Masami Hiramatsub0312202015-06-16 20:50:55 +0900246static bool kprobe_blacklist__listed(unsigned long address);
247static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
248{
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900249 u64 etext_addr = 0;
250 int ret;
He Kuang7c31bb82015-06-18 02:49:10 +0000251
Masami Hiramatsub0312202015-06-16 20:50:55 +0900252 /* Get the address of _etext for checking non-probable text symbol */
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900253 ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
254 false, false);
He Kuang7c31bb82015-06-18 02:49:10 +0000255
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900256 if (ret == 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900257 pr_warning("%s is out of .text, skip it.\n", symbol);
258 else if (kprobe_blacklist__listed(address))
259 pr_warning("%s is blacklisted function, skip it.\n", symbol);
260 else
261 return false;
262
263 return true;
264}
265
Ravi Bangoriac61fb952016-04-26 19:55:40 +0530266/*
267 * NOTE:
268 * '.gnu.linkonce.this_module' section of kernel module elf directly
269 * maps to 'struct module' from linux/module.h. This section contains
270 * actual module name which will be used by kernel after loading it.
271 * But, we cannot use 'struct module' here since linux/module.h is not
272 * exposed to user-space. Offset of 'name' has remained same from long
273 * time, so hardcoding it here.
274 */
275#ifdef __LP64__
276#define MOD_NAME_OFFSET 24
277#else
278#define MOD_NAME_OFFSET 12
279#endif
280
281/*
282 * @module can be module name of module file path. In case of path,
283 * inspect elf and find out what is actual module name.
284 * Caller has to free mod_name after using it.
285 */
286static char *find_module_name(const char *module)
287{
288 int fd;
289 Elf *elf;
290 GElf_Ehdr ehdr;
291 GElf_Shdr shdr;
292 Elf_Data *data;
293 Elf_Scn *sec;
294 char *mod_name = NULL;
295
296 fd = open(module, O_RDONLY);
297 if (fd < 0)
298 return NULL;
299
300 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
301 if (elf == NULL)
302 goto elf_err;
303
304 if (gelf_getehdr(elf, &ehdr) == NULL)
305 goto ret_err;
306
307 sec = elf_section_by_name(elf, &ehdr, &shdr,
308 ".gnu.linkonce.this_module", NULL);
309 if (!sec)
310 goto ret_err;
311
312 data = elf_getdata(sec, NULL);
313 if (!data || !data->d_buf)
314 goto ret_err;
315
316 mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
317
318ret_err:
319 elf_end(elf);
320elf_err:
321 close(fd);
322 return mod_name;
323}
324
Ingo Molnar89fe8082013-09-30 12:07:11 +0200325#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000326
327static int kernel_get_module_dso(const char *module, struct dso **pdso)
328{
329 struct dso *dso;
330 struct map *map;
331 const char *vmlinux_name;
332 int ret = 0;
333
334 if (module) {
Arnaldo Carvalho de Melo266fa2b2015-09-24 11:24:18 -0300335 char module_name[128];
336
337 snprintf(module_name, sizeof(module_name), "[%s]", module);
338 map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
339 if (map) {
340 dso = map->dso;
341 goto found;
Wang Nan60fb7742015-05-28 02:25:05 +0000342 }
343 pr_debug("Failed to find module %s.\n", module);
344 return -ENOENT;
345 }
346
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300347 map = machine__kernel_map(host_machine);
Wang Nan60fb7742015-05-28 02:25:05 +0000348 dso = map->dso;
349
350 vmlinux_name = symbol_conf.vmlinux_name;
351 dso->load_errno = 0;
352 if (vmlinux_name)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300353 ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
Wang Nan60fb7742015-05-28 02:25:05 +0000354 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300355 ret = dso__load_vmlinux_path(dso, map);
Wang Nan60fb7742015-05-28 02:25:05 +0000356found:
357 *pdso = dso;
358 return ret;
359}
360
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900361/*
362 * Some binaries like glibc have special symbols which are on the symbol
363 * table, but not in the debuginfo. If we can find the address of the
364 * symbol from map, we can translate the address back to the probe point.
365 */
366static int find_alternative_probe_point(struct debuginfo *dinfo,
367 struct perf_probe_point *pp,
368 struct perf_probe_point *result,
369 const char *target, bool uprobes)
370{
371 struct map *map = NULL;
372 struct symbol *sym;
373 u64 address = 0;
374 int ret = -ENOENT;
375
376 /* This can work only for function-name based one */
377 if (!pp->function || pp->file)
378 return -ENOTSUP;
379
380 map = get_target_map(target, uprobes);
381 if (!map)
382 return -EINVAL;
383
384 /* Find the address of given function */
385 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900386 if (uprobes)
387 address = sym->start;
388 else
Masami Hiramatsu8e341892016-08-06 19:29:48 +0900389 address = map->unmap_ip(map, sym->start) - map->reloc;
Namhyung Kime578da32015-03-06 16:31:29 +0900390 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900391 }
392 if (!address) {
393 ret = -ENOENT;
394 goto out;
395 }
Wang Nanf6c15622015-04-08 02:14:34 +0000396 pr_debug("Symbol %s address found : %" PRIx64 "\n",
397 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900398
399 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
400 result);
401 if (ret <= 0)
402 ret = (!ret) ? -ENOENT : ret;
403 else {
404 result->offset += pp->offset;
405 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800406 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900407 ret = 0;
408 }
409
410out:
411 put_target_map(map, uprobes);
412 return ret;
413
414}
415
416static int get_alternative_probe_event(struct debuginfo *dinfo,
417 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900418 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900419{
420 int ret;
421
422 memcpy(tmp, &pev->point, sizeof(*tmp));
423 memset(&pev->point, 0, sizeof(pev->point));
424 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900425 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900426 if (ret < 0)
427 memcpy(&pev->point, tmp, sizeof(*tmp));
428
429 return ret;
430}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000431
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900432static int get_alternative_line_range(struct debuginfo *dinfo,
433 struct line_range *lr,
434 const char *target, bool user)
435{
David Ahern6d4a4892015-03-11 10:36:20 -0400436 struct perf_probe_point pp = { .function = lr->function,
437 .file = lr->file,
438 .line = lr->start };
439 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900440 int ret, len = 0;
441
David Ahern6d4a4892015-03-11 10:36:20 -0400442 memset(&result, 0, sizeof(result));
443
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900444 if (lr->end != INT_MAX)
445 len = lr->end - lr->start;
446 ret = find_alternative_probe_point(dinfo, &pp, &result,
447 target, user);
448 if (!ret) {
449 lr->function = result.function;
450 lr->file = result.file;
451 lr->start = result.line;
452 if (lr->end != INT_MAX)
453 lr->end = lr->start + len;
454 clear_perf_probe_point(&pp);
455 }
456 return ret;
457}
458
Masami Hiramatsuff741782011-06-27 16:27:39 +0900459/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000460static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900461{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000462 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900463 char reason[STRERR_BUFSIZE];
464 struct debuginfo *ret = NULL;
465 struct dso *dso = NULL;
466 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900467
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000468 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900469 err = kernel_get_module_dso(module, &dso);
470 if (err < 0) {
471 if (!dso || dso->load_errno == 0) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300472 if (!str_error_r(-err, reason, STRERR_BUFSIZE))
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900473 strcpy(reason, "(unknown)");
474 } else
475 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000476 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900477 pr_err("Failed to find the path for %s: %s\n",
478 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900479 return NULL;
480 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900481 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900482 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000483 ret = debuginfo__new(path);
484 if (!ret && !silent) {
485 pr_warning("The %s file has no debug information.\n", path);
486 if (!module || !strtailcmp(path, ".ko"))
487 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
488 else
489 pr_warning("Rebuild with -g, ");
490 pr_warning("or install an appropriate debuginfo package.\n");
491 }
492 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400493}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300494
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900495/* For caching the last debuginfo */
496static struct debuginfo *debuginfo_cache;
497static char *debuginfo_cache_path;
498
499static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
500{
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900501 const char *path = module;
502
503 /* If the module is NULL, it should be the kernel. */
504 if (!module)
505 path = "kernel";
506
507 if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900508 goto out;
509
510 /* Copy module path */
511 free(debuginfo_cache_path);
Masami Hiramatsu20f49852015-10-01 01:41:35 +0900512 debuginfo_cache_path = strdup(path);
513 if (!debuginfo_cache_path) {
514 debuginfo__delete(debuginfo_cache);
515 debuginfo_cache = NULL;
516 goto out;
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900517 }
518
519 debuginfo_cache = open_debuginfo(module, silent);
520 if (!debuginfo_cache)
521 zfree(&debuginfo_cache_path);
522out:
523 return debuginfo_cache;
524}
525
526static void debuginfo_cache__exit(void)
527{
528 debuginfo__delete(debuginfo_cache);
529 debuginfo_cache = NULL;
530 zfree(&debuginfo_cache_path);
531}
532
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000533
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000534static int get_text_start_address(const char *exec, unsigned long *address)
535{
536 Elf *elf;
537 GElf_Ehdr ehdr;
538 GElf_Shdr shdr;
539 int fd, ret = -ENOENT;
540
541 fd = open(exec, O_RDONLY);
542 if (fd < 0)
543 return -errno;
544
545 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900546 if (elf == NULL) {
547 ret = -EINVAL;
548 goto out_close;
549 }
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000550
551 if (gelf_getehdr(elf, &ehdr) == NULL)
552 goto out;
553
554 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
555 goto out;
556
557 *address = shdr.sh_addr - shdr.sh_offset;
558 ret = 0;
559out:
560 elf_end(elf);
Masami Hiramatsu062d6c22016-04-26 15:47:37 +0900561out_close:
562 close(fd);
563
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000564 return ret;
565}
566
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000567/*
568 * Convert trace point to probe point with debuginfo
569 */
570static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
571 struct perf_probe_point *pp,
572 bool is_kprobe)
573{
574 struct debuginfo *dinfo = NULL;
575 unsigned long stext = 0;
576 u64 addr = tp->address;
577 int ret = -ENOENT;
578
579 /* convert the address to dwarf address */
580 if (!is_kprobe) {
581 if (!addr) {
582 ret = -EINVAL;
583 goto error;
584 }
585 ret = get_text_start_address(tp->module, &stext);
586 if (ret < 0)
587 goto error;
588 addr += stext;
Wang Nane4863672015-08-25 13:27:35 +0000589 } else if (tp->symbol) {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +0900590 /* If the module is given, this returns relative address */
591 ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
592 false, !!tp->module);
593 if (ret != 0)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000594 goto error;
595 addr += tp->offset;
596 }
597
598 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
599 tp->module ? : "kernel");
600
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900601 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
602 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000603 ret = debuginfo__find_probe_point(dinfo,
604 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900605 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000606 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000607
608 if (ret > 0) {
609 pp->retprobe = tp->retprobe;
610 return 0;
611 }
612error:
613 pr_debug("Failed to find corresponding probes from debuginfo.\n");
614 return ret ? : -ENOENT;
615}
616
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000617static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
618 int ntevs, const char *exec)
619{
620 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000621 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000622
623 if (!exec)
624 return 0;
625
626 ret = get_text_start_address(exec, &stext);
627 if (ret < 0)
628 return ret;
629
630 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000631 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000632 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000633 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000634 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000635 ret = -ENOMEM;
636 break;
637 }
638 tevs[i].uprobes = true;
639 }
640
641 return ret;
642}
643
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900644static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
645 int ntevs, const char *module)
646{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900647 int i, ret = 0;
Ravi Bangoria63a29612016-04-26 19:55:41 +0530648 char *mod_name = NULL;
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900649
650 if (!module)
651 return 0;
652
Ravi Bangoria63a29612016-04-26 19:55:41 +0530653 mod_name = find_module_name(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900654
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900655 for (i = 0; i < ntevs; i++) {
Ravi Bangoria63a29612016-04-26 19:55:41 +0530656 tevs[i].point.module =
657 strdup(mod_name ? mod_name : module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900658 if (!tevs[i].point.module) {
659 ret = -ENOMEM;
660 break;
661 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900662 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900663
Ravi Bangoria63a29612016-04-26 19:55:41 +0530664 free(mod_name);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900665 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900666}
667
Ravi Bangoriad8204562016-08-09 01:23:24 -0500668static int
669post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
670 int ntevs)
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000671{
672 struct ref_reloc_sym *reloc_sym;
673 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900674 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000675
Masami Hiramatsu428aff82016-08-26 01:24:42 +0900676 /* Skip post process if the target is an offline kernel */
677 if (symbol_conf.ignore_vmlinux_buildid)
678 return 0;
679
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000680 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000681 if (!reloc_sym) {
682 pr_warning("Relocated base symbol is not found!\n");
683 return -EINVAL;
684 }
685
686 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900687 if (!tevs[i].point.address || tevs[i].point.retprobe)
688 continue;
689 /* If we found a wrong one, mark it by NULL symbol */
690 if (kprobe_warn_out_range(tevs[i].point.symbol,
691 tevs[i].point.address)) {
692 tmp = NULL;
693 skipped++;
694 } else {
695 tmp = strdup(reloc_sym->name);
696 if (!tmp)
697 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000698 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900699 /* If we have no realname, use symbol for it */
700 if (!tevs[i].point.realname)
701 tevs[i].point.realname = tevs[i].point.symbol;
702 else
703 free(tevs[i].point.symbol);
704 tevs[i].point.symbol = tmp;
705 tevs[i].point.offset = tevs[i].point.address -
706 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000707 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900708 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000709}
710
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500711void __weak
712arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
713 int ntevs __maybe_unused)
714{
715}
716
Ravi Bangoriad8204562016-08-09 01:23:24 -0500717/* Post processing the probe events */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500718static int post_process_probe_trace_events(struct perf_probe_event *pev,
719 struct probe_trace_event *tevs,
Ravi Bangoriad8204562016-08-09 01:23:24 -0500720 int ntevs, const char *module,
721 bool uprobe)
722{
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500723 int ret;
724
Ravi Bangoriad8204562016-08-09 01:23:24 -0500725 if (uprobe)
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500726 ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
727 else if (module)
Ravi Bangoriad8204562016-08-09 01:23:24 -0500728 /* Currently ref_reloc_sym based probe is not for drivers */
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500729 ret = add_module_to_probe_trace_events(tevs, ntevs, module);
730 else
731 ret = post_process_kernel_probe_trace_events(tevs, ntevs);
Ravi Bangoriad8204562016-08-09 01:23:24 -0500732
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500733 if (ret >= 0)
734 arch__post_process_probe_trace_events(pev, ntevs);
735
736 return ret;
Ravi Bangoriad8204562016-08-09 01:23:24 -0500737}
738
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300739/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530740static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900741 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300742{
743 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900744 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530745 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900746 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300747
Masami Hiramatsu44225522015-05-08 10:03:28 +0900748 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900749 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000750 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900751 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900752 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300753 return 0;
754 }
755
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000756 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900757 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900758 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900759
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900760 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900761 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900762 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900763 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900764 /*
765 * Write back to the original probe_event for
766 * setting appropriate (user given) event name
767 */
768 clear_perf_probe_point(&pev->point);
769 memcpy(&pev->point, &tmp, sizeof(tmp));
770 }
771 }
772
Masami Hiramatsuff741782011-06-27 16:27:39 +0900773 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300774
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400775 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000776 pr_debug("Found %d probe_trace_events.\n", ntevs);
Ravi Bangoria99e608b2016-08-09 01:23:25 -0500777 ret = post_process_probe_trace_events(pev, *tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900778 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900779 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000780 clear_probe_trace_events(*tevs, ntevs);
781 zfree(tevs);
782 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900783 if (ret != ntevs)
784 return ret < 0 ? ret : ntevs;
785 ntevs = 0;
786 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400787 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300788
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400789 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900790 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400791 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900792 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400793 }
794 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400795 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
Wang Nan1c0bd0e2015-08-21 10:09:02 +0000796 if (ntevs < 0) {
797 if (ntevs == -EBADF)
798 pr_warning("Warning: No dwarf info found in the vmlinux - "
799 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400800 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900801 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400802 return 0;
803 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300804 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400805 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300806}
807
808#define LINEBUF_SIZE 256
809#define NR_ADDITIONAL_LINES 2
810
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100811static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000813 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100814 const char *color = show_num ? "" : PERF_COLOR_BLUE;
815 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300816
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100817 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300818 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
819 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100820 if (skip)
821 continue;
822 if (!prefix) {
823 prefix = show_num ? "%7d " : " ";
824 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300825 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100826 color_fprintf(stdout, color, "%s", buf);
827
828 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400829
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100830 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300831error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100832 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000833 pr_warning("File read error: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300834 str_error_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100835 return -1;
836 }
837 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300838}
839
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100840static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
841{
842 int rv = __show_one_line(fp, l, skip, show_num);
843 if (rv == 0) {
844 pr_warning("Source file is shorter than expected.\n");
845 rv = -1;
846 }
847 return rv;
848}
849
850#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
851#define show_one_line(f,l) _show_one_line(f,l,false,false)
852#define skip_one_line(f,l) _show_one_line(f,l,true,false)
853#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
854
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300855/*
856 * Show line-range always requires debuginfo to find source file and
857 * line number.
858 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900859static int __show_line_range(struct line_range *lr, const char *module,
860 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300861{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400862 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000863 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900864 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300865 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900866 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900867 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000868 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300869
870 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000871 dinfo = open_debuginfo(module, false);
872 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900873 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400874
Masami Hiramatsuff741782011-06-27 16:27:39 +0900875 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900876 if (!ret) { /* Not found, retry with an alternative */
877 ret = get_alternative_line_range(dinfo, lr, module, user);
878 if (!ret)
879 ret = debuginfo__find_line_range(dinfo, lr);
880 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900881 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000882 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400883 pr_warning("Specified source line is not found.\n");
884 return -ENOENT;
885 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000886 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400887 return ret;
888 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300889
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900890 /* Convert source file path */
891 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900892 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800893
894 /* Free old path when new path is assigned */
895 if (tmp != lr->path)
896 free(tmp);
897
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900898 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000899 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900900 return ret;
901 }
902
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300903 setup_pager();
904
905 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900906 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300907 lr->start - lr->offset);
908 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100909 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300910
911 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400912 if (fp == NULL) {
913 pr_warning("Failed to open %s: %s\n", lr->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300914 str_error_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400915 return -errno;
916 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300917 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100918 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100919 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100920 if (ret < 0)
921 goto end;
922 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300923
Arnaldo Carvalho de Melo10daf4d2016-06-23 11:39:19 -0300924 intlist__for_each_entry(ln, lr->line_list) {
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000925 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100926 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100927 if (ret < 0)
928 goto end;
929 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100930 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400931 if (ret < 0)
932 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300933 }
934
935 if (lr->end == INT_MAX)
936 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100937 while (l <= lr->end) {
938 ret = show_one_line_or_eof(fp, l++ - lr->offset);
939 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100940 break;
941 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400942end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300943 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400944 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300945}
946
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000947int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000948{
949 int ret;
950
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900951 ret = init_probe_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000952 if (ret < 0)
953 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900954 ret = __show_line_range(lr, module, user);
Namhyung Kim9bae1e82015-09-10 11:27:05 +0900955 exit_probe_symbol_maps();
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000956
957 return ret;
958}
959
Masami Hiramatsuff741782011-06-27 16:27:39 +0900960static int show_available_vars_at(struct debuginfo *dinfo,
961 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900962 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900963{
964 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900965 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900966 struct str_node *node;
967 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900968 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900969 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900970
971 buf = synthesize_perf_probe_point(&pev->point);
972 if (!buf)
973 return -EINVAL;
974 pr_debug("Searching variables at %s\n", buf);
975
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900976 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900977 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900978 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900979 if (!ret) {
980 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900981 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900982 /* Release the old probe_point */
983 clear_perf_probe_point(&tmp);
984 }
985 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900986 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000987 if (ret == 0 || ret == -ENOENT) {
988 pr_err("Failed to find the address of %s\n", buf);
989 ret = -ENOENT;
990 } else
991 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900992 goto end;
993 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000994
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900995 /* Some variables are found */
996 fprintf(stdout, "Available variables at %s\n", buf);
997 for (i = 0; i < ret; i++) {
998 vl = &vls[i];
999 /*
1000 * A probe point might be converted to
1001 * several trace points.
1002 */
1003 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
1004 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001005 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001006 nvars = 0;
1007 if (vl->vars) {
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001008 strlist__for_each_entry(node, vl->vars) {
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +09001009 var = strchr(node->s, '\t') + 1;
1010 if (strfilter__compare(_filter, var)) {
1011 fprintf(stdout, "\t\t%s\n", node->s);
1012 nvars++;
1013 }
1014 }
1015 strlist__delete(vl->vars);
1016 }
1017 if (nvars == 0)
1018 fprintf(stdout, "\t\t(No matched variables)\n");
1019 }
1020 free(vls);
1021end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001022 free(buf);
1023 return ret;
1024}
1025
1026/* Show available variables on given probe point */
1027int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001028 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001029{
Masami Hiramatsuff741782011-06-27 16:27:39 +09001030 int i, ret = 0;
1031 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001032
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001033 ret = init_probe_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001034 if (ret < 0)
1035 return ret;
1036
Masami Hiramatsu44225522015-05-08 10:03:28 +09001037 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001038 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001039 ret = -ENOENT;
1040 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +09001041 }
1042
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001043 setup_pager();
1044
Masami Hiramatsuff741782011-06-27 16:27:39 +09001045 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001046 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +09001047
1048 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00001049out:
Namhyung Kim9bae1e82015-09-10 11:27:05 +09001050 exit_probe_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001051 return ret;
1052}
1053
Ingo Molnar89fe8082013-09-30 12:07:11 +02001054#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001055
Masami Hiramatsu7737af02015-06-17 23:58:54 +09001056static void debuginfo_cache__exit(void)
1057{
1058}
1059
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001060static int
1061find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
1062 struct perf_probe_point *pp __maybe_unused,
1063 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001064{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001065 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001066}
1067
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301068static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001069 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001070{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001071 if (perf_probe_event_need_dwarf(pev)) {
1072 pr_warning("Debuginfo-analysis is not supported.\n");
1073 return -ENOSYS;
1074 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301075
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001076 return 0;
1077}
1078
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001079int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +00001080 const char *module __maybe_unused,
1081 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001082{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001083 pr_warning("Debuginfo-analysis is not supported.\n");
1084 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001085}
1086
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001087int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001088 int npevs __maybe_unused,
1089 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001090{
1091 pr_warning("Debuginfo-analysis is not supported.\n");
1092 return -ENOSYS;
1093}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001094#endif
1095
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001096void line_range__clear(struct line_range *lr)
1097{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001098 free(lr->function);
1099 free(lr->file);
1100 free(lr->path);
1101 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001102 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001103 memset(lr, 0, sizeof(*lr));
1104}
1105
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001106int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001107{
1108 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001109 lr->line_list = intlist__new(NULL);
1110 if (!lr->line_list)
1111 return -ENOMEM;
1112 else
1113 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001114}
1115
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001116static int parse_line_num(char **ptr, int *val, const char *what)
1117{
1118 const char *start = *ptr;
1119
1120 errno = 0;
1121 *val = strtol(*ptr, ptr, 0);
1122 if (errno || *ptr == start) {
1123 semantic_error("'%s' is not a valid number.\n", what);
1124 return -EINVAL;
1125 }
1126 return 0;
1127}
1128
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001129/* Check the name is good for event, group or function */
1130static bool is_c_func_name(const char *name)
1131{
1132 if (!isalpha(*name) && *name != '_')
1133 return false;
1134 while (*++name != '\0') {
1135 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1136 return false;
1137 }
1138 return true;
1139}
1140
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001141/*
1142 * Stuff 'lr' according to the line range described by 'arg'.
1143 * The line range syntax is described by:
1144 *
1145 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001146 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001147 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001148int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001149{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001150 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001151 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001152
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001153 if (!name)
1154 return -ENOMEM;
1155
1156 lr->start = 0;
1157 lr->end = INT_MAX;
1158
1159 range = strchr(name, ':');
1160 if (range) {
1161 *range++ = '\0';
1162
1163 err = parse_line_num(&range, &lr->start, "start line");
1164 if (err)
1165 goto err;
1166
1167 if (*range == '+' || *range == '-') {
1168 const char c = *range++;
1169
1170 err = parse_line_num(&range, &lr->end, "end line");
1171 if (err)
1172 goto err;
1173
1174 if (c == '+') {
1175 lr->end += lr->start;
1176 /*
1177 * Adjust the number of lines here.
1178 * If the number of lines == 1, the
1179 * the end of line should be equal to
1180 * the start of line.
1181 */
1182 lr->end--;
1183 }
1184 }
1185
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001186 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001187
1188 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001189 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001190 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001191 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001192 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001193 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001194 if (*range != '\0') {
1195 semantic_error("Tailing with invalid str '%s'.\n", range);
1196 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001197 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001198 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001199
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001200 file = strchr(name, '@');
1201 if (file) {
1202 *file = '\0';
1203 lr->file = strdup(++file);
1204 if (lr->file == NULL) {
1205 err = -ENOMEM;
1206 goto err;
1207 }
1208 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001209 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001210 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001211 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001212 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001213 else { /* Invalid name */
1214 semantic_error("'%s' is not a valid function name.\n", name);
1215 err = -EINVAL;
1216 goto err;
1217 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218
1219 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001220err:
1221 free(name);
1222 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001223}
1224
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001225static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
1226{
1227 char *ptr;
1228
1229 ptr = strchr(*arg, ':');
1230 if (ptr) {
1231 *ptr = '\0';
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001232 if (!pev->sdt && !is_c_func_name(*arg))
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001233 goto ng_name;
1234 pev->group = strdup(*arg);
1235 if (!pev->group)
1236 return -ENOMEM;
1237 *arg = ptr + 1;
1238 } else
1239 pev->group = NULL;
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001240 if (!pev->sdt && !is_c_func_name(*arg)) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001241ng_name:
1242 semantic_error("%s is bad for event name -it must "
1243 "follow C symbol-naming rule.\n", *arg);
1244 return -EINVAL;
1245 }
1246 pev->event = strdup(*arg);
1247 if (pev->event == NULL)
1248 return -ENOMEM;
1249
1250 return 0;
1251}
1252
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001253/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001254static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001255{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001256 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257 char *ptr, *tmp;
1258 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301259 bool file_spec = false;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001260 int ret;
1261
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001262 /*
1263 * <Syntax>
Masami Hiramatsu8d993d92016-07-01 17:04:01 +09001264 * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
1265 * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001266 * perf probe %[GRP:]SDT_EVENT
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001267 */
Wang Nane59d29e2015-04-28 08:46:09 +00001268 if (!arg)
1269 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001270
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001271 /*
1272 * If the probe point starts with '%',
1273 * or starts with "sdt_" and has a ':' but no '=',
1274 * then it should be a SDT/cached probe point.
1275 */
1276 if (arg[0] == '%' ||
1277 (!strncmp(arg, "sdt_", 4) &&
1278 !!strchr(arg, ':') && !strchr(arg, '='))) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001279 pev->sdt = true;
Masami Hiramatsu7e9fca52016-07-12 19:05:46 +09001280 if (arg[0] == '%')
1281 arg++;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001282 }
1283
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001284 ptr = strpbrk(arg, ";=@+%");
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001285 if (pev->sdt) {
1286 if (ptr) {
Masami Hiramatsua5981802016-07-12 19:05:37 +09001287 if (*ptr != '@') {
1288 semantic_error("%s must be an SDT name.\n",
1289 arg);
1290 return -EINVAL;
1291 }
1292 /* This must be a target file name or build id */
1293 tmp = build_id_cache__complement(ptr + 1);
1294 if (tmp) {
1295 pev->target = build_id_cache__origname(tmp);
1296 free(tmp);
1297 } else
1298 pev->target = strdup(ptr + 1);
1299 if (!pev->target)
1300 return -ENOMEM;
1301 *ptr = '\0';
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001302 }
1303 ret = parse_perf_probe_event_name(&arg, pev);
1304 if (ret == 0) {
1305 if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
1306 ret = -errno;
1307 }
1308 return ret;
1309 }
1310
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001311 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001312 *ptr = '\0';
1313 tmp = ptr + 1;
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09001314 ret = parse_perf_probe_event_name(&arg, pev);
1315 if (ret < 0)
1316 return ret;
1317
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001318 arg = tmp;
1319 }
1320
Naveen N. Rao3099c022015-04-28 17:35:34 +05301321 /*
1322 * Check arg is function or file name and copy it.
1323 *
1324 * We consider arg to be a file spec if and only if it satisfies
1325 * all of the below criteria::
1326 * - it does not include any of "+@%",
1327 * - it includes one of ":;", and
1328 * - it has a period '.' in the name.
1329 *
1330 * Otherwise, we consider arg to be a function specification.
1331 */
1332 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1333 /* This is a file spec if it includes a '.' before ; or : */
1334 if (memchr(arg, '.', ptr - arg))
1335 file_spec = true;
1336 }
1337
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001338 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001339 if (ptr) {
1340 nc = *ptr;
1341 *ptr++ = '\0';
1342 }
1343
Wang Nan6c6e0242015-08-26 10:57:44 +00001344 if (arg[0] == '\0')
1345 tmp = NULL;
1346 else {
1347 tmp = strdup(arg);
1348 if (tmp == NULL)
1349 return -ENOMEM;
1350 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001351
Naveen N. Rao3099c022015-04-28 17:35:34 +05301352 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001353 pp->file = tmp;
Wang Nanda15bd92015-08-26 10:57:45 +00001354 else {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001355 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001356
Wang Nanda15bd92015-08-26 10:57:45 +00001357 /*
1358 * Keep pp->function even if this is absolute address,
1359 * so it can mark whether abs_address is valid.
1360 * Which make 'perf probe lib.bin 0x0' possible.
1361 *
1362 * Note that checking length of tmp is not needed
1363 * because when we access tmp[1] we know tmp[0] is '0',
1364 * so tmp[1] should always valid (but could be '\0').
1365 */
1366 if (tmp && !strncmp(tmp, "0x", 2)) {
1367 pp->abs_address = strtoul(pp->function, &tmp, 0);
1368 if (*tmp != '\0') {
1369 semantic_error("Invalid absolute address.\n");
1370 return -EINVAL;
1371 }
1372 }
1373 }
1374
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001375 /* Parse other options */
1376 while (ptr) {
1377 arg = ptr;
1378 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001379 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001380 pp->lazy_line = strdup(arg);
1381 if (pp->lazy_line == NULL)
1382 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001383 break;
1384 }
1385 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001386 if (ptr) {
1387 nc = *ptr;
1388 *ptr++ = '\0';
1389 }
1390 switch (c) {
1391 case ':': /* Line number */
1392 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001394 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395 " in line number.\n");
1396 return -EINVAL;
1397 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001398 break;
1399 case '+': /* Byte offset from a symbol */
1400 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001401 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001402 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001403 " in offset.\n");
1404 return -EINVAL;
1405 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001406 break;
1407 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 if (pp->file) {
1409 semantic_error("SRC@SRC is not allowed.\n");
1410 return -EINVAL;
1411 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001412 pp->file = strdup(arg);
1413 if (pp->file == NULL)
1414 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001415 break;
1416 case '%': /* Probe places */
1417 if (strcmp(arg, "return") == 0) {
1418 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001419 } else { /* Others not supported yet */
1420 semantic_error("%%%s is not supported.\n", arg);
1421 return -ENOTSUP;
1422 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001423 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001424 default: /* Buggy case */
1425 pr_err("This program has a bug at %s:%d.\n",
1426 __FILE__, __LINE__);
1427 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001428 break;
1429 }
1430 }
1431
1432 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001433 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001434 semantic_error("Lazy pattern can't be used with"
1435 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001436 return -EINVAL;
1437 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001438
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001439 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001440 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001441 return -EINVAL;
1442 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001443
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001445 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001446 return -EINVAL;
1447 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001448
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001449 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001450 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001451 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001452 return -EINVAL;
1453 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001454
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001455 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001456 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001457 return -EINVAL;
1458 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001459
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001460 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001461 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001462 return -EINVAL;
1463 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001464
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001465 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001466 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001467 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001468 return -EINVAL;
1469 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001470
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001471 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001472 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1473 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001474 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001475}
1476
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001477/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001478static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001479{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001480 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001481 struct perf_probe_arg_field **fieldp;
1482
1483 pr_debug("parsing arg: %s into ", str);
1484
Masami Hiramatsu48481932010-04-12 13:16:53 -04001485 tmp = strchr(str, '=');
1486 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001487 arg->name = strndup(str, tmp - str);
1488 if (arg->name == NULL)
1489 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001490 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001491 str = tmp + 1;
1492 }
1493
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001494 tmp = strchr(str, ':');
1495 if (tmp) { /* Type setting */
1496 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001497 arg->type = strdup(tmp + 1);
1498 if (arg->type == NULL)
1499 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001500 pr_debug("type:%s ", arg->type);
1501 }
1502
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001503 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001504 if (!is_c_varname(str) || !tmp) {
1505 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001506 arg->var = strdup(str);
1507 if (arg->var == NULL)
1508 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001509 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001511 }
1512
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001513 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001514 arg->var = strndup(str, tmp - str);
1515 if (arg->var == NULL)
1516 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001517 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001518 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001519 fieldp = &arg->field;
1520
1521 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001522 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1523 if (*fieldp == NULL)
1524 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001525 if (*tmp == '[') { /* Array */
1526 str = tmp;
1527 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001528 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001529 if (*tmp != ']' || tmp == str + 1) {
1530 semantic_error("Array index must be a"
1531 " number.\n");
1532 return -EINVAL;
1533 }
1534 tmp++;
1535 if (*tmp == '\0')
1536 tmp = NULL;
1537 } else { /* Structure */
1538 if (*tmp == '.') {
1539 str = tmp + 1;
1540 (*fieldp)->ref = false;
1541 } else if (tmp[1] == '>') {
1542 str = tmp + 2;
1543 (*fieldp)->ref = true;
1544 } else {
1545 semantic_error("Argument parse error: %s\n",
1546 str);
1547 return -EINVAL;
1548 }
1549 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001550 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001551 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001552 (*fieldp)->name = strndup(str, tmp - str);
1553 if ((*fieldp)->name == NULL)
1554 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001555 if (*str != '[')
1556 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001557 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1558 fieldp = &(*fieldp)->next;
1559 }
1560 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001561 (*fieldp)->name = strdup(str);
1562 if ((*fieldp)->name == NULL)
1563 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001564 if (*str != '[')
1565 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001566 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001567
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001568 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001569 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001570 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001571 if (arg->name == NULL)
1572 return -ENOMEM;
1573 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001574 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001575}
1576
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001577/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001578int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001579{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001580 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001581 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001582
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001583 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001584 if (!argv) {
1585 pr_debug("Failed to split arguments.\n");
1586 return -ENOMEM;
1587 }
1588 if (argc - 1 > MAX_PROBE_ARGS) {
1589 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1590 ret = -ERANGE;
1591 goto out;
1592 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001593 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001594 ret = parse_perf_probe_point(argv[0], pev);
1595 if (ret < 0)
1596 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001597
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001598 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001599 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001600 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1601 if (pev->args == NULL) {
1602 ret = -ENOMEM;
1603 goto out;
1604 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001605 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1606 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1607 if (ret >= 0 &&
1608 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001609 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001610 " kretprobe.\n");
1611 ret = -EINVAL;
1612 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001613 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001614out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001615 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001616
1617 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001618}
1619
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301620/* Returns true if *any* ARG is either C variable, $params or $vars. */
1621bool perf_probe_with_var(struct perf_probe_event *pev)
1622{
1623 int i = 0;
1624
1625 for (i = 0; i < pev->nargs; i++)
1626 if (is_c_varname(pev->args[i].var) ||
1627 !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
1628 !strcmp(pev->args[i].var, PROBE_ARG_VARS))
1629 return true;
1630 return false;
1631}
1632
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001633/* Return true if this perf_probe_event requires debuginfo */
1634bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001635{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1637 return true;
1638
Ravi Bangoriab3f33f92016-08-03 14:28:44 +05301639 if (perf_probe_with_var(pev))
1640 return true;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001641
1642 return false;
1643}
1644
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301645/* Parse probe_events event into struct probe_point */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09001646int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001647{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301648 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001649 char pr;
1650 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001651 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001652 int ret, i, argc;
1653 char **argv;
1654
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301655 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001656 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001657 if (!argv) {
1658 pr_debug("Failed to split arguments.\n");
1659 return -ENOMEM;
1660 }
1661 if (argc < 2) {
1662 semantic_error("Too few probe arguments.\n");
1663 ret = -ERANGE;
1664 goto out;
1665 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001666
1667 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001668 argv0_str = strdup(argv[0]);
1669 if (argv0_str == NULL) {
1670 ret = -ENOMEM;
1671 goto out;
1672 }
1673 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1674 fmt2_str = strtok_r(NULL, "/", &fmt);
1675 fmt3_str = strtok_r(NULL, " \t", &fmt);
1676 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1677 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001678 semantic_error("Failed to parse event name: %s\n", argv[0]);
1679 ret = -EINVAL;
1680 goto out;
1681 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001682 pr = fmt1_str[0];
1683 tev->group = strdup(fmt2_str);
1684 tev->event = strdup(fmt3_str);
1685 if (tev->group == NULL || tev->event == NULL) {
1686 ret = -ENOMEM;
1687 goto out;
1688 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001689 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001690
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001691 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001692
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001693 /* Scan module name(if there), function name and offset */
1694 p = strchr(argv[1], ':');
1695 if (p) {
1696 tp->module = strndup(argv[1], p - argv[1]);
Masami Hiramatsu844faa42016-06-08 18:29:21 +09001697 if (!tp->module) {
1698 ret = -ENOMEM;
1699 goto out;
1700 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09001701 tev->uprobes = (tp->module[0] == '/');
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001702 p++;
1703 } else
1704 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001705 fmt1_str = strtok_r(p, "+", &fmt);
Wang Nanbe07afe2015-08-26 10:57:43 +00001706 /* only the address started with 0x */
1707 if (fmt1_str[0] == '0') {
1708 /*
1709 * Fix a special case:
1710 * if address == 0, kernel reports something like:
1711 * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
1712 * Newer kernel may fix that, but we want to
1713 * support old kernel also.
1714 */
1715 if (strcmp(fmt1_str, "0x") == 0) {
1716 if (!argv[2] || strcmp(argv[2], "(null)")) {
1717 ret = -EINVAL;
1718 goto out;
1719 }
1720 tp->address = 0;
1721
1722 free(argv[2]);
1723 for (i = 2; argv[i + 1] != NULL; i++)
1724 argv[i] = argv[i + 1];
1725
1726 argv[i] = NULL;
1727 argc -= 1;
1728 } else
1729 tp->address = strtoul(fmt1_str, NULL, 0);
1730 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001731 /* Only the symbol-based probe has offset */
1732 tp->symbol = strdup(fmt1_str);
1733 if (tp->symbol == NULL) {
1734 ret = -ENOMEM;
1735 goto out;
1736 }
1737 fmt2_str = strtok_r(NULL, "", &fmt);
1738 if (fmt2_str == NULL)
1739 tp->offset = 0;
1740 else
1741 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001742 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001743
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301745 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001746 if (tev->args == NULL) {
1747 ret = -ENOMEM;
1748 goto out;
1749 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001750 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001751 p = strchr(argv[i + 2], '=');
1752 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001753 *p++ = '\0';
1754 else
1755 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001756 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001757 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001758 tev->args[i].value = strdup(p);
1759 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1760 ret = -ENOMEM;
1761 goto out;
1762 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001763 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001764 ret = 0;
1765out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001766 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001767 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001768 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001769}
1770
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001771/* Compose only probe arg */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001772char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001773{
1774 struct perf_probe_arg_field *field = pa->field;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001775 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001776 char *ret = NULL;
1777 int err;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001778
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001779 if (strbuf_init(&buf, 64) < 0)
1780 return NULL;
1781
Masami Hiramatsu48481932010-04-12 13:16:53 -04001782 if (pa->name && pa->var)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001783 err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001784 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001785 err = strbuf_addstr(&buf, pa->name ?: pa->var);
1786 if (err)
1787 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001788
1789 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001790 if (field->name[0] == '[')
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001791 err = strbuf_addstr(&buf, field->name);
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001792 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001793 err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
1794 field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001795 field = field->next;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001796 if (err)
1797 goto out;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001798 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001799
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001800 if (pa->type)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001801 if (strbuf_addf(&buf, ":%s", pa->type) < 0)
1802 goto out;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001803
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001804 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001805out:
1806 strbuf_release(&buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001807 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001808}
1809
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001810/* Compose only probe point (not argument) */
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001811char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001812{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001813 struct strbuf buf;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001814 char *tmp, *ret = NULL;
1815 int len, err = 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001816
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001817 if (strbuf_init(&buf, 64) < 0)
1818 return NULL;
1819
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001820 if (pp->function) {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001821 if (strbuf_addstr(&buf, pp->function) < 0)
1822 goto out;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001823 if (pp->offset)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001824 err = strbuf_addf(&buf, "+%lu", pp->offset);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001825 else if (pp->line)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001826 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001827 else if (pp->retprobe)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001828 err = strbuf_addstr(&buf, "%return");
1829 if (err)
1830 goto out;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001831 }
1832 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001833 tmp = pp->file;
1834 len = strlen(tmp);
1835 if (len > 30) {
1836 tmp = strchr(pp->file + len - 30, '/');
1837 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1838 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001839 err = strbuf_addf(&buf, "@%s", tmp);
1840 if (!err && !pp->function && pp->line)
1841 err = strbuf_addf(&buf, ":%d", pp->line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001842 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001843 if (!err)
1844 ret = strbuf_detach(&buf, NULL);
1845out:
1846 strbuf_release(&buf);
1847 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001848}
1849
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001850char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1851{
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001852 struct strbuf buf;
1853 char *tmp, *ret = NULL;
1854 int i;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001855
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001856 if (strbuf_init(&buf, 64))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001857 return NULL;
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001858 if (pev->event)
1859 if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
1860 pev->event) < 0)
1861 goto out;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001862
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001863 tmp = synthesize_perf_probe_point(&pev->point);
1864 if (!tmp || strbuf_addstr(&buf, tmp) < 0)
1865 goto out;
1866 free(tmp);
1867
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001868 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001869 tmp = synthesize_perf_probe_arg(pev->args + i);
1870 if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
1871 goto out;
1872 free(tmp);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001873 }
1874
Masami Hiramatsuc4ff4922016-06-08 18:29:50 +09001875 ret = strbuf_detach(&buf, NULL);
1876out:
1877 strbuf_release(&buf);
1878 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879}
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001880
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301881static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001882 struct strbuf *buf, int depth)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001883{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001884 int err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001885 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301886 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001887 depth + 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001888 if (depth < 0)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001889 return depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001891 err = strbuf_addf(buf, "%+ld(", ref->offset);
1892 return (err < 0) ? err : depth;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893}
1894
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301895static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001896 struct strbuf *buf)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001897{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301898 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001899 int depth = 0, err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900
1901 /* Argument name or separator */
1902 if (arg->name)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001903 err = strbuf_addf(buf, " %s=", arg->name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001904 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001905 err = strbuf_addch(buf, ' ');
1906 if (err)
1907 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001908
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001909 /* Special case: @XXX */
1910 if (arg->value[0] == '@' && arg->ref)
1911 ref = ref->next;
1912
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001913 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001914 if (ref) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001915 depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001916 if (depth < 0)
1917 return depth;
1918 }
1919
1920 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001921 if (arg->value[0] == '@' && arg->ref)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001922 err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001923 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001924 err = strbuf_addstr(buf, arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001925
1926 /* Closing */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001927 while (!err && depth--)
1928 err = strbuf_addch(buf, ')');
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001929
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001930 /* Print argument type */
1931 if (!err && arg->type)
1932 err = strbuf_addf(buf, ":%s", arg->type);
1933
1934 return err;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001935}
1936
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301937char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001938{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301939 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001940 struct strbuf buf;
1941 char *ret = NULL;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001942 int i, err;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001943
Wang Nanda15bd92015-08-26 10:57:45 +00001944 /* Uprobes must have tp->module */
1945 if (tev->uprobes && !tp->module)
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001946 return NULL;
1947
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001948 if (strbuf_init(&buf, 32) < 0)
1949 return NULL;
1950
1951 if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1952 tev->group, tev->event) < 0)
1953 goto error;
Wang Nanda15bd92015-08-26 10:57:45 +00001954 /*
1955 * If tp->address == 0, then this point must be a
1956 * absolute address uprobe.
1957 * try_to_find_absolute_address() should have made
1958 * tp->symbol to "0x0".
1959 */
1960 if (tev->uprobes && !tp->address) {
1961 if (!tp->symbol || strcmp(tp->symbol, "0x0"))
1962 goto error;
1963 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001964
1965 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301966 if (tev->uprobes)
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001967 err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
Wang Nanda15bd92015-08-26 10:57:45 +00001968 else if (!strncmp(tp->symbol, "0x", 2))
1969 /* Absolute address. See try_to_find_absolute_address() */
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001970 err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
1971 tp->module ? ":" : "", tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301972 else
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09001973 err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
1974 tp->module ? ":" : "", tp->symbol, tp->offset);
1975 if (err)
1976 goto error;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301977
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001978 for (i = 0; i < tev->nargs; i++)
1979 if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001980 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001981
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001982 ret = strbuf_detach(&buf, NULL);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001983error:
Masami Hiramatsu909b0362016-04-28 03:37:14 +09001984 strbuf_release(&buf);
1985 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001986}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001987
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001988static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1989 struct perf_probe_point *pp,
1990 bool is_kprobe)
1991{
1992 struct symbol *sym = NULL;
1993 struct map *map;
Wang Nane4863672015-08-25 13:27:35 +00001994 u64 addr = tp->address;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001995 int ret = -ENOENT;
1996
1997 if (!is_kprobe) {
1998 map = dso__new_map(tp->module);
1999 if (!map)
2000 goto out;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002001 sym = map__find_symbol(map, addr);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002002 } else {
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002003 if (tp->symbol && !addr) {
Masami Hiramatsu0a62f682015-11-06 17:30:03 +09002004 if (kernel_get_symbol_address_by_name(tp->symbol,
2005 &addr, true, false) < 0)
Masami Hiramatsu9b239a12015-10-01 01:41:33 +09002006 goto out;
2007 }
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002008 if (addr) {
2009 addr += tp->offset;
2010 sym = __find_kernel_function(addr, &map);
2011 }
2012 }
Wang Nan98d3b252015-11-05 13:19:25 +00002013
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002014 if (!sym)
2015 goto out;
2016
2017 pp->retprobe = tp->retprobe;
2018 pp->offset = addr - map->unmap_ip(map, sym->start);
2019 pp->function = strdup(sym->name);
2020 ret = pp->function ? 0 : -ENOMEM;
2021
2022out:
2023 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002024 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002025 }
2026
2027 return ret;
2028}
2029
2030static int convert_to_perf_probe_point(struct probe_trace_point *tp,
Wang Nanda15bd92015-08-26 10:57:45 +00002031 struct perf_probe_point *pp,
2032 bool is_kprobe)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002033{
2034 char buf[128];
2035 int ret;
2036
2037 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
2038 if (!ret)
2039 return 0;
2040 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
2041 if (!ret)
2042 return 0;
2043
2044 pr_debug("Failed to find probe point from both of dwarf and map.\n");
2045
2046 if (tp->symbol) {
2047 pp->function = strdup(tp->symbol);
2048 pp->offset = tp->offset;
Wang Nan614e2fd2015-08-26 10:57:42 +00002049 } else {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002050 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
2051 if (ret < 0)
2052 return ret;
2053 pp->function = strdup(buf);
2054 pp->offset = 0;
2055 }
2056 if (pp->function == NULL)
2057 return -ENOMEM;
2058
2059 pp->retprobe = tp->retprobe;
2060
2061 return 0;
2062}
2063
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302064static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302065 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002066{
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002067 struct strbuf buf = STRBUF_INIT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002068 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002069
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002070 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002071 pev->event = strdup(tev->event);
2072 pev->group = strdup(tev->group);
2073 if (pev->event == NULL || pev->group == NULL)
2074 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04002075
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002076 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00002077 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002078 if (ret < 0)
2079 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002080
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002081 /* Convert trace_arg to probe_arg */
2082 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002083 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
2084 if (pev->args == NULL)
2085 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002086 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002087 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002088 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002089 else {
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002090 if ((ret = strbuf_init(&buf, 32)) < 0)
2091 goto error;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002092 ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
2093 pev->args[i].name = strbuf_detach(&buf, NULL);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002094 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002095 if (pev->args[i].name == NULL && ret >= 0)
2096 ret = -ENOMEM;
2097 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002098error:
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002099 if (ret < 0)
2100 clear_perf_probe_event(pev);
2101
2102 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002103}
2104
2105void clear_perf_probe_event(struct perf_probe_event *pev)
2106{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002107 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002108 int i;
2109
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002110 free(pev->event);
2111 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09002112 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002113 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002114
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002115 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002116 free(pev->args[i].name);
2117 free(pev->args[i].var);
2118 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002119 field = pev->args[i].field;
2120 while (field) {
2121 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002122 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002123 free(field);
2124 field = next;
2125 }
2126 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002127 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002128 memset(pev, 0, sizeof(*pev));
2129}
2130
Masami Hiramatsu0542bb92016-06-08 18:29:40 +09002131#define strdup_or_goto(str, label) \
2132({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
2133
2134static int perf_probe_point__copy(struct perf_probe_point *dst,
2135 struct perf_probe_point *src)
2136{
2137 dst->file = strdup_or_goto(src->file, out_err);
2138 dst->function = strdup_or_goto(src->function, out_err);
2139 dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
2140 dst->line = src->line;
2141 dst->retprobe = src->retprobe;
2142 dst->offset = src->offset;
2143 return 0;
2144
2145out_err:
2146 clear_perf_probe_point(dst);
2147 return -ENOMEM;
2148}
2149
2150static int perf_probe_arg__copy(struct perf_probe_arg *dst,
2151 struct perf_probe_arg *src)
2152{
2153 struct perf_probe_arg_field *field, **ppfield;
2154
2155 dst->name = strdup_or_goto(src->name, out_err);
2156 dst->var = strdup_or_goto(src->var, out_err);
2157 dst->type = strdup_or_goto(src->type, out_err);
2158
2159 field = src->field;
2160 ppfield = &(dst->field);
2161 while (field) {
2162 *ppfield = zalloc(sizeof(*field));
2163 if (!*ppfield)
2164 goto out_err;
2165 (*ppfield)->name = strdup_or_goto(field->name, out_err);
2166 (*ppfield)->index = field->index;
2167 (*ppfield)->ref = field->ref;
2168 field = field->next;
2169 ppfield = &((*ppfield)->next);
2170 }
2171 return 0;
2172out_err:
2173 return -ENOMEM;
2174}
2175
2176int perf_probe_event__copy(struct perf_probe_event *dst,
2177 struct perf_probe_event *src)
2178{
2179 int i;
2180
2181 dst->event = strdup_or_goto(src->event, out_err);
2182 dst->group = strdup_or_goto(src->group, out_err);
2183 dst->target = strdup_or_goto(src->target, out_err);
2184 dst->uprobes = src->uprobes;
2185
2186 if (perf_probe_point__copy(&dst->point, &src->point) < 0)
2187 goto out_err;
2188
2189 dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
2190 if (!dst->args)
2191 goto out_err;
2192 dst->nargs = src->nargs;
2193
2194 for (i = 0; i < src->nargs; i++)
2195 if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
2196 goto out_err;
2197 return 0;
2198
2199out_err:
2200 clear_perf_probe_event(dst);
2201 return -ENOMEM;
2202}
2203
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002204void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002205{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302206 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002207 int i;
2208
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002209 free(tev->event);
2210 free(tev->group);
2211 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002212 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002213 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002214 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002215 free(tev->args[i].name);
2216 free(tev->args[i].value);
2217 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002218 ref = tev->args[i].ref;
2219 while (ref) {
2220 next = ref->next;
2221 free(ref);
2222 ref = next;
2223 }
2224 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03002225 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002226 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002227}
2228
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002229struct kprobe_blacklist_node {
2230 struct list_head list;
2231 unsigned long start;
2232 unsigned long end;
2233 char *symbol;
2234};
2235
2236static void kprobe_blacklist__delete(struct list_head *blacklist)
2237{
2238 struct kprobe_blacklist_node *node;
2239
2240 while (!list_empty(blacklist)) {
2241 node = list_first_entry(blacklist,
2242 struct kprobe_blacklist_node, list);
2243 list_del(&node->list);
2244 free(node->symbol);
2245 free(node);
2246 }
2247}
2248
2249static int kprobe_blacklist__load(struct list_head *blacklist)
2250{
2251 struct kprobe_blacklist_node *node;
Jiri Olsa4605eab2015-09-02 09:56:43 +02002252 const char *__debugfs = debugfs__mountpoint();
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002253 char buf[PATH_MAX], *p;
2254 FILE *fp;
2255 int ret;
2256
2257 if (__debugfs == NULL)
2258 return -ENOTSUP;
2259
2260 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2261 if (ret < 0)
2262 return ret;
2263
2264 fp = fopen(buf, "r");
2265 if (!fp)
2266 return -errno;
2267
2268 ret = 0;
2269 while (fgets(buf, PATH_MAX, fp)) {
2270 node = zalloc(sizeof(*node));
2271 if (!node) {
2272 ret = -ENOMEM;
2273 break;
2274 }
2275 INIT_LIST_HEAD(&node->list);
2276 list_add_tail(&node->list, blacklist);
2277 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2278 ret = -EINVAL;
2279 break;
2280 }
2281 p = strchr(buf, '\t');
2282 if (p) {
2283 p++;
2284 if (p[strlen(p) - 1] == '\n')
2285 p[strlen(p) - 1] = '\0';
2286 } else
2287 p = (char *)"unknown";
2288 node->symbol = strdup(p);
2289 if (!node->symbol) {
2290 ret = -ENOMEM;
2291 break;
2292 }
2293 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2294 node->start, node->end, node->symbol);
2295 ret++;
2296 }
2297 if (ret < 0)
2298 kprobe_blacklist__delete(blacklist);
2299 fclose(fp);
2300
2301 return ret;
2302}
2303
2304static struct kprobe_blacklist_node *
2305kprobe_blacklist__find_by_address(struct list_head *blacklist,
2306 unsigned long address)
2307{
2308 struct kprobe_blacklist_node *node;
2309
2310 list_for_each_entry(node, blacklist, list) {
2311 if (node->start <= address && address <= node->end)
2312 return node;
2313 }
2314
2315 return NULL;
2316}
2317
Masami Hiramatsub0312202015-06-16 20:50:55 +09002318static LIST_HEAD(kprobe_blacklist);
2319
2320static void kprobe_blacklist__init(void)
2321{
2322 if (!list_empty(&kprobe_blacklist))
2323 return;
2324
2325 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2326 pr_debug("No kprobe blacklist support, ignored\n");
2327}
2328
2329static void kprobe_blacklist__release(void)
2330{
2331 kprobe_blacklist__delete(&kprobe_blacklist);
2332}
2333
2334static bool kprobe_blacklist__listed(unsigned long address)
2335{
2336 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2337}
2338
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002339static int perf_probe_event__sprintf(const char *group, const char *event,
2340 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002341 const char *module,
2342 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002343{
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002344 int i, ret;
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002345 char *buf;
2346
2347 if (asprintf(&buf, "%s:%s", group, event) < 0)
2348 return -errno;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002349 ret = strbuf_addf(result, " %-20s (on ", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002350 free(buf);
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002351 if (ret)
2352 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002353
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002354 /* Synthesize only event probe point */
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002355 buf = synthesize_perf_probe_point(&pev->point);
2356 if (!buf)
2357 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002358 ret = strbuf_addstr(result, buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002359 free(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002360
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002361 if (!ret && module)
2362 ret = strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002363
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002364 if (!ret && pev->nargs > 0) {
2365 ret = strbuf_add(result, " with", 5);
2366 for (i = 0; !ret && i < pev->nargs; i++) {
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002367 buf = synthesize_perf_probe_arg(&pev->args[i]);
2368 if (!buf)
2369 return -ENOMEM;
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002370 ret = strbuf_addf(result, " %s", buf);
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002371 free(buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002372 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002373 }
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002374 if (!ret)
2375 ret = strbuf_addch(result, ')');
Masami Hiramatsu909b0362016-04-28 03:37:14 +09002376
Masami Hiramatsubf4d5f22016-05-10 14:47:07 +09002377 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002378}
2379
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002380/* Show an event */
Namhyung Kimb02137c2015-09-04 21:16:01 +09002381int show_perf_probe_event(const char *group, const char *event,
2382 struct perf_probe_event *pev,
2383 const char *module, bool use_stdout)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002384{
2385 struct strbuf buf = STRBUF_INIT;
2386 int ret;
2387
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002388 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002389 if (ret >= 0) {
2390 if (use_stdout)
2391 printf("%s\n", buf.buf);
2392 else
2393 pr_info("%s\n", buf.buf);
2394 }
2395 strbuf_release(&buf);
2396
2397 return ret;
2398}
2399
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002400static bool filter_probe_trace_event(struct probe_trace_event *tev,
2401 struct strfilter *filter)
2402{
2403 char tmp[128];
2404
2405 /* At first, check the event name itself */
2406 if (strfilter__compare(filter, tev->event))
2407 return true;
2408
2409 /* Next, check the combination of name and group */
2410 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2411 return false;
2412 return strfilter__compare(filter, tmp);
2413}
2414
2415static int __show_perf_probe_events(int fd, bool is_kprobe,
2416 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002417{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302418 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302419 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002420 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002421 struct strlist *rawlist;
2422 struct str_node *ent;
2423
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002424 memset(&tev, 0, sizeof(tev));
2425 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002426
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002427 rawlist = probe_file__get_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002428 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002429 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002430
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03002431 strlist__for_each_entry(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302432 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002433 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002434 if (!filter_probe_trace_event(&tev, filter))
2435 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302436 ret = convert_to_perf_probe_event(&tev, &pev,
2437 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002438 if (ret < 0)
2439 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002440 ret = show_perf_probe_event(pev.group, pev.event,
2441 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002442 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002444next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002445 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302446 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447 if (ret < 0)
2448 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002449 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002450 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002451 /* Cleanup cached debuginfo if needed */
2452 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002453
2454 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002455}
2456
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302457/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002458int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302459{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002460 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302461
2462 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302463
Masami Hiramatsu1f3736c2016-07-01 17:03:26 +09002464 if (probe_conf.cache)
2465 return probe_cache__show_all_caches(filter);
2466
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002467 ret = init_probe_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302468 if (ret < 0)
2469 return ret;
2470
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002471 ret = probe_file__open_both(&kp_fd, &up_fd, 0);
2472 if (ret < 0)
2473 return ret;
2474
2475 if (kp_fd >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002476 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002477 if (up_fd >= 0 && ret >= 0)
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002478 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002479 if (kp_fd > 0)
2480 close(kp_fd);
2481 if (up_fd > 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002482 close(up_fd);
Namhyung Kim9bae1e82015-09-10 11:27:05 +09002483 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302484
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002485 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002486}
2487
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002488static int get_new_event_name(char *buf, size_t len, const char *base,
2489 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002490{
2491 int i, ret;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002492 char *p, *nbase;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002493
Naveen N. Rao3099c022015-04-28 17:35:34 +05302494 if (*base == '.')
2495 base++;
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002496 nbase = strdup(base);
2497 if (!nbase)
2498 return -ENOMEM;
Naveen N. Rao3099c022015-04-28 17:35:34 +05302499
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002500 /* Cut off the dot suffixes (e.g. .const, .isra)*/
2501 p = strchr(nbase, '.');
2502 if (p && p != nbase)
2503 *p = '\0';
2504
2505 /* Try no suffix number */
2506 ret = e_snprintf(buf, len, "%s", nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002507 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002508 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002509 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002510 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002511 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002512 goto out;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002513
Masami Hiramatsud761b082009-12-15 10:32:25 -05002514 if (!allow_suffix) {
Wang Nan03e01f52015-11-16 12:10:08 +00002515 pr_warning("Error: event \"%s\" already exists.\n"
2516 " Hint: Remove existing event by 'perf probe -d'\n"
2517 " or force duplicates by 'perf probe -f'\n"
2518 " or set 'force=yes' in BPF source.\n",
2519 buf);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002520 ret = -EEXIST;
2521 goto out;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002522 }
2523
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002524 /* Try to add suffix */
2525 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002526 ret = e_snprintf(buf, len, "%s_%d", nbase, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002527 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002528 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002529 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002530 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002531 if (!strlist__has_entry(namelist, buf))
2532 break;
2533 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002534 if (i == MAX_EVENT_INDEX) {
2535 pr_warning("Too many events are on the same function.\n");
2536 ret = -ERANGE;
2537 }
2538
Masami Hiramatsu663b1152015-10-01 01:41:30 +09002539out:
2540 free(nbase);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002541 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002542}
2543
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002544/* Warn if the current kernel's uprobe implementation is old */
2545static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2546{
2547 int i;
2548 char *buf = synthesize_probe_trace_command(tev);
2549
2550 /* Old uprobe event doesn't support memory dereference */
2551 if (!tev->uprobes || tev->nargs == 0 || !buf)
2552 goto out;
2553
2554 for (i = 0; i < tev->nargs; i++)
2555 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2556 pr_warning("Please upgrade your kernel to at least "
2557 "3.14 to have access to feature %s\n",
2558 tev->args[i].value);
2559 break;
2560 }
2561out:
2562 free(buf);
2563}
2564
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002565/* Set new name from original perf_probe_event and namelist */
2566static int probe_trace_event__set_name(struct probe_trace_event *tev,
2567 struct perf_probe_event *pev,
2568 struct strlist *namelist,
2569 bool allow_suffix)
2570{
2571 const char *event, *group;
2572 char buf[64];
2573 int ret;
2574
Masami Hiramatsubc062232016-07-01 17:03:12 +09002575 /* If probe_event or trace_event already have the name, reuse it */
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002576 if (pev->event && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002577 event = pev->event;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002578 else if (tev->event)
2579 event = tev->event;
2580 else {
2581 /* Or generate new one from probe point */
Wang Nanda15bd92015-08-26 10:57:45 +00002582 if (pev->point.function &&
2583 (strncmp(pev->point.function, "0x", 2) != 0) &&
2584 !strisglob(pev->point.function))
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002585 event = pev->point.function;
2586 else
2587 event = tev->point.realname;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002588 }
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002589 if (pev->group && !pev->sdt)
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002590 group = pev->group;
Masami Hiramatsubc062232016-07-01 17:03:12 +09002591 else if (tev->group)
2592 group = tev->group;
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002593 else
2594 group = PERFPROBE_GROUP;
2595
2596 /* Get an unused new event name */
2597 ret = get_new_event_name(buf, 64, event,
2598 namelist, allow_suffix);
2599 if (ret < 0)
2600 return ret;
2601
2602 event = buf;
2603
2604 tev->event = strdup(event);
2605 tev->group = strdup(group);
2606 if (tev->event == NULL || tev->group == NULL)
2607 return -ENOMEM;
2608
2609 /* Add added event name to namelist */
2610 strlist__add(namelist, event);
2611 return 0;
2612}
2613
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002614static int __open_probe_file_and_namelist(bool uprobe,
2615 struct strlist **namelist)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002616{
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002617 int fd;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002618
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002619 fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09002620 if (fd < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002621 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002622
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002623 /* Get current event names */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002624 *namelist = probe_file__get_namelist(fd);
2625 if (!(*namelist)) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002626 pr_debug("Failed to get current event list.\n");
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002627 close(fd);
2628 return -ENOMEM;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002629 }
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002630 return fd;
2631}
2632
2633static int __add_probe_trace_events(struct perf_probe_event *pev,
2634 struct probe_trace_event *tevs,
2635 int ntevs, bool allow_suffix)
2636{
2637 int i, fd[2] = {-1, -1}, up, ret;
2638 struct probe_trace_event *tev = NULL;
2639 struct probe_cache *cache = NULL;
2640 struct strlist *namelist[2] = {NULL, NULL};
2641
2642 up = pev->uprobes ? 1 : 0;
2643 fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
2644 if (fd[up] < 0)
2645 return fd[up];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002646
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002647 ret = 0;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002648 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002649 tev = &tevs[i];
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002650 up = tev->uprobes ? 1 : 0;
2651 if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
2652 fd[up] = __open_probe_file_and_namelist(up,
2653 &namelist[up]);
2654 if (fd[up] < 0)
2655 goto close_out;
2656 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002657 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsubc062232016-07-01 17:03:12 +09002658 if (!tev->point.symbol && !pev->uprobes)
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002659 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002660
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002661 /* Set new name for tev (and update namelist) */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002662 ret = probe_trace_event__set_name(tev, pev, namelist[up],
Masami Hiramatsua3c9de62015-07-15 18:14:00 +09002663 allow_suffix);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002664 if (ret < 0)
2665 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002666
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002667 ret = probe_file__add_event(fd[up], tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002668 if (ret < 0)
2669 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002670
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002671 /*
2672 * Probes after the first probe which comes from same
2673 * user input are always allowed to add suffix, because
2674 * there might be several addresses corresponding to
2675 * one code line.
2676 */
2677 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002678 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002679 if (ret == -EINVAL && pev->uprobes)
2680 warn_uprobe_event_compat(tev);
Masami Hiramatsu2fd457a2016-06-15 12:28:40 +09002681 if (ret == 0 && probe_conf.cache) {
2682 cache = probe_cache__new(pev->target);
2683 if (!cache ||
2684 probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
2685 probe_cache__commit(cache) < 0)
2686 pr_warning("Failed to add event to probe cache\n");
2687 probe_cache__delete(cache);
2688 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002690close_out:
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09002691 for (up = 0; up < 2; up++) {
2692 strlist__delete(namelist[up]);
2693 if (fd[up] >= 0)
2694 close(fd[up]);
2695 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002697}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002698
Wang Nan6bb536c2015-05-29 09:45:47 +00002699static int find_probe_functions(struct map *map, char *name,
2700 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002701{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002702 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002703 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002704 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002705
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002706 if (map__load(map) < 0)
Wang Nan75e4a2a2015-05-15 12:14:44 +00002707 return 0;
2708
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002709 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002710 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002711 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002712 if (syms && found < probe_conf.max_probes)
2713 syms[found - 1] = sym;
2714 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002715 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002716
2717 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002718}
2719
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302720void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2721 struct probe_trace_event *tev __maybe_unused,
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302722 struct map *map __maybe_unused,
2723 struct symbol *sym __maybe_unused) { }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302724
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002725/*
2726 * Find probe function addresses from map.
2727 * Return an error or the number of found probe_trace_event
2728 */
2729static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002730 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002731{
2732 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002733 struct ref_reloc_sym *reloc_sym = NULL;
2734 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002735 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002736 struct probe_trace_event *tev;
2737 struct perf_probe_point *pp = &pev->point;
2738 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002739 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002740 int ret, i, j, skipped = 0;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302741 char *mod_name;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002742
Masami Hiramatsu44225522015-05-08 10:03:28 +09002743 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002744 if (!map) {
2745 ret = -EINVAL;
2746 goto out;
2747 }
2748
Wang Nan6bb536c2015-05-29 09:45:47 +00002749 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2750 if (!syms) {
2751 ret = -ENOMEM;
2752 goto out;
2753 }
2754
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002755 /*
2756 * Load matched symbols: Since the different local symbols may have
2757 * same name but different addresses, this lists all the symbols.
2758 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002759 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002760 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002761 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002762 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002763 ret = -ENOENT;
2764 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002765 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002766 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002767 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002768 ret = -E2BIG;
2769 goto out;
2770 }
2771
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002772 /* Note that the symbols in the kmodule are not relocated */
2773 if (!pev->uprobes && !pp->retprobe && !pev->target) {
He Kuang0560a0c2015-03-20 09:56:56 +08002774 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002775 if (!reloc_sym) {
2776 pr_warning("Relocated base symbol is not found!\n");
2777 ret = -EINVAL;
2778 goto out;
2779 }
2780 }
2781
2782 /* Setup result trace-probe-events */
2783 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2784 if (!*tevs) {
2785 ret = -ENOMEM;
2786 goto out;
2787 }
2788
2789 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002790
Wang Nan6bb536c2015-05-29 09:45:47 +00002791 for (j = 0; j < num_matched_functions; j++) {
2792 sym = syms[j];
2793
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002794 tev = (*tevs) + ret;
2795 tp = &tev->point;
2796 if (ret == num_matched_functions) {
2797 pr_warning("Too many symbols are listed. Skip it.\n");
2798 break;
2799 }
2800 ret++;
2801
2802 if (pp->offset > sym->end - sym->start) {
2803 pr_warning("Offset %ld is bigger than the size of %s\n",
2804 pp->offset, sym->name);
2805 ret = -ENOENT;
2806 goto err_out;
2807 }
2808 /* Add one probe point */
2809 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsu1a8ac292015-10-02 21:58:32 +09002810
2811 /* Check the kprobe (not in module) is within .text */
2812 if (!pev->uprobes && !pev->target &&
Masami Hiramatsub0312202015-06-16 20:50:55 +09002813 kprobe_warn_out_range(sym->name, tp->address)) {
2814 tp->symbol = NULL; /* Skip it */
2815 skipped++;
2816 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002817 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2818 tp->offset = tp->address - reloc_sym->addr;
2819 } else {
2820 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2821 tp->offset = pp->offset;
2822 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002823 tp->realname = strdup_or_goto(sym->name, nomem_out);
2824
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002825 tp->retprobe = pp->retprobe;
Ravi Bangoriac61fb952016-04-26 19:55:40 +05302826 if (pev->target) {
2827 if (pev->uprobes) {
2828 tev->point.module = strdup_or_goto(pev->target,
2829 nomem_out);
2830 } else {
2831 mod_name = find_module_name(pev->target);
2832 tev->point.module =
2833 strdup(mod_name ? mod_name : pev->target);
2834 free(mod_name);
2835 if (!tev->point.module)
2836 goto nomem_out;
2837 }
2838 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002839 tev->uprobes = pev->uprobes;
2840 tev->nargs = pev->nargs;
2841 if (tev->nargs) {
2842 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2843 tev->nargs);
2844 if (tev->args == NULL)
2845 goto nomem_out;
2846 }
2847 for (i = 0; i < tev->nargs; i++) {
2848 if (pev->args[i].name)
2849 tev->args[i].name =
2850 strdup_or_goto(pev->args[i].name,
2851 nomem_out);
2852
2853 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2854 nomem_out);
2855 if (pev->args[i].type)
2856 tev->args[i].type =
2857 strdup_or_goto(pev->args[i].type,
2858 nomem_out);
2859 }
Naveen N. Rao0b3c2262016-04-12 14:40:50 +05302860 arch__fix_tev_from_maps(pev, tev, map, sym);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002861 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002862 if (ret == skipped) {
2863 ret = -ENOENT;
2864 goto err_out;
2865 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002866
2867out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002868 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002869 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002870 return ret;
2871
2872nomem_out:
2873 ret = -ENOMEM;
2874err_out:
2875 clear_probe_trace_events(*tevs, num_matched_functions);
2876 zfree(tevs);
2877 goto out;
2878}
2879
Wang Nanda15bd92015-08-26 10:57:45 +00002880static int try_to_find_absolute_address(struct perf_probe_event *pev,
2881 struct probe_trace_event **tevs)
2882{
2883 struct perf_probe_point *pp = &pev->point;
2884 struct probe_trace_event *tev;
2885 struct probe_trace_point *tp;
2886 int i, err;
2887
2888 if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
2889 return -EINVAL;
2890 if (perf_probe_event_need_dwarf(pev))
2891 return -EINVAL;
2892
2893 /*
2894 * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
2895 * absolute address.
2896 *
2897 * Only one tev can be generated by this.
2898 */
2899 *tevs = zalloc(sizeof(*tev));
2900 if (!*tevs)
2901 return -ENOMEM;
2902
2903 tev = *tevs;
2904 tp = &tev->point;
2905
2906 /*
2907 * Don't use tp->offset, use address directly, because
2908 * in synthesize_probe_trace_command() address cannot be
2909 * zero.
2910 */
2911 tp->address = pev->point.abs_address;
2912 tp->retprobe = pp->retprobe;
2913 tev->uprobes = pev->uprobes;
2914
2915 err = -ENOMEM;
2916 /*
2917 * Give it a '0x' leading symbol name.
2918 * In __add_probe_trace_events, a NULL symbol is interpreted as
2919 * invalud.
2920 */
2921 if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
2922 goto errout;
2923
2924 /* For kprobe, check range */
2925 if ((!tev->uprobes) &&
2926 (kprobe_warn_out_range(tev->point.symbol,
2927 tev->point.address))) {
2928 err = -EACCES;
2929 goto errout;
2930 }
2931
2932 if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
2933 goto errout;
2934
2935 if (pev->target) {
2936 tp->module = strdup(pev->target);
2937 if (!tp->module)
2938 goto errout;
2939 }
2940
2941 if (tev->group) {
2942 tev->group = strdup(pev->group);
2943 if (!tev->group)
2944 goto errout;
2945 }
2946
2947 if (pev->event) {
2948 tev->event = strdup(pev->event);
2949 if (!tev->event)
2950 goto errout;
2951 }
2952
2953 tev->nargs = pev->nargs;
2954 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
2955 if (!tev->args) {
2956 err = -ENOMEM;
2957 goto errout;
2958 }
2959 for (i = 0; i < tev->nargs; i++)
2960 copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
2961
2962 return 1;
2963
2964errout:
2965 if (*tevs) {
2966 clear_probe_trace_events(*tevs, 1);
2967 *tevs = NULL;
2968 }
2969 return err;
2970}
2971
Masami Hiramatsu42bba262016-07-12 19:05:18 +09002972/* Concatinate two arrays */
2973static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
2974{
2975 void *ret;
2976
2977 ret = malloc(sz_a + sz_b);
2978 if (ret) {
2979 memcpy(ret, a, sz_a);
2980 memcpy(ret + sz_a, b, sz_b);
2981 }
2982 return ret;
2983}
2984
2985static int
2986concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
2987 struct probe_trace_event **tevs2, int ntevs2)
2988{
2989 struct probe_trace_event *new_tevs;
2990 int ret = 0;
2991
2992 if (ntevs == 0) {
2993 *tevs = *tevs2;
2994 *ntevs = ntevs2;
2995 *tevs2 = NULL;
2996 return 0;
2997 }
2998
2999 if (*ntevs + ntevs2 > probe_conf.max_probes)
3000 ret = -E2BIG;
3001 else {
3002 /* Concatinate the array of probe_trace_event */
3003 new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
3004 *tevs2, ntevs2 * sizeof(**tevs2));
3005 if (!new_tevs)
3006 ret = -ENOMEM;
3007 else {
3008 free(*tevs);
3009 *tevs = new_tevs;
3010 *ntevs += ntevs2;
3011 }
3012 }
3013 if (ret < 0)
3014 clear_probe_trace_events(*tevs2, ntevs2);
3015 zfree(tevs2);
3016
3017 return ret;
3018}
3019
3020/*
3021 * Try to find probe_trace_event from given probe caches. Return the number
3022 * of cached events found, if an error occurs return the error.
3023 */
3024static int find_cached_events(struct perf_probe_event *pev,
3025 struct probe_trace_event **tevs,
3026 const char *target)
3027{
3028 struct probe_cache *cache;
3029 struct probe_cache_entry *entry;
3030 struct probe_trace_event *tmp_tevs = NULL;
3031 int ntevs = 0;
3032 int ret = 0;
3033
3034 cache = probe_cache__new(target);
3035 /* Return 0 ("not found") if the target has no probe cache. */
3036 if (!cache)
3037 return 0;
3038
3039 for_each_probe_cache_entry(entry, cache) {
3040 /* Skip the cache entry which has no name */
3041 if (!entry->pev.event || !entry->pev.group)
3042 continue;
3043 if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
3044 strglobmatch(entry->pev.event, pev->event)) {
3045 ret = probe_cache_entry__get_event(entry, &tmp_tevs);
3046 if (ret > 0)
3047 ret = concat_probe_trace_events(tevs, &ntevs,
3048 &tmp_tevs, ret);
3049 if (ret < 0)
3050 break;
3051 }
3052 }
3053 probe_cache__delete(cache);
3054 if (ret < 0) {
3055 clear_probe_trace_events(*tevs, ntevs);
3056 zfree(tevs);
3057 } else {
3058 ret = ntevs;
3059 if (ntevs > 0 && target && target[0] == '/')
3060 pev->uprobes = true;
3061 }
3062
3063 return ret;
3064}
3065
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003066/* Try to find probe_trace_event from all probe caches */
3067static int find_cached_events_all(struct perf_probe_event *pev,
3068 struct probe_trace_event **tevs)
3069{
3070 struct probe_trace_event *tmp_tevs = NULL;
3071 struct strlist *bidlist;
3072 struct str_node *nd;
3073 char *pathname;
3074 int ntevs = 0;
3075 int ret;
3076
3077 /* Get the buildid list of all valid caches */
3078 bidlist = build_id_cache__list_all(true);
3079 if (!bidlist) {
3080 ret = -errno;
3081 pr_debug("Failed to get buildids: %d\n", ret);
3082 return ret;
3083 }
3084
3085 ret = 0;
3086 strlist__for_each_entry(nd, bidlist) {
3087 pathname = build_id_cache__origname(nd->s);
3088 ret = find_cached_events(pev, &tmp_tevs, pathname);
3089 /* In the case of cnt == 0, we just skip it */
3090 if (ret > 0)
3091 ret = concat_probe_trace_events(tevs, &ntevs,
3092 &tmp_tevs, ret);
3093 free(pathname);
3094 if (ret < 0)
3095 break;
3096 }
3097 strlist__delete(bidlist);
3098
3099 if (ret < 0) {
3100 clear_probe_trace_events(*tevs, ntevs);
3101 zfree(tevs);
3102 } else
3103 ret = ntevs;
3104
3105 return ret;
3106}
3107
Masami Hiramatsubc062232016-07-01 17:03:12 +09003108static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
3109 struct probe_trace_event **tevs)
3110{
3111 struct probe_cache *cache;
3112 struct probe_cache_entry *entry;
3113 struct probe_trace_event *tev;
3114 struct str_node *node;
3115 int ret, i;
3116
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003117 if (pev->sdt) {
Masami Hiramatsu42bba262016-07-12 19:05:18 +09003118 /* For SDT/cached events, we use special search functions */
Masami Hiramatsu1de7b8b2016-07-12 19:05:28 +09003119 if (!pev->target)
3120 return find_cached_events_all(pev, tevs);
3121 else
3122 return find_cached_events(pev, tevs, pev->target);
3123 }
Masami Hiramatsubc062232016-07-01 17:03:12 +09003124 cache = probe_cache__new(pev->target);
3125 if (!cache)
3126 return 0;
3127
3128 entry = probe_cache__find(cache, pev);
3129 if (!entry) {
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003130 /* SDT must be in the cache */
3131 ret = pev->sdt ? -ENOENT : 0;
Masami Hiramatsubc062232016-07-01 17:03:12 +09003132 goto out;
3133 }
3134
3135 ret = strlist__nr_entries(entry->tevlist);
3136 if (ret > probe_conf.max_probes) {
3137 pr_debug("Too many entries matched in the cache of %s\n",
3138 pev->target ? : "kernel");
3139 ret = -E2BIG;
3140 goto out;
3141 }
3142
3143 *tevs = zalloc(ret * sizeof(*tev));
3144 if (!*tevs) {
3145 ret = -ENOMEM;
3146 goto out;
3147 }
3148
3149 i = 0;
3150 strlist__for_each_entry(node, entry->tevlist) {
3151 tev = &(*tevs)[i++];
3152 ret = parse_probe_trace_command(node->s, tev);
3153 if (ret < 0)
3154 goto out;
3155 /* Set the uprobes attribute as same as original */
3156 tev->uprobes = pev->uprobes;
3157 }
3158 ret = i;
3159
3160out:
3161 probe_cache__delete(cache);
3162 return ret;
3163}
3164
Srikar Dronamraju0e608362010-07-29 19:43:51 +05303165static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003166 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003167{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00003168 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003169
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003170 if (!pev->group && !pev->sdt) {
Masami Hiramatsu2a12ec12016-04-26 18:04:13 +09003171 /* Set group name if not given */
3172 if (!pev->uprobes) {
3173 pev->group = strdup(PERFPROBE_GROUP);
3174 ret = pev->group ? 0 : -ENOMEM;
3175 } else
3176 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00003177 if (ret != 0) {
3178 pr_warning("Failed to make a group name.\n");
3179 return ret;
3180 }
3181 }
3182
Wang Nanda15bd92015-08-26 10:57:45 +00003183 ret = try_to_find_absolute_address(pev, tevs);
3184 if (ret > 0)
3185 return ret;
3186
Masami Hiramatsubc062232016-07-01 17:03:12 +09003187 /* At first, we need to lookup cache entry */
3188 ret = find_probe_trace_events_from_cache(pev, tevs);
Masami Hiramatsu36a009f2016-07-12 19:04:43 +09003189 if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
3190 return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
Masami Hiramatsubc062232016-07-01 17:03:12 +09003191
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03003192 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003193 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04003194 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09003195 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003196
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003197 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003198}
3199
Wang Nan12fae5e2015-09-04 21:16:00 +09003200int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003201{
Namhyung Kim844dffa2015-09-04 21:15:59 +09003202 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003203
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003204 /* Loop 1: convert all events */
3205 for (i = 0; i < npevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +09003206 /* Init kprobe blacklist if needed */
Wang Nan12fae5e2015-09-04 21:16:00 +09003207 if (!pevs[i].uprobes)
Masami Hiramatsub0312202015-06-16 20:50:55 +09003208 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003209 /* Convert with or without debuginfo */
Wang Nan12fae5e2015-09-04 21:16:00 +09003210 ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003211 if (ret < 0)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003212 return ret;
Wang Nan12fae5e2015-09-04 21:16:00 +09003213 pevs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003214 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09003215 /* This just release blacklist only if allocated */
3216 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003217
Namhyung Kim844dffa2015-09-04 21:15:59 +09003218 return 0;
3219}
3220
Masami Hiramatsu1c20b1d2016-08-26 01:24:27 +09003221static int show_probe_trace_event(struct probe_trace_event *tev)
3222{
3223 char *buf = synthesize_probe_trace_command(tev);
3224
3225 if (!buf) {
3226 pr_debug("Failed to synthesize probe trace event.\n");
3227 return -EINVAL;
3228 }
3229
3230 /* Showing definition always go stdout */
3231 printf("%s\n", buf);
3232 free(buf);
3233
3234 return 0;
3235}
3236
3237int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
3238{
3239 struct strlist *namelist = strlist__new(NULL, NULL);
3240 struct probe_trace_event *tev;
3241 struct perf_probe_event *pev;
3242 int i, j, ret = 0;
3243
3244 if (!namelist)
3245 return -ENOMEM;
3246
3247 for (j = 0; j < npevs && !ret; j++) {
3248 pev = &pevs[j];
3249 for (i = 0; i < pev->ntevs && !ret; i++) {
3250 tev = &pev->tevs[i];
3251 /* Skip if the symbol is out of .text or blacklisted */
3252 if (!tev->point.symbol && !pev->uprobes)
3253 continue;
3254
3255 /* Set new name for tev (and update namelist) */
3256 ret = probe_trace_event__set_name(tev, pev,
3257 namelist, true);
3258 if (!ret)
3259 ret = show_probe_trace_event(tev);
3260 }
3261 }
3262 strlist__delete(namelist);
3263
3264 return ret;
3265}
3266
Wang Nan12fae5e2015-09-04 21:16:00 +09003267int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003268{
3269 int i, ret = 0;
3270
Masami Hiramatsu4235b042010-03-16 18:06:12 -04003271 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03003272 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003273 ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
3274 pevs[i].ntevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09003275 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03003276 if (ret < 0)
3277 break;
3278 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003279 return ret;
3280}
3281
Wang Nan12fae5e2015-09-04 21:16:00 +09003282void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Namhyung Kim844dffa2015-09-04 21:15:59 +09003283{
3284 int i, j;
3285
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003286 /* Loop 3: cleanup and free trace events */
3287 for (i = 0; i < npevs; i++) {
Wang Nan12fae5e2015-09-04 21:16:00 +09003288 for (j = 0; j < pevs[i].ntevs; j++)
3289 clear_probe_trace_event(&pevs[i].tevs[j]);
3290 zfree(&pevs[i].tevs);
3291 pevs[i].ntevs = 0;
Namhyung Kima43aac22015-09-10 11:27:04 +09003292 clear_perf_probe_event(&pevs[i]);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09003293 }
Namhyung Kim844dffa2015-09-04 21:15:59 +09003294}
3295
3296int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
3297{
3298 int ret;
Namhyung Kim844dffa2015-09-04 21:15:59 +09003299
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003300 ret = init_probe_symbol_maps(pevs->uprobes);
3301 if (ret < 0)
3302 return ret;
3303
Wang Nan12fae5e2015-09-04 21:16:00 +09003304 ret = convert_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003305 if (ret == 0)
Wang Nan12fae5e2015-09-04 21:16:00 +09003306 ret = apply_perf_probe_events(pevs, npevs);
Namhyung Kim844dffa2015-09-04 21:15:59 +09003307
Wang Nan12fae5e2015-09-04 21:16:00 +09003308 cleanup_perf_probe_events(pevs, npevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003309
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003310 exit_probe_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003311 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04003312}
3313
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003314int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05003315{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003316 int ret, ret2, ufd = -1, kfd = -1;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003317 char *str = strfilter__string(filter);
3318
3319 if (!str)
3320 return -EINVAL;
3321
Masami Hiramatsufa282442009-12-08 17:03:23 -05003322 /* Get current event names */
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003323 ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
3324 if (ret < 0)
3325 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303326
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003327 ret = probe_file__del_events(kfd, filter);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003328 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303329 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003330
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003331 ret2 = probe_file__del_events(ufd, filter);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003332 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003333 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003334 goto error;
3335 }
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09003336 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303337
3338error:
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003339 if (kfd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303340 close(kfd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003341 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303342 close(ufd);
Masami Hiramatsu92f6c722015-07-15 18:14:07 +09003343out:
Masami Hiramatsu307a4642015-05-05 11:29:48 +09003344 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04003345
3346 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05003347}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303348
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003349int show_available_funcs(const char *target, struct strfilter *_filter,
3350 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003351{
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003352 struct rb_node *nd;
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003353 struct map *map;
3354 int ret;
3355
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003356 ret = init_probe_symbol_maps(user);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003357 if (ret < 0)
3358 return ret;
3359
3360 /* Get a symbol map */
3361 if (user)
3362 map = dso__new_map(target);
3363 else
3364 map = kernel_get_module_map(target);
3365 if (!map) {
3366 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003367 return -EINVAL;
3368 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003369
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03003370 ret = map__load(map);
Masami Hiramatsue7049342016-07-19 01:12:41 +09003371 if (ret) {
3372 if (ret == -2) {
3373 char *str = strfilter__string(_filter);
3374 pr_err("Failed to find symbols matched to \"%s\"\n",
3375 str);
3376 free(str);
3377 } else
3378 pr_err("Failed to load symbols in %s\n",
3379 (target) ? : "kernel");
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003380 goto end;
3381 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09003382 if (!dso__sorted_by_name(map->dso, map->type))
3383 dso__sort_by_name(map->dso, map->type);
3384
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003385 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303386 setup_pager();
Arnaldo Carvalho de Melofd227592016-08-31 10:04:27 -03003387
3388 for (nd = rb_first(&map->dso->symbol_names[map->type]); nd; nd = rb_next(nd)) {
3389 struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
3390
3391 if (strfilter__compare(_filter, pos->sym.name))
3392 printf("%s\n", pos->sym.name);
3393 }
3394
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003395end:
3396 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003397 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003398 }
Namhyung Kim9bae1e82015-09-10 11:27:05 +09003399 exit_probe_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303400
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003401 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303402}
3403
Wang Nanda15bd92015-08-26 10:57:45 +00003404int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
3405 struct perf_probe_arg *pvar)
3406{
3407 tvar->value = strdup(pvar->var);
3408 if (tvar->value == NULL)
3409 return -ENOMEM;
3410 if (pvar->type) {
3411 tvar->type = strdup(pvar->type);
3412 if (tvar->type == NULL)
3413 return -ENOMEM;
3414 }
3415 if (pvar->name) {
3416 tvar->name = strdup(pvar->name);
3417 if (tvar->name == NULL)
3418 return -ENOMEM;
3419 } else
3420 tvar->name = NULL;
3421 return 0;
3422}