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