0030-libunwind-Added-unw_backtrace-method.patch 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. From 34e4284dc6f57ecc69e29538bc244df5c803c7ec Mon Sep 17 00:00:00 2001
  2. From: Jun Yuan Tan <junyuan.tan@starfivetech.com>
  3. Date: Fri, 22 Oct 2021 12:11:57 +0800
  4. Subject: [PATCH] libunwind: Added unw_backtrace method
  5. Source: https://github.com/ClickHouse-Extras/libunwind/commit/52f0f7861926cbfaef7e6c97d8a6d7ba2a1f6747#diff-a82fc885e2e4facf4b92d26171c13aa4aa5db296f77e1158ba2f8664e3bd1f5c
  6. Upstream-Status: Pending
  7. Rebased to LLVM 14.0.0 by Jun Yuan Tan
  8. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  9. Signed-off-by: Jun Yuan Tan <junyuan.tan@starfivetech.com>
  10. ---
  11. libunwind/include/libunwind.h | 1 +
  12. libunwind/src/libunwind.cpp | 18 ++++++++++++++++++
  13. 2 files changed, 19 insertions(+)
  14. diff --git a/libunwind/include/libunwind.h b/libunwind/include/libunwind.h
  15. index 5ba63e5b7e5b..c49172f87362 100644
  16. --- a/libunwind/include/libunwind.h
  17. +++ b/libunwind/include/libunwind.h
  18. @@ -127,6 +127,7 @@ extern int unw_is_fpreg(unw_cursor_t *, unw_regnum_t) LIBUNWIND_AVAIL;
  19. extern int unw_is_signal_frame(unw_cursor_t *) LIBUNWIND_AVAIL;
  20. extern int unw_get_proc_name(unw_cursor_t *, char *, size_t, unw_word_t *) LIBUNWIND_AVAIL;
  21. //extern int unw_get_save_loc(unw_cursor_t*, int, unw_save_loc_t*);
  22. +extern int unw_backtrace(void **, int) LIBUNWIND_AVAIL;
  23. extern unw_addr_space_t unw_local_addr_space;
  24. diff --git a/libunwind/src/libunwind.cpp b/libunwind/src/libunwind.cpp
  25. index 93e1bc131f0c..7ae1a10092ab 100644
  26. --- a/libunwind/src/libunwind.cpp
  27. +++ b/libunwind/src/libunwind.cpp
  28. @@ -295,7 +295,25 @@ void __unw_remove_dynamic_fde(unw_word_t fde) {
  29. #endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
  30. #endif // !defined(__USING_SJLJ_EXCEPTIONS__)
  31. +int unw_backtrace(void **buffer, int size) {
  32. + unw_context_t context;
  33. + unw_cursor_t cursor;
  34. + if (unw_getcontext(&context) || unw_init_local(&cursor, &context)) {
  35. + return 0;
  36. + }
  37. +
  38. + unw_word_t ip;
  39. + int current = 0;
  40. + while (unw_step(&cursor) > 0) {
  41. + if (current >= size || unw_get_reg(&cursor, UNW_REG_IP, &ip)) {
  42. + break;
  43. + }
  44. + buffer[current++] = reinterpret_cast<void *>(static_cast<uintptr_t>(ip));
  45. + }
  46. +
  47. + return current;
  48. +}
  49. // Add logging hooks in Debug builds only
  50. #ifndef NDEBUG
  51. --
  52. 2.33.0