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

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