unwind-libunwind.c 2.1 KB

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