Skip to content

Commit

Permalink
native/backtrace: improve its print capabilitys and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kfessel committed Mar 26, 2024
1 parent 9f51dae commit a6b513e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
28 changes: 26 additions & 2 deletions cpu/native/backtrace/backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,44 @@
#include <execinfo.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#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);

Check warning on line 43 in cpu/native/backtrace/backtrace.c

View workflow job for this annotation

GitHub Actions / static-tests

comma should not be preceded by whitespace

/* 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;
}

/** @} */
14 changes: 13 additions & 1 deletion cpu/native/include/backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/cpu/native_backtrace/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions tests/cpu/native_backtrace/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit a6b513e

Please sign in to comment.