blob: d4cf50b918393e81ac43267e2e44b93713e060e3 [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 Melo1eee78a2015-05-22 12:58:53 -0300166 struct maps *maps = &grp->maps[MAP__FUNCTION];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300167 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, '/'))
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -0300171 return machine__findnew_module_map(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 */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300198 map__put(map);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900199 }
200}
201
202
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000203static int convert_exec_to_group(const char *exec, char **result)
204{
205 char *ptr1, *ptr2, *exec_copy;
206 char buf[64];
207 int ret;
208
209 exec_copy = strdup(exec);
210 if (!exec_copy)
211 return -ENOMEM;
212
213 ptr1 = basename(exec_copy);
214 if (!ptr1) {
215 ret = -EINVAL;
216 goto out;
217 }
218
219 ptr2 = strpbrk(ptr1, "-._");
220 if (ptr2)
221 *ptr2 = '\0';
222 ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
223 if (ret < 0)
224 goto out;
225
226 *result = strdup(buf);
227 ret = *result ? 0 : -ENOMEM;
228
229out:
230 free(exec_copy);
231 return ret;
232}
233
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900234static void clear_perf_probe_point(struct perf_probe_point *pp)
235{
236 free(pp->file);
237 free(pp->function);
238 free(pp->lazy_line);
239}
240
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000241static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
242{
243 int i;
244
245 for (i = 0; i < ntevs; i++)
246 clear_probe_trace_event(tevs + i);
247}
248
Ingo Molnar89fe8082013-09-30 12:07:11 +0200249#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000250
251static int kernel_get_module_dso(const char *module, struct dso **pdso)
252{
253 struct dso *dso;
254 struct map *map;
255 const char *vmlinux_name;
256 int ret = 0;
257
258 if (module) {
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300259 list_for_each_entry(dso, &host_machine->dsos.head, node) {
260 if (!dso->kernel)
261 continue;
Wang Nan60fb7742015-05-28 02:25:05 +0000262 if (strncmp(dso->short_name + 1, module,
263 dso->short_name_len - 2) == 0)
264 goto found;
265 }
266 pr_debug("Failed to find module %s.\n", module);
267 return -ENOENT;
268 }
269
270 map = host_machine->vmlinux_maps[MAP__FUNCTION];
271 dso = map->dso;
272
273 vmlinux_name = symbol_conf.vmlinux_name;
274 dso->load_errno = 0;
275 if (vmlinux_name)
276 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
277 else
278 ret = dso__load_vmlinux_path(dso, map, NULL);
279found:
280 *pdso = dso;
281 return ret;
282}
283
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900284/*
285 * Some binaries like glibc have special symbols which are on the symbol
286 * table, but not in the debuginfo. If we can find the address of the
287 * symbol from map, we can translate the address back to the probe point.
288 */
289static int find_alternative_probe_point(struct debuginfo *dinfo,
290 struct perf_probe_point *pp,
291 struct perf_probe_point *result,
292 const char *target, bool uprobes)
293{
294 struct map *map = NULL;
295 struct symbol *sym;
296 u64 address = 0;
297 int ret = -ENOENT;
298
299 /* This can work only for function-name based one */
300 if (!pp->function || pp->file)
301 return -ENOTSUP;
302
303 map = get_target_map(target, uprobes);
304 if (!map)
305 return -EINVAL;
306
307 /* Find the address of given function */
308 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900309 if (uprobes)
310 address = sym->start;
311 else
312 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900313 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900314 }
315 if (!address) {
316 ret = -ENOENT;
317 goto out;
318 }
Wang Nanf6c15622015-04-08 02:14:34 +0000319 pr_debug("Symbol %s address found : %" PRIx64 "\n",
320 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900321
322 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
323 result);
324 if (ret <= 0)
325 ret = (!ret) ? -ENOENT : ret;
326 else {
327 result->offset += pp->offset;
328 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800329 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900330 ret = 0;
331 }
332
333out:
334 put_target_map(map, uprobes);
335 return ret;
336
337}
338
339static int get_alternative_probe_event(struct debuginfo *dinfo,
340 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900341 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900342{
343 int ret;
344
345 memcpy(tmp, &pev->point, sizeof(*tmp));
346 memset(&pev->point, 0, sizeof(pev->point));
347 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900348 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900349 if (ret < 0)
350 memcpy(&pev->point, tmp, sizeof(*tmp));
351
352 return ret;
353}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000354
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900355static int get_alternative_line_range(struct debuginfo *dinfo,
356 struct line_range *lr,
357 const char *target, bool user)
358{
David Ahern6d4a4892015-03-11 10:36:20 -0400359 struct perf_probe_point pp = { .function = lr->function,
360 .file = lr->file,
361 .line = lr->start };
362 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900363 int ret, len = 0;
364
David Ahern6d4a4892015-03-11 10:36:20 -0400365 memset(&result, 0, sizeof(result));
366
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900367 if (lr->end != INT_MAX)
368 len = lr->end - lr->start;
369 ret = find_alternative_probe_point(dinfo, &pp, &result,
370 target, user);
371 if (!ret) {
372 lr->function = result.function;
373 lr->file = result.file;
374 lr->start = result.line;
375 if (lr->end != INT_MAX)
376 lr->end = lr->start + len;
377 clear_perf_probe_point(&pp);
378 }
379 return ret;
380}
381
Masami Hiramatsuff741782011-06-27 16:27:39 +0900382/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000383static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900384{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000385 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900386 char reason[STRERR_BUFSIZE];
387 struct debuginfo *ret = NULL;
388 struct dso *dso = NULL;
389 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900390
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000391 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900392 err = kernel_get_module_dso(module, &dso);
393 if (err < 0) {
394 if (!dso || dso->load_errno == 0) {
395 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
396 strcpy(reason, "(unknown)");
397 } else
398 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000399 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900400 pr_err("Failed to find the path for %s: %s\n",
401 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900402 return NULL;
403 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900404 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900405 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000406 ret = debuginfo__new(path);
407 if (!ret && !silent) {
408 pr_warning("The %s file has no debug information.\n", path);
409 if (!module || !strtailcmp(path, ".ko"))
410 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
411 else
412 pr_warning("Rebuild with -g, ");
413 pr_warning("or install an appropriate debuginfo package.\n");
414 }
415 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400416}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300417
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000418
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000419static int get_text_start_address(const char *exec, unsigned long *address)
420{
421 Elf *elf;
422 GElf_Ehdr ehdr;
423 GElf_Shdr shdr;
424 int fd, ret = -ENOENT;
425
426 fd = open(exec, O_RDONLY);
427 if (fd < 0)
428 return -errno;
429
430 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
431 if (elf == NULL)
432 return -EINVAL;
433
434 if (gelf_getehdr(elf, &ehdr) == NULL)
435 goto out;
436
437 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
438 goto out;
439
440 *address = shdr.sh_addr - shdr.sh_offset;
441 ret = 0;
442out:
443 elf_end(elf);
444 return ret;
445}
446
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000447/*
448 * Convert trace point to probe point with debuginfo
449 */
450static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
451 struct perf_probe_point *pp,
452 bool is_kprobe)
453{
454 struct debuginfo *dinfo = NULL;
455 unsigned long stext = 0;
456 u64 addr = tp->address;
457 int ret = -ENOENT;
458
459 /* convert the address to dwarf address */
460 if (!is_kprobe) {
461 if (!addr) {
462 ret = -EINVAL;
463 goto error;
464 }
465 ret = get_text_start_address(tp->module, &stext);
466 if (ret < 0)
467 goto error;
468 addr += stext;
469 } else {
470 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
471 if (addr == 0)
472 goto error;
473 addr += tp->offset;
474 }
475
476 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
477 tp->module ? : "kernel");
478
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000479 dinfo = open_debuginfo(tp->module, verbose == 0);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000480 if (dinfo) {
481 ret = debuginfo__find_probe_point(dinfo,
482 (unsigned long)addr, pp);
483 debuginfo__delete(dinfo);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000484 } else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000485 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000486
487 if (ret > 0) {
488 pp->retprobe = tp->retprobe;
489 return 0;
490 }
491error:
492 pr_debug("Failed to find corresponding probes from debuginfo.\n");
493 return ret ? : -ENOENT;
494}
495
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000496static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
497 int ntevs, const char *exec)
498{
499 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000500 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000501
502 if (!exec)
503 return 0;
504
505 ret = get_text_start_address(exec, &stext);
506 if (ret < 0)
507 return ret;
508
509 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000510 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000511 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000512 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000513 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000514 ret = -ENOMEM;
515 break;
516 }
517 tevs[i].uprobes = true;
518 }
519
520 return ret;
521}
522
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900523static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
524 int ntevs, const char *module)
525{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900526 int i, ret = 0;
527 char *tmp;
528
529 if (!module)
530 return 0;
531
532 tmp = strrchr(module, '/');
533 if (tmp) {
534 /* This is a module path -- get the module name */
535 module = strdup(tmp + 1);
536 if (!module)
537 return -ENOMEM;
538 tmp = strchr(module, '.');
539 if (tmp)
540 *tmp = '\0';
541 tmp = (char *)module; /* For free() */
542 }
543
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900544 for (i = 0; i < ntevs; i++) {
545 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900546 if (!tevs[i].point.module) {
547 ret = -ENOMEM;
548 break;
549 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900550 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900551
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300552 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900553 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900554}
555
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000556/* Post processing the probe events */
557static int post_process_probe_trace_events(struct probe_trace_event *tevs,
558 int ntevs, const char *module,
559 bool uprobe)
560{
561 struct ref_reloc_sym *reloc_sym;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900562 u64 etext_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000563 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900564 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000565
566 if (uprobe)
567 return add_exec_to_probe_trace_events(tevs, ntevs, module);
568
569 /* Note that currently ref_reloc_sym based probe is not for drivers */
570 if (module)
571 return add_module_to_probe_trace_events(tevs, ntevs, module);
572
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000573 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000574 if (!reloc_sym) {
575 pr_warning("Relocated base symbol is not found!\n");
576 return -EINVAL;
577 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900578 /* Get the address of _etext for checking non-probable text symbol */
579 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000580
581 for (i = 0; i < ntevs; i++) {
Namhyung Kim25dd9172015-01-14 20:18:08 +0900582 if (tevs[i].point.address && !tevs[i].point.retprobe) {
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900583 /* If we found a wrong one, mark it by NULL symbol */
584 if (etext_addr < tevs[i].point.address) {
585 pr_warning("%s+%lu is out of .text, skip it.\n",
586 tevs[i].point.symbol, tevs[i].point.offset);
587 tmp = NULL;
588 skipped++;
589 } else {
590 tmp = strdup(reloc_sym->name);
591 if (!tmp)
592 return -ENOMEM;
593 }
Masami Hiramatsu4c859352015-05-08 10:03:35 +0900594 /* If we have no realname, use symbol for it */
595 if (!tevs[i].point.realname)
596 tevs[i].point.realname = tevs[i].point.symbol;
597 else
598 free(tevs[i].point.symbol);
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000599 tevs[i].point.symbol = tmp;
600 tevs[i].point.offset = tevs[i].point.address -
601 reloc_sym->unrelocated_addr;
602 }
603 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900604 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000605}
606
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300607/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530608static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900609 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300610{
611 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900612 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530613 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900614 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300615
Masami Hiramatsu44225522015-05-08 10:03:28 +0900616 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900617 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000618 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900619 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900620 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300621 return 0;
622 }
623
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000624 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900625 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900626 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900627
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900628 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900629 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900630 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900631 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900632 /*
633 * Write back to the original probe_event for
634 * setting appropriate (user given) event name
635 */
636 clear_perf_probe_point(&pev->point);
637 memcpy(&pev->point, &tmp, sizeof(tmp));
638 }
639 }
640
Masami Hiramatsuff741782011-06-27 16:27:39 +0900641 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300642
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400643 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000644 pr_debug("Found %d probe_trace_events.\n", ntevs);
645 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900646 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900647 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000648 clear_probe_trace_events(*tevs, ntevs);
649 zfree(tevs);
650 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900651 if (ret != ntevs)
652 return ret < 0 ? ret : ntevs;
653 ntevs = 0;
654 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400655 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300656
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400657 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900658 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400659 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900660 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400661 }
662 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400663 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
664 if (ntevs == -EBADF) {
665 pr_warning("Warning: No dwarf info found in the vmlinux - "
666 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
667 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900668 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400669 return 0;
670 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300671 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400672 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300673}
674
675#define LINEBUF_SIZE 256
676#define NR_ADDITIONAL_LINES 2
677
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100678static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300679{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000680 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100681 const char *color = show_num ? "" : PERF_COLOR_BLUE;
682 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300683
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100684 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300685 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
686 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100687 if (skip)
688 continue;
689 if (!prefix) {
690 prefix = show_num ? "%7d " : " ";
691 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300692 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100693 color_fprintf(stdout, color, "%s", buf);
694
695 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400696
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100697 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300698error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100699 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000700 pr_warning("File read error: %s\n",
701 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100702 return -1;
703 }
704 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300705}
706
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100707static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
708{
709 int rv = __show_one_line(fp, l, skip, show_num);
710 if (rv == 0) {
711 pr_warning("Source file is shorter than expected.\n");
712 rv = -1;
713 }
714 return rv;
715}
716
717#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
718#define show_one_line(f,l) _show_one_line(f,l,false,false)
719#define skip_one_line(f,l) _show_one_line(f,l,true,false)
720#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
721
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300722/*
723 * Show line-range always requires debuginfo to find source file and
724 * line number.
725 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900726static int __show_line_range(struct line_range *lr, const char *module,
727 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300728{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400729 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000730 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900731 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300732 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900733 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900734 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000735 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300736
737 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000738 dinfo = open_debuginfo(module, false);
739 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900740 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400741
Masami Hiramatsuff741782011-06-27 16:27:39 +0900742 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900743 if (!ret) { /* Not found, retry with an alternative */
744 ret = get_alternative_line_range(dinfo, lr, module, user);
745 if (!ret)
746 ret = debuginfo__find_line_range(dinfo, lr);
747 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900748 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000749 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400750 pr_warning("Specified source line is not found.\n");
751 return -ENOENT;
752 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000753 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400754 return ret;
755 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300756
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900757 /* Convert source file path */
758 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900759 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800760
761 /* Free old path when new path is assigned */
762 if (tmp != lr->path)
763 free(tmp);
764
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900765 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000766 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900767 return ret;
768 }
769
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300770 setup_pager();
771
772 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900773 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300774 lr->start - lr->offset);
775 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100776 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300777
778 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400779 if (fp == NULL) {
780 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000781 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400782 return -errno;
783 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100785 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100786 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100787 if (ret < 0)
788 goto end;
789 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300790
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000791 intlist__for_each(ln, lr->line_list) {
792 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100793 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100794 if (ret < 0)
795 goto end;
796 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100797 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 if (ret < 0)
799 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300800 }
801
802 if (lr->end == INT_MAX)
803 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100804 while (l <= lr->end) {
805 ret = show_one_line_or_eof(fp, l++ - lr->offset);
806 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100807 break;
808 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400809end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300810 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400811 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300812}
813
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000814int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000815{
816 int ret;
817
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000818 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000819 if (ret < 0)
820 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900821 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000822 exit_symbol_maps();
823
824 return ret;
825}
826
Masami Hiramatsuff741782011-06-27 16:27:39 +0900827static int show_available_vars_at(struct debuginfo *dinfo,
828 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900829 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900830{
831 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900832 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900833 struct str_node *node;
834 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900835 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900836 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900837
838 buf = synthesize_perf_probe_point(&pev->point);
839 if (!buf)
840 return -EINVAL;
841 pr_debug("Searching variables at %s\n", buf);
842
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900843 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900844 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900845 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900846 if (!ret) {
847 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900848 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900849 /* Release the old probe_point */
850 clear_perf_probe_point(&tmp);
851 }
852 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900853 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000854 if (ret == 0 || ret == -ENOENT) {
855 pr_err("Failed to find the address of %s\n", buf);
856 ret = -ENOENT;
857 } else
858 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900859 goto end;
860 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000861
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900862 /* Some variables are found */
863 fprintf(stdout, "Available variables at %s\n", buf);
864 for (i = 0; i < ret; i++) {
865 vl = &vls[i];
866 /*
867 * A probe point might be converted to
868 * several trace points.
869 */
870 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
871 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300872 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900873 nvars = 0;
874 if (vl->vars) {
875 strlist__for_each(node, vl->vars) {
876 var = strchr(node->s, '\t') + 1;
877 if (strfilter__compare(_filter, var)) {
878 fprintf(stdout, "\t\t%s\n", node->s);
879 nvars++;
880 }
881 }
882 strlist__delete(vl->vars);
883 }
884 if (nvars == 0)
885 fprintf(stdout, "\t\t(No matched variables)\n");
886 }
887 free(vls);
888end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900889 free(buf);
890 return ret;
891}
892
893/* Show available variables on given probe point */
894int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900895 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900896{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900897 int i, ret = 0;
898 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900899
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000900 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900901 if (ret < 0)
902 return ret;
903
Masami Hiramatsu44225522015-05-08 10:03:28 +0900904 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900905 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000906 ret = -ENOENT;
907 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900908 }
909
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900910 setup_pager();
911
Masami Hiramatsuff741782011-06-27 16:27:39 +0900912 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900913 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900914
915 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000916out:
917 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900918 return ret;
919}
920
Ingo Molnar89fe8082013-09-30 12:07:11 +0200921#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300922
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000923static int
924find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
925 struct perf_probe_point *pp __maybe_unused,
926 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300927{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000928 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300929}
930
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530931static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900932 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300933{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400934 if (perf_probe_event_need_dwarf(pev)) {
935 pr_warning("Debuginfo-analysis is not supported.\n");
936 return -ENOSYS;
937 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530938
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300939 return 0;
940}
941
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300942int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000943 const char *module __maybe_unused,
944 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300945{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400946 pr_warning("Debuginfo-analysis is not supported.\n");
947 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300948}
949
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300950int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900951 int npevs __maybe_unused,
952 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900953{
954 pr_warning("Debuginfo-analysis is not supported.\n");
955 return -ENOSYS;
956}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400957#endif
958
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000959void line_range__clear(struct line_range *lr)
960{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000961 free(lr->function);
962 free(lr->file);
963 free(lr->path);
964 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000965 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000966 memset(lr, 0, sizeof(*lr));
967}
968
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000969int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000970{
971 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000972 lr->line_list = intlist__new(NULL);
973 if (!lr->line_list)
974 return -ENOMEM;
975 else
976 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +0000977}
978
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +0100979static int parse_line_num(char **ptr, int *val, const char *what)
980{
981 const char *start = *ptr;
982
983 errno = 0;
984 *val = strtol(*ptr, ptr, 0);
985 if (errno || *ptr == start) {
986 semantic_error("'%s' is not a valid number.\n", what);
987 return -EINVAL;
988 }
989 return 0;
990}
991
Masami Hiramatsu573709f2015-05-06 21:46:47 +0900992/* Check the name is good for event, group or function */
993static bool is_c_func_name(const char *name)
994{
995 if (!isalpha(*name) && *name != '_')
996 return false;
997 while (*++name != '\0') {
998 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
999 return false;
1000 }
1001 return true;
1002}
1003
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001004/*
1005 * Stuff 'lr' according to the line range described by 'arg'.
1006 * The line range syntax is described by:
1007 *
1008 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001009 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001010 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001011int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001012{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001013 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001014 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001015
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001016 if (!name)
1017 return -ENOMEM;
1018
1019 lr->start = 0;
1020 lr->end = INT_MAX;
1021
1022 range = strchr(name, ':');
1023 if (range) {
1024 *range++ = '\0';
1025
1026 err = parse_line_num(&range, &lr->start, "start line");
1027 if (err)
1028 goto err;
1029
1030 if (*range == '+' || *range == '-') {
1031 const char c = *range++;
1032
1033 err = parse_line_num(&range, &lr->end, "end line");
1034 if (err)
1035 goto err;
1036
1037 if (c == '+') {
1038 lr->end += lr->start;
1039 /*
1040 * Adjust the number of lines here.
1041 * If the number of lines == 1, the
1042 * the end of line should be equal to
1043 * the start of line.
1044 */
1045 lr->end--;
1046 }
1047 }
1048
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001049 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001050
1051 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001052 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001053 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001054 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001055 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001056 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001057 if (*range != '\0') {
1058 semantic_error("Tailing with invalid str '%s'.\n", range);
1059 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001060 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001061 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001062
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001063 file = strchr(name, '@');
1064 if (file) {
1065 *file = '\0';
1066 lr->file = strdup(++file);
1067 if (lr->file == NULL) {
1068 err = -ENOMEM;
1069 goto err;
1070 }
1071 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001072 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001073 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001074 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001075 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001076 else { /* Invalid name */
1077 semantic_error("'%s' is not a valid function name.\n", name);
1078 err = -EINVAL;
1079 goto err;
1080 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001081
1082 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001083err:
1084 free(name);
1085 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001086}
1087
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001088/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001089static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001090{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001091 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001092 char *ptr, *tmp;
1093 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301094 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001095 /*
1096 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001097 * perf probe [EVENT=]SRC[:LN|;PTN]
1098 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001099 *
1100 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001101 */
Wang Nane59d29e2015-04-28 08:46:09 +00001102 if (!arg)
1103 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001104
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001105 ptr = strpbrk(arg, ";=@+%");
1106 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001107 *ptr = '\0';
1108 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001109 if (strchr(arg, ':')) {
1110 semantic_error("Group name is not supported yet.\n");
1111 return -ENOTSUP;
1112 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001113 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001114 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001115 "follow C symbol-naming rule.\n", arg);
1116 return -EINVAL;
1117 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001118 pev->event = strdup(arg);
1119 if (pev->event == NULL)
1120 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001121 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001122 arg = tmp;
1123 }
1124
Naveen N. Rao3099c022015-04-28 17:35:34 +05301125 /*
1126 * Check arg is function or file name and copy it.
1127 *
1128 * We consider arg to be a file spec if and only if it satisfies
1129 * all of the below criteria::
1130 * - it does not include any of "+@%",
1131 * - it includes one of ":;", and
1132 * - it has a period '.' in the name.
1133 *
1134 * Otherwise, we consider arg to be a function specification.
1135 */
1136 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1137 /* This is a file spec if it includes a '.' before ; or : */
1138 if (memchr(arg, '.', ptr - arg))
1139 file_spec = true;
1140 }
1141
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001142 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001143 if (ptr) {
1144 nc = *ptr;
1145 *ptr++ = '\0';
1146 }
1147
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001148 tmp = strdup(arg);
1149 if (tmp == NULL)
1150 return -ENOMEM;
1151
Naveen N. Rao3099c022015-04-28 17:35:34 +05301152 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001153 pp->file = tmp;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301154 else
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001155 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001156
1157 /* Parse other options */
1158 while (ptr) {
1159 arg = ptr;
1160 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001161 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001162 pp->lazy_line = strdup(arg);
1163 if (pp->lazy_line == NULL)
1164 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001165 break;
1166 }
1167 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001168 if (ptr) {
1169 nc = *ptr;
1170 *ptr++ = '\0';
1171 }
1172 switch (c) {
1173 case ':': /* Line number */
1174 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001175 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001176 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001177 " in line number.\n");
1178 return -EINVAL;
1179 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001180 break;
1181 case '+': /* Byte offset from a symbol */
1182 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001183 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001184 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001185 " in offset.\n");
1186 return -EINVAL;
1187 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001188 break;
1189 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001190 if (pp->file) {
1191 semantic_error("SRC@SRC is not allowed.\n");
1192 return -EINVAL;
1193 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001194 pp->file = strdup(arg);
1195 if (pp->file == NULL)
1196 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001197 break;
1198 case '%': /* Probe places */
1199 if (strcmp(arg, "return") == 0) {
1200 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001201 } else { /* Others not supported yet */
1202 semantic_error("%%%s is not supported.\n", arg);
1203 return -ENOTSUP;
1204 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001205 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001206 default: /* Buggy case */
1207 pr_err("This program has a bug at %s:%d.\n",
1208 __FILE__, __LINE__);
1209 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001210 break;
1211 }
1212 }
1213
1214 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001215 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001216 semantic_error("Lazy pattern can't be used with"
1217 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001218 return -EINVAL;
1219 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001220
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001221 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001222 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001223 return -EINVAL;
1224 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001225
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001226 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001227 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001228 return -EINVAL;
1229 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001230
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001231 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001232 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001233 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001234 return -EINVAL;
1235 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001236
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001238 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001239 return -EINVAL;
1240 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001241
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001243 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001244 return -EINVAL;
1245 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001246
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001247 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001248 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001249 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001250 return -EINVAL;
1251 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001252
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001253 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001254 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1255 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001256 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257}
1258
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001259/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001260static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001261{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001262 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001263 struct perf_probe_arg_field **fieldp;
1264
1265 pr_debug("parsing arg: %s into ", str);
1266
Masami Hiramatsu48481932010-04-12 13:16:53 -04001267 tmp = strchr(str, '=');
1268 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001269 arg->name = strndup(str, tmp - str);
1270 if (arg->name == NULL)
1271 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001272 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001273 str = tmp + 1;
1274 }
1275
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001276 tmp = strchr(str, ':');
1277 if (tmp) { /* Type setting */
1278 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001279 arg->type = strdup(tmp + 1);
1280 if (arg->type == NULL)
1281 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001282 pr_debug("type:%s ", arg->type);
1283 }
1284
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001285 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001286 if (!is_c_varname(str) || !tmp) {
1287 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001288 arg->var = strdup(str);
1289 if (arg->var == NULL)
1290 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001291 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001292 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001293 }
1294
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001295 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001296 arg->var = strndup(str, tmp - str);
1297 if (arg->var == NULL)
1298 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001299 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001300 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001301 fieldp = &arg->field;
1302
1303 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001304 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1305 if (*fieldp == NULL)
1306 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001307 if (*tmp == '[') { /* Array */
1308 str = tmp;
1309 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001310 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001311 if (*tmp != ']' || tmp == str + 1) {
1312 semantic_error("Array index must be a"
1313 " number.\n");
1314 return -EINVAL;
1315 }
1316 tmp++;
1317 if (*tmp == '\0')
1318 tmp = NULL;
1319 } else { /* Structure */
1320 if (*tmp == '.') {
1321 str = tmp + 1;
1322 (*fieldp)->ref = false;
1323 } else if (tmp[1] == '>') {
1324 str = tmp + 2;
1325 (*fieldp)->ref = true;
1326 } else {
1327 semantic_error("Argument parse error: %s\n",
1328 str);
1329 return -EINVAL;
1330 }
1331 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001332 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001333 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001334 (*fieldp)->name = strndup(str, tmp - str);
1335 if ((*fieldp)->name == NULL)
1336 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001337 if (*str != '[')
1338 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001339 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1340 fieldp = &(*fieldp)->next;
1341 }
1342 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001343 (*fieldp)->name = strdup(str);
1344 if ((*fieldp)->name == NULL)
1345 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001346 if (*str != '[')
1347 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001348 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001349
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001350 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001351 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001352 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001353 if (arg->name == NULL)
1354 return -ENOMEM;
1355 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001356 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001357}
1358
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001359/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001360int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001361{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001362 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001363 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001364
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001365 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001366 if (!argv) {
1367 pr_debug("Failed to split arguments.\n");
1368 return -ENOMEM;
1369 }
1370 if (argc - 1 > MAX_PROBE_ARGS) {
1371 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1372 ret = -ERANGE;
1373 goto out;
1374 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001375 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001376 ret = parse_perf_probe_point(argv[0], pev);
1377 if (ret < 0)
1378 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001379
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001380 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001381 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001382 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1383 if (pev->args == NULL) {
1384 ret = -ENOMEM;
1385 goto out;
1386 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001387 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1388 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1389 if (ret >= 0 &&
1390 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001391 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001392 " kretprobe.\n");
1393 ret = -EINVAL;
1394 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001395 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001396out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001397 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001398
1399 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001400}
1401
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001402/* Return true if this perf_probe_event requires debuginfo */
1403bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001404{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001405 int i;
1406
1407 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1408 return true;
1409
1410 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001411 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001412 return true;
1413
1414 return false;
1415}
1416
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301417/* Parse probe_events event into struct probe_point */
1418static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001419 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001420{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301421 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001422 char pr;
1423 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001424 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001425 int ret, i, argc;
1426 char **argv;
1427
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301428 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001429 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001430 if (!argv) {
1431 pr_debug("Failed to split arguments.\n");
1432 return -ENOMEM;
1433 }
1434 if (argc < 2) {
1435 semantic_error("Too few probe arguments.\n");
1436 ret = -ERANGE;
1437 goto out;
1438 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001439
1440 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001441 argv0_str = strdup(argv[0]);
1442 if (argv0_str == NULL) {
1443 ret = -ENOMEM;
1444 goto out;
1445 }
1446 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1447 fmt2_str = strtok_r(NULL, "/", &fmt);
1448 fmt3_str = strtok_r(NULL, " \t", &fmt);
1449 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1450 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001451 semantic_error("Failed to parse event name: %s\n", argv[0]);
1452 ret = -EINVAL;
1453 goto out;
1454 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001455 pr = fmt1_str[0];
1456 tev->group = strdup(fmt2_str);
1457 tev->event = strdup(fmt3_str);
1458 if (tev->group == NULL || tev->event == NULL) {
1459 ret = -ENOMEM;
1460 goto out;
1461 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001462 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001463
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001465
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001466 /* Scan module name(if there), function name and offset */
1467 p = strchr(argv[1], ':');
1468 if (p) {
1469 tp->module = strndup(argv[1], p - argv[1]);
1470 p++;
1471 } else
1472 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001473 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001474 if (fmt1_str[0] == '0') /* only the address started with 0x */
1475 tp->address = strtoul(fmt1_str, NULL, 0);
1476 else {
1477 /* Only the symbol-based probe has offset */
1478 tp->symbol = strdup(fmt1_str);
1479 if (tp->symbol == NULL) {
1480 ret = -ENOMEM;
1481 goto out;
1482 }
1483 fmt2_str = strtok_r(NULL, "", &fmt);
1484 if (fmt2_str == NULL)
1485 tp->offset = 0;
1486 else
1487 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001488 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001489
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001490 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301491 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001492 if (tev->args == NULL) {
1493 ret = -ENOMEM;
1494 goto out;
1495 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001496 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001497 p = strchr(argv[i + 2], '=');
1498 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001499 *p++ = '\0';
1500 else
1501 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001502 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001503 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001504 tev->args[i].value = strdup(p);
1505 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1506 ret = -ENOMEM;
1507 goto out;
1508 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001509 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001510 ret = 0;
1511out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001512 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001513 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001514 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001515}
1516
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001517/* Compose only probe arg */
1518int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1519{
1520 struct perf_probe_arg_field *field = pa->field;
1521 int ret;
1522 char *tmp = buf;
1523
Masami Hiramatsu48481932010-04-12 13:16:53 -04001524 if (pa->name && pa->var)
1525 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1526 else
1527 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001528 if (ret <= 0)
1529 goto error;
1530 tmp += ret;
1531 len -= ret;
1532
1533 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001534 if (field->name[0] == '[')
1535 ret = e_snprintf(tmp, len, "%s", field->name);
1536 else
1537 ret = e_snprintf(tmp, len, "%s%s",
1538 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001539 if (ret <= 0)
1540 goto error;
1541 tmp += ret;
1542 len -= ret;
1543 field = field->next;
1544 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001545
1546 if (pa->type) {
1547 ret = e_snprintf(tmp, len, ":%s", pa->type);
1548 if (ret <= 0)
1549 goto error;
1550 tmp += ret;
1551 len -= ret;
1552 }
1553
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001554 return tmp - buf;
1555error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001556 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001557 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001558}
1559
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001560/* Compose only probe point (not argument) */
1561static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001562{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001563 char *buf, *tmp;
1564 char offs[32] = "", line[32] = "", file[32] = "";
1565 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001566
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001567 buf = zalloc(MAX_CMDLEN);
1568 if (buf == NULL) {
1569 ret = -ENOMEM;
1570 goto error;
1571 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001572 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001573 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001574 if (ret <= 0)
1575 goto error;
1576 }
1577 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001578 ret = e_snprintf(line, 32, ":%d", pp->line);
1579 if (ret <= 0)
1580 goto error;
1581 }
1582 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001583 tmp = pp->file;
1584 len = strlen(tmp);
1585 if (len > 30) {
1586 tmp = strchr(pp->file + len - 30, '/');
1587 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1588 }
1589 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001590 if (ret <= 0)
1591 goto error;
1592 }
1593
1594 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001595 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1596 offs, pp->retprobe ? "%return" : "", line,
1597 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001598 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001599 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001600 if (ret <= 0)
1601 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001602
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001603 return buf;
1604error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001605 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001606 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001607 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001608}
1609
1610#if 0
1611char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1612{
1613 char *buf;
1614 int i, len, ret;
1615
1616 buf = synthesize_perf_probe_point(&pev->point);
1617 if (!buf)
1618 return NULL;
1619
1620 len = strlen(buf);
1621 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001622 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001623 pev->args[i].name);
1624 if (ret <= 0) {
1625 free(buf);
1626 return NULL;
1627 }
1628 len += ret;
1629 }
1630
1631 return buf;
1632}
1633#endif
1634
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301635static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001636 char **buf, size_t *buflen,
1637 int depth)
1638{
1639 int ret;
1640 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301641 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001642 buflen, depth + 1);
1643 if (depth < 0)
1644 goto out;
1645 }
1646
1647 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1648 if (ret < 0)
1649 depth = ret;
1650 else {
1651 *buf += ret;
1652 *buflen -= ret;
1653 }
1654out:
1655 return depth;
1656
1657}
1658
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301659static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001660 char *buf, size_t buflen)
1661{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301662 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001663 int ret, depth = 0;
1664 char *tmp = buf;
1665
1666 /* Argument name or separator */
1667 if (arg->name)
1668 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1669 else
1670 ret = e_snprintf(buf, buflen, " ");
1671 if (ret < 0)
1672 return ret;
1673 buf += ret;
1674 buflen -= ret;
1675
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001676 /* Special case: @XXX */
1677 if (arg->value[0] == '@' && arg->ref)
1678 ref = ref->next;
1679
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001680 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001681 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301682 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001683 &buflen, 1);
1684 if (depth < 0)
1685 return depth;
1686 }
1687
1688 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001689 if (arg->value[0] == '@' && arg->ref)
1690 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1691 arg->ref->offset);
1692 else
1693 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694 if (ret < 0)
1695 return ret;
1696 buf += ret;
1697 buflen -= ret;
1698
1699 /* Closing */
1700 while (depth--) {
1701 ret = e_snprintf(buf, buflen, ")");
1702 if (ret < 0)
1703 return ret;
1704 buf += ret;
1705 buflen -= ret;
1706 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001707 /* Print argument type */
1708 if (arg->type) {
1709 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1710 if (ret <= 0)
1711 return ret;
1712 buf += ret;
1713 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001714
1715 return buf - tmp;
1716}
1717
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301718char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001719{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301720 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001721 char *buf;
1722 int i, len, ret;
1723
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001724 buf = zalloc(MAX_CMDLEN);
1725 if (buf == NULL)
1726 return NULL;
1727
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001728 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1729 tev->group, tev->event);
1730 if (len <= 0)
1731 goto error;
1732
1733 /* Uprobes must have tp->address and tp->module */
1734 if (tev->uprobes && (!tp->address || !tp->module))
1735 goto error;
1736
1737 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301738 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001739 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1740 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301741 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001742 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301743 tp->module ?: "", tp->module ? ":" : "",
1744 tp->symbol, tp->offset);
1745
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001746 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001747 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001748 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001749
1750 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301751 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001752 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001753 if (ret <= 0)
1754 goto error;
1755 len += ret;
1756 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001757
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001758 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001759error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001760 free(buf);
1761 return NULL;
1762}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001763
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001764static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1765 struct perf_probe_point *pp,
1766 bool is_kprobe)
1767{
1768 struct symbol *sym = NULL;
1769 struct map *map;
1770 u64 addr;
1771 int ret = -ENOENT;
1772
1773 if (!is_kprobe) {
1774 map = dso__new_map(tp->module);
1775 if (!map)
1776 goto out;
1777 addr = tp->address;
1778 sym = map__find_symbol(map, addr, NULL);
1779 } else {
1780 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1781 if (addr) {
1782 addr += tp->offset;
1783 sym = __find_kernel_function(addr, &map);
1784 }
1785 }
1786 if (!sym)
1787 goto out;
1788
1789 pp->retprobe = tp->retprobe;
1790 pp->offset = addr - map->unmap_ip(map, sym->start);
1791 pp->function = strdup(sym->name);
1792 ret = pp->function ? 0 : -ENOMEM;
1793
1794out:
1795 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001796 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001797 }
1798
1799 return ret;
1800}
1801
1802static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1803 struct perf_probe_point *pp,
1804 bool is_kprobe)
1805{
1806 char buf[128];
1807 int ret;
1808
1809 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1810 if (!ret)
1811 return 0;
1812 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1813 if (!ret)
1814 return 0;
1815
1816 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1817
1818 if (tp->symbol) {
1819 pp->function = strdup(tp->symbol);
1820 pp->offset = tp->offset;
1821 } else if (!tp->module && !is_kprobe) {
1822 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1823 if (ret < 0)
1824 return ret;
1825 pp->function = strdup(buf);
1826 pp->offset = 0;
1827 }
1828 if (pp->function == NULL)
1829 return -ENOMEM;
1830
1831 pp->retprobe = tp->retprobe;
1832
1833 return 0;
1834}
1835
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301836static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301837 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001838{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001839 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001840 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001841
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001842 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001843 pev->event = strdup(tev->event);
1844 pev->group = strdup(tev->group);
1845 if (pev->event == NULL || pev->group == NULL)
1846 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001847
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001848 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001849 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001850 if (ret < 0)
1851 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001852
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001853 /* Convert trace_arg to probe_arg */
1854 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001855 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1856 if (pev->args == NULL)
1857 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001858 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001859 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001860 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001861 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301862 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001863 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001864 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001865 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001866 if (pev->args[i].name == NULL && ret >= 0)
1867 ret = -ENOMEM;
1868 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001869
1870 if (ret < 0)
1871 clear_perf_probe_event(pev);
1872
1873 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001874}
1875
1876void clear_perf_probe_event(struct perf_probe_event *pev)
1877{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001878 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001879 int i;
1880
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001881 free(pev->event);
1882 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001883 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001884 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001885
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001886 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001887 free(pev->args[i].name);
1888 free(pev->args[i].var);
1889 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001890 field = pev->args[i].field;
1891 while (field) {
1892 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001893 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001894 free(field);
1895 field = next;
1896 }
1897 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001898 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001899 memset(pev, 0, sizeof(*pev));
1900}
1901
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301902static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001903{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301904 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001905 int i;
1906
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001907 free(tev->event);
1908 free(tev->group);
1909 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001910 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001911 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001912 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001913 free(tev->args[i].name);
1914 free(tev->args[i].value);
1915 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001916 ref = tev->args[i].ref;
1917 while (ref) {
1918 next = ref->next;
1919 free(ref);
1920 ref = next;
1921 }
1922 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001923 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001924 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001925}
1926
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001927static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301928{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001929 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301930
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001931 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301932 const char *config;
1933
1934 if (!is_kprobe)
1935 config = "CONFIG_UPROBE_EVENTS";
1936 else
1937 config = "CONFIG_KPROBE_EVENTS";
1938
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001939 pr_warning("%cprobe_events file does not exist"
1940 " - please rebuild kernel with %s.\n",
1941 is_kprobe ? 'k' : 'u', config);
1942 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001943 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001944 else
1945 pr_warning("Failed to open %cprobe_events: %s\n",
1946 is_kprobe ? 'k' : 'u',
1947 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301948}
1949
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001950static void print_both_open_warning(int kerr, int uerr)
1951{
1952 /* Both kprobes and uprobes are disabled, warn it. */
1953 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001954 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001955 else if (kerr == -ENOENT && uerr == -ENOENT)
1956 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
1957 "or/and CONFIG_UPROBE_EVENTS.\n");
1958 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001959 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00001960 pr_warning("Failed to open kprobe events: %s.\n",
1961 strerror_r(-kerr, sbuf, sizeof(sbuf)));
1962 pr_warning("Failed to open uprobe events: %s.\n",
1963 strerror_r(-uerr, sbuf, sizeof(sbuf)));
1964 }
1965}
1966
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001967static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001968{
1969 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001970 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001971 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001972 int ret;
1973
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001974 __debugfs = tracefs_find_mountpoint();
1975 if (__debugfs == NULL) {
1976 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001977
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001978 __debugfs = debugfs_find_mountpoint();
1979 if (__debugfs == NULL)
1980 return -ENOTSUP;
1981 }
1982
1983 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
1984 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001985 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04001986 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001987 if (readwrite && !probe_event_dry_run)
Masami Hiramatsub8dc3982015-05-06 21:46:42 +09001988 ret = open(buf, O_RDWR | O_APPEND, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001989 else
1990 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04001991
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301992 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001993 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001994 }
1995 return ret;
1996}
1997
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301998static int open_kprobe_events(bool readwrite)
1999{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002000 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302001}
2002
2003static int open_uprobe_events(bool readwrite)
2004{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002005 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302006}
2007
2008/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302009static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002010{
2011 int ret, idx;
2012 FILE *fp;
2013 char buf[MAX_CMDLEN];
2014 char *p;
2015 struct strlist *sl;
2016
2017 sl = strlist__new(true, NULL);
2018
2019 fp = fdopen(dup(fd), "r");
2020 while (!feof(fp)) {
2021 p = fgets(buf, MAX_CMDLEN, fp);
2022 if (!p)
2023 break;
2024
2025 idx = strlen(p) - 1;
2026 if (p[idx] == '\n')
2027 p[idx] = '\0';
2028 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002029 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002030 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002031 strlist__delete(sl);
2032 return NULL;
2033 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002034 }
2035 fclose(fp);
2036
2037 return sl;
2038}
2039
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002040struct kprobe_blacklist_node {
2041 struct list_head list;
2042 unsigned long start;
2043 unsigned long end;
2044 char *symbol;
2045};
2046
2047static void kprobe_blacklist__delete(struct list_head *blacklist)
2048{
2049 struct kprobe_blacklist_node *node;
2050
2051 while (!list_empty(blacklist)) {
2052 node = list_first_entry(blacklist,
2053 struct kprobe_blacklist_node, list);
2054 list_del(&node->list);
2055 free(node->symbol);
2056 free(node);
2057 }
2058}
2059
2060static int kprobe_blacklist__load(struct list_head *blacklist)
2061{
2062 struct kprobe_blacklist_node *node;
2063 const char *__debugfs = debugfs_find_mountpoint();
2064 char buf[PATH_MAX], *p;
2065 FILE *fp;
2066 int ret;
2067
2068 if (__debugfs == NULL)
2069 return -ENOTSUP;
2070
2071 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2072 if (ret < 0)
2073 return ret;
2074
2075 fp = fopen(buf, "r");
2076 if (!fp)
2077 return -errno;
2078
2079 ret = 0;
2080 while (fgets(buf, PATH_MAX, fp)) {
2081 node = zalloc(sizeof(*node));
2082 if (!node) {
2083 ret = -ENOMEM;
2084 break;
2085 }
2086 INIT_LIST_HEAD(&node->list);
2087 list_add_tail(&node->list, blacklist);
2088 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2089 ret = -EINVAL;
2090 break;
2091 }
2092 p = strchr(buf, '\t');
2093 if (p) {
2094 p++;
2095 if (p[strlen(p) - 1] == '\n')
2096 p[strlen(p) - 1] = '\0';
2097 } else
2098 p = (char *)"unknown";
2099 node->symbol = strdup(p);
2100 if (!node->symbol) {
2101 ret = -ENOMEM;
2102 break;
2103 }
2104 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2105 node->start, node->end, node->symbol);
2106 ret++;
2107 }
2108 if (ret < 0)
2109 kprobe_blacklist__delete(blacklist);
2110 fclose(fp);
2111
2112 return ret;
2113}
2114
2115static struct kprobe_blacklist_node *
2116kprobe_blacklist__find_by_address(struct list_head *blacklist,
2117 unsigned long address)
2118{
2119 struct kprobe_blacklist_node *node;
2120
2121 list_for_each_entry(node, blacklist, list) {
2122 if (node->start <= address && address <= node->end)
2123 return node;
2124 }
2125
2126 return NULL;
2127}
2128
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002129/* Show an event */
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002130static int show_perf_probe_event(struct perf_probe_event *pev,
2131 const char *module)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002132{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002133 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002134 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002135 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002136
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002137 /* Synthesize only event probe point */
2138 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002139 if (!place)
2140 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002141
2142 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002143 if (ret < 0)
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002144 return ret;
2145
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002146 pr_info(" %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002147 if (module)
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002148 pr_info(" in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002149
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002150 if (pev->nargs > 0) {
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002151 pr_info(" with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002152 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002153 ret = synthesize_perf_probe_arg(&pev->args[i],
2154 buf, 128);
2155 if (ret < 0)
2156 break;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002157 pr_info(" %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002158 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002159 }
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002160 pr_info(")\n");
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002161 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002162 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002163}
2164
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002165static bool filter_probe_trace_event(struct probe_trace_event *tev,
2166 struct strfilter *filter)
2167{
2168 char tmp[128];
2169
2170 /* At first, check the event name itself */
2171 if (strfilter__compare(filter, tev->event))
2172 return true;
2173
2174 /* Next, check the combination of name and group */
2175 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2176 return false;
2177 return strfilter__compare(filter, tmp);
2178}
2179
2180static int __show_perf_probe_events(int fd, bool is_kprobe,
2181 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002182{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302183 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302184 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002185 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002186 struct strlist *rawlist;
2187 struct str_node *ent;
2188
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002189 memset(&tev, 0, sizeof(tev));
2190 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002191
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302192 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002193 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002194 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002195
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002196 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302197 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002198 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002199 if (!filter_probe_trace_event(&tev, filter))
2200 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302201 ret = convert_to_perf_probe_event(&tev, &pev,
2202 is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002203 if (ret >= 0)
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002204 ret = show_perf_probe_event(&pev,
2205 tev.point.module);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002206 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002207next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002208 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302209 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002210 if (ret < 0)
2211 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002212 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002213 strlist__delete(rawlist);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002214
2215 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002216}
2217
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302218/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002219int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302220{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002221 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302222
2223 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302224
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002225 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302226 if (ret < 0)
2227 return ret;
2228
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002229 kp_fd = open_kprobe_events(false);
2230 if (kp_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002231 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002232 close(kp_fd);
2233 if (ret < 0)
2234 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302235 }
2236
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002237 up_fd = open_uprobe_events(false);
2238 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002239 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002240 ret = kp_fd;
2241 goto out;
2242 }
2243
2244 if (up_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002245 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002246 close(up_fd);
2247 }
2248out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002249 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302250 return ret;
2251}
2252
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002253/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302254static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002255{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002256 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002257 struct strlist *sl, *rawlist;
2258 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302259 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002260 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002261
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002262 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302263 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002264 if (!rawlist)
2265 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002266 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002267 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302268 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002269 if (ret < 0)
2270 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002271 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002272 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2273 tev.event);
2274 if (ret >= 0)
2275 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002276 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002277 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302278 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002279 if (ret < 0)
2280 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002281 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002282 strlist__delete(rawlist);
2283
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002284 if (ret < 0) {
2285 strlist__delete(sl);
2286 return NULL;
2287 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002288 return sl;
2289}
2290
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302291static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002292{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002293 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302294 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002295 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002296
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002297 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302298 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002299 return -EINVAL;
2300 }
2301
Masami Hiramatsufa282442009-12-08 17:03:23 -05002302 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002303 if (!probe_event_dry_run) {
2304 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002305 if (ret <= 0) {
2306 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002307 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002308 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002309 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002310 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002311 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002312 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002313}
2314
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002315static int get_new_event_name(char *buf, size_t len, const char *base,
2316 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002317{
2318 int i, ret;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002319
Naveen N. Rao3099c022015-04-28 17:35:34 +05302320 if (*base == '.')
2321 base++;
2322
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002323 /* Try no suffix */
2324 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002325 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002326 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002327 return ret;
2328 }
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002329 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002330 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002331
Masami Hiramatsud761b082009-12-15 10:32:25 -05002332 if (!allow_suffix) {
2333 pr_warning("Error: event \"%s\" already exists. "
2334 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002335 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002336 }
2337
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002338 /* Try to add suffix */
2339 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002340 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002341 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002342 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002343 return ret;
2344 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002345 if (!strlist__has_entry(namelist, buf))
2346 break;
2347 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002348 if (i == MAX_EVENT_INDEX) {
2349 pr_warning("Too many events are on the same function.\n");
2350 ret = -ERANGE;
2351 }
2352
2353 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002354}
2355
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002356/* Warn if the current kernel's uprobe implementation is old */
2357static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2358{
2359 int i;
2360 char *buf = synthesize_probe_trace_command(tev);
2361
2362 /* Old uprobe event doesn't support memory dereference */
2363 if (!tev->uprobes || tev->nargs == 0 || !buf)
2364 goto out;
2365
2366 for (i = 0; i < tev->nargs; i++)
2367 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2368 pr_warning("Please upgrade your kernel to at least "
2369 "3.14 to have access to feature %s\n",
2370 tev->args[i].value);
2371 break;
2372 }
2373out:
2374 free(buf);
2375}
2376
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302377static int __add_probe_trace_events(struct perf_probe_event *pev,
2378 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002379 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002380{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002381 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302382 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002383 char buf[64];
2384 const char *event, *group;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002385 struct strlist *namelist;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002386 LIST_HEAD(blacklist);
2387 struct kprobe_blacklist_node *node;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002388 bool safename;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002389
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302390 if (pev->uprobes)
2391 fd = open_uprobe_events(true);
2392 else
2393 fd = open_kprobe_events(true);
2394
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002395 if (fd < 0) {
2396 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002397 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002398 }
2399
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002400 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302401 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002402 if (!namelist) {
2403 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002404 ret = -ENOMEM;
2405 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002406 }
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002407 /* Get kprobe blacklist if exists */
2408 if (!pev->uprobes) {
2409 ret = kprobe_blacklist__load(&blacklist);
2410 if (ret < 0)
2411 pr_debug("No kprobe blacklist support, ignored\n");
2412 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002413
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002414 safename = (pev->point.function && !strisglob(pev->point.function));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002415 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002416 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002417 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002418 tev = &tevs[i];
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002419 /* Skip if the symbol is out of .text (marked previously) */
2420 if (!tev->point.symbol)
2421 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002422 /* Ensure that the address is NOT blacklisted */
2423 node = kprobe_blacklist__find_by_address(&blacklist,
2424 tev->point.address);
2425 if (node) {
2426 pr_warning("Warning: Skipped probing on blacklisted function: %s\n", node->symbol);
2427 continue;
2428 }
2429
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002430 if (pev->event)
2431 event = pev->event;
2432 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002433 if (safename)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002434 event = pev->point.function;
2435 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002436 event = tev->point.realname;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002437 if (pev->group)
2438 group = pev->group;
2439 else
2440 group = PERFPROBE_GROUP;
2441
2442 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002443 ret = get_new_event_name(buf, 64, event,
2444 namelist, allow_suffix);
2445 if (ret < 0)
2446 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002447 event = buf;
2448
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002449 tev->event = strdup(event);
2450 tev->group = strdup(group);
2451 if (tev->event == NULL || tev->group == NULL) {
2452 ret = -ENOMEM;
2453 break;
2454 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302455 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002456 if (ret < 0)
2457 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002458 /* Add added event name to namelist */
2459 strlist__add(namelist, event);
2460
2461 /* Trick here - save current event/group */
2462 event = pev->event;
2463 group = pev->group;
2464 pev->event = tev->event;
2465 pev->group = tev->group;
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002466 show_perf_probe_event(pev, tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002467 /* Trick here - restore current event/group */
2468 pev->event = (char *)event;
2469 pev->group = (char *)group;
2470
2471 /*
2472 * Probes after the first probe which comes from same
2473 * user input are always allowed to add suffix, because
2474 * there might be several addresses corresponding to
2475 * one code line.
2476 */
2477 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002478 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002479 if (ret == -EINVAL && pev->uprobes)
2480 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002481
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002482 /* Note that it is possible to skip all events because of blacklist */
2483 if (ret >= 0 && tev->event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002484 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002485 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
2486 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002487 tev->event);
2488 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002489
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002490 kprobe_blacklist__delete(&blacklist);
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002491 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002492close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002493 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002494 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002495}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002496
Wang Nan6bb536c2015-05-29 09:45:47 +00002497static int find_probe_functions(struct map *map, char *name,
2498 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002499{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002500 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002501 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002502 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002503
Wang Nan75e4a2a2015-05-15 12:14:44 +00002504 if (map__load(map, NULL) < 0)
2505 return 0;
2506
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002507 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002508 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002509 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002510 if (syms && found < probe_conf.max_probes)
2511 syms[found - 1] = sym;
2512 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002513 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002514
2515 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002516}
2517
2518#define strdup_or_goto(str, label) \
2519 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2520
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302521void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2522 struct probe_trace_event *tev __maybe_unused,
2523 struct map *map __maybe_unused) { }
2524
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002525/*
2526 * Find probe function addresses from map.
2527 * Return an error or the number of found probe_trace_event
2528 */
2529static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002530 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002531{
2532 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002533 struct ref_reloc_sym *reloc_sym = NULL;
2534 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002535 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002536 struct probe_trace_event *tev;
2537 struct perf_probe_point *pp = &pev->point;
2538 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002539 int num_matched_functions;
Wang Nan6bb536c2015-05-29 09:45:47 +00002540 int ret, i, j;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002541
Masami Hiramatsu44225522015-05-08 10:03:28 +09002542 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002543 if (!map) {
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547
Wang Nan6bb536c2015-05-29 09:45:47 +00002548 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2549 if (!syms) {
2550 ret = -ENOMEM;
2551 goto out;
2552 }
2553
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002554 /*
2555 * Load matched symbols: Since the different local symbols may have
2556 * same name but different addresses, this lists all the symbols.
2557 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002558 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002559 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002560 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002561 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002562 ret = -ENOENT;
2563 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002564 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002565 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002566 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002567 ret = -E2BIG;
2568 goto out;
2569 }
2570
Namhyung Kim25dd9172015-01-14 20:18:08 +09002571 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002572 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002573 if (!reloc_sym) {
2574 pr_warning("Relocated base symbol is not found!\n");
2575 ret = -EINVAL;
2576 goto out;
2577 }
2578 }
2579
2580 /* Setup result trace-probe-events */
2581 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2582 if (!*tevs) {
2583 ret = -ENOMEM;
2584 goto out;
2585 }
2586
2587 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002588
Wang Nan6bb536c2015-05-29 09:45:47 +00002589 for (j = 0; j < num_matched_functions; j++) {
2590 sym = syms[j];
2591
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002592 tev = (*tevs) + ret;
2593 tp = &tev->point;
2594 if (ret == num_matched_functions) {
2595 pr_warning("Too many symbols are listed. Skip it.\n");
2596 break;
2597 }
2598 ret++;
2599
2600 if (pp->offset > sym->end - sym->start) {
2601 pr_warning("Offset %ld is bigger than the size of %s\n",
2602 pp->offset, sym->name);
2603 ret = -ENOENT;
2604 goto err_out;
2605 }
2606 /* Add one probe point */
2607 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
2608 if (reloc_sym) {
2609 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2610 tp->offset = tp->address - reloc_sym->addr;
2611 } else {
2612 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2613 tp->offset = pp->offset;
2614 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002615 tp->realname = strdup_or_goto(sym->name, nomem_out);
2616
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002617 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002618 if (pev->target)
2619 tev->point.module = strdup_or_goto(pev->target,
2620 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002621 tev->uprobes = pev->uprobes;
2622 tev->nargs = pev->nargs;
2623 if (tev->nargs) {
2624 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2625 tev->nargs);
2626 if (tev->args == NULL)
2627 goto nomem_out;
2628 }
2629 for (i = 0; i < tev->nargs; i++) {
2630 if (pev->args[i].name)
2631 tev->args[i].name =
2632 strdup_or_goto(pev->args[i].name,
2633 nomem_out);
2634
2635 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2636 nomem_out);
2637 if (pev->args[i].type)
2638 tev->args[i].type =
2639 strdup_or_goto(pev->args[i].type,
2640 nomem_out);
2641 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302642 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002643 }
2644
2645out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002646 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002647 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002648 return ret;
2649
2650nomem_out:
2651 ret = -ENOMEM;
2652err_out:
2653 clear_probe_trace_events(*tevs, num_matched_functions);
2654 zfree(tevs);
2655 goto out;
2656}
2657
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302658bool __weak arch__prefers_symtab(void) { return false; }
2659
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302660static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002661 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002662{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002663 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002664
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002665 if (pev->uprobes && !pev->group) {
2666 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002667 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002668 if (ret != 0) {
2669 pr_warning("Failed to make a group name.\n");
2670 return ret;
2671 }
2672 }
2673
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302674 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002675 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302676 if (ret > 0)
2677 return ret; /* Found in symbol table */
2678 }
2679
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002680 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002681 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002682 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002683 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002684
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002685 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002686}
2687
2688struct __event_package {
2689 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302690 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002691 int ntevs;
2692};
2693
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002694int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002695{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002696 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002697 struct __event_package *pkgs;
2698
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302699 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002700 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302701
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002702 if (pkgs == NULL)
2703 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002704
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002705 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002706 if (ret < 0) {
2707 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002708 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002709 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002710
2711 /* Loop 1: convert all events */
2712 for (i = 0; i < npevs; i++) {
2713 pkgs[i].pev = &pevs[i];
2714 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302715 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002716 &pkgs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002717 if (ret < 0)
2718 goto end;
2719 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002720 }
2721
2722 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002723 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302724 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002725 pkgs[i].ntevs,
2726 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002727 if (ret < 0)
2728 break;
2729 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002730end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002731 /* Loop 3: cleanup and free trace events */
2732 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002733 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302734 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002735 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002736 }
2737 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002738 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002739
2740 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002741}
2742
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302743static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002744{
2745 char *p;
2746 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002747 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002748
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302749 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002750 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2751 if (ret < 0)
2752 goto error;
2753
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002754 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002755 if (!p) {
2756 pr_debug("Internal error: %s should have ':' but not.\n",
2757 ent->s);
2758 ret = -ENOTSUP;
2759 goto error;
2760 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002761 *p = '/';
2762
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002763 pr_debug("Writing event: %s\n", buf);
2764 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002765 if (ret < 0) {
2766 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002767 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002768 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002769
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002770 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002771 return 0;
2772error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002773 pr_warning("Failed to delete event: %s\n",
2774 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002775 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002776}
2777
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002778static int del_trace_probe_events(int fd, struct strfilter *filter,
2779 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002780{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002781 struct str_node *ent;
2782 const char *p;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002783 int ret = -ENOENT;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002784
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002785 if (!namelist)
2786 return -ENOENT;
2787
2788 strlist__for_each(ent, namelist) {
2789 p = strchr(ent->s, ':');
2790 if ((p && strfilter__compare(filter, p + 1)) ||
2791 strfilter__compare(filter, ent->s)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302792 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002793 if (ret < 0)
2794 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002795 }
2796 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002797
2798 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002799}
2800
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002801int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002802{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002803 int ret, ret2, ufd = -1, kfd = -1;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302804 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002805 char *str = strfilter__string(filter);
2806
2807 if (!str)
2808 return -EINVAL;
2809
2810 pr_debug("Delete filter: \'%s\'\n", str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002811
Masami Hiramatsufa282442009-12-08 17:03:23 -05002812 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302813 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002814 if (kfd >= 0)
2815 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302816
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302817 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002818 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302819 unamelist = get_probe_trace_event_names(ufd, true);
2820
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002821 if (kfd < 0 && ufd < 0) {
2822 print_both_open_warning(kfd, ufd);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002823 ret = kfd;
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002824 goto error;
2825 }
2826
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002827 ret = del_trace_probe_events(kfd, filter, namelist);
2828 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302829 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002830
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002831 ret2 = del_trace_probe_events(ufd, filter, unamelist);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002832 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002833 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002834 goto error;
2835 }
2836 if (ret == -ENOENT && ret2 == -ENOENT)
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002837 pr_debug("\"%s\" does not hit any event.\n", str);
2838 /* Note that this is silently ignored */
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002839 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302840
2841error:
2842 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302843 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302844 close(kfd);
2845 }
2846
2847 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302848 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302849 close(ufd);
2850 }
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002851 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002852
2853 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002854}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302855
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002856/* TODO: don't use a global variable for filter ... */
2857static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002858
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002859/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002860 * If a symbol corresponds to a function with global binding and
2861 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002862 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002863static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002864 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002865{
Namhyung Kime578da32015-03-06 16:31:29 +09002866 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002867 return 0;
2868 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002869}
2870
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002871int show_available_funcs(const char *target, struct strfilter *_filter,
2872 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002873{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002874 struct map *map;
2875 int ret;
2876
2877 ret = init_symbol_maps(user);
2878 if (ret < 0)
2879 return ret;
2880
2881 /* Get a symbol map */
2882 if (user)
2883 map = dso__new_map(target);
2884 else
2885 map = kernel_get_module_map(target);
2886 if (!map) {
2887 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002888 return -EINVAL;
2889 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002890
2891 /* Load symbols with given filter */
2892 available_func_filter = _filter;
2893 if (map__load(map, filter_available_functions)) {
2894 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2895 goto end;
2896 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002897 if (!dso__sorted_by_name(map->dso, map->type))
2898 dso__sort_by_name(map->dso, map->type);
2899
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002900 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302901 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002902 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
2903end:
2904 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03002905 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002906 }
2907 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302908
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002909 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302910}
2911