builtin-kallsyms.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. static int __cmd_kallsyms(int argc, const char **argv)
  20. {
  21. int i, err;
  22. struct perf_env host_env;
  23. struct machine *machine = NULL;
  24. perf_env__init(&host_env);
  25. err = perf_env__set_cmdline(&host_env, argc, argv);
  26. if (err)
  27. goto out;
  28. machine = machine__new_kallsyms(&host_env);
  29. if (machine == NULL) {
  30. pr_err("Couldn't read /proc/kallsyms\n");
  31. err = -1;
  32. goto out;
  33. }
  34. for (i = 0; i < argc; ++i) {
  35. struct map *map;
  36. const struct dso *dso;
  37. struct symbol *symbol = machine__find_kernel_symbol_by_name(machine, argv[i], &map);
  38. if (symbol == NULL) {
  39. printf("%s: not found\n", argv[i]);
  40. continue;
  41. }
  42. dso = map__dso(map);
  43. printf("%s: %s %s %#" PRIx64 "-%#" PRIx64 " (%#" PRIx64 "-%#" PRIx64")\n",
  44. symbol->name, dso__short_name(dso), dso__long_name(dso),
  45. map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end),
  46. symbol->start, symbol->end);
  47. }
  48. out:
  49. machine__delete(machine);
  50. perf_env__exit(&host_env);
  51. return err;
  52. }
  53. int cmd_kallsyms(int argc, const char **argv)
  54. {
  55. const struct option options[] = {
  56. OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
  57. OPT_END()
  58. };
  59. const char * const kallsyms_usage[] = {
  60. "perf kallsyms [<options>] symbol_name",
  61. NULL
  62. };
  63. argc = parse_options(argc, argv, options, kallsyms_usage, 0);
  64. if (argc < 1)
  65. usage_with_options(kallsyms_usage, options);
  66. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  67. if (symbol__init(NULL) < 0)
  68. return -1;
  69. return __cmd_kallsyms(argc, argv);
  70. }