From 4fdee8080912fe6dd76e98526457125e9ee8ec03 Mon Sep 17 00:00:00 2001 From: Gregory LEOCADIE Date: Wed, 21 Sep 2022 21:25:31 +0200 Subject: Add unw_backtrace2 implementation --- include/libunwind-common.h.in | 1 + src/mi/backtrace.c | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/libunwind-common.h.in b/include/libunwind-common.h.in index 9d62aa91..3f04a1c4 100644 --- a/include/libunwind-common.h.in +++ b/include/libunwind-common.h.in @@ -316,5 +316,6 @@ extern int unw_get_proc_name_by_ip (unw_addr_space_t, unw_word_t, char *, size_t, unw_word_t *, void *); extern const char *unw_strerror (int); extern int unw_backtrace (void **, int); +extern int unw_backtrace2 (void **, int, unw_context_t*); extern unw_addr_space_t unw_local_addr_space; diff --git a/src/mi/backtrace.c b/src/mi/backtrace.c index b2815137..b6c7457f 100644 --- a/src/mi/backtrace.c +++ b/src/mi/backtrace.c @@ -33,15 +33,16 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* See glibc manual for a description of this function. */ static ALWAYS_INLINE int -slow_backtrace (void **buffer, int size, unw_context_t *uc) +slow_backtrace (void **buffer, int size, unw_context_t *uc, int flag) { unw_cursor_t cursor; unw_word_t ip; int n = 0; - if (unlikely (unw_init_local (&cursor, uc) < 0)) + if (unlikely (unw_init_local2 (&cursor, uc, flag) < 0)) return 0; + while (unw_step (&cursor) > 0) { if (n >= size) @@ -69,7 +70,33 @@ unw_backtrace (void **buffer, int size) if (unlikely (tdep_trace (&cursor, buffer, &n) < 0)) { unw_getcontext (&uc); - return slow_backtrace (buffer, size, &uc); + return slow_backtrace (buffer, size, &uc, 0); + } + + return n; +} + +int +unw_backtrace2 (void **buffer, int size, unw_context_t* uc2) +{ + if (size == 0) + return 0; + + if (uc2 == NULL) + return unw_backtrace(buffer, size); + + unw_cursor_t cursor; + // need to copy, because the context will be modified by tdep_trace + unw_context_t uc = *(unw_context_t*)uc2; + + if (unlikely (unw_init_local2 (&cursor, &uc, UNW_INIT_SIGNAL_FRAME) < 0)) + return 0; + + int n = size; + + if (unlikely (tdep_trace (&cursor, buffer, &n) < 0)) + { + return slow_backtrace (buffer, n, &uc, UNW_INIT_SIGNAL_FRAME); } return n; -- cgit v1.2.1