blob: 381f23a443c7e71a78c008cb2e56a88326830d98 [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
Masami Hiramatsub0312202015-06-16 20:50:55 +0900249static bool kprobe_blacklist__listed(unsigned long address);
250static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
251{
He Kuang7c31bb82015-06-18 02:49:10 +0000252 u64 etext_addr;
253
Masami Hiramatsub0312202015-06-16 20:50:55 +0900254 /* Get the address of _etext for checking non-probable text symbol */
He Kuang7c31bb82015-06-18 02:49:10 +0000255 etext_addr = kernel_get_symbol_address_by_name("_etext", false);
256
257 if (etext_addr != 0 && etext_addr < address)
Masami Hiramatsub0312202015-06-16 20:50:55 +0900258 pr_warning("%s is out of .text, skip it.\n", symbol);
259 else if (kprobe_blacklist__listed(address))
260 pr_warning("%s is blacklisted function, skip it.\n", symbol);
261 else
262 return false;
263
264 return true;
265}
266
Ingo Molnar89fe8082013-09-30 12:07:11 +0200267#ifdef HAVE_DWARF_SUPPORT
Wang Nan60fb7742015-05-28 02:25:05 +0000268
269static int kernel_get_module_dso(const char *module, struct dso **pdso)
270{
271 struct dso *dso;
272 struct map *map;
273 const char *vmlinux_name;
274 int ret = 0;
275
276 if (module) {
Arnaldo Carvalho de Melo3d39ac52015-05-28 13:06:42 -0300277 list_for_each_entry(dso, &host_machine->dsos.head, node) {
278 if (!dso->kernel)
279 continue;
Wang Nan60fb7742015-05-28 02:25:05 +0000280 if (strncmp(dso->short_name + 1, module,
281 dso->short_name_len - 2) == 0)
282 goto found;
283 }
284 pr_debug("Failed to find module %s.\n", module);
285 return -ENOENT;
286 }
287
288 map = host_machine->vmlinux_maps[MAP__FUNCTION];
289 dso = map->dso;
290
291 vmlinux_name = symbol_conf.vmlinux_name;
292 dso->load_errno = 0;
293 if (vmlinux_name)
294 ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
295 else
296 ret = dso__load_vmlinux_path(dso, map, NULL);
297found:
298 *pdso = dso;
299 return ret;
300}
301
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900302/*
303 * Some binaries like glibc have special symbols which are on the symbol
304 * table, but not in the debuginfo. If we can find the address of the
305 * symbol from map, we can translate the address back to the probe point.
306 */
307static int find_alternative_probe_point(struct debuginfo *dinfo,
308 struct perf_probe_point *pp,
309 struct perf_probe_point *result,
310 const char *target, bool uprobes)
311{
312 struct map *map = NULL;
313 struct symbol *sym;
314 u64 address = 0;
315 int ret = -ENOENT;
316
317 /* This can work only for function-name based one */
318 if (!pp->function || pp->file)
319 return -ENOTSUP;
320
321 map = get_target_map(target, uprobes);
322 if (!map)
323 return -EINVAL;
324
325 /* Find the address of given function */
326 map__for_each_symbol_by_name(map, pp->function, sym) {
Masami Hiramatsue6d7c912015-03-22 20:40:22 +0900327 if (uprobes)
328 address = sym->start;
329 else
330 address = map->unmap_ip(map, sym->start);
Namhyung Kime578da32015-03-06 16:31:29 +0900331 break;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900332 }
333 if (!address) {
334 ret = -ENOENT;
335 goto out;
336 }
Wang Nanf6c15622015-04-08 02:14:34 +0000337 pr_debug("Symbol %s address found : %" PRIx64 "\n",
338 pp->function, address);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900339
340 ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
341 result);
342 if (ret <= 0)
343 ret = (!ret) ? -ENOENT : ret;
344 else {
345 result->offset += pp->offset;
346 result->line += pp->line;
He Kuang9d7b45c2015-04-13 19:41:28 +0800347 result->retprobe = pp->retprobe;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900348 ret = 0;
349 }
350
351out:
352 put_target_map(map, uprobes);
353 return ret;
354
355}
356
357static int get_alternative_probe_event(struct debuginfo *dinfo,
358 struct perf_probe_event *pev,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900359 struct perf_probe_point *tmp)
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900360{
361 int ret;
362
363 memcpy(tmp, &pev->point, sizeof(*tmp));
364 memset(&pev->point, 0, sizeof(pev->point));
365 ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900366 pev->target, pev->uprobes);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900367 if (ret < 0)
368 memcpy(&pev->point, tmp, sizeof(*tmp));
369
370 return ret;
371}
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000372
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900373static int get_alternative_line_range(struct debuginfo *dinfo,
374 struct line_range *lr,
375 const char *target, bool user)
376{
David Ahern6d4a4892015-03-11 10:36:20 -0400377 struct perf_probe_point pp = { .function = lr->function,
378 .file = lr->file,
379 .line = lr->start };
380 struct perf_probe_point result;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900381 int ret, len = 0;
382
David Ahern6d4a4892015-03-11 10:36:20 -0400383 memset(&result, 0, sizeof(result));
384
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900385 if (lr->end != INT_MAX)
386 len = lr->end - lr->start;
387 ret = find_alternative_probe_point(dinfo, &pp, &result,
388 target, user);
389 if (!ret) {
390 lr->function = result.function;
391 lr->file = result.file;
392 lr->start = result.line;
393 if (lr->end != INT_MAX)
394 lr->end = lr->start + len;
395 clear_perf_probe_point(&pp);
396 }
397 return ret;
398}
399
Masami Hiramatsuff741782011-06-27 16:27:39 +0900400/* Open new debuginfo of given module */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000401static struct debuginfo *open_debuginfo(const char *module, bool silent)
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900402{
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000403 const char *path = module;
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900404 char reason[STRERR_BUFSIZE];
405 struct debuginfo *ret = NULL;
406 struct dso *dso = NULL;
407 int err;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900408
Masami Hiramatsua15ad2f2014-02-06 05:32:27 +0000409 if (!module || !strchr(module, '/')) {
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900410 err = kernel_get_module_dso(module, &dso);
411 if (err < 0) {
412 if (!dso || dso->load_errno == 0) {
413 if (!strerror_r(-err, reason, STRERR_BUFSIZE))
414 strcpy(reason, "(unknown)");
415 } else
416 dso__strerror_load(dso, reason, STRERR_BUFSIZE);
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000417 if (!silent)
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900418 pr_err("Failed to find the path for %s: %s\n",
419 module ?: "kernel", reason);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900420 return NULL;
421 }
Masami Hiramatsu419e8732015-05-27 17:37:18 +0900422 path = dso->long_name;
Masami Hiramatsu469b9b82010-10-21 19:13:41 +0900423 }
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000424 ret = debuginfo__new(path);
425 if (!ret && !silent) {
426 pr_warning("The %s file has no debug information.\n", path);
427 if (!module || !strtailcmp(path, ".ko"))
428 pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
429 else
430 pr_warning("Rebuild with -g, ");
431 pr_warning("or install an appropriate debuginfo package.\n");
432 }
433 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -0400434}
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300435
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900436/* For caching the last debuginfo */
437static struct debuginfo *debuginfo_cache;
438static char *debuginfo_cache_path;
439
440static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
441{
442 if ((debuginfo_cache_path && !strcmp(debuginfo_cache_path, module)) ||
443 (!debuginfo_cache_path && !module && debuginfo_cache))
444 goto out;
445
446 /* Copy module path */
447 free(debuginfo_cache_path);
448 if (module) {
449 debuginfo_cache_path = strdup(module);
450 if (!debuginfo_cache_path) {
451 debuginfo__delete(debuginfo_cache);
452 debuginfo_cache = NULL;
453 goto out;
454 }
455 }
456
457 debuginfo_cache = open_debuginfo(module, silent);
458 if (!debuginfo_cache)
459 zfree(&debuginfo_cache_path);
460out:
461 return debuginfo_cache;
462}
463
464static void debuginfo_cache__exit(void)
465{
466 debuginfo__delete(debuginfo_cache);
467 debuginfo_cache = NULL;
468 zfree(&debuginfo_cache_path);
469}
470
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000471
Masami Hiramatsu99ca4232014-01-16 09:39:49 +0000472static int get_text_start_address(const char *exec, unsigned long *address)
473{
474 Elf *elf;
475 GElf_Ehdr ehdr;
476 GElf_Shdr shdr;
477 int fd, ret = -ENOENT;
478
479 fd = open(exec, O_RDONLY);
480 if (fd < 0)
481 return -errno;
482
483 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
484 if (elf == NULL)
485 return -EINVAL;
486
487 if (gelf_getehdr(elf, &ehdr) == NULL)
488 goto out;
489
490 if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
491 goto out;
492
493 *address = shdr.sh_addr - shdr.sh_offset;
494 ret = 0;
495out:
496 elf_end(elf);
497 return ret;
498}
499
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000500/*
501 * Convert trace point to probe point with debuginfo
502 */
503static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
504 struct perf_probe_point *pp,
505 bool is_kprobe)
506{
507 struct debuginfo *dinfo = NULL;
508 unsigned long stext = 0;
509 u64 addr = tp->address;
510 int ret = -ENOENT;
511
512 /* convert the address to dwarf address */
513 if (!is_kprobe) {
514 if (!addr) {
515 ret = -EINVAL;
516 goto error;
517 }
518 ret = get_text_start_address(tp->module, &stext);
519 if (ret < 0)
520 goto error;
521 addr += stext;
522 } else {
523 addr = kernel_get_symbol_address_by_name(tp->symbol, false);
524 if (addr == 0)
525 goto error;
526 addr += tp->offset;
527 }
528
529 pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
530 tp->module ? : "kernel");
531
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900532 dinfo = debuginfo_cache__open(tp->module, verbose == 0);
533 if (dinfo)
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000534 ret = debuginfo__find_probe_point(dinfo,
535 (unsigned long)addr, pp);
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900536 else
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000537 ret = -ENOENT;
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000538
539 if (ret > 0) {
540 pp->retprobe = tp->retprobe;
541 return 0;
542 }
543error:
544 pr_debug("Failed to find corresponding probes from debuginfo.\n");
545 return ret ? : -ENOENT;
546}
547
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000548static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
549 int ntevs, const char *exec)
550{
551 int i, ret = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000552 unsigned long stext = 0;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000553
554 if (!exec)
555 return 0;
556
557 ret = get_text_start_address(exec, &stext);
558 if (ret < 0)
559 return ret;
560
561 for (i = 0; i < ntevs && ret >= 0; i++) {
Masami Hiramatsu981a2372014-02-05 05:18:58 +0000562 /* point.address is the addres of point.symbol + point.offset */
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000563 tevs[i].point.address -= stext;
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000564 tevs[i].point.module = strdup(exec);
Masami Hiramatsueb948e52014-02-06 05:32:25 +0000565 if (!tevs[i].point.module) {
Masami Hiramatsufb7345b2013-12-26 05:41:53 +0000566 ret = -ENOMEM;
567 break;
568 }
569 tevs[i].uprobes = true;
570 }
571
572 return ret;
573}
574
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900575static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
576 int ntevs, const char *module)
577{
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900578 int i, ret = 0;
579 char *tmp;
580
581 if (!module)
582 return 0;
583
584 tmp = strrchr(module, '/');
585 if (tmp) {
586 /* This is a module path -- get the module name */
587 module = strdup(tmp + 1);
588 if (!module)
589 return -ENOMEM;
590 tmp = strchr(module, '.');
591 if (tmp)
592 *tmp = '\0';
593 tmp = (char *)module; /* For free() */
594 }
595
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900596 for (i = 0; i < ntevs; i++) {
597 tevs[i].point.module = strdup(module);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900598 if (!tevs[i].point.module) {
599 ret = -ENOMEM;
600 break;
601 }
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900602 }
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900603
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300604 free(tmp);
Masami Hiramatsu14a8fd72011-06-27 16:27:51 +0900605 return ret;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900606}
607
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000608/* Post processing the probe events */
609static int post_process_probe_trace_events(struct probe_trace_event *tevs,
610 int ntevs, const char *module,
611 bool uprobe)
612{
613 struct ref_reloc_sym *reloc_sym;
614 char *tmp;
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900615 int i, skipped = 0;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000616
617 if (uprobe)
618 return add_exec_to_probe_trace_events(tevs, ntevs, module);
619
620 /* Note that currently ref_reloc_sym based probe is not for drivers */
621 if (module)
622 return add_module_to_probe_trace_events(tevs, ntevs, module);
623
Masami Hiramatsu8f33f7d2014-02-06 05:32:20 +0000624 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000625 if (!reloc_sym) {
626 pr_warning("Relocated base symbol is not found!\n");
627 return -EINVAL;
628 }
629
630 for (i = 0; i < ntevs; i++) {
Masami Hiramatsub0312202015-06-16 20:50:55 +0900631 if (!tevs[i].point.address || tevs[i].point.retprobe)
632 continue;
633 /* If we found a wrong one, mark it by NULL symbol */
634 if (kprobe_warn_out_range(tevs[i].point.symbol,
635 tevs[i].point.address)) {
636 tmp = NULL;
637 skipped++;
638 } else {
639 tmp = strdup(reloc_sym->name);
640 if (!tmp)
641 return -ENOMEM;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000642 }
Masami Hiramatsub0312202015-06-16 20:50:55 +0900643 /* If we have no realname, use symbol for it */
644 if (!tevs[i].point.realname)
645 tevs[i].point.realname = tevs[i].point.symbol;
646 else
647 free(tevs[i].point.symbol);
648 tevs[i].point.symbol = tmp;
649 tevs[i].point.offset = tevs[i].point.address -
650 reloc_sym->unrelocated_addr;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000651 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900652 return skipped;
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000653}
654
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300655/* Try to find perf_probe_event with debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530656static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900657 struct probe_trace_event **tevs)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300658{
659 bool need_dwarf = perf_probe_event_need_dwarf(pev);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900660 struct perf_probe_point tmp;
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530661 struct debuginfo *dinfo;
Masami Hiramatsu190b57f2011-06-27 16:27:45 +0900662 int ntevs, ret = 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300663
Masami Hiramatsu44225522015-05-08 10:03:28 +0900664 dinfo = open_debuginfo(pev->target, !need_dwarf);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900665 if (!dinfo) {
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000666 if (need_dwarf)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900667 return -ENOENT;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900668 pr_debug("Could not open debuginfo. Try to use symbols.\n");
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300669 return 0;
670 }
671
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000672 pr_debug("Try to find probe point from debuginfo.\n");
Masami Hiramatsuff741782011-06-27 16:27:39 +0900673 /* Searching trace events corresponding to a probe event */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900674 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900675
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900676 if (ntevs == 0) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900677 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900678 if (!ret) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900679 ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900680 /*
681 * Write back to the original probe_event for
682 * setting appropriate (user given) event name
683 */
684 clear_perf_probe_point(&pev->point);
685 memcpy(&pev->point, &tmp, sizeof(tmp));
686 }
687 }
688
Masami Hiramatsuff741782011-06-27 16:27:39 +0900689 debuginfo__delete(dinfo);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300690
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400691 if (ntevs > 0) { /* Succeeded to find trace events */
Masami Hiramatsudfef99c2014-02-06 05:32:16 +0000692 pr_debug("Found %d probe_trace_events.\n", ntevs);
693 ret = post_process_probe_trace_events(*tevs, ntevs,
Masami Hiramatsu44225522015-05-08 10:03:28 +0900694 pev->target, pev->uprobes);
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900695 if (ret < 0 || ret == ntevs) {
Masami Hiramatsu981d05a2014-01-16 09:39:44 +0000696 clear_probe_trace_events(*tevs, ntevs);
697 zfree(tevs);
698 }
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +0900699 if (ret != ntevs)
700 return ret < 0 ? ret : ntevs;
701 ntevs = 0;
702 /* Fall through */
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400703 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300704
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400705 if (ntevs == 0) { /* No error but failed to find probe point. */
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900706 pr_warning("Probe point '%s' not found.\n",
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400707 synthesize_perf_probe_point(&pev->point));
Masami Hiramatsu0687eba2015-03-06 16:31:25 +0900708 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400709 }
710 /* Error path : ntevs < 0 */
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400711 pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
712 if (ntevs == -EBADF) {
713 pr_warning("Warning: No dwarf info found in the vmlinux - "
714 "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
715 if (!need_dwarf) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +0900716 pr_debug("Trying to use symbols.\n");
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400717 return 0;
718 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300719 }
Masami Hiramatsu15eca302010-04-21 15:56:24 -0400720 return ntevs;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300721}
722
723#define LINEBUF_SIZE 256
724#define NR_ADDITIONAL_LINES 2
725
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100726static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300727{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000728 char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100729 const char *color = show_num ? "" : PERF_COLOR_BLUE;
730 const char *prefix = NULL;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300731
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100732 do {
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300733 if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
734 goto error;
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100735 if (skip)
736 continue;
737 if (!prefix) {
738 prefix = show_num ? "%7d " : " ";
739 color_fprintf(stdout, color, prefix, l);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300740 }
Franck Bui-Huubefe3412010-12-20 15:18:01 +0100741 color_fprintf(stdout, color, "%s", buf);
742
743 } while (strchr(buf, '\n') == NULL);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400744
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100745 return 1;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300746error:
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100747 if (ferror(fp)) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000748 pr_warning("File read error: %s\n",
749 strerror_r(errno, sbuf, sizeof(sbuf)));
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100750 return -1;
751 }
752 return 0;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300753}
754
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100755static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
756{
757 int rv = __show_one_line(fp, l, skip, show_num);
758 if (rv == 0) {
759 pr_warning("Source file is shorter than expected.\n");
760 rv = -1;
761 }
762 return rv;
763}
764
765#define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
766#define show_one_line(f,l) _show_one_line(f,l,false,false)
767#define skip_one_line(f,l) _show_one_line(f,l,true,false)
768#define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
769
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300770/*
771 * Show line-range always requires debuginfo to find source file and
772 * line number.
773 */
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900774static int __show_line_range(struct line_range *lr, const char *module,
775 bool user)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300776{
Masami Hiramatsud3b63d72010-04-14 18:39:42 -0400777 int l = 1;
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000778 struct int_node *ln;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900779 struct debuginfo *dinfo;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300780 FILE *fp;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900781 int ret;
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900782 char *tmp;
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000783 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300784
785 /* Search a line range */
Masami Hiramatsu92561cb2014-08-15 01:44:32 +0000786 dinfo = open_debuginfo(module, false);
787 if (!dinfo)
Masami Hiramatsuff741782011-06-27 16:27:39 +0900788 return -ENOENT;
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400789
Masami Hiramatsuff741782011-06-27 16:27:39 +0900790 ret = debuginfo__find_line_range(dinfo, lr);
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900791 if (!ret) { /* Not found, retry with an alternative */
792 ret = get_alternative_line_range(dinfo, lr, module, user);
793 if (!ret)
794 ret = debuginfo__find_line_range(dinfo, lr);
795 }
Masami Hiramatsuff741782011-06-27 16:27:39 +0900796 debuginfo__delete(dinfo);
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000797 if (ret == 0 || ret == -ENOENT) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400798 pr_warning("Specified source line is not found.\n");
799 return -ENOENT;
800 } else if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000801 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400802 return ret;
803 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300804
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900805 /* Convert source file path */
806 tmp = lr->path;
Masami Hiramatsu6a330a32010-07-09 18:29:11 +0900807 ret = get_real_path(tmp, lr->comp_dir, &lr->path);
He Kuanga78604d2015-03-04 18:01:42 +0800808
809 /* Free old path when new path is assigned */
810 if (tmp != lr->path)
811 free(tmp);
812
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900813 if (ret < 0) {
Masami Hiramatsu5ee05b82014-06-06 07:14:06 +0000814 pr_warning("Failed to find source file path.\n");
Masami Hiramatsu7cf0b792010-07-09 18:28:59 +0900815 return ret;
816 }
817
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300818 setup_pager();
819
820 if (lr->function)
Masami Hiramatsu8737ebd2011-02-10 18:08:16 +0900821 fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300822 lr->start - lr->offset);
823 else
Franck Bui-Huu62c15fc2010-12-20 15:18:00 +0100824 fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300825
826 fp = fopen(lr->path, "r");
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400827 if (fp == NULL) {
828 pr_warning("Failed to open %s: %s\n", lr->path,
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +0000829 strerror_r(errno, sbuf, sizeof(sbuf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400830 return -errno;
831 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300832 /* Skip to starting line number */
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100833 while (l < lr->start) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100834 ret = skip_one_line(fp, l++);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100835 if (ret < 0)
836 goto end;
837 }
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300838
Masami Hiramatsu5a622572014-02-06 05:32:09 +0000839 intlist__for_each(ln, lr->line_list) {
840 for (; ln->i > l; l++) {
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100841 ret = show_one_line(fp, l - lr->offset);
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100842 if (ret < 0)
843 goto end;
844 }
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100845 ret = show_one_line_with_num(fp, l++ - lr->offset);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400846 if (ret < 0)
847 goto end;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300848 }
849
850 if (lr->end == INT_MAX)
851 lr->end = l + NR_ADDITIONAL_LINES;
Franck Bui-Huufde52db2010-12-20 15:18:04 +0100852 while (l <= lr->end) {
853 ret = show_one_line_or_eof(fp, l++ - lr->offset);
854 if (ret <= 0)
Franck Bui-Huu44b81e92010-12-20 15:18:02 +0100855 break;
856 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400857end:
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300858 fclose(fp);
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400859 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300860}
861
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000862int show_line_range(struct line_range *lr, const char *module, bool user)
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000863{
864 int ret;
865
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000866 ret = init_symbol_maps(user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000867 if (ret < 0)
868 return ret;
Masami Hiramatsu811dd2a2015-03-06 16:31:22 +0900869 ret = __show_line_range(lr, module, user);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000870 exit_symbol_maps();
871
872 return ret;
873}
874
Masami Hiramatsuff741782011-06-27 16:27:39 +0900875static int show_available_vars_at(struct debuginfo *dinfo,
876 struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900877 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900878{
879 char *buf;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900880 int ret, i, nvars;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900881 struct str_node *node;
882 struct variable_list *vls = NULL, *vl;
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900883 struct perf_probe_point tmp;
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900884 const char *var;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900885
886 buf = synthesize_perf_probe_point(&pev->point);
887 if (!buf)
888 return -EINVAL;
889 pr_debug("Searching variables at %s\n", buf);
890
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900891 ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900892 if (!ret) { /* Not found, retry with an alternative */
Masami Hiramatsu44225522015-05-08 10:03:28 +0900893 ret = get_alternative_probe_event(dinfo, pev, &tmp);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900894 if (!ret) {
895 ret = debuginfo__find_available_vars_at(dinfo, pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900896 &vls);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +0900897 /* Release the old probe_point */
898 clear_perf_probe_point(&tmp);
899 }
900 }
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900901 if (ret <= 0) {
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000902 if (ret == 0 || ret == -ENOENT) {
903 pr_err("Failed to find the address of %s\n", buf);
904 ret = -ENOENT;
905 } else
906 pr_warning("Debuginfo analysis failed.\n");
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900907 goto end;
908 }
Masami Hiramatsu69e96ea2014-06-06 07:13:59 +0000909
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900910 /* Some variables are found */
911 fprintf(stdout, "Available variables at %s\n", buf);
912 for (i = 0; i < ret; i++) {
913 vl = &vls[i];
914 /*
915 * A probe point might be converted to
916 * several trace points.
917 */
918 fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
919 vl->point.offset);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300920 zfree(&vl->point.symbol);
Masami Hiramatsubd09d7b2011-01-20 23:15:39 +0900921 nvars = 0;
922 if (vl->vars) {
923 strlist__for_each(node, vl->vars) {
924 var = strchr(node->s, '\t') + 1;
925 if (strfilter__compare(_filter, var)) {
926 fprintf(stdout, "\t\t%s\n", node->s);
927 nvars++;
928 }
929 }
930 strlist__delete(vl->vars);
931 }
932 if (nvars == 0)
933 fprintf(stdout, "\t\t(No matched variables)\n");
934 }
935 free(vls);
936end:
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900937 free(buf);
938 return ret;
939}
940
941/* Show available variables on given probe point */
942int show_available_vars(struct perf_probe_event *pevs, int npevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900943 struct strfilter *_filter)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900944{
Masami Hiramatsuff741782011-06-27 16:27:39 +0900945 int i, ret = 0;
946 struct debuginfo *dinfo;
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900947
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000948 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900949 if (ret < 0)
950 return ret;
951
Masami Hiramatsu44225522015-05-08 10:03:28 +0900952 dinfo = open_debuginfo(pevs->target, false);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900953 if (!dinfo) {
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000954 ret = -ENOENT;
955 goto out;
Masami Hiramatsuff741782011-06-27 16:27:39 +0900956 }
957
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900958 setup_pager();
959
Masami Hiramatsuff741782011-06-27 16:27:39 +0900960 for (i = 0; i < npevs && ret >= 0; i++)
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900961 ret = show_available_vars_at(dinfo, &pevs[i], _filter);
Masami Hiramatsuff741782011-06-27 16:27:39 +0900962
963 debuginfo__delete(dinfo);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +0000964out:
965 exit_symbol_maps();
Masami Hiramatsucf6eb482010-10-21 19:13:23 +0900966 return ret;
967}
968
Ingo Molnar89fe8082013-09-30 12:07:11 +0200969#else /* !HAVE_DWARF_SUPPORT */
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300970
Masami Hiramatsu7737af02015-06-17 23:58:54 +0900971static void debuginfo_cache__exit(void)
972{
973}
974
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000975static int
976find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
977 struct perf_probe_point *pp __maybe_unused,
978 bool is_kprobe __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300979{
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +0000980 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300981}
982
Srikar Dronamraju0e608362010-07-29 19:43:51 +0530983static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +0900984 struct probe_trace_event **tevs __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300985{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400986 if (perf_probe_event_need_dwarf(pev)) {
987 pr_warning("Debuginfo-analysis is not supported.\n");
988 return -ENOSYS;
989 }
Srikar Dronamraju225466f2012-04-16 17:39:09 +0530990
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300991 return 0;
992}
993
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300994int show_line_range(struct line_range *lr __maybe_unused,
Masami Hiramatsu2b394bc2014-09-17 08:40:54 +0000995 const char *module __maybe_unused,
996 bool user __maybe_unused)
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -0300997{
Masami Hiramatsu146a1432010-04-12 13:17:42 -0400998 pr_warning("Debuginfo-analysis is not supported.\n");
999 return -ENOSYS;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001000}
1001
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001002int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09001003 int npevs __maybe_unused,
1004 struct strfilter *filter __maybe_unused)
Masami Hiramatsucf6eb482010-10-21 19:13:23 +09001005{
1006 pr_warning("Debuginfo-analysis is not supported.\n");
1007 return -ENOSYS;
1008}
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04001009#endif
1010
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001011void line_range__clear(struct line_range *lr)
1012{
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001013 free(lr->function);
1014 free(lr->file);
1015 free(lr->path);
1016 free(lr->comp_dir);
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001017 intlist__delete(lr->line_list);
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001018 memset(lr, 0, sizeof(*lr));
1019}
1020
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001021int line_range__init(struct line_range *lr)
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001022{
1023 memset(lr, 0, sizeof(*lr));
Masami Hiramatsu5a622572014-02-06 05:32:09 +00001024 lr->line_list = intlist__new(NULL);
1025 if (!lr->line_list)
1026 return -ENOMEM;
1027 else
1028 return 0;
Masami Hiramatsue53b00d2014-01-16 09:39:47 +00001029}
1030
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001031static int parse_line_num(char **ptr, int *val, const char *what)
1032{
1033 const char *start = *ptr;
1034
1035 errno = 0;
1036 *val = strtol(*ptr, ptr, 0);
1037 if (errno || *ptr == start) {
1038 semantic_error("'%s' is not a valid number.\n", what);
1039 return -EINVAL;
1040 }
1041 return 0;
1042}
1043
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001044/* Check the name is good for event, group or function */
1045static bool is_c_func_name(const char *name)
1046{
1047 if (!isalpha(*name) && *name != '_')
1048 return false;
1049 while (*++name != '\0') {
1050 if (!isalpha(*name) && !isdigit(*name) && *name != '_')
1051 return false;
1052 }
1053 return true;
1054}
1055
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001056/*
1057 * Stuff 'lr' according to the line range described by 'arg'.
1058 * The line range syntax is described by:
1059 *
1060 * SRC[:SLN[+NUM|-ELN]]
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001061 * FNC[@SRC][:SLN[+NUM|-ELN]]
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001062 */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001063int parse_line_range_desc(const char *arg, struct line_range *lr)
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001064{
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001065 char *range, *file, *name = strdup(arg);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001066 int err;
Franck Bui-Huu9d95b582010-12-20 15:18:03 +01001067
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001068 if (!name)
1069 return -ENOMEM;
1070
1071 lr->start = 0;
1072 lr->end = INT_MAX;
1073
1074 range = strchr(name, ':');
1075 if (range) {
1076 *range++ = '\0';
1077
1078 err = parse_line_num(&range, &lr->start, "start line");
1079 if (err)
1080 goto err;
1081
1082 if (*range == '+' || *range == '-') {
1083 const char c = *range++;
1084
1085 err = parse_line_num(&range, &lr->end, "end line");
1086 if (err)
1087 goto err;
1088
1089 if (c == '+') {
1090 lr->end += lr->start;
1091 /*
1092 * Adjust the number of lines here.
1093 * If the number of lines == 1, the
1094 * the end of line should be equal to
1095 * the start of line.
1096 */
1097 lr->end--;
1098 }
1099 }
1100
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001101 pr_debug("Line range is %d to %d\n", lr->start, lr->end);
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001102
1103 err = -EINVAL;
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001104 if (lr->start > lr->end) {
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001105 semantic_error("Start line must be smaller"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001106 " than end line.\n");
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001107 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001108 }
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001109 if (*range != '\0') {
1110 semantic_error("Tailing with invalid str '%s'.\n", range);
1111 goto err;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001112 }
Masami Hiramatsud3b63d72010-04-14 18:39:42 -04001113 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001114
Masami Hiramatsue116dfa2011-02-10 18:08:10 +09001115 file = strchr(name, '@');
1116 if (file) {
1117 *file = '\0';
1118 lr->file = strdup(++file);
1119 if (lr->file == NULL) {
1120 err = -ENOMEM;
1121 goto err;
1122 }
1123 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001124 } else if (strchr(name, '/') || strchr(name, '.'))
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001125 lr->file = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001126 else if (is_c_func_name(name))/* We reuse it for checking funcname */
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001127 lr->function = name;
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001128 else { /* Invalid name */
1129 semantic_error("'%s' is not a valid function name.\n", name);
1130 err = -EINVAL;
1131 goto err;
1132 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001133
1134 return 0;
Franck Bui-Huu21dd9ae2010-12-20 15:18:05 +01001135err:
1136 free(name);
1137 return err;
Masami Hiramatsu631c9de2010-01-06 09:45:34 -05001138}
1139
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001140/* Parse probepoint definition. */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001141static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001142{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001143 struct perf_probe_point *pp = &pev->point;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001144 char *ptr, *tmp;
1145 char c, nc = 0;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301146 bool file_spec = false;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001147 /*
1148 * <Syntax>
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001149 * perf probe [EVENT=]SRC[:LN|;PTN]
1150 * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001151 *
1152 * TODO:Group name support
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001153 */
Wang Nane59d29e2015-04-28 08:46:09 +00001154 if (!arg)
1155 return -EINVAL;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001156
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001157 ptr = strpbrk(arg, ";=@+%");
1158 if (ptr && *ptr == '=') { /* Event name */
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001159 *ptr = '\0';
1160 tmp = ptr + 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001161 if (strchr(arg, ':')) {
1162 semantic_error("Group name is not supported yet.\n");
1163 return -ENOTSUP;
1164 }
Masami Hiramatsu573709f2015-05-06 21:46:47 +09001165 if (!is_c_func_name(arg)) {
Masami Hiramatsub7702a22009-12-16 17:24:15 -05001166 semantic_error("%s is bad for event name -it must "
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001167 "follow C symbol-naming rule.\n", arg);
1168 return -EINVAL;
1169 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001170 pev->event = strdup(arg);
1171 if (pev->event == NULL)
1172 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001173 pev->group = NULL;
Masami Hiramatsuaf663d72009-12-15 10:32:18 -05001174 arg = tmp;
1175 }
1176
Naveen N. Rao3099c022015-04-28 17:35:34 +05301177 /*
1178 * Check arg is function or file name and copy it.
1179 *
1180 * We consider arg to be a file spec if and only if it satisfies
1181 * all of the below criteria::
1182 * - it does not include any of "+@%",
1183 * - it includes one of ":;", and
1184 * - it has a period '.' in the name.
1185 *
1186 * Otherwise, we consider arg to be a function specification.
1187 */
1188 if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
1189 /* This is a file spec if it includes a '.' before ; or : */
1190 if (memchr(arg, '.', ptr - arg))
1191 file_spec = true;
1192 }
1193
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001194 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001195 if (ptr) {
1196 nc = *ptr;
1197 *ptr++ = '\0';
1198 }
1199
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001200 tmp = strdup(arg);
1201 if (tmp == NULL)
1202 return -ENOMEM;
1203
Naveen N. Rao3099c022015-04-28 17:35:34 +05301204 if (file_spec)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001205 pp->file = tmp;
Naveen N. Rao3099c022015-04-28 17:35:34 +05301206 else
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001207 pp->function = tmp;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001208
1209 /* Parse other options */
1210 while (ptr) {
1211 arg = ptr;
1212 c = nc;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001213 if (c == ';') { /* Lazy pattern must be the last part */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001214 pp->lazy_line = strdup(arg);
1215 if (pp->lazy_line == NULL)
1216 return -ENOMEM;
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001217 break;
1218 }
1219 ptr = strpbrk(arg, ";:+@%");
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001220 if (ptr) {
1221 nc = *ptr;
1222 *ptr++ = '\0';
1223 }
1224 switch (c) {
1225 case ':': /* Line number */
1226 pp->line = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001227 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001228 semantic_error("There is non-digit char"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001229 " in line number.\n");
1230 return -EINVAL;
1231 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001232 break;
1233 case '+': /* Byte offset from a symbol */
1234 pp->offset = strtoul(arg, &tmp, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001235 if (*tmp != '\0') {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001236 semantic_error("There is non-digit character"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001237 " in offset.\n");
1238 return -EINVAL;
1239 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001240 break;
1241 case '@': /* File name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001242 if (pp->file) {
1243 semantic_error("SRC@SRC is not allowed.\n");
1244 return -EINVAL;
1245 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001246 pp->file = strdup(arg);
1247 if (pp->file == NULL)
1248 return -ENOMEM;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001249 break;
1250 case '%': /* Probe places */
1251 if (strcmp(arg, "return") == 0) {
1252 pp->retprobe = 1;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001253 } else { /* Others not supported yet */
1254 semantic_error("%%%s is not supported.\n", arg);
1255 return -ENOTSUP;
1256 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001257 break;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001258 default: /* Buggy case */
1259 pr_err("This program has a bug at %s:%d.\n",
1260 __FILE__, __LINE__);
1261 return -ENOTSUP;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001262 break;
1263 }
1264 }
1265
1266 /* Exclusion check */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001267 if (pp->lazy_line && pp->line) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001268 semantic_error("Lazy pattern can't be used with"
1269 " line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001270 return -EINVAL;
1271 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001272
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001273 if (pp->lazy_line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001274 semantic_error("Lazy pattern can't be used with offset.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001275 return -EINVAL;
1276 }
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001277
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001278 if (pp->line && pp->offset) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001279 semantic_error("Offset can't be used with line number.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001280 return -EINVAL;
1281 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001282
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001283 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001284 semantic_error("File always requires line number or "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001285 "lazy pattern.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001286 return -EINVAL;
1287 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001288
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001289 if (pp->offset && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001290 semantic_error("Offset requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001291 return -EINVAL;
1292 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001293
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001294 if (pp->retprobe && !pp->function) {
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001295 semantic_error("Return probe requires an entry function.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001296 return -EINVAL;
1297 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001298
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001299 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001300 semantic_error("Offset/Line/Lazy pattern can't be used with "
Masami Hiramatsu0e43e5d2010-12-17 22:12:11 +09001301 "return probe.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001302 return -EINVAL;
1303 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001304
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001305 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
Masami Hiramatsu2a9c8c32010-02-25 08:36:12 -05001306 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
1307 pp->lazy_line);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001308 return 0;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001309}
1310
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001311/* Parse perf-probe event argument */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001312static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001313{
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001314 char *tmp, *goodname;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001315 struct perf_probe_arg_field **fieldp;
1316
1317 pr_debug("parsing arg: %s into ", str);
1318
Masami Hiramatsu48481932010-04-12 13:16:53 -04001319 tmp = strchr(str, '=');
1320 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001321 arg->name = strndup(str, tmp - str);
1322 if (arg->name == NULL)
1323 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001324 pr_debug("name:%s ", arg->name);
Masami Hiramatsu48481932010-04-12 13:16:53 -04001325 str = tmp + 1;
1326 }
1327
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001328 tmp = strchr(str, ':');
1329 if (tmp) { /* Type setting */
1330 *tmp = '\0';
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001331 arg->type = strdup(tmp + 1);
1332 if (arg->type == NULL)
1333 return -ENOMEM;
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001334 pr_debug("type:%s ", arg->type);
1335 }
1336
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001337 tmp = strpbrk(str, "-.[");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001338 if (!is_c_varname(str) || !tmp) {
1339 /* A variable, register, symbol or special value */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001340 arg->var = strdup(str);
1341 if (arg->var == NULL)
1342 return -ENOMEM;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001343 pr_debug("%s\n", arg->var);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001344 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001345 }
1346
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001347 /* Structure fields or array element */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001348 arg->var = strndup(str, tmp - str);
1349 if (arg->var == NULL)
1350 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001351 goodname = arg->var;
Masami Hiramatsu48481932010-04-12 13:16:53 -04001352 pr_debug("%s, ", arg->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001353 fieldp = &arg->field;
1354
1355 do {
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001356 *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
1357 if (*fieldp == NULL)
1358 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001359 if (*tmp == '[') { /* Array */
1360 str = tmp;
1361 (*fieldp)->index = strtol(str + 1, &tmp, 0);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001362 (*fieldp)->ref = true;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001363 if (*tmp != ']' || tmp == str + 1) {
1364 semantic_error("Array index must be a"
1365 " number.\n");
1366 return -EINVAL;
1367 }
1368 tmp++;
1369 if (*tmp == '\0')
1370 tmp = NULL;
1371 } else { /* Structure */
1372 if (*tmp == '.') {
1373 str = tmp + 1;
1374 (*fieldp)->ref = false;
1375 } else if (tmp[1] == '>') {
1376 str = tmp + 2;
1377 (*fieldp)->ref = true;
1378 } else {
1379 semantic_error("Argument parse error: %s\n",
1380 str);
1381 return -EINVAL;
1382 }
1383 tmp = strpbrk(str, "-.[");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001384 }
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001385 if (tmp) {
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001386 (*fieldp)->name = strndup(str, tmp - str);
1387 if ((*fieldp)->name == NULL)
1388 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001389 if (*str != '[')
1390 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001391 pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
1392 fieldp = &(*fieldp)->next;
1393 }
1394 } while (tmp);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001395 (*fieldp)->name = strdup(str);
1396 if ((*fieldp)->name == NULL)
1397 return -ENOMEM;
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001398 if (*str != '[')
1399 goodname = (*fieldp)->name;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001400 pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
Masami Hiramatsudf0faf42010-04-12 13:17:00 -04001401
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001402 /* If no name is specified, set the last field name (not array index)*/
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001403 if (!arg->name) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001404 arg->name = strdup(goodname);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001405 if (arg->name == NULL)
1406 return -ENOMEM;
1407 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001408 return 0;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001409}
1410
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001411/* Parse perf-probe event command */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001412int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001413{
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001414 char **argv;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001415 int argc, i, ret = 0;
Masami Hiramatsufac13fd2009-12-15 10:31:14 -05001416
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001417 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001418 if (!argv) {
1419 pr_debug("Failed to split arguments.\n");
1420 return -ENOMEM;
1421 }
1422 if (argc - 1 > MAX_PROBE_ARGS) {
1423 semantic_error("Too many probe arguments (%d).\n", argc - 1);
1424 ret = -ERANGE;
1425 goto out;
1426 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001427 /* Parse probe point */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001428 ret = parse_perf_probe_point(argv[0], pev);
1429 if (ret < 0)
1430 goto out;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001431
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001432 /* Copy arguments and ensure return probe has no C argument */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001433 pev->nargs = argc - 1;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001434 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1435 if (pev->args == NULL) {
1436 ret = -ENOMEM;
1437 goto out;
1438 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001439 for (i = 0; i < pev->nargs && ret >= 0; i++) {
1440 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
1441 if (ret >= 0 &&
1442 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001443 semantic_error("You can't specify local variable for"
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001444 " kretprobe.\n");
1445 ret = -EINVAL;
1446 }
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001447 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001448out:
Masami Hiramatsue1c01d62009-11-30 19:20:05 -05001449 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001450
1451 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001452}
1453
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001454/* Return true if this perf_probe_event requires debuginfo */
1455bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001456{
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001457 int i;
1458
1459 if (pev->point.file || pev->point.line || pev->point.lazy_line)
1460 return true;
1461
1462 for (i = 0; i < pev->nargs; i++)
Masami Hiramatsu48481932010-04-12 13:16:53 -04001463 if (is_c_varname(pev->args[i].var))
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001464 return true;
1465
1466 return false;
1467}
1468
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301469/* Parse probe_events event into struct probe_point */
1470static int parse_probe_trace_command(const char *cmd,
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001471 struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001472{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301473 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001474 char pr;
1475 char *p;
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001476 char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001477 int ret, i, argc;
1478 char **argv;
1479
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301480 pr_debug("Parsing probe_events: %s\n", cmd);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001481 argv = argv_split(cmd, &argc);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001482 if (!argv) {
1483 pr_debug("Failed to split arguments.\n");
1484 return -ENOMEM;
1485 }
1486 if (argc < 2) {
1487 semantic_error("Too few probe arguments.\n");
1488 ret = -ERANGE;
1489 goto out;
1490 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001491
1492 /* Scan event and group name. */
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001493 argv0_str = strdup(argv[0]);
1494 if (argv0_str == NULL) {
1495 ret = -ENOMEM;
1496 goto out;
1497 }
1498 fmt1_str = strtok_r(argv0_str, ":", &fmt);
1499 fmt2_str = strtok_r(NULL, "/", &fmt);
1500 fmt3_str = strtok_r(NULL, " \t", &fmt);
1501 if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
1502 || fmt3_str == NULL) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001503 semantic_error("Failed to parse event name: %s\n", argv[0]);
1504 ret = -EINVAL;
1505 goto out;
1506 }
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001507 pr = fmt1_str[0];
1508 tev->group = strdup(fmt2_str);
1509 tev->event = strdup(fmt3_str);
1510 if (tev->group == NULL || tev->event == NULL) {
1511 ret = -ENOMEM;
1512 goto out;
1513 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001514 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001515
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001516 tp->retprobe = (pr == 'r');
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001517
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09001518 /* Scan module name(if there), function name and offset */
1519 p = strchr(argv[1], ':');
1520 if (p) {
1521 tp->module = strndup(argv[1], p - argv[1]);
1522 p++;
1523 } else
1524 p = argv[1];
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001525 fmt1_str = strtok_r(p, "+", &fmt);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001526 if (fmt1_str[0] == '0') /* only the address started with 0x */
1527 tp->address = strtoul(fmt1_str, NULL, 0);
1528 else {
1529 /* Only the symbol-based probe has offset */
1530 tp->symbol = strdup(fmt1_str);
1531 if (tp->symbol == NULL) {
1532 ret = -ENOMEM;
1533 goto out;
1534 }
1535 fmt2_str = strtok_r(NULL, "", &fmt);
1536 if (fmt2_str == NULL)
1537 tp->offset = 0;
1538 else
1539 tp->offset = strtoul(fmt2_str, NULL, 10);
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001540 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001541
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001542 tev->nargs = argc - 2;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301543 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001544 if (tev->args == NULL) {
1545 ret = -ENOMEM;
1546 goto out;
1547 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001548 for (i = 0; i < tev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001549 p = strchr(argv[i + 2], '=');
1550 if (p) /* We don't need which register is assigned. */
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001551 *p++ = '\0';
1552 else
1553 p = argv[i + 2];
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001554 tev->args[i].name = strdup(argv[i + 2]);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001555 /* TODO: parse regs and offset */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001556 tev->args[i].value = strdup(p);
1557 if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
1558 ret = -ENOMEM;
1559 goto out;
1560 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001561 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001562 ret = 0;
1563out:
Irina Tirdeabcbd0042012-09-20 23:37:50 +03001564 free(argv0_str);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001565 argv_free(argv);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001566 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001567}
1568
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001569/* Compose only probe arg */
1570int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
1571{
1572 struct perf_probe_arg_field *field = pa->field;
1573 int ret;
1574 char *tmp = buf;
1575
Masami Hiramatsu48481932010-04-12 13:16:53 -04001576 if (pa->name && pa->var)
1577 ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
1578 else
1579 ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001580 if (ret <= 0)
1581 goto error;
1582 tmp += ret;
1583 len -= ret;
1584
1585 while (field) {
Masami Hiramatsub2a3c122010-05-19 15:57:42 -04001586 if (field->name[0] == '[')
1587 ret = e_snprintf(tmp, len, "%s", field->name);
1588 else
1589 ret = e_snprintf(tmp, len, "%s%s",
1590 field->ref ? "->" : ".", field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001591 if (ret <= 0)
1592 goto error;
1593 tmp += ret;
1594 len -= ret;
1595 field = field->next;
1596 }
Masami Hiramatsu11a1ca32010-04-12 13:17:22 -04001597
1598 if (pa->type) {
1599 ret = e_snprintf(tmp, len, ":%s", pa->type);
1600 if (ret <= 0)
1601 goto error;
1602 tmp += ret;
1603 len -= ret;
1604 }
1605
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001606 return tmp - buf;
1607error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001608 pr_debug("Failed to synthesize perf probe argument: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001609 return ret;
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001610}
1611
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001612/* Compose only probe point (not argument) */
1613static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001614{
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001615 char *buf, *tmp;
1616 char offs[32] = "", line[32] = "", file[32] = "";
1617 int ret, len;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05001618
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001619 buf = zalloc(MAX_CMDLEN);
1620 if (buf == NULL) {
1621 ret = -ENOMEM;
1622 goto error;
1623 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001624 if (pp->offset) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001625 ret = e_snprintf(offs, 32, "+%lu", pp->offset);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001626 if (ret <= 0)
1627 goto error;
1628 }
1629 if (pp->line) {
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001630 ret = e_snprintf(line, 32, ":%d", pp->line);
1631 if (ret <= 0)
1632 goto error;
1633 }
1634 if (pp->file) {
Franck Bui-Huu32ae2ad2010-12-23 16:04:23 +01001635 tmp = pp->file;
1636 len = strlen(tmp);
1637 if (len > 30) {
1638 tmp = strchr(pp->file + len - 30, '/');
1639 tmp = tmp ? tmp + 1 : pp->file + len - 30;
1640 }
1641 ret = e_snprintf(file, 32, "@%s", tmp);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001642 if (ret <= 0)
1643 goto error;
1644 }
1645
1646 if (pp->function)
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001647 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
1648 offs, pp->retprobe ? "%return" : "", line,
1649 file);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001650 else
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001651 ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001652 if (ret <= 0)
1653 goto error;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001654
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001655 return buf;
1656error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001657 pr_debug("Failed to synthesize perf probe point: %d\n", ret);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001658 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001659 return NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001660}
1661
1662#if 0
1663char *synthesize_perf_probe_command(struct perf_probe_event *pev)
1664{
1665 char *buf;
1666 int i, len, ret;
1667
1668 buf = synthesize_perf_probe_point(&pev->point);
1669 if (!buf)
1670 return NULL;
1671
1672 len = strlen(buf);
1673 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001674 ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001675 pev->args[i].name);
1676 if (ret <= 0) {
1677 free(buf);
1678 return NULL;
1679 }
1680 len += ret;
1681 }
1682
1683 return buf;
1684}
1685#endif
1686
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301687static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001688 char **buf, size_t *buflen,
1689 int depth)
1690{
1691 int ret;
1692 if (ref->next) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301693 depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001694 buflen, depth + 1);
1695 if (depth < 0)
1696 goto out;
1697 }
1698
1699 ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
1700 if (ret < 0)
1701 depth = ret;
1702 else {
1703 *buf += ret;
1704 *buflen -= ret;
1705 }
1706out:
1707 return depth;
1708
1709}
1710
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301711static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001712 char *buf, size_t buflen)
1713{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301714 struct probe_trace_arg_ref *ref = arg->ref;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001715 int ret, depth = 0;
1716 char *tmp = buf;
1717
1718 /* Argument name or separator */
1719 if (arg->name)
1720 ret = e_snprintf(buf, buflen, " %s=", arg->name);
1721 else
1722 ret = e_snprintf(buf, buflen, " ");
1723 if (ret < 0)
1724 return ret;
1725 buf += ret;
1726 buflen -= ret;
1727
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001728 /* Special case: @XXX */
1729 if (arg->value[0] == '@' && arg->ref)
1730 ref = ref->next;
1731
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001732 /* Dereferencing arguments */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001733 if (ref) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301734 depth = __synthesize_probe_trace_arg_ref(ref, &buf,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001735 &buflen, 1);
1736 if (depth < 0)
1737 return depth;
1738 }
1739
1740 /* Print argument value */
Masami Hiramatsub7dcb852010-05-19 15:57:49 -04001741 if (arg->value[0] == '@' && arg->ref)
1742 ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
1743 arg->ref->offset);
1744 else
1745 ret = e_snprintf(buf, buflen, "%s", arg->value);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001746 if (ret < 0)
1747 return ret;
1748 buf += ret;
1749 buflen -= ret;
1750
1751 /* Closing */
1752 while (depth--) {
1753 ret = e_snprintf(buf, buflen, ")");
1754 if (ret < 0)
1755 return ret;
1756 buf += ret;
1757 buflen -= ret;
1758 }
Masami Hiramatsu49849122010-04-12 13:17:15 -04001759 /* Print argument type */
1760 if (arg->type) {
1761 ret = e_snprintf(buf, buflen, ":%s", arg->type);
1762 if (ret <= 0)
1763 return ret;
1764 buf += ret;
1765 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001766
1767 return buf - tmp;
1768}
1769
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301770char *synthesize_probe_trace_command(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001771{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301772 struct probe_trace_point *tp = &tev->point;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001773 char *buf;
1774 int i, len, ret;
1775
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001776 buf = zalloc(MAX_CMDLEN);
1777 if (buf == NULL)
1778 return NULL;
1779
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001780 len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
1781 tev->group, tev->event);
1782 if (len <= 0)
1783 goto error;
1784
1785 /* Uprobes must have tp->address and tp->module */
1786 if (tev->uprobes && (!tp->address || !tp->module))
1787 goto error;
1788
1789 /* Use the tp->address for uprobes */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301790 if (tev->uprobes)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001791 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s:0x%lx",
1792 tp->module, tp->address);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301793 else
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001794 ret = e_snprintf(buf + len, MAX_CMDLEN - len, "%s%s%s+%lu",
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301795 tp->module ?: "", tp->module ? ":" : "",
1796 tp->symbol, tp->offset);
1797
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001798 if (ret <= 0)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001799 goto error;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00001800 len += ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001801
1802 for (i = 0; i < tev->nargs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301803 ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001804 MAX_CMDLEN - len);
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001805 if (ret <= 0)
1806 goto error;
1807 len += ret;
1808 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001809
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001810 return buf;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001811error:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001812 free(buf);
1813 return NULL;
1814}
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001815
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001816static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
1817 struct perf_probe_point *pp,
1818 bool is_kprobe)
1819{
1820 struct symbol *sym = NULL;
1821 struct map *map;
1822 u64 addr;
1823 int ret = -ENOENT;
1824
1825 if (!is_kprobe) {
1826 map = dso__new_map(tp->module);
1827 if (!map)
1828 goto out;
1829 addr = tp->address;
1830 sym = map__find_symbol(map, addr, NULL);
1831 } else {
1832 addr = kernel_get_symbol_address_by_name(tp->symbol, true);
1833 if (addr) {
1834 addr += tp->offset;
1835 sym = __find_kernel_function(addr, &map);
1836 }
1837 }
1838 if (!sym)
1839 goto out;
1840
1841 pp->retprobe = tp->retprobe;
1842 pp->offset = addr - map->unmap_ip(map, sym->start);
1843 pp->function = strdup(sym->name);
1844 ret = pp->function ? 0 : -ENOMEM;
1845
1846out:
1847 if (map && !is_kprobe) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001848 map__put(map);
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001849 }
1850
1851 return ret;
1852}
1853
1854static int convert_to_perf_probe_point(struct probe_trace_point *tp,
1855 struct perf_probe_point *pp,
1856 bool is_kprobe)
1857{
1858 char buf[128];
1859 int ret;
1860
1861 ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
1862 if (!ret)
1863 return 0;
1864 ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
1865 if (!ret)
1866 return 0;
1867
1868 pr_debug("Failed to find probe point from both of dwarf and map.\n");
1869
1870 if (tp->symbol) {
1871 pp->function = strdup(tp->symbol);
1872 pp->offset = tp->offset;
1873 } else if (!tp->module && !is_kprobe) {
1874 ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
1875 if (ret < 0)
1876 return ret;
1877 pp->function = strdup(buf);
1878 pp->offset = 0;
1879 }
1880 if (pp->function == NULL)
1881 return -ENOMEM;
1882
1883 pp->retprobe = tp->retprobe;
1884
1885 return 0;
1886}
1887
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301888static int convert_to_perf_probe_event(struct probe_trace_event *tev,
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301889 struct perf_probe_event *pev, bool is_kprobe)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001890{
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001891 char buf[64] = "";
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001892 int i, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001893
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001894 /* Convert event/group name */
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001895 pev->event = strdup(tev->event);
1896 pev->group = strdup(tev->group);
1897 if (pev->event == NULL || pev->group == NULL)
1898 return -ENOMEM;
Masami Hiramatsufb1587d2010-03-16 18:06:19 -04001899
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001900 /* Convert trace_point to probe_point */
Masami Hiramatsu5a6f6312014-02-06 05:32:23 +00001901 ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001902 if (ret < 0)
1903 return ret;
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03001904
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001905 /* Convert trace_arg to probe_arg */
1906 pev->nargs = tev->nargs;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04001907 pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
1908 if (pev->args == NULL)
1909 return -ENOMEM;
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001910 for (i = 0; i < tev->nargs && ret >= 0; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001911 if (tev->args[i].name)
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001912 pev->args[i].name = strdup(tev->args[i].name);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001913 else {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301914 ret = synthesize_probe_trace_arg(&tev->args[i],
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001915 buf, 64);
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001916 pev->args[i].name = strdup(buf);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001917 }
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04001918 if (pev->args[i].name == NULL && ret >= 0)
1919 ret = -ENOMEM;
1920 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04001921
1922 if (ret < 0)
1923 clear_perf_probe_event(pev);
1924
1925 return ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001926}
1927
1928void clear_perf_probe_event(struct perf_probe_event *pev)
1929{
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001930 struct perf_probe_arg_field *field, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001931 int i;
1932
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001933 free(pev->event);
1934 free(pev->group);
Masami Hiramatsu7afb3fa2015-04-01 19:25:39 +09001935 free(pev->target);
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09001936 clear_perf_probe_point(&pev->point);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001937
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001938 for (i = 0; i < pev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001939 free(pev->args[i].name);
1940 free(pev->args[i].var);
1941 free(pev->args[i].type);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001942 field = pev->args[i].field;
1943 while (field) {
1944 next = field->next;
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001945 zfree(&field->name);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04001946 free(field);
1947 field = next;
1948 }
1949 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001950 free(pev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001951 memset(pev, 0, sizeof(*pev));
1952}
1953
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301954static void clear_probe_trace_event(struct probe_trace_event *tev)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001955{
Srikar Dronamraju0e608362010-07-29 19:43:51 +05301956 struct probe_trace_arg_ref *ref, *next;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001957 int i;
1958
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001959 free(tev->event);
1960 free(tev->group);
1961 free(tev->point.symbol);
Masami Hiramatsu4c859352015-05-08 10:03:35 +09001962 free(tev->point.realname);
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001963 free(tev->point.module);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001964 for (i = 0; i < tev->nargs; i++) {
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001965 free(tev->args[i].name);
1966 free(tev->args[i].value);
1967 free(tev->args[i].type);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001968 ref = tev->args[i].ref;
1969 while (ref) {
1970 next = ref->next;
1971 free(ref);
1972 ref = next;
1973 }
1974 }
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -03001975 free(tev->args);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04001976 memset(tev, 0, sizeof(*tev));
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05001977}
1978
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001979static void print_open_warning(int err, bool is_kprobe)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301980{
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00001981 char sbuf[STRERR_BUFSIZE];
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301982
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001983 if (err == -ENOENT) {
Srikar Dronamraju225466f2012-04-16 17:39:09 +05301984 const char *config;
1985
1986 if (!is_kprobe)
1987 config = "CONFIG_UPROBE_EVENTS";
1988 else
1989 config = "CONFIG_KPROBE_EVENTS";
1990
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001991 pr_warning("%cprobe_events file does not exist"
1992 " - please rebuild kernel with %s.\n",
1993 is_kprobe ? 'k' : 'u', config);
1994 } else if (err == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05001995 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu5e451872014-08-13 16:12:48 +00001996 else
1997 pr_warning("Failed to open %cprobe_events: %s\n",
1998 is_kprobe ? 'k' : 'u',
1999 strerror_r(-err, sbuf, sizeof(sbuf)));
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302000}
2001
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002002static void print_both_open_warning(int kerr, int uerr)
2003{
2004 /* Both kprobes and uprobes are disabled, warn it. */
2005 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002006 pr_warning("Tracefs or debugfs is not mounted.\n");
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002007 else if (kerr == -ENOENT && uerr == -ENOENT)
2008 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
2009 "or/and CONFIG_UPROBE_EVENTS.\n");
2010 else {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002011 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002012 pr_warning("Failed to open kprobe events: %s.\n",
2013 strerror_r(-kerr, sbuf, sizeof(sbuf)));
2014 pr_warning("Failed to open uprobe events: %s.\n",
2015 strerror_r(-uerr, sbuf, sizeof(sbuf)));
2016 }
2017}
2018
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002019static int open_probe_events(const char *trace_file, bool readwrite)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002020{
2021 char buf[PATH_MAX];
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002022 const char *__debugfs;
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002023 const char *tracing_dir = "";
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002024 int ret;
2025
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002026 __debugfs = tracefs_find_mountpoint();
2027 if (__debugfs == NULL) {
2028 tracing_dir = "tracing/";
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002029
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002030 __debugfs = debugfs_find_mountpoint();
2031 if (__debugfs == NULL)
2032 return -ENOTSUP;
2033 }
2034
2035 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
2036 __debugfs, tracing_dir, trace_file);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002037 if (ret >= 0) {
Masami Hiramatsu7ca59892010-04-14 18:39:28 -04002038 pr_debug("Opening %s write=%d\n", buf, readwrite);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002039 if (readwrite && !probe_event_dry_run)
Masami Hiramatsub8dc39842015-05-06 21:46:42 +09002040 ret = open(buf, O_RDWR | O_APPEND, 0);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002041 else
2042 ret = open(buf, O_RDONLY, 0);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002043
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302044 if (ret < 0)
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002045 ret = -errno;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002046 }
2047 return ret;
2048}
2049
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302050static int open_kprobe_events(bool readwrite)
2051{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002052 return open_probe_events("kprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302053}
2054
2055static int open_uprobe_events(bool readwrite)
2056{
Steven Rostedt (Red Hat)23773ca2015-02-02 14:35:07 -05002057 return open_probe_events("uprobe_events", readwrite);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302058}
2059
2060/* Get raw string list of current kprobe_events or uprobe_events */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302061static struct strlist *get_probe_trace_command_rawlist(int fd)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002062{
2063 int ret, idx;
2064 FILE *fp;
2065 char buf[MAX_CMDLEN];
2066 char *p;
2067 struct strlist *sl;
2068
2069 sl = strlist__new(true, NULL);
2070
2071 fp = fdopen(dup(fd), "r");
2072 while (!feof(fp)) {
2073 p = fgets(buf, MAX_CMDLEN, fp);
2074 if (!p)
2075 break;
2076
2077 idx = strlen(p) - 1;
2078 if (p[idx] == '\n')
2079 p[idx] = '\0';
2080 ret = strlist__add(sl, buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002081 if (ret < 0) {
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002082 pr_debug("strlist__add failed (%d)\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002083 strlist__delete(sl);
2084 return NULL;
2085 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002086 }
2087 fclose(fp);
2088
2089 return sl;
2090}
2091
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002092struct kprobe_blacklist_node {
2093 struct list_head list;
2094 unsigned long start;
2095 unsigned long end;
2096 char *symbol;
2097};
2098
2099static void kprobe_blacklist__delete(struct list_head *blacklist)
2100{
2101 struct kprobe_blacklist_node *node;
2102
2103 while (!list_empty(blacklist)) {
2104 node = list_first_entry(blacklist,
2105 struct kprobe_blacklist_node, list);
2106 list_del(&node->list);
2107 free(node->symbol);
2108 free(node);
2109 }
2110}
2111
2112static int kprobe_blacklist__load(struct list_head *blacklist)
2113{
2114 struct kprobe_blacklist_node *node;
2115 const char *__debugfs = debugfs_find_mountpoint();
2116 char buf[PATH_MAX], *p;
2117 FILE *fp;
2118 int ret;
2119
2120 if (__debugfs == NULL)
2121 return -ENOTSUP;
2122
2123 ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
2124 if (ret < 0)
2125 return ret;
2126
2127 fp = fopen(buf, "r");
2128 if (!fp)
2129 return -errno;
2130
2131 ret = 0;
2132 while (fgets(buf, PATH_MAX, fp)) {
2133 node = zalloc(sizeof(*node));
2134 if (!node) {
2135 ret = -ENOMEM;
2136 break;
2137 }
2138 INIT_LIST_HEAD(&node->list);
2139 list_add_tail(&node->list, blacklist);
2140 if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
2141 ret = -EINVAL;
2142 break;
2143 }
2144 p = strchr(buf, '\t');
2145 if (p) {
2146 p++;
2147 if (p[strlen(p) - 1] == '\n')
2148 p[strlen(p) - 1] = '\0';
2149 } else
2150 p = (char *)"unknown";
2151 node->symbol = strdup(p);
2152 if (!node->symbol) {
2153 ret = -ENOMEM;
2154 break;
2155 }
2156 pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
2157 node->start, node->end, node->symbol);
2158 ret++;
2159 }
2160 if (ret < 0)
2161 kprobe_blacklist__delete(blacklist);
2162 fclose(fp);
2163
2164 return ret;
2165}
2166
2167static struct kprobe_blacklist_node *
2168kprobe_blacklist__find_by_address(struct list_head *blacklist,
2169 unsigned long address)
2170{
2171 struct kprobe_blacklist_node *node;
2172
2173 list_for_each_entry(node, blacklist, list) {
2174 if (node->start <= address && address <= node->end)
2175 return node;
2176 }
2177
2178 return NULL;
2179}
2180
Masami Hiramatsub0312202015-06-16 20:50:55 +09002181static LIST_HEAD(kprobe_blacklist);
2182
2183static void kprobe_blacklist__init(void)
2184{
2185 if (!list_empty(&kprobe_blacklist))
2186 return;
2187
2188 if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
2189 pr_debug("No kprobe blacklist support, ignored\n");
2190}
2191
2192static void kprobe_blacklist__release(void)
2193{
2194 kprobe_blacklist__delete(&kprobe_blacklist);
2195}
2196
2197static bool kprobe_blacklist__listed(unsigned long address)
2198{
2199 return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
2200}
2201
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002202static int perf_probe_event__sprintf(const char *group, const char *event,
2203 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002204 const char *module,
2205 struct strbuf *result)
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002206{
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002207 int i, ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002208 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002209 char *place;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002210
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002211 /* Synthesize only event probe point */
2212 place = synthesize_perf_probe_point(&pev->point);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002213 if (!place)
2214 return -EINVAL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002215
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002216 ret = e_snprintf(buf, 128, "%s:%s", group, event);
Masami Hiramatsu7e990a52009-12-15 10:31:21 -05002217 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002218 goto out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002219
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002220 strbuf_addf(result, " %-20s (on %s", buf, place);
Masami Hiramatsufb226cc2014-02-06 05:32:13 +00002221 if (module)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002222 strbuf_addf(result, " in %s", module);
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002223
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002224 if (pev->nargs > 0) {
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002225 strbuf_addstr(result, " with");
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002226 for (i = 0; i < pev->nargs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002227 ret = synthesize_perf_probe_arg(&pev->args[i],
2228 buf, 128);
2229 if (ret < 0)
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002230 goto out;
2231 strbuf_addf(result, " %s", buf);
Masami Hiramatsu7df2f322010-03-16 18:06:26 -04002232 }
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002233 }
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002234 strbuf_addch(result, ')');
2235out:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002236 free(place);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002237 return ret;
Masami Hiramatsu278498d2009-12-08 17:02:40 -05002238}
2239
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002240/* Show an event */
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002241static int show_perf_probe_event(const char *group, const char *event,
2242 struct perf_probe_event *pev,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002243 const char *module, bool use_stdout)
2244{
2245 struct strbuf buf = STRBUF_INIT;
2246 int ret;
2247
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002248 ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002249 if (ret >= 0) {
2250 if (use_stdout)
2251 printf("%s\n", buf.buf);
2252 else
2253 pr_info("%s\n", buf.buf);
2254 }
2255 strbuf_release(&buf);
2256
2257 return ret;
2258}
2259
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002260static bool filter_probe_trace_event(struct probe_trace_event *tev,
2261 struct strfilter *filter)
2262{
2263 char tmp[128];
2264
2265 /* At first, check the event name itself */
2266 if (strfilter__compare(filter, tev->event))
2267 return true;
2268
2269 /* Next, check the combination of name and group */
2270 if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
2271 return false;
2272 return strfilter__compare(filter, tmp);
2273}
2274
2275static int __show_perf_probe_events(int fd, bool is_kprobe,
2276 struct strfilter *filter)
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002277{
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302278 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302279 struct probe_trace_event tev;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002280 struct perf_probe_event pev;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002281 struct strlist *rawlist;
2282 struct str_node *ent;
2283
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002284 memset(&tev, 0, sizeof(tev));
2285 memset(&pev, 0, sizeof(pev));
Masami Hiramatsu72041332010-01-05 17:47:10 -05002286
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302287 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002288 if (!rawlist)
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002289 return -ENOMEM;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002290
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002291 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302292 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002293 if (ret >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002294 if (!filter_probe_trace_event(&tev, filter))
2295 goto next;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302296 ret = convert_to_perf_probe_event(&tev, &pev,
2297 is_kprobe);
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002298 if (ret < 0)
2299 goto next;
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002300 ret = show_perf_probe_event(pev.group, pev.event,
2301 &pev, tev.point.module,
Masami Hiramatsuba7ecb02015-06-13 10:31:16 +09002302 true);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002303 }
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002304next:
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002305 clear_perf_probe_event(&pev);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302306 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002307 if (ret < 0)
2308 break;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002309 }
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002310 strlist__delete(rawlist);
Masami Hiramatsu7737af02015-06-17 23:58:54 +09002311 /* Cleanup cached debuginfo if needed */
2312 debuginfo_cache__exit();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002313
2314 return ret;
Masami Hiramatsu4de189f2009-11-30 19:20:17 -05002315}
2316
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302317/* List up current perf-probe events */
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002318int show_perf_probe_events(struct strfilter *filter)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302319{
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002320 int kp_fd, up_fd, ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302321
2322 setup_pager();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302323
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002324 ret = init_symbol_maps(false);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302325 if (ret < 0)
2326 return ret;
2327
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002328 kp_fd = open_kprobe_events(false);
2329 if (kp_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002330 ret = __show_perf_probe_events(kp_fd, true, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002331 close(kp_fd);
2332 if (ret < 0)
2333 goto out;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302334 }
2335
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002336 up_fd = open_uprobe_events(false);
2337 if (kp_fd < 0 && up_fd < 0) {
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002338 print_both_open_warning(kp_fd, up_fd);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002339 ret = kp_fd;
2340 goto out;
2341 }
2342
2343 if (up_fd >= 0) {
Masami Hiramatsub6a89642015-04-24 18:47:50 +09002344 ret = __show_perf_probe_events(up_fd, false, filter);
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002345 close(up_fd);
2346 }
2347out:
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002348 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302349 return ret;
2350}
2351
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002352/* Get current perf-probe event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302353static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002354{
Masami Hiramatsufa282442009-12-08 17:03:23 -05002355 char buf[128];
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002356 struct strlist *sl, *rawlist;
2357 struct str_node *ent;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302358 struct probe_trace_event tev;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002359 int ret = 0;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002360
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002361 memset(&tev, 0, sizeof(tev));
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302362 rawlist = get_probe_trace_command_rawlist(fd);
Masami Hiramatsu6eb08662014-08-14 02:22:30 +00002363 if (!rawlist)
2364 return NULL;
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002365 sl = strlist__new(true, NULL);
Masami Hiramatsuadf365f2009-12-15 10:32:03 -05002366 strlist__for_each(ent, rawlist) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302367 ret = parse_probe_trace_command(ent->s, &tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002368 if (ret < 0)
2369 break;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002370 if (include_group) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002371 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
2372 tev.event);
2373 if (ret >= 0)
2374 ret = strlist__add(sl, buf);
Masami Hiramatsufa282442009-12-08 17:03:23 -05002375 } else
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002376 ret = strlist__add(sl, tev.event);
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302377 clear_probe_trace_event(&tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002378 if (ret < 0)
2379 break;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002380 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002381 strlist__delete(rawlist);
2382
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002383 if (ret < 0) {
2384 strlist__delete(sl);
2385 return NULL;
2386 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002387 return sl;
2388}
2389
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302390static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002391{
Frederic Weisbecker6eca8cc2010-04-21 02:01:05 +02002392 int ret = 0;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302393 char *buf = synthesize_probe_trace_command(tev);
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002394 char sbuf[STRERR_BUFSIZE];
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002395
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002396 if (!buf) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302397 pr_debug("Failed to synthesize probe trace event.\n");
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002398 return -EINVAL;
2399 }
2400
Masami Hiramatsufa282442009-12-08 17:03:23 -05002401 pr_debug("Writing event: %s\n", buf);
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002402 if (!probe_event_dry_run) {
2403 ret = write(fd, buf, strlen(buf));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002404 if (ret <= 0) {
2405 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002406 pr_warning("Failed to write event: %s\n",
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002407 strerror_r(errno, sbuf, sizeof(sbuf)));
Namhyung Kim7949ba12015-01-10 19:33:48 +09002408 }
Masami Hiramatsuf4d7da42010-03-16 18:06:05 -04002409 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002410 free(buf);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002411 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002412}
2413
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002414static int get_new_event_name(char *buf, size_t len, const char *base,
2415 struct strlist *namelist, bool allow_suffix)
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002416{
2417 int i, ret;
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002418 char *p;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002419
Naveen N. Rao3099c022015-04-28 17:35:34 +05302420 if (*base == '.')
2421 base++;
2422
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002423 /* Try no suffix */
2424 ret = e_snprintf(buf, len, "%s", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002425 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002426 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002427 return ret;
2428 }
Masami Hiramatsu35a23ff92015-06-12 14:08:20 +09002429 /* Cut off the postfixes (e.g. .const, .isra)*/
2430 p = strchr(buf, '.');
2431 if (p && p != buf)
2432 *p = '\0';
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002433 if (!strlist__has_entry(namelist, buf))
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002434 return 0;
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002435
Masami Hiramatsud761b082009-12-15 10:32:25 -05002436 if (!allow_suffix) {
2437 pr_warning("Error: event \"%s\" already exists. "
2438 "(Use -f to force duplicates.)\n", base);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002439 return -EEXIST;
Masami Hiramatsud761b082009-12-15 10:32:25 -05002440 }
2441
Masami Hiramatsu17f88fc2009-12-08 17:03:02 -05002442 /* Try to add suffix */
2443 for (i = 1; i < MAX_EVENT_INDEX; i++) {
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002444 ret = e_snprintf(buf, len, "%s_%d", base, i);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002445 if (ret < 0) {
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002446 pr_debug("snprintf() failed: %d\n", ret);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002447 return ret;
2448 }
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002449 if (!strlist__has_entry(namelist, buf))
2450 break;
2451 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002452 if (i == MAX_EVENT_INDEX) {
2453 pr_warning("Too many events are on the same function.\n");
2454 ret = -ERANGE;
2455 }
2456
2457 return ret;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002458}
2459
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002460/* Warn if the current kernel's uprobe implementation is old */
2461static void warn_uprobe_event_compat(struct probe_trace_event *tev)
2462{
2463 int i;
2464 char *buf = synthesize_probe_trace_command(tev);
2465
2466 /* Old uprobe event doesn't support memory dereference */
2467 if (!tev->uprobes || tev->nargs == 0 || !buf)
2468 goto out;
2469
2470 for (i = 0; i < tev->nargs; i++)
2471 if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
2472 pr_warning("Please upgrade your kernel to at least "
2473 "3.14 to have access to feature %s\n",
2474 tev->args[i].value);
2475 break;
2476 }
2477out:
2478 free(buf);
2479}
2480
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302481static int __add_probe_trace_events(struct perf_probe_event *pev,
2482 struct probe_trace_event *tevs,
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002483 int ntevs, bool allow_suffix)
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002484{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002485 int i, fd, ret;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302486 struct probe_trace_event *tev = NULL;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002487 char buf[64];
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002488 const char *event = NULL, *group = NULL;
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002489 struct strlist *namelist;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002490 bool safename;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002491
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302492 if (pev->uprobes)
2493 fd = open_uprobe_events(true);
2494 else
2495 fd = open_kprobe_events(true);
2496
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002497 if (fd < 0) {
2498 print_open_warning(fd, !pev->uprobes);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002499 return fd;
Masami Hiramatsu5e451872014-08-13 16:12:48 +00002500 }
2501
Masami Hiramatsub498ce12009-11-30 19:20:25 -05002502 /* Get current event names */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302503 namelist = get_probe_trace_event_names(fd, false);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002504 if (!namelist) {
2505 pr_debug("Failed to get current event list.\n");
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002506 ret = -ENOMEM;
2507 goto close_out;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002508 }
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002509
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002510 safename = (pev->point.function && !strisglob(pev->point.function));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002511 ret = 0;
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002512 pr_info("Added new event%s\n", (ntevs > 1) ? "s:" : ":");
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002513 for (i = 0; i < ntevs; i++) {
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002514 tev = &tevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002515 /* Skip if the symbol is out of .text or blacklisted */
Masami Hiramatsu5a51fcd2015-05-06 21:46:49 +09002516 if (!tev->point.symbol)
2517 continue;
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002518
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002519 if (pev->event)
2520 event = pev->event;
2521 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002522 if (safename)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002523 event = pev->point.function;
2524 else
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002525 event = tev->point.realname;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002526 if (pev->group)
2527 group = pev->group;
2528 else
2529 group = PERFPROBE_GROUP;
2530
2531 /* Get an unused new event name */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002532 ret = get_new_event_name(buf, 64, event,
2533 namelist, allow_suffix);
2534 if (ret < 0)
2535 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002536 event = buf;
2537
Masami Hiramatsu02b95da2010-04-12 13:17:56 -04002538 tev->event = strdup(event);
2539 tev->group = strdup(group);
2540 if (tev->event == NULL || tev->group == NULL) {
2541 ret = -ENOMEM;
2542 break;
2543 }
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302544 ret = write_probe_trace_event(fd, tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002545 if (ret < 0)
2546 break;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002547 /* Add added event name to namelist */
2548 strlist__add(namelist, event);
2549
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002550 /* We use tev's name for showing new events */
2551 show_perf_probe_event(tev->group, tev->event, pev,
2552 tev->point.module, false);
2553 /* Save the last valid name */
2554 event = tev->event;
2555 group = tev->group;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002556
2557 /*
2558 * Probes after the first probe which comes from same
2559 * user input are always allowed to add suffix, because
2560 * there might be several addresses corresponding to
2561 * one code line.
2562 */
2563 allow_suffix = true;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002564 }
Masami Hiramatsu79702f62015-02-28 11:53:29 +09002565 if (ret == -EINVAL && pev->uprobes)
2566 warn_uprobe_event_compat(tev);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002567
Masami Hiramatsu9aaf5a52015-02-19 23:31:13 +09002568 /* Note that it is possible to skip all events because of blacklist */
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002569 if (ret >= 0 && event) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002570 /* Show how to use the event. */
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002571 pr_info("\nYou can now use it in all perf tools, such as:\n\n");
Masami Hiramatsud350bd52015-06-16 20:50:57 +09002572 pr_info("\tperf record -e %s:%s -aR sleep 1\n\n", group, event);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002573 }
Masami Hiramatsua9b495b2009-12-08 17:02:47 -05002574
Masami Hiramatsue1d20172009-12-07 12:00:46 -05002575 strlist__delete(namelist);
Masami Hiramatsuae2cb1a2015-05-06 21:46:40 +09002576close_out:
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002577 close(fd);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002578 return ret;
Masami Hiramatsu50656ee2009-11-30 19:19:58 -05002579}
Masami Hiramatsufa282442009-12-08 17:03:23 -05002580
Wang Nan6bb536c2015-05-29 09:45:47 +00002581static int find_probe_functions(struct map *map, char *name,
2582 struct symbol **syms)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002583{
Namhyung Kim564c62a2015-01-14 20:18:07 +09002584 int found = 0;
Arnaldo Carvalho de Melo0a3873a2015-01-16 16:40:25 -03002585 struct symbol *sym;
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002586 struct rb_node *tmp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002587
Wang Nan75e4a2a2015-05-15 12:14:44 +00002588 if (map__load(map, NULL) < 0)
2589 return 0;
2590
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002591 map__for_each_symbol(map, sym, tmp) {
Wang Nan6bb536c2015-05-29 09:45:47 +00002592 if (strglobmatch(sym->name, name)) {
Masami Hiramatsu4c859352015-05-08 10:03:35 +09002593 found++;
Wang Nan6bb536c2015-05-29 09:45:47 +00002594 if (syms && found < probe_conf.max_probes)
2595 syms[found - 1] = sym;
2596 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002597 }
Namhyung Kim564c62a2015-01-14 20:18:07 +09002598
2599 return found;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002600}
2601
2602#define strdup_or_goto(str, label) \
2603 ({ char *__p = strdup(str); if (!__p) goto label; __p; })
2604
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302605void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
2606 struct probe_trace_event *tev __maybe_unused,
2607 struct map *map __maybe_unused) { }
2608
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002609/*
2610 * Find probe function addresses from map.
2611 * Return an error or the number of found probe_trace_event
2612 */
2613static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002614 struct probe_trace_event **tevs)
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002615{
2616 struct map *map = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002617 struct ref_reloc_sym *reloc_sym = NULL;
2618 struct symbol *sym;
Wang Nan6bb536c2015-05-29 09:45:47 +00002619 struct symbol **syms = NULL;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002620 struct probe_trace_event *tev;
2621 struct perf_probe_point *pp = &pev->point;
2622 struct probe_trace_point *tp;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002623 int num_matched_functions;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002624 int ret, i, j, skipped = 0;
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002625
Masami Hiramatsu44225522015-05-08 10:03:28 +09002626 map = get_target_map(pev->target, pev->uprobes);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002627 if (!map) {
2628 ret = -EINVAL;
2629 goto out;
2630 }
2631
Wang Nan6bb536c2015-05-29 09:45:47 +00002632 syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
2633 if (!syms) {
2634 ret = -ENOMEM;
2635 goto out;
2636 }
2637
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002638 /*
2639 * Load matched symbols: Since the different local symbols may have
2640 * same name but different addresses, this lists all the symbols.
2641 */
Wang Nan6bb536c2015-05-29 09:45:47 +00002642 num_matched_functions = find_probe_functions(map, pp->function, syms);
Namhyung Kim564c62a2015-01-14 20:18:07 +09002643 if (num_matched_functions == 0) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002644 pr_err("Failed to find symbol %s in %s\n", pp->function,
Masami Hiramatsu44225522015-05-08 10:03:28 +09002645 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002646 ret = -ENOENT;
2647 goto out;
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002648 } else if (num_matched_functions > probe_conf.max_probes) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002649 pr_err("Too many functions matched in %s\n",
Masami Hiramatsu44225522015-05-08 10:03:28 +09002650 pev->target ? : "kernel");
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002651 ret = -E2BIG;
2652 goto out;
2653 }
2654
Namhyung Kim25dd9172015-01-14 20:18:08 +09002655 if (!pev->uprobes && !pp->retprobe) {
He Kuang0560a0c2015-03-20 09:56:56 +08002656 reloc_sym = kernel_get_ref_reloc_sym();
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002657 if (!reloc_sym) {
2658 pr_warning("Relocated base symbol is not found!\n");
2659 ret = -EINVAL;
2660 goto out;
2661 }
2662 }
2663
2664 /* Setup result trace-probe-events */
2665 *tevs = zalloc(sizeof(*tev) * num_matched_functions);
2666 if (!*tevs) {
2667 ret = -ENOMEM;
2668 goto out;
2669 }
2670
2671 ret = 0;
Namhyung Kim564c62a2015-01-14 20:18:07 +09002672
Wang Nan6bb536c2015-05-29 09:45:47 +00002673 for (j = 0; j < num_matched_functions; j++) {
2674 sym = syms[j];
2675
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002676 tev = (*tevs) + ret;
2677 tp = &tev->point;
2678 if (ret == num_matched_functions) {
2679 pr_warning("Too many symbols are listed. Skip it.\n");
2680 break;
2681 }
2682 ret++;
2683
2684 if (pp->offset > sym->end - sym->start) {
2685 pr_warning("Offset %ld is bigger than the size of %s\n",
2686 pp->offset, sym->name);
2687 ret = -ENOENT;
2688 goto err_out;
2689 }
2690 /* Add one probe point */
2691 tp->address = map->unmap_ip(map, sym->start) + pp->offset;
Masami Hiramatsub0312202015-06-16 20:50:55 +09002692 /* If we found a wrong one, mark it by NULL symbol */
2693 if (!pev->uprobes &&
2694 kprobe_warn_out_range(sym->name, tp->address)) {
2695 tp->symbol = NULL; /* Skip it */
2696 skipped++;
2697 } else if (reloc_sym) {
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002698 tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
2699 tp->offset = tp->address - reloc_sym->addr;
2700 } else {
2701 tp->symbol = strdup_or_goto(sym->name, nomem_out);
2702 tp->offset = pp->offset;
2703 }
Wang Nan6bb536c2015-05-29 09:45:47 +00002704 tp->realname = strdup_or_goto(sym->name, nomem_out);
2705
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002706 tp->retprobe = pp->retprobe;
Masami Hiramatsu44225522015-05-08 10:03:28 +09002707 if (pev->target)
2708 tev->point.module = strdup_or_goto(pev->target,
2709 nomem_out);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002710 tev->uprobes = pev->uprobes;
2711 tev->nargs = pev->nargs;
2712 if (tev->nargs) {
2713 tev->args = zalloc(sizeof(struct probe_trace_arg) *
2714 tev->nargs);
2715 if (tev->args == NULL)
2716 goto nomem_out;
2717 }
2718 for (i = 0; i < tev->nargs; i++) {
2719 if (pev->args[i].name)
2720 tev->args[i].name =
2721 strdup_or_goto(pev->args[i].name,
2722 nomem_out);
2723
2724 tev->args[i].value = strdup_or_goto(pev->args[i].var,
2725 nomem_out);
2726 if (pev->args[i].type)
2727 tev->args[i].type =
2728 strdup_or_goto(pev->args[i].type,
2729 nomem_out);
2730 }
Naveen N. Rao7b6ff0b2015-04-28 17:35:40 +05302731 arch__fix_tev_from_maps(pev, tev, map);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002732 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002733 if (ret == skipped) {
2734 ret = -ENOENT;
2735 goto err_out;
2736 }
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002737
2738out:
Masami Hiramatsu9b118ac2015-03-06 16:31:20 +09002739 put_target_map(map, pev->uprobes);
Wang Nan6bb536c2015-05-29 09:45:47 +00002740 free(syms);
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002741 return ret;
2742
2743nomem_out:
2744 ret = -ENOMEM;
2745err_out:
2746 clear_probe_trace_events(*tevs, num_matched_functions);
2747 zfree(tevs);
2748 goto out;
2749}
2750
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302751bool __weak arch__prefers_symtab(void) { return false; }
2752
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302753static int convert_to_probe_trace_events(struct perf_probe_event *pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002754 struct probe_trace_event **tevs)
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002755{
Masami Hiramatsueb948e52014-02-06 05:32:25 +00002756 int ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002757
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002758 if (pev->uprobes && !pev->group) {
2759 /* Replace group name if not given */
Masami Hiramatsu44225522015-05-08 10:03:28 +09002760 ret = convert_exec_to_group(pev->target, &pev->group);
Masami Hiramatsufb7345b2013-12-26 05:41:53 +00002761 if (ret != 0) {
2762 pr_warning("Failed to make a group name.\n");
2763 return ret;
2764 }
2765 }
2766
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302767 if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002768 ret = find_probe_trace_events_from_map(pev, tevs);
Naveen N. Raod5c2e2c2015-04-28 17:35:39 +05302769 if (ret > 0)
2770 return ret; /* Found in symbol table */
2771 }
2772
Masami Hiramatsu4b4da7f2010-03-22 13:10:26 -03002773 /* Convert perf_probe_event with debuginfo */
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002774 ret = try_to_find_probe_trace_events(pev, tevs);
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002775 if (ret != 0)
Masami Hiramatsu190b57f2011-06-27 16:27:45 +09002776 return ret; /* Found in debuginfo or got an error */
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002777
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002778 return find_probe_trace_events_from_map(pev, tevs);
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002779}
2780
2781struct __event_package {
2782 struct perf_probe_event *pev;
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302783 struct probe_trace_event *tevs;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002784 int ntevs;
2785};
2786
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002787int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002788{
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002789 int i, j, ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002790 struct __event_package *pkgs;
2791
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302792 ret = 0;
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002793 pkgs = zalloc(sizeof(struct __event_package) * npevs);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302794
Masami Hiramatsue334016f12010-04-12 13:17:49 -04002795 if (pkgs == NULL)
2796 return -ENOMEM;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002797
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002798 ret = init_symbol_maps(pevs->uprobes);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002799 if (ret < 0) {
2800 free(pkgs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002801 return ret;
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002802 }
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002803
2804 /* Loop 1: convert all events */
2805 for (i = 0; i < npevs; i++) {
2806 pkgs[i].pev = &pevs[i];
Masami Hiramatsub0312202015-06-16 20:50:55 +09002807 /* Init kprobe blacklist if needed */
2808 if (!pkgs[i].pev->uprobes)
2809 kprobe_blacklist__init();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002810 /* Convert with or without debuginfo */
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302811 ret = convert_to_probe_trace_events(pkgs[i].pev,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002812 &pkgs[i].tevs);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002813 if (ret < 0)
2814 goto end;
2815 pkgs[i].ntevs = ret;
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002816 }
Masami Hiramatsub0312202015-06-16 20:50:55 +09002817 /* This just release blacklist only if allocated */
2818 kprobe_blacklist__release();
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002819
2820 /* Loop 2: add all events */
Arnaldo Carvalho de Melo8635bf62011-02-22 06:56:18 -03002821 for (i = 0; i < npevs; i++) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302822 ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
Masami Hiramatsuddb2f582015-05-08 10:03:31 +09002823 pkgs[i].ntevs,
2824 probe_conf.force_add);
Arnaldo Carvalho de Melofbee6322011-02-21 13:23:57 -03002825 if (ret < 0)
2826 break;
2827 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002828end:
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002829 /* Loop 3: cleanup and free trace events */
2830 for (i = 0; i < npevs; i++) {
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002831 for (j = 0; j < pkgs[i].ntevs; j++)
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302832 clear_probe_trace_event(&pkgs[i].tevs[j]);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002833 zfree(&pkgs[i].tevs);
Masami Hiramatsu449e5b22010-08-03 11:11:40 +09002834 }
2835 free(pkgs);
Masami Hiramatsuee45b6c2014-02-06 05:32:04 +00002836 exit_symbol_maps();
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002837
2838 return ret;
Masami Hiramatsue0faa8d2010-03-16 18:05:37 -04002839}
2840
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302841static int __del_trace_probe_event(int fd, struct str_node *ent)
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002842{
2843 char *p;
2844 char buf[128];
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002845 int ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002846
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302847 /* Convert from perf-probe event to trace-probe event */
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002848 ret = e_snprintf(buf, 128, "-:%s", ent->s);
2849 if (ret < 0)
2850 goto error;
2851
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002852 p = strchr(buf + 2, ':');
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002853 if (!p) {
2854 pr_debug("Internal error: %s should have ':' but not.\n",
2855 ent->s);
2856 ret = -ENOTSUP;
2857 goto error;
2858 }
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002859 *p = '/';
2860
Masami Hiramatsu4235b042010-03-16 18:06:12 -04002861 pr_debug("Writing event: %s\n", buf);
2862 ret = write(fd, buf, strlen(buf));
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002863 if (ret < 0) {
2864 ret = -errno;
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002865 goto error;
Masami Hiramatsu44a56042011-10-04 19:45:04 +09002866 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002867
Masami Hiramatsu5e17b282014-10-27 16:31:31 -04002868 pr_info("Removed event: %s\n", ent->s);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002869 return 0;
2870error:
Masami Hiramatsu5f03cba2014-08-14 02:22:34 +00002871 pr_warning("Failed to delete event: %s\n",
2872 strerror_r(-ret, buf, sizeof(buf)));
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002873 return ret;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002874}
2875
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002876static int del_trace_probe_events(int fd, struct strfilter *filter,
2877 struct strlist *namelist)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002878{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002879 struct str_node *ent;
2880 const char *p;
Masami Hiramatsu6dbe31f2015-04-23 22:46:14 +09002881 int ret = -ENOENT;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002882
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002883 if (!namelist)
2884 return -ENOENT;
2885
2886 strlist__for_each(ent, namelist) {
2887 p = strchr(ent->s, ':');
2888 if ((p && strfilter__compare(filter, p + 1)) ||
2889 strfilter__compare(filter, ent->s)) {
Srikar Dronamraju0e608362010-07-29 19:43:51 +05302890 ret = __del_trace_probe_event(fd, ent);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002891 if (ret < 0)
2892 break;
Masami Hiramatsubbbb5212009-12-15 10:32:10 -05002893 }
2894 }
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002895
2896 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002897}
2898
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002899int del_perf_probe_events(struct strfilter *filter)
Masami Hiramatsufa282442009-12-08 17:03:23 -05002900{
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002901 int ret, ret2, ufd = -1, kfd = -1;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302902 struct strlist *namelist = NULL, *unamelist = NULL;
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002903 char *str = strfilter__string(filter);
2904
2905 if (!str)
2906 return -EINVAL;
2907
2908 pr_debug("Delete filter: \'%s\'\n", str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002909
Masami Hiramatsufa282442009-12-08 17:03:23 -05002910 /* Get current event names */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302911 kfd = open_kprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002912 if (kfd >= 0)
2913 namelist = get_probe_trace_event_names(kfd, true);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302914
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302915 ufd = open_uprobe_events(true);
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002916 if (ufd >= 0)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302917 unamelist = get_probe_trace_event_names(ufd, true);
2918
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002919 if (kfd < 0 && ufd < 0) {
2920 print_both_open_warning(kfd, ufd);
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002921 ret = kfd;
Masami Hiramatsu467ec082014-08-13 16:12:50 +00002922 goto error;
2923 }
2924
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002925 ret = del_trace_probe_events(kfd, filter, namelist);
2926 if (ret < 0 && ret != -ENOENT)
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302927 goto error;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002928
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002929 ret2 = del_trace_probe_events(ufd, filter, unamelist);
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002930 if (ret2 < 0 && ret2 != -ENOENT) {
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002931 ret = ret2;
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002932 goto error;
2933 }
2934 if (ret == -ENOENT && ret2 == -ENOENT)
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002935 pr_debug("\"%s\" does not hit any event.\n", str);
2936 /* Note that this is silently ignored */
Masami Hiramatsudddc7ee2015-05-27 17:37:25 +09002937 ret = 0;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302938
2939error:
2940 if (kfd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302941 strlist__delete(namelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302942 close(kfd);
2943 }
2944
2945 if (ufd >= 0) {
Srikar Dronamrajua23c4dc2012-05-31 17:16:43 +05302946 strlist__delete(unamelist);
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302947 close(ufd);
2948 }
Masami Hiramatsu307a4642015-05-05 11:29:48 +09002949 free(str);
Masami Hiramatsu146a1432010-04-12 13:17:42 -04002950
2951 return ret;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002952}
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302953
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002954/* TODO: don't use a global variable for filter ... */
2955static struct strfilter *available_func_filter;
Masami Hiramatsufa282442009-12-08 17:03:23 -05002956
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002957/*
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002958 * If a symbol corresponds to a function with global binding and
2959 * matches filter return 0. For all others return 1.
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002960 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002961static int filter_available_functions(struct map *map __maybe_unused,
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002962 struct symbol *sym)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002963{
Namhyung Kime578da32015-03-06 16:31:29 +09002964 if (strfilter__compare(available_func_filter, sym->name))
Masami Hiramatsu3c422582011-01-20 23:15:45 +09002965 return 0;
2966 return 1;
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002967}
2968
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002969int show_available_funcs(const char *target, struct strfilter *_filter,
2970 bool user)
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002971{
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002972 struct map *map;
2973 int ret;
2974
2975 ret = init_symbol_maps(user);
2976 if (ret < 0)
2977 return ret;
2978
2979 /* Get a symbol map */
2980 if (user)
2981 map = dso__new_map(target);
2982 else
2983 map = kernel_get_module_map(target);
2984 if (!map) {
2985 pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002986 return -EINVAL;
2987 }
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002988
2989 /* Load symbols with given filter */
2990 available_func_filter = _filter;
2991 if (map__load(map, filter_available_functions)) {
2992 pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
2993 goto end;
2994 }
Masami Hiramatsue80711c2011-01-13 21:46:11 +09002995 if (!dso__sorted_by_name(map->dso, map->type))
2996 dso__sort_by_name(map->dso, map->type);
2997
Masami Hiramatsu2df58632014-02-06 05:32:11 +00002998 /* Show all (filtered) symbols */
Srikar Dronamraju225466f2012-04-16 17:39:09 +05302999 setup_pager();
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003000 dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
3001end:
3002 if (user) {
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03003003 map__put(map);
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003004 }
3005 exit_symbol_maps();
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303006
Masami Hiramatsu2df58632014-02-06 05:32:11 +00003007 return ret;
Srikar Dronamraju225466f2012-04-16 17:39:09 +05303008}
3009