unwind-libunwind.c 2.2 KB

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