unwind-libunwind.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "unwind.h"
  3. #include "dso.h"
  4. #include "map.h"
  5. #include "thread.h"
  6. #include "session.h"
  7. #include "debug.h"
  8. #include "env.h"
  9. #include "callchain.h"
  10. struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops;
  11. struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops;
  12. struct unwind_libunwind_ops __weak *arm64_unwind_libunwind_ops;
  13. static void unwind__register_ops(struct maps *maps, struct unwind_libunwind_ops *ops)
  14. {
  15. maps->unwind_libunwind_ops = ops;
  16. }
  17. int unwind__prepare_access(struct maps *maps, struct map *map, bool *initialized)
  18. {
  19. const char *arch;
  20. enum dso_type dso_type;
  21. struct unwind_libunwind_ops *ops = local_unwind_libunwind_ops;
  22. int err;
  23. if (!dwarf_callchain_users)
  24. return 0;
  25. if (maps->addr_space) {
  26. pr_debug("unwind: thread map already set, dso=%s\n",
  27. map->dso->name);
  28. if (initialized)
  29. *initialized = true;
  30. return 0;
  31. }
  32. /* env->arch is NULL for live-mode (i.e. perf top) */
  33. if (!maps->machine->env || !maps->machine->env->arch)
  34. goto out_register;
  35. dso_type = dso__type(map->dso, maps->machine);
  36. if (dso_type == DSO__TYPE_UNKNOWN)
  37. return 0;
  38. arch = perf_env__arch(maps->machine->env);
  39. if (!strcmp(arch, "x86")) {
  40. if (dso_type != DSO__TYPE_64BIT)
  41. ops = x86_32_unwind_libunwind_ops;
  42. } else if (!strcmp(arch, "arm64") || !strcmp(arch, "arm")) {
  43. if (dso_type == DSO__TYPE_64BIT)
  44. ops = arm64_unwind_libunwind_ops;
  45. }
  46. if (!ops) {
  47. pr_err("unwind: target platform=%s is not supported\n", arch);
  48. return 0;
  49. }
  50. out_register:
  51. unwind__register_ops(maps, ops);
  52. err = maps->unwind_libunwind_ops->prepare_access(maps);
  53. if (initialized)
  54. *initialized = err ? false : true;
  55. return err;
  56. }
  57. void unwind__flush_access(struct maps *maps)
  58. {
  59. if (maps->unwind_libunwind_ops)
  60. maps->unwind_libunwind_ops->flush_access(maps);
  61. }
  62. void unwind__finish_access(struct maps *maps)
  63. {
  64. if (maps->unwind_libunwind_ops)
  65. maps->unwind_libunwind_ops->finish_access(maps);
  66. }
  67. int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  68. struct thread *thread,
  69. struct perf_sample *data, int max_stack)
  70. {
  71. if (thread->maps->unwind_libunwind_ops)
  72. return thread->maps->unwind_libunwind_ops->get_entries(cb, arg, thread, data, max_stack);
  73. return 0;
  74. }