From 37627ce2b80ecefa1517fe957ba7328eb21aac56 Mon Sep 17 00:00:00 2001 From: Karl Fessel Date: Fri, 3 Sep 2021 18:45:21 +0200 Subject: [PATCH] native/backtrace: improve print capabilitys and test --- cpu/native/backtrace/backtrace.c | 28 ++++++++++++++++++++-- cpu/native/include/backtrace.h | 14 ++++++++++- tests/cpu/native_backtrace/main.c | 5 +++- tests/cpu/native_backtrace/tests/01-run.py | 4 ++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cpu/native/backtrace/backtrace.c b/cpu/native/backtrace/backtrace.c index bdfe3ede9c..f17157f1cf 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 db3bfbe1fd..2174607080 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 889754611a..af4411ce38 100644 --- a/tests/cpu/native_backtrace/main.c +++ b/tests/cpu/native_backtrace/main.c @@ -24,7 +24,10 @@ int main(void) { - printf("BACKTRACE_SIZE: %u\n", BACKTRACE_SIZE); + printf("BACKTRACE_SIZE: %d\n", backtrace_len()); + printf("\n## backtrace_print: print addresses\n"); backtrace_print(); + printf("\n## backtrace_print_symbols: print symbol information\n"); + 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 8b2dd2c44a..c8cd2061aa 100755 --- a/tests/cpu/native_backtrace/tests/01-run.py +++ b/tests/cpu/native_backtrace/tests/01-run.py @@ -13,8 +13,12 @@ from testrunner import run def testfunc(child): child.expect(r"BACKTRACE_SIZE: (\d+)\r\n") trace_size = int(child.match.group(1)) + child.expect("backtrace_print:") for i in range(trace_size): child.expect(r"0x[0-9a-f]+") + child.expect("backtrace_print_symbols:") + for i in range(trace_size): + child.expect(r".*") print("All tests successful")