1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * builtin-kallsyms.c
4  *
5  * Builtin command: Look for a symbol in the running kernel and its modules
6  *
7  * Copyright (C) 2017, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
8  */
9 #include <inttypes.h>
10 #include "builtin.h"
11 #include <linux/compiler.h>
12 #include <subcmd/parse-options.h>
13 #include "debug.h"
14 #include "dso.h"
15 #include "env.h"
16 #include "machine.h"
17 #include "map.h"
18 #include "symbol.h"
19 
__cmd_kallsyms(int argc,const char ** argv)20 static int __cmd_kallsyms(int argc, const char **argv)
21 {
22 	int i, err;
23 	struct perf_env host_env;
24 	struct machine *machine = NULL;
25 
26 
27 	perf_env__init(&host_env);
28 	err = perf_env__set_cmdline(&host_env, argc, argv);
29 	if (err)
30 		goto out;
31 
32 	machine = machine__new_kallsyms(&host_env);
33 	if (machine == NULL) {
34 		pr_err("Couldn't read /proc/kallsyms\n");
35 		err = -1;
36 		goto out;
37 	}
38 
39 	for (i = 0; i < argc; ++i) {
40 		struct map *map;
41 		const struct dso *dso;
42 		struct symbol *symbol = machine__find_kernel_symbol_by_name(machine, argv[i], &map);
43 
44 		if (symbol == NULL) {
45 			printf("%s: not found\n", argv[i]);
46 			continue;
47 		}
48 
49 		dso = map__dso(map);
50 		printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n",
51 			symbol->name, dso__short_name(dso), dso__long_name(dso),
52 			map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end),
53 			symbol->start, symbol->end);
54 	}
55 out:
56 	machine__delete(machine);
57 	perf_env__exit(&host_env);
58 	return err;
59 }
60 
cmd_kallsyms(int argc,const char ** argv)61 int cmd_kallsyms(int argc, const char **argv)
62 {
63 	const struct option options[] = {
64 	OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
65 	OPT_END()
66 	};
67 	const char * const kallsyms_usage[] = {
68 		"perf kallsyms [<options>] symbol_name",
69 		NULL
70 	};
71 
72 	argc = parse_options(argc, argv, options, kallsyms_usage, 0);
73 	if (argc < 1)
74 		usage_with_options(kallsyms_usage, options);
75 
76 	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
77 	if (symbol__init(NULL) < 0)
78 		return -1;
79 
80 	return __cmd_kallsyms(argc, argv);
81 }
82