From a6b513eb43856b3b2931257535bbe042ccf83def Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Fri, 3 Sep 2021 18:45:21 +0200 Subject: [PATCH] native/backtrace: improve its print capabilitys and test --- cpu/native/backtrace/backtrace.c | 28 ++++++++++++++++++++-- cpu/native/include/backtrace.h | 14 ++++++++++- tests/cpu/native_backtrace/main.c | 3 ++- tests/cpu/native_backtrace/tests/01-run.py | 2 ++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/cpu/native/backtrace/backtrace.c b/cpu/native/backtrace/backtrace.c index bdfe3ede9c3c4..a11125f04df3b 100644 --- a/cpu/native/backtrace/backtrace.c +++ b/cpu/native/backtrace/backtrace.c @@ -16,20 +16,44 @@ #include #include #include +#include #include "backtrace.h" void backtrace_print(void) { void *array[BACKTRACE_SIZE + 1]; - size_t size; + int size; size = backtrace(array, BACKTRACE_SIZE + 1); /* skip above line's return address and start with 1 */ - for (size_t i = 1; i < size; i++) { + for (int i = 1; i < size; i++) { printf("%p\n", array[i]); } } +void backtrace_print_symbols(void) +{ + void *array[BACKTRACE_SIZE + 1]; + int size; + + size = backtrace(array, BACKTRACE_SIZE + 1); + + char ** symbols = backtrace_symbols(array , size); + + /* skip above line's return address and start with 1 */ + for (int i = 1; i < size; i++) { + printf("%s\n", symbols[i]); + } + free(symbols); +} + +int backtrace_len(void) +{ + void *array[BACKTRACE_SIZE + 1]; + + return backtrace(array, BACKTRACE_SIZE + 1) - 1; +} + /** @} */ diff --git a/cpu/native/include/backtrace.h b/cpu/native/include/backtrace.h index db3bfbe1fdfc9..217460708033e 100644 --- a/cpu/native/include/backtrace.h +++ b/cpu/native/include/backtrace.h @@ -36,11 +36,23 @@ extern "C" { #endif /** - * @brief Print the last @ref BACKTRACE_SIZE return addresses from call of this + * @brief Print up to the last @ref BACKTRACE_SIZE return addresses from call of this * function */ void backtrace_print(void); +/** + * @brief Print up to the last @ref BACKTRACE_SIZE symbol_names from call of this + * function + */ +void backtrace_print_symbols(void); + +/** + * @brief get the number of stack frames that are printed by print or print_symbols + * + */ +int backtrace_len(void); + #ifdef __cplusplus } #endif diff --git a/tests/cpu/native_backtrace/main.c b/tests/cpu/native_backtrace/main.c index 889754611a351..59efb4d77b2e5 100644 --- a/tests/cpu/native_backtrace/main.c +++ b/tests/cpu/native_backtrace/main.c @@ -24,7 +24,8 @@ int main(void) { - printf("BACKTRACE_SIZE: %u\n", BACKTRACE_SIZE); + printf("BACKTRACE_SIZE: %d\n", backtrace_len()); backtrace_print(); + backtrace_print_symbols(); return 0; } diff --git a/tests/cpu/native_backtrace/tests/01-run.py b/tests/cpu/native_backtrace/tests/01-run.py index 8b2dd2c44a4f1..a7d9b3150c460 100755 --- a/tests/cpu/native_backtrace/tests/01-run.py +++ b/tests/cpu/native_backtrace/tests/01-run.py @@ -15,6 +15,8 @@ def testfunc(child): trace_size = int(child.match.group(1)) for i in range(trace_size): child.expect(r"0x[0-9a-f]+") + for i in range(trace_size): + child.expect(r".*") print("All tests successful")