1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

native/backtrace: improve print capabilitys and test

This commit is contained in:
Karl Fessel 2021-09-03 18:45:21 +02:00 committed by Karl Fessel
parent 9f51daeb53
commit 37627ce2b8
4 changed files with 47 additions and 4 deletions

View File

@ -16,20 +16,44 @@
#include <execinfo.h> #include <execinfo.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "backtrace.h" #include "backtrace.h"
void backtrace_print(void) void backtrace_print(void)
{ {
void *array[BACKTRACE_SIZE + 1]; void *array[BACKTRACE_SIZE + 1];
size_t size; int size;
size = backtrace(array, BACKTRACE_SIZE + 1); size = backtrace(array, BACKTRACE_SIZE + 1);
/* skip above line's return address and start with 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]); 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;
}
/** @} */ /** @} */

View File

@ -36,11 +36,23 @@ extern "C" {
#endif #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 * function
*/ */
void backtrace_print(void); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -24,7 +24,10 @@
int main(void) 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(); backtrace_print();
printf("\n## backtrace_print_symbols: print symbol information\n");
backtrace_print_symbols();
return 0; return 0;
} }

View File

@ -13,8 +13,12 @@ from testrunner import run
def testfunc(child): def testfunc(child):
child.expect(r"BACKTRACE_SIZE: (\d+)\r\n") child.expect(r"BACKTRACE_SIZE: (\d+)\r\n")
trace_size = int(child.match.group(1)) trace_size = int(child.match.group(1))
child.expect("backtrace_print:")
for i in range(trace_size): for i in range(trace_size):
child.expect(r"0x[0-9a-f]+") 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") print("All tests successful")