blob: 09225651626254404438abe516b9c79eb964ef47 [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"
Borislav Petkov553873e2013-12-09 17:14:23 +010043#include <api/fs/debugfs.h>
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -050044#include <api/fs/tracefs.h>
Irina Tirdea1d037ca2012-09-11 01:15:03 +030045#include "trace-event.h" /* For __maybe_unused */
Masami Hiramatsu50656ee2009-11-30 19:19:58 -050046#include "probe-event.h"
Masami Hiramatsu4235b042010-03-16 18:06:12 -040047#include "probe-finder.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 Hiramatsu4de189f2009-11-30 19:20:17 -050058/* If there is no space to write, returns -E2BIG. */
59static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu84988452009-12-07 12:00:53 -050060 __attribute__((format(printf, 3, 4)));
61
62static int e_snprintf(char *str, size_t size, const char *format, ...)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -050063{
64 int ret;
65 va_list ap;
66 va_start(ap, format);
67 ret = vsnprintf(str, size, format, ap);
68 va_end(ap);
69 if (ret >= (int)size)
70 ret = -E2BIG;
71 return ret;
72}
73
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -030074static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
Masami Hiramatsu981d05a2014-01-16 09:39:44 +000075static void clear_probe_trace_event(struct probe_trace_event *tev);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000076static struct machine *host_machine;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040077
Masami Hiramatsu469b9b82010-10-21 19:13:41 +090078/* Initialize symbol maps and path of vmlinux/modules */
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000079static int init_symbol_maps(bool user_only)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040080{
Masami Hiramatsu146a1432010-04-12 13:17:42 -040081 int ret;
82
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040083 symbol_conf.sort_by_name = true;
Namhyung Kim680d9262015-03-06 16:31:27 +090084 symbol_conf.allow_aliases = true;
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090085 ret = symbol__init(NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -040086 if (ret < 0) {
87 pr_debug("Failed to init symbol map.\n");
88 goto out;
89 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -040090
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000091 if (host_machine || user_only) /* already initialized */
92 return 0;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -030093
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +000094 if (symbol_conf.vmlinux_name)
95 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
96
97 host_machine = machine__new_host();
98 if (!host_machine) {
99 pr_debug("machine__new_host() failed.\n");
100 symbol__exit();
101 ret = -1;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900102 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400103out:
104 if (ret < 0)
105 pr_warning("Failed to init vmlinux path.\n");
106 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400107}
108
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000109static void exit_symbol_maps(void)
110{
111 if (host_machine) {
112 machine__delete(host_machine);
113 host_machine = NULL;
114 }
115 symbol__exit();
116}
117
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900118static struct symbol *__find_kernel_function_by_name(const char *name,
119 struct map **mapp)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400120{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000121 return machine__find_kernel_function_by_name(host_machine, name, mapp,
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900122 NULL);
123}
124
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000125static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
126{
127 return machine__find_kernel_function(host_machine, addr, mapp, NULL);
128}
129
130static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
131{
132 /* kmap->ref_reloc_sym should be set if host_machine is initialized */
133 struct kmap *kmap;
134
135 if (map__load(host_machine->vmlinux_maps[MAP__FUNCTION], NULL) < 0)
136 return NULL;
137
138 kmap = map__kmap(host_machine->vmlinux_maps[MAP__FUNCTION]);
Wang Nanba927322015-04-07 08:22:45 +0000139 if (!kmap)
140 return NULL;
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000141 return kmap->ref_reloc_sym;
142}
143
144static u64 kernel_get_symbol_address_by_name(const char *name, bool reloc)
145{
146 struct ref_reloc_sym *reloc_sym;
147 struct symbol *sym;
148 struct map *map;
149
150 /* ref_reloc_sym is just a label. Need a special fix*/
151 reloc_sym = kernel_get_ref_reloc_sym();
152 if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
153 return (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
154 else {
155 sym = __find_kernel_function_by_name(name, &map);
156 if (sym)
157 return map->unmap_ip(map, sym->start) -
He Kuangf56847c2015-02-27 18:52:53 +0800158 ((reloc) ? 0 : map->reloc);
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000159 }
160 return 0;
161}
162
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900163static struct map *kernel_get_module_map(const char *module)
164{
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000165 struct map_groups *grp = &host_machine->kmaps;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300166 struct rb_root *maps = &grp->maps[MAP__FUNCTION];
167 struct map *pos;
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900168
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900169 /* A file path -- this is an offline module */
170 if (module && strchr(module, '/'))
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000171 return machine__new_module(host_machine, 0, module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900172
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900173 if (!module)
174 module = "kernel";
175
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300176 for (pos = maps__first(maps); pos; pos = map__next(pos)) {
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900177 if (strncmp(pos->dso->short_name + 1, module,
178 pos->dso->short_name_len - 2) == 0) {
179 return pos;
180 }
181 }
182 return NULL;
183}
184
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900185static struct map *get_target_map(const char *target, bool user)
186{
187 /* Init maps of given executable or kernel */
188 if (user)
189 return dso__new_map(target);
190 else
191 return kernel_get_module_map(target);
192}
193
194static void put_target_map(struct map *map, bool user)
195{
196 if (map && user) {
197 /* Only the user map needs to be released */
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900198 map__delete(map);
199 }
200}
201
202
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900203static struct dso *kernel_get_module_dso(const char *module)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900204{
205 struct dso *dso;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100206 struct map *map;
207 const char *vmlinux_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900208
209 if (module) {
Waiman Long8fa7d872014-09-29 16:07:28 -0400210 list_for_each_entry(dso, &host_machine->kernel_dsos.head,
211 node) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900212 if (strncmp(dso->short_name + 1, module,
213 dso->short_name_len - 2) == 0)
214 goto found;
215 }
216 pr_debug("Failed to find module %s.\n", module);
217 return NULL;
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100218 }
219
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000220 map = host_machine->vmlinux_maps[MAP__FUNCTION];
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100221 dso = map->dso;
222
223 vmlinux_name = symbol_conf.vmlinux_name;
224 if (vmlinux_name) {
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -0300225 if (dso__load_vmlinux(dso, map, vmlinux_name, false, NULL) <= 0)
Franck Bui-Huufd930ff2010-12-10 14:06:03 +0100226 return NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900227 } else {
Franck Bui-Huuc3a34e02010-12-10 14:07:14 +0100228 if (dso__load_vmlinux_path(dso, map, NULL) <= 0) {
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900229 pr_debug("Failed to load kernel map.\n");
230 return NULL;
231 }
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400232 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900233found:
Masami Hiramatsue80711c2011-01-13 21:46:11 +0900234 return dso;
235}
236
237const char *kernel_get_module_path(const char *module)
238{
239 struct dso *dso = kernel_get_module_dso(module);
240 return (dso) ? dso->long_name : NULL;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900241}
242
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000243static int convert_exec_to_group(const char *exec, char **result)
244{
245 char *ptr1, *ptr2, *exec_copy;
246 char buf[64];
247 int ret;
248
249 exec_copy = strdup(exec);
250 if (!exec_copy)
251 return -ENOMEM;
252
253 ptr1 = basename(exec_copy);
254 if (!ptr1) {
255 ret = -EINVAL;
256 goto out;
257 }
258
259 ptr2 = strpbrk(ptr1, "-._");
260 if (ptr2)
261 *ptr2 = '\0';
262 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
263 if (ret < 0)
264 goto out;
265
266 *result = strdup(buf);
267 ret = *result ? 0 : -ENOMEM;
268
269out:
270 free(exec_copy);
271 return ret;
272}
273
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900274static void clear_perf_probe_point(struct perf_probe_point *pp)
275{
276 free(pp->file);
277 free(pp->function);
278 free(pp->lazy_line);
279}
280
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000281static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
282{
283 int i;
284
285 for (i = 0; i < ntevs; i++)
286 clear_probe_trace_event(tevs + i);
287}
288
Ingo Molnar89fe8082013-09-30 12:07:11 +0200289#ifdef HAVE_DWARF_SUPPORT
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900290/*
291 * Some binaries like glibc have special symbols which are on the symbol
292 * table, but not in the debuginfo. If we can find the address of the
293 * symbol from map, we can translate the address back to the probe point.
294 */
295static int find_alternative_probe_point(struct debuginfo *dinfo,
296 struct perf_probe_point *pp,
297 struct perf_probe_point *result,
298 const char *target, bool uprobes)
299{
300 struct map *map = NULL;
301 struct symbol *sym;
302 u64 address = 0;
303 int ret = -ENOENT;
304
305 /* This can work only for function-name based one */
306 if (!pp->function || pp->file)
307 return -ENOTSUP;
308
309 map = get_target_map(target, uprobes);
310 if (!map)
311 return -EINVAL;
312
313 /* Find the address of given function */
314 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900315 if (uprobes)
316 address = sym->start;
317 else
318 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900319 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900320 }
321 if (!address) {
322 ret = -ENOENT;
323 goto out;
324 }
Wang Nanf6c15622015-04-08 02:14:34 +0000325 pr_debug("Symbol %s address found : %" PRIx64 "\n",
326 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900327
328 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
329 result);
330 if (ret <= 0)
331 ret = (!ret) ? -ENOENT : ret;
332 else {
333 result->offset += pp->offset;
334 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800335 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900336 ret = 0;
337 }
338
339out:
340 put_target_map(map, uprobes);
341 return ret;
342
343}
344
345static int get_alternative_probe_event(struct debuginfo *dinfo,
346 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900347 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900348{
349 int ret;
350
351 memcpy(tmp, &pev->point, sizeof(*tmp));
352 memset(&pev->point, 0, sizeof(pev->point));
353 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900354 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900355 if (ret < 0)
356 memcpy(&pev->point, tmp, sizeof(*tmp));
357
358 return ret;
359}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000360
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900361static int get_alternative_line_range(struct debuginfo *dinfo,
362 struct line_range *lr,
363 const char *target, bool user)
364{
David Ahern6d4a4892015-03-11 10:36:20 -0400365 struct perf_probe_point pp = { .function = lr->function,
366 .file = lr->file,
367 .line = lr->start };
368 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900369 int ret, len = 0;
370
David Ahern6d4a4892015-03-11 10:36:20 -0400371 memset(&result, 0, sizeof(result));
372
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900373 if (lr->end != INT_MAX)
374 len = lr->end - lr->start;
375 ret = find_alternative_probe_point(dinfo, &pp, &result,
376 target, user);
377 if (!ret) {
378 lr->function = result.function;
379 lr->file = result.file;
380 lr->start = result.line;
381 if (lr->end != INT_MAX)
382 lr->end = lr->start + len;
383 clear_perf_probe_point(&pp);
384 }
385 return ret;
386}
387
Masami Hiramatsuff741782011-06-27 16:27:39 +0900388/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000389static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900390{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000391 const char *path = module;
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000392 struct debuginfo *ret;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900393
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000394 if (!module || !strchr(module, '/')) {
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900395 path = kernel_get_module_path(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900396 if (!path) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000397 if (!silent)
398 pr_err("Failed to find path of %s module.\n",
399 module ?: "kernel");
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900400 return NULL;
401 }
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900402 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000403 ret = debuginfo__new(path);
404 if (!ret && !silent) {
405 pr_warning("The %s file has no debug information.\n", path);
406 if (!module || !strtailcmp(path, ".ko"))
407 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
408 else
409 pr_warning("Rebuild with -g, ");
410 pr_warning("or install an appropriate debuginfo package.\n");
411 }
412 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400413}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300414
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000415
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000416static int get_text_start_address(const char *exec, unsigned long *address)
417{
418 Elf *elf;
419 GElf_Ehdr ehdr;
420 GElf_Shdr shdr;
421 int fd, ret = -ENOENT;
422
423 fd = open(exec, O_RDONLY);
424 if (fd < 0)
425 return -errno;
426
427 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
428 if (elf == NULL)
429 return -EINVAL;
430
431 if (gelf_getehdr(elf, &ehdr) == NULL)
432 goto out;
433
434 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
435 goto out;
436
437 *address = shdr.sh_addr - shdr.sh_offset;
438 ret = 0;
439out:
440 elf_end(elf);
441 return ret;
442}
443
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000444/*
445 * Convert trace point to probe point with debuginfo
446 */
447static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
448 struct perf_probe_point *pp,
449 bool is_kprobe)
450{
451 struct debuginfo *dinfo = NULL;
452 unsigned long stext = 0;
453 u64 addr = tp->address;
454 int ret = -ENOENT;
455
456 /* convert the address to dwarf address */
457 if (!is_kprobe) {
458 if (!addr) {
459 ret = -EINVAL;
460 goto error;
461 }
462 ret = get_text_start_address(tp->module, &stext);
463 if (ret < 0)
464 goto error;
465 addr += stext;
466 } else {
467 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
468 if (addr == 0)
469 goto error;
470 addr += tp->offset;
471 }
472
473 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
474 tp->module ? : "kernel");
475
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000476 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000477 if (dinfo) {
478 ret = debuginfo__find_probe_point(dinfo,
479 (unsigned long)addr, pp);
480 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000481 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000482 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000483
484 if (ret > 0) {
485 pp->retprobe = tp->retprobe;
486 return 0;
487 }
488error:
489 pr_debug("Failed to find corresponding probes from debuginfo.\n");
490 return ret ? : -ENOENT;
491}
492
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000493static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
494 int ntevs, const char *exec)
495{
496 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000497 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000498
499 if (!exec)
500 return 0;
501
502 ret = get_text_start_address(exec, &stext);
503 if (ret < 0)
504 return ret;
505
506 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000507 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000508 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000509 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000510 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000511 ret = -ENOMEM;
512 break;
513 }
514 tevs[i].uprobes = true;
515 }
516
517 return ret;
518}
519
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900520static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
521 int ntevs, const char *module)
522{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900523 int i, ret = 0;
524 char *tmp;
525
526 if (!module)
527 return 0;
528
529 tmp = strrchr(module, '/');
530 if (tmp) {
531 /* This is a module path -- get the module name */
532 module = strdup(tmp + 1);
533 if (!module)
534 return -ENOMEM;
535 tmp = strchr(module, '.');
536 if (tmp)
537 *tmp = '\0';
538 tmp = (char *)module; /* For free() */
539 }
540
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900541 for (i = 0; i < ntevs; i++) {
542 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900543 if (!tevs[i].point.module) {
544 ret = -ENOMEM;
545 break;
546 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900547 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900548
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300549 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900550 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900551}
552
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000553/* Post processing the probe events */
554static int post_process_probe_trace_events(struct probe_trace_event *tevs,
555 int ntevs, const char *module,
556 bool uprobe)
557{
558 struct ref_reloc_sym *reloc_sym;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900559 u64 etext_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000560 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900561 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000562
563 if (uprobe)
564 return add_exec_to_probe_trace_events(tevs, ntevs, module);
565
566 /* Note that currently ref_reloc_sym based probe is not for drivers */
567 if (module)
568 return add_module_to_probe_trace_events(tevs, ntevs, module);
569
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000570 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000571 if (!reloc_sym) {
572 pr_warning("Relocated base symbol is not found!\n");
573 return -EINVAL;
574 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900575 /* Get the address of _etext for checking non-probable text symbol */
576 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000577
578 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900579 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900580 /* If we found a wrong one, mark it by NULL symbol */
581 if (etext_addr < tevs[i].point.address) {
582 pr_warning("%s+%lu is out of .text, skip it.\n",
583 tevs[i].point.symbol, tevs[i].point.offset);
584 tmp = NULL;
585 skipped++;
586 } else {
587 tmp = strdup(reloc_sym->name);
588 if (!tmp)
589 return -ENOMEM;
590 }
Masami Hiramatsu4c859352015-05-08 10:03:35 +0900591 /* If we have no realname, use symbol for it */
592 if (!tevs[i].point.realname)
593 tevs[i].point.realname = tevs[i].point.symbol;
594 else
595 free(tevs[i].point.symbol);
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000596 tevs[i].point.symbol = tmp;
597 tevs[i].point.offset = tevs[i].point.address -
598 reloc_sym->unrelocated_addr;
599 }
600 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900601 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000602}
603
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300604/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530605static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900606 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300607{
608 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900609 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530610 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900611 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300612
Masami Hiramatsu44225522015-05-08 10:03:28 +0900613 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900614 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000615 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900616 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900617 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300618 return 0;
619 }
620
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000621 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900622 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900623 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900624
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900625 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900626 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900627 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900628 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900629 /*
630 * Write back to the original probe_event for
631 * setting appropriate (user given) event name
632 */
633 clear_perf_probe_point(&pev->point);
634 memcpy(&pev->point, &tmp, sizeof(tmp));
635 }
636 }
637
Masami Hiramatsuff741782011-06-27 16:27:39 +0900638 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300639
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400640 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000641 pr_debug("Found %d probe_trace_events.\n", ntevs);
642 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900643 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900644 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000645 clear_probe_trace_events(*tevs, ntevs);
646 zfree(tevs);
647 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900648 if (ret != ntevs)
649 return ret < 0 ? ret : ntevs;
650 ntevs = 0;
651 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400652 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300653
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400654 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900655 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400656 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900657 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400658 }
659 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400660 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
661 if (ntevs == -EBADF) {
662 pr_warning("Warning: No dwarf info found in the vmlinux - "
663 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
664 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900665 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400666 return 0;
667 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300668 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400669 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300670}
671
672#define LINEBUF_SIZE 256
673#define NR_ADDITIONAL_LINES 2
674
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100675static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300676{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000677 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100678 const char *color = show_num ? "" : PERF_COLOR_BLUE;
679 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300680
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100681 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300682 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
683 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100684 if (skip)
685 continue;
686 if (!prefix) {
687 prefix = show_num ? "%7d " : " ";
688 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300689 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100690 color_fprintf(stdout, color, "%s", buf);
691
692 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400693
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100694 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300695error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100696 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000697 pr_warning("File read error: %s\n",
698 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100699 return -1;
700 }
701 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300702}
703
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100704static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
705{
706 int rv = __show_one_line(fp, l, skip, show_num);
707 if (rv == 0) {
708 pr_warning("Source file is shorter than expected.\n");
709 rv = -1;
710 }
711 return rv;
712}
713
714#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
715#define show_one_line(f,l) _show_one_line(f,l,false,false)
716#define skip_one_line(f,l) _show_one_line(f,l,true,false)
717#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
718
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719/*
720 * Show line-range always requires debuginfo to find source file and
721 * line number.
722 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900723static int __show_line_range(struct line_range *lr, const char *module,
724 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300725{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400726 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000727 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900728 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300729 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900730 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900731 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000732 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300733
734 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000735 dinfo = open_debuginfo(module, false);
736 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900737 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400738
Masami Hiramatsuff741782011-06-27 16:27:39 +0900739 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900740 if (!ret) { /* Not found, retry with an alternative */
741 ret = get_alternative_line_range(dinfo, lr, module, user);
742 if (!ret)
743 ret = debuginfo__find_line_range(dinfo, lr);
744 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900745 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000746 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400747 pr_warning("Specified source line is not found.\n");
748 return -ENOENT;
749 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000750 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400751 return ret;
752 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300753
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900754 /* Convert source file path */
755 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900756 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800757
758 /* Free old path when new path is assigned */
759 if (tmp != lr->path)
760 free(tmp);
761
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900762 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000763 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900764 return ret;
765 }
766
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300767 setup_pager();
768
769 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900770 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300771 lr->start - lr->offset);
772 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100773 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300774
775 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400776 if (fp == NULL) {
777 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000778 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400779 return -errno;
780 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300781 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100782 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100783 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100784 if (ret < 0)
785 goto end;
786 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300787
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000788 intlist__for_each(ln, lr->line_list) {
789 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100790 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100791 if (ret < 0)
792 goto end;
793 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100794 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400795 if (ret < 0)
796 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300797 }
798
799 if (lr->end == INT_MAX)
800 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100801 while (l <= lr->end) {
802 ret = show_one_line_or_eof(fp, l++ - lr->offset);
803 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100804 break;
805 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400806end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300807 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400808 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300809}
810
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000811int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000812{
813 int ret;
814
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000815 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000816 if (ret < 0)
817 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900818 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000819 exit_symbol_maps();
820
821 return ret;
822}
823
Masami Hiramatsuff741782011-06-27 16:27:39 +0900824static int show_available_vars_at(struct debuginfo *dinfo,
825 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900826 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900827{
828 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900829 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900830 struct str_node *node;
831 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900832 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900833 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900834
835 buf = synthesize_perf_probe_point(&pev->point);
836 if (!buf)
837 return -EINVAL;
838 pr_debug("Searching variables at %s\n", buf);
839
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900840 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900841 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900842 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900843 if (!ret) {
844 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900845 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900846 /* Release the old probe_point */
847 clear_perf_probe_point(&tmp);
848 }
849 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900850 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000851 if (ret == 0 || ret == -ENOENT) {
852 pr_err("Failed to find the address of %s\n", buf);
853 ret = -ENOENT;
854 } else
855 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900856 goto end;
857 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000858
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900859 /* Some variables are found */
860 fprintf(stdout, "Available variables at %s\n", buf);
861 for (i = 0; i < ret; i++) {
862 vl = &vls[i];
863 /*
864 * A probe point might be converted to
865 * several trace points.
866 */
867 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
868 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300869 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900870 nvars = 0;
871 if (vl->vars) {
872 strlist__for_each(node, vl->vars) {
873 var = strchr(node->s, '\t') + 1;
874 if (strfilter__compare(_filter, var)) {
875 fprintf(stdout, "\t\t%s\n", node->s);
876 nvars++;
877 }
878 }
879 strlist__delete(vl->vars);
880 }
881 if (nvars == 0)
882 fprintf(stdout, "\t\t(No matched variables)\n");
883 }
884 free(vls);
885end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900886 free(buf);
887 return ret;
888}
889
890/* Show available variables on given probe point */
891int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900892 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900893{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900894 int i, ret = 0;
895 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900896
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000897 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900898 if (ret < 0)
899 return ret;
900
Masami Hiramatsu44225522015-05-08 10:03:28 +0900901 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900902 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000903 ret = -ENOENT;
904 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900905 }
906
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900907 setup_pager();
908
Masami Hiramatsuff741782011-06-27 16:27:39 +0900909 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900910 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900911
912 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000913out:
914 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900915 return ret;
916}
917
Ingo Molnar89fe8082013-09-30 12:07:11 +0200918#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300919
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000920static int
921find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
922 struct perf_probe_point *pp __maybe_unused,
923 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300924{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000925 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300926}
927
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530928static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900929 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300930{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400931 if (perf_probe_event_need_dwarf(pev)) {
932 pr_warning("Debuginfo-analysis is not supported.\n");
933 return -ENOSYS;
934 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530935
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300936 return 0;
937}
938
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300939int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000940 const char *module __maybe_unused,
941 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300942{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400943 pr_warning("Debuginfo-analysis is not supported.\n");
944 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300945}
946
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300947int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900948 int npevs __maybe_unused,
949 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900950{
951 pr_warning("Debuginfo-analysis is not supported.\n");
952 return -ENOSYS;
953}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400954#endif
955
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000956void line_range__clear(struct line_range *lr)
957{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000958 free(lr->function);
959 free(lr->file);
960 free(lr->path);
961 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000962 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000963 memset(lr, 0, sizeof(*lr));
964}
965
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000966int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000967{
968 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000969 lr->line_list = intlist__new(NULL);
970 if (!lr->line_list)
971 return -ENOMEM;
972 else
973 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000974}
975
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100976static int parse_line_num(char **ptr, int *val, const char *what)
977{
978 const char *start = *ptr;
979
980 errno = 0;
981 *val = strtol(*ptr, ptr, 0);
982 if (errno || *ptr == start) {
983 semantic_error("'%s' is not a valid number.\n", what);
984 return -EINVAL;
985 }
986 return 0;
987}
988
Masami Hiramatsu573709f2015-05-06 21:46:47 +0900989/* Check the name is good for event, group or function */
990static bool is_c_func_name(const char *name)
991{
992 if (!isalpha(*name) && *name != '_')
993 return false;
994 while (*++name != '\0') {
995 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
996 return false;
997 }
998 return true;
999}
1000
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001001/*
1002 * Stuff 'lr' according to the line range described by 'arg'.
1003 * The line range syntax is described by:
1004 *
1005 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001006 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001007 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001008int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001009{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001010 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001011 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001012
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001013 if (!name)
1014 return -ENOMEM;
1015
1016 lr->start = 0;
1017 lr->end = INT_MAX;
1018
1019 range = strchr(name, ':');
1020 if (range) {
1021 *range++ = '\0';
1022
1023 err = parse_line_num(&range, &lr->start, "start line");
1024 if (err)
1025 goto err;
1026
1027 if (*range == '+' || *range == '-') {
1028 const char c = *range++;
1029
1030 err = parse_line_num(&range, &lr->end, "end line");
1031 if (err)
1032 goto err;
1033
1034 if (c == '+') {
1035 lr->end += lr->start;
1036 /*
1037 * Adjust the number of lines here.
1038 * If the number of lines == 1, the
1039 * the end of line should be equal to
1040 * the start of line.
1041 */
1042 lr->end--;
1043 }
1044 }
1045
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001046 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001047
1048 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001049 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001050 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001051 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001052 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001053 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001054 if (*range != '\0') {
1055 semantic_error("Tailing with invalid str '%s'.\n", range);
1056 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001057 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001058 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001059
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001060 file = strchr(name, '@');
1061 if (file) {
1062 *file = '\0';
1063 lr->file = strdup(++file);
1064 if (lr->file == NULL) {
1065 err = -ENOMEM;
1066 goto err;
1067 }
1068 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001069 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001070 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001071 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001072 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001073 else { /* Invalid name */
1074 semantic_error("'%s' is not a valid function name.\n", name);
1075 err = -EINVAL;
1076 goto err;
1077 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001078
1079 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001080err:
1081 free(name);
1082 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001083}
1084
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001085/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001086static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001087{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001088 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001089 char *ptr, *tmp;
1090 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301091 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001092 /*
1093 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001094 * perf probe [EVENT=]SRC[:LN|;PTN]
1095 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001096 *
1097 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001098 */
Wang Nane59d29e2015-04-28 08:46:09 +00001099 if (!arg)
1100 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001101
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001102 ptr = strpbrk(arg, ";=@+%");
1103 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001104 *ptr = '\0';
1105 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 if (strchr(arg, ':')) {
1107 semantic_error("Group name is not supported yet.\n");
1108 return -ENOTSUP;
1109 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001110 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001111 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001112 "follow C symbol-naming rule.\n", arg);
1113 return -EINVAL;
1114 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001115 pev->event = strdup(arg);
1116 if (pev->event == NULL)
1117 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001118 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001119 arg = tmp;
1120 }
1121
Naveen N. Rao3099c022015-04-28 17:35:34 +05301122 /*
1123 * Check arg is function or file name and copy it.
1124 *
1125 * We consider arg to be a file spec if and only if it satisfies
1126 * all of the below criteria::
1127 * - it does not include any of "+@%",
1128 * - it includes one of ":;", and
1129 * - it has a period '.' in the name.
1130 *
1131 * Otherwise, we consider arg to be a function specification.
1132 */
1133 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1134 /* This is a file spec if it includes a '.' before ; or : */
1135 if (memchr(arg, '.', ptr - arg))
1136 file_spec = true;
1137 }
1138
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001139 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001140 if (ptr) {
1141 nc = *ptr;
1142 *ptr++ = '\0';
1143 }
1144
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001145 tmp = strdup(arg);
1146 if (tmp == NULL)
1147 return -ENOMEM;
1148
Naveen N. Rao3099c022015-04-28 17:35:34 +05301149 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001150 pp->file = tmp;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301151 else
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001152 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001153
1154 /* Parse other options */
1155 while (ptr) {
1156 arg = ptr;
1157 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001158 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001159 pp->lazy_line = strdup(arg);
1160 if (pp->lazy_line == NULL)
1161 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001162 break;
1163 }
1164 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001165 if (ptr) {
1166 nc = *ptr;
1167 *ptr++ = '\0';
1168 }
1169 switch (c) {
1170 case ':': /* Line number */
1171 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001172 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001173 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001174 " in line number.\n");
1175 return -EINVAL;
1176 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001177 break;
1178 case '+': /* Byte offset from a symbol */
1179 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001180 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001181 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001182 " in offset.\n");
1183 return -EINVAL;
1184 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001185 break;
1186 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001187 if (pp->file) {
1188 semantic_error("SRC@SRC is not allowed.\n");
1189 return -EINVAL;
1190 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001191 pp->file = strdup(arg);
1192 if (pp->file == NULL)
1193 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001194 break;
1195 case '%': /* Probe places */
1196 if (strcmp(arg, "return") == 0) {
1197 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001198 } else { /* Others not supported yet */
1199 semantic_error("%%%s is not supported.\n", arg);
1200 return -ENOTSUP;
1201 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001202 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001203 default: /* Buggy case */
1204 pr_err("This program has a bug at %s:%d.\n",
1205 __FILE__, __LINE__);
1206 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001207 break;
1208 }
1209 }
1210
1211 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001212 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001213 semantic_error("Lazy pattern can't be used with"
1214 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001215 return -EINVAL;
1216 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001217
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001219 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001220 return -EINVAL;
1221 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001222
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001224 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001225 return -EINVAL;
1226 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001227
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001229 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001230 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001231 return -EINVAL;
1232 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001233
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001235 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001236 return -EINVAL;
1237 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001238
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001240 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001241 return -EINVAL;
1242 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001243
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001245 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001246 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 return -EINVAL;
1248 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001249
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001250 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001251 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1252 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001254}
1255
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001256/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001257static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001258{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001259 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001260 struct perf_probe_arg_field **fieldp;
1261
1262 pr_debug("parsing arg: %s into ", str);
1263
Masami Hiramatsu48481932010-04-12 13:16:53 -04001264 tmp = strchr(str, '=');
1265 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001266 arg->name = strndup(str, tmp - str);
1267 if (arg->name == NULL)
1268 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001269 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001270 str = tmp + 1;
1271 }
1272
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001273 tmp = strchr(str, ':');
1274 if (tmp) { /* Type setting */
1275 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001276 arg->type = strdup(tmp + 1);
1277 if (arg->type == NULL)
1278 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001279 pr_debug("type:%s ", arg->type);
1280 }
1281
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001282 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001283 if (!is_c_varname(str) || !tmp) {
1284 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001285 arg->var = strdup(str);
1286 if (arg->var == NULL)
1287 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001288 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001289 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001290 }
1291
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001292 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001293 arg->var = strndup(str, tmp - str);
1294 if (arg->var == NULL)
1295 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001296 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001297 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001298 fieldp = &arg->field;
1299
1300 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001301 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1302 if (*fieldp == NULL)
1303 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001304 if (*tmp == '[') { /* Array */
1305 str = tmp;
1306 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001307 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001308 if (*tmp != ']' || tmp == str + 1) {
1309 semantic_error("Array index must be a"
1310 " number.\n");
1311 return -EINVAL;
1312 }
1313 tmp++;
1314 if (*tmp == '\0')
1315 tmp = NULL;
1316 } else { /* Structure */
1317 if (*tmp == '.') {
1318 str = tmp + 1;
1319 (*fieldp)->ref = false;
1320 } else if (tmp[1] == '>') {
1321 str = tmp + 2;
1322 (*fieldp)->ref = true;
1323 } else {
1324 semantic_error("Argument parse error: %s\n",
1325 str);
1326 return -EINVAL;
1327 }
1328 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001329 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001330 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001331 (*fieldp)->name = strndup(str, tmp - str);
1332 if ((*fieldp)->name == NULL)
1333 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001334 if (*str != '[')
1335 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001336 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1337 fieldp = &(*fieldp)->next;
1338 }
1339 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001340 (*fieldp)->name = strdup(str);
1341 if ((*fieldp)->name == NULL)
1342 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001343 if (*str != '[')
1344 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001345 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001346
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001347 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001348 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001349 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001350 if (arg->name == NULL)
1351 return -ENOMEM;
1352 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001353 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001354}
1355
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001356/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001357int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001358{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001359 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001361
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001362 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363 if (!argv) {
1364 pr_debug("Failed to split arguments.\n");
1365 return -ENOMEM;
1366 }
1367 if (argc - 1 > MAX_PROBE_ARGS) {
1368 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1369 ret = -ERANGE;
1370 goto out;
1371 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001372 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001373 ret = parse_perf_probe_point(argv[0], pev);
1374 if (ret < 0)
1375 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001376
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001377 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001378 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001379 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1380 if (pev->args == NULL) {
1381 ret = -ENOMEM;
1382 goto out;
1383 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1385 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1386 if (ret >= 0 &&
1387 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001388 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001389 " kretprobe.\n");
1390 ret = -EINVAL;
1391 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001392 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001393out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001394 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001395
1396 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001397}
1398
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001399/* Return true if this perf_probe_event requires debuginfo */
1400bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001401{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001402 int i;
1403
1404 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1405 return true;
1406
1407 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001408 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001409 return true;
1410
1411 return false;
1412}
1413
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301414/* Parse probe_events event into struct probe_point */
1415static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001416 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001417{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301418 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001419 char pr;
1420 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001421 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001422 int ret, i, argc;
1423 char **argv;
1424
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301425 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001426 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001427 if (!argv) {
1428 pr_debug("Failed to split arguments.\n");
1429 return -ENOMEM;
1430 }
1431 if (argc < 2) {
1432 semantic_error("Too few probe arguments.\n");
1433 ret = -ERANGE;
1434 goto out;
1435 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001436
1437 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001438 argv0_str = strdup(argv[0]);
1439 if (argv0_str == NULL) {
1440 ret = -ENOMEM;
1441 goto out;
1442 }
1443 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1444 fmt2_str = strtok_r(NULL, "/", &fmt);
1445 fmt3_str = strtok_r(NULL, " \t", &fmt);
1446 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1447 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448 semantic_error("Failed to parse event name: %s\n", argv[0]);
1449 ret = -EINVAL;
1450 goto out;
1451 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001452 pr = fmt1_str[0];
1453 tev->group = strdup(fmt2_str);
1454 tev->event = strdup(fmt3_str);
1455 if (tev->group == NULL || tev->event == NULL) {
1456 ret = -ENOMEM;
1457 goto out;
1458 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001459 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001460
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001461 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001462
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001463 /* Scan module name(if there), function name and offset */
1464 p = strchr(argv[1], ':');
1465 if (p) {
1466 tp->module = strndup(argv[1], p - argv[1]);
1467 p++;
1468 } else
1469 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001470 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001471 if (fmt1_str[0] == '0') /* only the address started with 0x */
1472 tp->address = strtoul(fmt1_str, NULL, 0);
1473 else {
1474 /* Only the symbol-based probe has offset */
1475 tp->symbol = strdup(fmt1_str);
1476 if (tp->symbol == NULL) {
1477 ret = -ENOMEM;
1478 goto out;
1479 }
1480 fmt2_str = strtok_r(NULL, "", &fmt);
1481 if (fmt2_str == NULL)
1482 tp->offset = 0;
1483 else
1484 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001485 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001486
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001487 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301488 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001489 if (tev->args == NULL) {
1490 ret = -ENOMEM;
1491 goto out;
1492 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001493 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001494 p = strchr(argv[i + 2], '=');
1495 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496 *p++ = '\0';
1497 else
1498 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001499 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001500 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001501 tev->args[i].value = strdup(p);
1502 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1503 ret = -ENOMEM;
1504 goto out;
1505 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001506 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001507 ret = 0;
1508out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001509 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001510 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001511 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001512}
1513
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001514/* Compose only probe arg */
1515int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1516{
1517 struct perf_probe_arg_field *field = pa->field;
1518 int ret;
1519 char *tmp = buf;
1520
Masami Hiramatsu48481932010-04-12 13:16:53 -04001521 if (pa->name && pa->var)
1522 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1523 else
1524 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001525 if (ret <= 0)
1526 goto error;
1527 tmp += ret;
1528 len -= ret;
1529
1530 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001531 if (field->name[0] == '[')
1532 ret = e_snprintf(tmp, len, "%s", field->name);
1533 else
1534 ret = e_snprintf(tmp, len, "%s%s",
1535 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001536 if (ret <= 0)
1537 goto error;
1538 tmp += ret;
1539 len -= ret;
1540 field = field->next;
1541 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001542
1543 if (pa->type) {
1544 ret = e_snprintf(tmp, len, ":%s", pa->type);
1545 if (ret <= 0)
1546 goto error;
1547 tmp += ret;
1548 len -= ret;
1549 }
1550
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001551 return tmp - buf;
1552error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001553 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001554 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001555}
1556
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001557/* Compose only probe point (not argument) */
1558static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001559{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001560 char *buf, *tmp;
1561 char offs[32] = "", line[32] = "", file[32] = "";
1562 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001563
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001564 buf = zalloc(MAX_CMDLEN);
1565 if (buf == NULL) {
1566 ret = -ENOMEM;
1567 goto error;
1568 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001569 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001570 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001571 if (ret <= 0)
1572 goto error;
1573 }
1574 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001575 ret = e_snprintf(line, 32, ":%d", pp->line);
1576 if (ret <= 0)
1577 goto error;
1578 }
1579 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001580 tmp = pp->file;
1581 len = strlen(tmp);
1582 if (len > 30) {
1583 tmp = strchr(pp->file + len - 30, '/');
1584 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1585 }
1586 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001587 if (ret <= 0)
1588 goto error;
1589 }
1590
1591 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001592 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1593 offs, pp->retprobe ? "%return" : "", line,
1594 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001595 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001596 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001597 if (ret <= 0)
1598 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001599
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001600 return buf;
1601error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001602 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001603 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001604 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001605}
1606
1607#if 0
1608char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1609{
1610 char *buf;
1611 int i, len, ret;
1612
1613 buf = synthesize_perf_probe_point(&pev->point);
1614 if (!buf)
1615 return NULL;
1616
1617 len = strlen(buf);
1618 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001619 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001620 pev->args[i].name);
1621 if (ret <= 0) {
1622 free(buf);
1623 return NULL;
1624 }
1625 len += ret;
1626 }
1627
1628 return buf;
1629}
1630#endif
1631
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301632static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001633 char **buf, size_t *buflen,
1634 int depth)
1635{
1636 int ret;
1637 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301638 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001639 buflen, depth + 1);
1640 if (depth < 0)
1641 goto out;
1642 }
1643
1644 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1645 if (ret < 0)
1646 depth = ret;
1647 else {
1648 *buf += ret;
1649 *buflen -= ret;
1650 }
1651out:
1652 return depth;
1653
1654}
1655
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301656static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001657 char *buf, size_t buflen)
1658{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301659 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001660 int ret, depth = 0;
1661 char *tmp = buf;
1662
1663 /* Argument name or separator */
1664 if (arg->name)
1665 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1666 else
1667 ret = e_snprintf(buf, buflen, " ");
1668 if (ret < 0)
1669 return ret;
1670 buf += ret;
1671 buflen -= ret;
1672
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001673 /* Special case: @XXX */
1674 if (arg->value[0] == '@' && arg->ref)
1675 ref = ref->next;
1676
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001677 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001678 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301679 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001680 &buflen, 1);
1681 if (depth < 0)
1682 return depth;
1683 }
1684
1685 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001686 if (arg->value[0] == '@' && arg->ref)
1687 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1688 arg->ref->offset);
1689 else
1690 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001691 if (ret < 0)
1692 return ret;
1693 buf += ret;
1694 buflen -= ret;
1695
1696 /* Closing */
1697 while (depth--) {
1698 ret = e_snprintf(buf, buflen, ")");
1699 if (ret < 0)
1700 return ret;
1701 buf += ret;
1702 buflen -= ret;
1703 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001704 /* Print argument type */
1705 if (arg->type) {
1706 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1707 if (ret <= 0)
1708 return ret;
1709 buf += ret;
1710 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001711
1712 return buf - tmp;
1713}
1714
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301715char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001716{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301717 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001718 char *buf;
1719 int i, len, ret;
1720
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001721 buf = zalloc(MAX_CMDLEN);
1722 if (buf == NULL)
1723 return NULL;
1724
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001725 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1726 tev->group, tev->event);
1727 if (len <= 0)
1728 goto error;
1729
1730 /* Uprobes must have tp->address and tp->module */
1731 if (tev->uprobes && (!tp->address || !tp->module))
1732 goto error;
1733
1734 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301735 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001736 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1737 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301738 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001739 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301740 tp->module ?: "", tp->module ? ":" : "",
1741 tp->symbol, tp->offset);
1742
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001743 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001744 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001745 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001746
1747 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301748 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001749 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001750 if (ret <= 0)
1751 goto error;
1752 len += ret;
1753 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001754
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001755 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001756error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001757 free(buf);
1758 return NULL;
1759}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001760
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001761static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1762 struct perf_probe_point *pp,
1763 bool is_kprobe)
1764{
1765 struct symbol *sym = NULL;
1766 struct map *map;
1767 u64 addr;
1768 int ret = -ENOENT;
1769
1770 if (!is_kprobe) {
1771 map = dso__new_map(tp->module);
1772 if (!map)
1773 goto out;
1774 addr = tp->address;
1775 sym = map__find_symbol(map, addr, NULL);
1776 } else {
1777 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1778 if (addr) {
1779 addr += tp->offset;
1780 sym = __find_kernel_function(addr, &map);
1781 }
1782 }
1783 if (!sym)
1784 goto out;
1785
1786 pp->retprobe = tp->retprobe;
1787 pp->offset = addr - map->unmap_ip(map, sym->start);
1788 pp->function = strdup(sym->name);
1789 ret = pp->function ? 0 : -ENOMEM;
1790
1791out:
1792 if (map && !is_kprobe) {
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001793 map__delete(map);
1794 }
1795
1796 return ret;
1797}
1798
1799static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1800 struct perf_probe_point *pp,
1801 bool is_kprobe)
1802{
1803 char buf[128];
1804 int ret;
1805
1806 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1807 if (!ret)
1808 return 0;
1809 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1810 if (!ret)
1811 return 0;
1812
1813 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1814
1815 if (tp->symbol) {
1816 pp->function = strdup(tp->symbol);
1817 pp->offset = tp->offset;
1818 } else if (!tp->module && !is_kprobe) {
1819 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1820 if (ret < 0)
1821 return ret;
1822 pp->function = strdup(buf);
1823 pp->offset = 0;
1824 }
1825 if (pp->function == NULL)
1826 return -ENOMEM;
1827
1828 pp->retprobe = tp->retprobe;
1829
1830 return 0;
1831}
1832
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301833static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301834 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001835{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001836 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001837 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001838
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001839 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001840 pev->event = strdup(tev->event);
1841 pev->group = strdup(tev->group);
1842 if (pev->event == NULL || pev->group == NULL)
1843 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001844
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001845 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001846 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001847 if (ret < 0)
1848 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001849
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001850 /* Convert trace_arg to probe_arg */
1851 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001852 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1853 if (pev->args == NULL)
1854 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001855 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001856 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001857 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001858 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301859 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001860 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001861 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001862 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001863 if (pev->args[i].name == NULL && ret >= 0)
1864 ret = -ENOMEM;
1865 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001866
1867 if (ret < 0)
1868 clear_perf_probe_event(pev);
1869
1870 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001871}
1872
1873void clear_perf_probe_event(struct perf_probe_event *pev)
1874{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001875 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001876 int i;
1877
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001878 free(pev->event);
1879 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001880 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001881 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001882
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001883 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001884 free(pev->args[i].name);
1885 free(pev->args[i].var);
1886 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001887 field = pev->args[i].field;
1888 while (field) {
1889 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001890 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001891 free(field);
1892 field = next;
1893 }
1894 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001895 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001896 memset(pev, 0, sizeof(*pev));
1897}
1898
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301899static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001900{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301901 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001902 int i;
1903
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001904 free(tev->event);
1905 free(tev->group);
1906 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001907 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001908 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001909 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001910 free(tev->args[i].name);
1911 free(tev->args[i].value);
1912 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001913 ref = tev->args[i].ref;
1914 while (ref) {
1915 next = ref->next;
1916 free(ref);
1917 ref = next;
1918 }
1919 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001920 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001921 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001922}
1923
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001924static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301925{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001926 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301927
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001928 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301929 const char *config;
1930
1931 if (!is_kprobe)
1932 config = "CONFIG_UPROBE_EVENTS";
1933 else
1934 config = "CONFIG_KPROBE_EVENTS";
1935
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001936 pr_warning("%cprobe_events file does not exist"
1937 " - please rebuild kernel with %s.\n",
1938 is_kprobe ? 'k' : 'u', config);
1939 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001940 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001941 else
1942 pr_warning("Failed to open %cprobe_events: %s\n",
1943 is_kprobe ? 'k' : 'u',
1944 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301945}
1946
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001947static void print_both_open_warning(int kerr, int uerr)
1948{
1949 /* Both kprobes and uprobes are disabled, warn it. */
1950 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001951 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001952 else if (kerr == -ENOENT && uerr == -ENOENT)
1953 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1954 "or/and CONFIG_UPROBE_EVENTS.\n");
1955 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001956 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001957 pr_warning("Failed to open kprobe events: %s.\n",
1958 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1959 pr_warning("Failed to open uprobe events: %s.\n",
1960 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1961 }
1962}
1963
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001964static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001965{
1966 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001967 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001968 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001969 int ret;
1970
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001971 __debugfs = tracefs_find_mountpoint();
1972 if (__debugfs == NULL) {
1973 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001974
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001975 __debugfs = debugfs_find_mountpoint();
1976 if (__debugfs == NULL)
1977 return -ENOTSUP;
1978 }
1979
1980 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1981 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001982 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001983 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001984 if (readwrite && !probe_event_dry_run)
Masami Hiramatsub8dc3982015-05-06 21:46:42 +09001985 ret = open(buf, O_RDWR | O_APPEND, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001986 else
1987 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001988
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301989 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001990 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001991 }
1992 return ret;
1993}
1994
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301995static int open_kprobe_events(bool readwrite)
1996{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001997 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301998}
1999
2000static int open_uprobe_events(bool readwrite)
2001{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002002 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302003}
2004
2005/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302006static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002007{
2008 int ret, idx;
2009 FILE *fp;
2010 char buf[MAX_CMDLEN];
2011 char *p;
2012 struct strlist *sl;
2013
2014 sl = strlist__new(true, NULL);
2015
2016 fp = fdopen(dup(fd), "r");
2017 while (!feof(fp)) {
2018 p = fgets(buf, MAX_CMDLEN, fp);
2019 if (!p)
2020 break;
2021
2022 idx = strlen(p) - 1;
2023 if (p[idx] == '\n')
2024 p[idx] = '\0';
2025 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002026 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002027 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002028 strlist__delete(sl);
2029 return NULL;
2030 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002031 }
2032 fclose(fp);
2033
2034 return sl;
2035}
2036
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002037struct kprobe_blacklist_node {
2038 struct list_head list;
2039 unsigned long start;
2040 unsigned long end;
2041 char *symbol;
2042};
2043
2044static void kprobe_blacklist__delete(struct list_head *blacklist)
2045{
2046 struct kprobe_blacklist_node *node;
2047
2048 while (!list_empty(blacklist)) {
2049 node = list_first_entry(blacklist,
2050 struct kprobe_blacklist_node, list);
2051 list_del(&node->list);
2052 free(node->symbol);
2053 free(node);
2054 }
2055}
2056
2057static int kprobe_blacklist__load(struct list_head *blacklist)
2058{
2059 struct kprobe_blacklist_node *node;
2060 const char *__debugfs = debugfs_find_mountpoint();
2061 char buf[PATH_MAX], *p;
2062 FILE *fp;
2063 int ret;
2064
2065 if (__debugfs == NULL)
2066 return -ENOTSUP;
2067
2068 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2069 if (ret < 0)
2070 return ret;
2071
2072 fp = fopen(buf, "r");
2073 if (!fp)
2074 return -errno;
2075
2076 ret = 0;
2077 while (fgets(buf, PATH_MAX, fp)) {
2078 node = zalloc(sizeof(*node));
2079 if (!node) {
2080 ret = -ENOMEM;
2081 break;
2082 }
2083 INIT_LIST_HEAD(&node->list);
2084 list_add_tail(&node->list, blacklist);
2085 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2086 ret = -EINVAL;
2087 break;
2088 }
2089 p = strchr(buf, '\t');
2090 if (p) {
2091 p++;
2092 if (p[strlen(p) - 1] == '\n')
2093 p[strlen(p) - 1] = '\0';
2094 } else
2095 p = (char *)"unknown";
2096 node->symbol = strdup(p);
2097 if (!node->symbol) {
2098 ret = -ENOMEM;
2099 break;
2100 }
2101 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2102 node->start, node->end, node->symbol);
2103 ret++;
2104 }
2105 if (ret < 0)
2106 kprobe_blacklist__delete(blacklist);
2107 fclose(fp);
2108
2109 return ret;
2110}
2111
2112static struct kprobe_blacklist_node *
2113kprobe_blacklist__find_by_address(struct list_head *blacklist,
2114 unsigned long address)
2115{
2116 struct kprobe_blacklist_node *node;
2117
2118 list_for_each_entry(node, blacklist, list) {
2119 if (node->start <= address && address <= node->end)
2120 return node;
2121 }
2122
2123 return NULL;
2124}
2125
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002126/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002127static int show_perf_probe_event(struct perf_probe_event *pev,
2128 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002129{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002130 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002131 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002132 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002133
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002134 /* Synthesize only event probe point */
2135 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002136 if (!place)
2137 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002138
2139 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002140 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002141 return ret;
2142
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002143 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002144 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002145 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002146
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002147 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002148 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002149 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002150 ret = synthesize_perf_probe_arg(&pev->args[i],
2151 buf, 128);
2152 if (ret < 0)
2153 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002154 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002155 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002156 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002157 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002158 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002159 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002160}
2161
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002162static bool filter_probe_trace_event(struct probe_trace_event *tev,
2163 struct strfilter *filter)
2164{
2165 char tmp[128];
2166
2167 /* At first, check the event name itself */
2168 if (strfilter__compare(filter, tev->event))
2169 return true;
2170
2171 /* Next, check the combination of name and group */
2172 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2173 return false;
2174 return strfilter__compare(filter, tmp);
2175}
2176
2177static int __show_perf_probe_events(int fd, bool is_kprobe,
2178 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002179{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302180 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302181 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002182 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002183 struct strlist *rawlist;
2184 struct str_node *ent;
2185
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002186 memset(&tev, 0, sizeof(tev));
2187 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002188
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302189 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002190 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002191 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002192
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002193 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302194 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002195 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002196 if (!filter_probe_trace_event(&tev, filter))
2197 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302198 ret = convert_to_perf_probe_event(&tev, &pev,
2199 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002200 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002201 ret = show_perf_probe_event(&pev,
2202 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002204next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002205 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302206 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002207 if (ret < 0)
2208 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002209 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002210 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002211
2212 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002213}
2214
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302215/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002216int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302217{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002218 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302219
2220 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302221
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002222 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302223 if (ret < 0)
2224 return ret;
2225
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002226 kp_fd = open_kprobe_events(false);
2227 if (kp_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002228 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002229 close(kp_fd);
2230 if (ret < 0)
2231 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302232 }
2233
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002234 up_fd = open_uprobe_events(false);
2235 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002236 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002237 ret = kp_fd;
2238 goto out;
2239 }
2240
2241 if (up_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002242 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002243 close(up_fd);
2244 }
2245out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002246 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302247 return ret;
2248}
2249
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002250/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302251static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002252{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002253 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002254 struct strlist *sl, *rawlist;
2255 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302256 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002257 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002258
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002259 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302260 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002261 if (!rawlist)
2262 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002263 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002264 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302265 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002266 if (ret < 0)
2267 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002268 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002269 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2270 tev.event);
2271 if (ret >= 0)
2272 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002273 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002274 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302275 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002276 if (ret < 0)
2277 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002278 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002279 strlist__delete(rawlist);
2280
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002281 if (ret < 0) {
2282 strlist__delete(sl);
2283 return NULL;
2284 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002285 return sl;
2286}
2287
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302288static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002289{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002290 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302291 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002292 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002293
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002294 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302295 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002296 return -EINVAL;
2297 }
2298
Masami Hiramatsufa282442009-12-08 17:03:23 -05002299 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002300 if (!probe_event_dry_run) {
2301 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002302 if (ret <= 0) {
2303 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002304 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002305 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002306 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002307 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002308 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002309 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002310}
2311
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002312static int get_new_event_name(char *buf, size_t len, const char *base,
2313 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002314{
2315 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002316
Naveen N. Rao3099c022015-04-28 17:35:34 +05302317 if (*base == '.')
2318 base++;
2319
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002320 /* Try no suffix */
2321 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002322 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002323 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002324 return ret;
2325 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002326 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002327 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002328
Masami Hiramatsud761b082009-12-15 10:32:25 -05002329 if (!allow_suffix) {
2330 pr_warning("Error: event \"%s\" already exists. "
2331 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002332 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002333 }
2334
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002335 /* Try to add suffix */
2336 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002337 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002338 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002339 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002340 return ret;
2341 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002342 if (!strlist__has_entry(namelist, buf))
2343 break;
2344 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002345 if (i == MAX_EVENT_INDEX) {
2346 pr_warning("Too many events are on the same function.\n");
2347 ret = -ERANGE;
2348 }
2349
2350 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002351}
2352
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002353/* Warn if the current kernel's uprobe implementation is old */
2354static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2355{
2356 int i;
2357 char *buf = synthesize_probe_trace_command(tev);
2358
2359 /* Old uprobe event doesn't support memory dereference */
2360 if (!tev->uprobes || tev->nargs == 0 || !buf)
2361 goto out;
2362
2363 for (i = 0; i < tev->nargs; i++)
2364 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2365 pr_warning("Please upgrade your kernel to at least "
2366 "3.14 to have access to feature %s\n",
2367 tev->args[i].value);
2368 break;
2369 }
2370out:
2371 free(buf);
2372}
2373
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302374static int __add_probe_trace_events(struct perf_probe_event *pev,
2375 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002376 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002377{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002378 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302379 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002380 char buf[64];
2381 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002382 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002383 LIST_HEAD(blacklist);
2384 struct kprobe_blacklist_node *node;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002385 bool safename;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002386
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302387 if (pev->uprobes)
2388 fd = open_uprobe_events(true);
2389 else
2390 fd = open_kprobe_events(true);
2391
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002392 if (fd < 0) {
2393 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002394 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002395 }
2396
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002397 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302398 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002399 if (!namelist) {
2400 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002401 ret = -ENOMEM;
2402 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002403 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002404 /* Get kprobe blacklist if exists */
2405 if (!pev->uprobes) {
2406 ret = kprobe_blacklist__load(&blacklist);
2407 if (ret < 0)
2408 pr_debug("No kprobe blacklist support, ignored\n");
2409 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002410
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002411 safename = (pev->point.function && !strisglob(pev->point.function));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002412 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002413 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002414 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002415 tev = &tevs[i];
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002416 /* Skip if the symbol is out of .text (marked previously) */
2417 if (!tev->point.symbol)
2418 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002419 /* Ensure that the address is NOT blacklisted */
2420 node = kprobe_blacklist__find_by_address(&blacklist,
2421 tev->point.address);
2422 if (node) {
2423 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2424 continue;
2425 }
2426
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002427 if (pev->event)
2428 event = pev->event;
2429 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002430 if (safename)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002431 event = pev->point.function;
2432 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002433 event = tev->point.realname;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434 if (pev->group)
2435 group = pev->group;
2436 else
2437 group = PERFPROBE_GROUP;
2438
2439 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002440 ret = get_new_event_name(buf, 64, event,
2441 namelist, allow_suffix);
2442 if (ret < 0)
2443 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002444 event = buf;
2445
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002446 tev->event = strdup(event);
2447 tev->group = strdup(group);
2448 if (tev->event == NULL || tev->group == NULL) {
2449 ret = -ENOMEM;
2450 break;
2451 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302452 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002453 if (ret < 0)
2454 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002455 /* Add added event name to namelist */
2456 strlist__add(namelist, event);
2457
2458 /* Trick here - save current event/group */
2459 event = pev->event;
2460 group = pev->group;
2461 pev->event = tev->event;
2462 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002463 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002464 /* Trick here - restore current event/group */
2465 pev->event = (char *)event;
2466 pev->group = (char *)group;
2467
2468 /*
2469 * Probes after the first probe which comes from same
2470 * user input are always allowed to add suffix, because
2471 * there might be several addresses corresponding to
2472 * one code line.
2473 */
2474 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002475 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002476 if (ret == -EINVAL && pev->uprobes)
2477 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002478
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002479 /* Note that it is possible to skip all events because of blacklist */
2480 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002482 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2483 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002484 tev->event);
2485 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002486
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002487 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002488 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002489close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002490 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002491 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002492}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002493
Namhyung Kim564c62a2015-01-14 20:18:07 +09002494static int find_probe_functions(struct map *map, char *name)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002495{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002496 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002497 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002498 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002499
Wang Nan75e4a2a2015-05-15 12:14:44 +00002500 if (map__load(map, NULL) < 0)
2501 return 0;
2502
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002503 map__for_each_symbol(map, sym, tmp) {
2504 if (strglobmatch(sym->name, name))
2505 found++;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002506 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002507
2508 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002509}
2510
2511#define strdup_or_goto(str, label) \
2512 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2513
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302514void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2515 struct probe_trace_event *tev __maybe_unused,
2516 struct map *map __maybe_unused) { }
2517
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002518/*
2519 * Find probe function addresses from map.
2520 * Return an error or the number of found probe_trace_event
2521 */
2522static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002523 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002524{
2525 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002526 struct ref_reloc_sym *reloc_sym = NULL;
2527 struct symbol *sym;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002528 struct probe_trace_event *tev;
2529 struct perf_probe_point *pp = &pev->point;
2530 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002531 int num_matched_functions;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002532 int ret, i;
2533
Masami Hiramatsu44225522015-05-08 10:03:28 +09002534 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002535 if (!map) {
2536 ret = -EINVAL;
2537 goto out;
2538 }
2539
2540 /*
2541 * Load matched symbols: Since the different local symbols may have
2542 * same name but different addresses, this lists all the symbols.
2543 */
Namhyung Kim564c62a2015-01-14 20:18:07 +09002544 num_matched_functions = find_probe_functions(map, pp->function);
2545 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002546 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002547 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002548 ret = -ENOENT;
2549 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002550 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002551 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002552 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002553 ret = -E2BIG;
2554 goto out;
2555 }
2556
Namhyung Kim25dd9172015-01-14 20:18:08 +09002557 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002558 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002559 if (!reloc_sym) {
2560 pr_warning("Relocated base symbol is not found!\n");
2561 ret = -EINVAL;
2562 goto out;
2563 }
2564 }
2565
2566 /* Setup result trace-probe-events */
2567 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2568 if (!*tevs) {
2569 ret = -ENOMEM;
2570 goto out;
2571 }
2572
2573 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002574
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002575 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002576 tev = (*tevs) + ret;
2577 tp = &tev->point;
2578 if (ret == num_matched_functions) {
2579 pr_warning("Too many symbols are listed. Skip it.\n");
2580 break;
2581 }
2582 ret++;
2583
2584 if (pp->offset > sym->end - sym->start) {
2585 pr_warning("Offset %ld is bigger than the size of %s\n",
2586 pp->offset, sym->name);
2587 ret = -ENOENT;
2588 goto err_out;
2589 }
2590 /* Add one probe point */
2591 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2592 if (reloc_sym) {
2593 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2594 tp->offset = tp->address - reloc_sym->addr;
2595 } else {
2596 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2597 tp->offset = pp->offset;
2598 }
2599 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002600 if (pev->target)
2601 tev->point.module = strdup_or_goto(pev->target,
2602 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002603 tev->uprobes = pev->uprobes;
2604 tev->nargs = pev->nargs;
2605 if (tev->nargs) {
2606 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2607 tev->nargs);
2608 if (tev->args == NULL)
2609 goto nomem_out;
2610 }
2611 for (i = 0; i < tev->nargs; i++) {
2612 if (pev->args[i].name)
2613 tev->args[i].name =
2614 strdup_or_goto(pev->args[i].name,
2615 nomem_out);
2616
2617 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2618 nomem_out);
2619 if (pev->args[i].type)
2620 tev->args[i].type =
2621 strdup_or_goto(pev->args[i].type,
2622 nomem_out);
2623 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302624 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002625 }
2626
2627out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002628 put_target_map(map, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002629 return ret;
2630
2631nomem_out:
2632 ret = -ENOMEM;
2633err_out:
2634 clear_probe_trace_events(*tevs, num_matched_functions);
2635 zfree(tevs);
2636 goto out;
2637}
2638
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302639bool __weak arch__prefers_symtab(void) { return false; }
2640
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302641static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002642 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002643{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002644 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002645
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002646 if (pev->uprobes && !pev->group) {
2647 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002648 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002649 if (ret != 0) {
2650 pr_warning("Failed to make a group name.\n");
2651 return ret;
2652 }
2653 }
2654
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302655 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002656 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302657 if (ret > 0)
2658 return ret; /* Found in symbol table */
2659 }
2660
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002661 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002662 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002663 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002664 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002665
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002666 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002667}
2668
2669struct __event_package {
2670 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302671 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002672 int ntevs;
2673};
2674
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002675int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002676{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002677 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002678 struct __event_package *pkgs;
2679
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302680 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002681 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302682
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002683 if (pkgs == NULL)
2684 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002685
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002686 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002687 if (ret < 0) {
2688 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002689 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002690 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002691
2692 /* Loop 1: convert all events */
2693 for (i = 0; i < npevs; i++) {
2694 pkgs[i].pev = &pevs[i];
2695 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302696 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002697 &pkgs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002698 if (ret < 0)
2699 goto end;
2700 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002701 }
2702
2703 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002704 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302705 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002706 pkgs[i].ntevs,
2707 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002708 if (ret < 0)
2709 break;
2710 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002711end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002712 /* Loop 3: cleanup and free trace events */
2713 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002714 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302715 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002716 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002717 }
2718 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002719 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002720
2721 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002722}
2723
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302724static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002725{
2726 char *p;
2727 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002728 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002729
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302730 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002731 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2732 if (ret < 0)
2733 goto error;
2734
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002735 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002736 if (!p) {
2737 pr_debug("Internal error: %s should have ':' but not.\n",
2738 ent->s);
2739 ret = -ENOTSUP;
2740 goto error;
2741 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002742 *p = '/';
2743
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002744 pr_debug("Writing event: %s\n", buf);
2745 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002746 if (ret < 0) {
2747 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002748 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002749 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002750
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002751 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002752 return 0;
2753error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002754 pr_warning("Failed to delete event: %s\n",
2755 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002756 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002757}
2758
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002759static int del_trace_probe_events(int fd, struct strfilter *filter,
2760 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002761{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002762 struct str_node *ent;
2763 const char *p;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002764 int ret = -ENOENT;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002765
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002766 if (!namelist)
2767 return -ENOENT;
2768
2769 strlist__for_each(ent, namelist) {
2770 p = strchr(ent->s, ':');
2771 if ((p && strfilter__compare(filter, p + 1)) ||
2772 strfilter__compare(filter, ent->s)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302773 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002774 if (ret < 0)
2775 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002776 }
2777 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002778
2779 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002780}
2781
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002782int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002783{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002784 int ret, ret2, ufd = -1, kfd = -1;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302785 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002786 char *str = strfilter__string(filter);
2787
2788 if (!str)
2789 return -EINVAL;
2790
2791 pr_debug("Delete filter: \'%s\'\n", str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002792
Masami Hiramatsufa282442009-12-08 17:03:23 -05002793 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302794 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002795 if (kfd >= 0)
2796 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302797
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302798 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002799 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302800 unamelist = get_probe_trace_event_names(ufd, true);
2801
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002802 if (kfd < 0 && ufd < 0) {
2803 print_both_open_warning(kfd, ufd);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002804 ret = kfd;
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002805 goto error;
2806 }
2807
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002808 ret = del_trace_probe_events(kfd, filter, namelist);
2809 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302810 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002811
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002812 ret2 = del_trace_probe_events(ufd, filter, unamelist);
2813 if (ret2 < 0 && ret2 != -ENOENT)
2814 ret = ret2;
2815 else if (ret == -ENOENT && ret2 == -ENOENT) {
2816 pr_debug("\"%s\" does not hit any event.\n", str);
2817 /* Note that this is silently ignored */
2818 ret = 0;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002819 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302820
2821error:
2822 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302823 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302824 close(kfd);
2825 }
2826
2827 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302828 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302829 close(ufd);
2830 }
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002831 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002832
2833 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002834}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302835
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002836/* TODO: don't use a global variable for filter ... */
2837static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002838
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002839/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002840 * If a symbol corresponds to a function with global binding and
2841 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002842 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002843static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002844 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002845{
Namhyung Kime578da32015-03-06 16:31:29 +09002846 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002847 return 0;
2848 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002849}
2850
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002851int show_available_funcs(const char *target, struct strfilter *_filter,
2852 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002853{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002854 struct map *map;
2855 int ret;
2856
2857 ret = init_symbol_maps(user);
2858 if (ret < 0)
2859 return ret;
2860
2861 /* Get a symbol map */
2862 if (user)
2863 map = dso__new_map(target);
2864 else
2865 map = kernel_get_module_map(target);
2866 if (!map) {
2867 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002868 return -EINVAL;
2869 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002870
2871 /* Load symbols with given filter */
2872 available_func_filter = _filter;
2873 if (map__load(map, filter_available_functions)) {
2874 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2875 goto end;
2876 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002877 if (!dso__sorted_by_name(map->dso, map->type))
2878 dso__sort_by_name(map->dso, map->type);
2879
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002880 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302881 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002882 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2883end:
2884 if (user) {
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002885 map__delete(map);
2886 }
2887 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302888
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002889 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302890}
2891