diff --git a/cpu/native/Makefile b/cpu/native/Makefile index 1891eebc41..9c2b87b709 100644 --- a/cpu/native/Makefile +++ b/cpu/native/Makefile @@ -22,6 +22,9 @@ endif ifneq (,$(filter can_linux,$(USEMODULE))) DIRS += can endif +ifneq (,$(filter trace,$(USEMODULE))) + DIRS += trace +endif include $(RIOTBASE)/Makefile.base diff --git a/cpu/native/include/trace.h b/cpu/native/include/trace.h new file mode 100644 index 0000000000..a926a3d7ef --- /dev/null +++ b/cpu/native/include/trace.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2017 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup trace Stack traceback (only under native) + * @ingroup core_util + * @brief Address-trace back. + * + * If you call the @ref trace_print() function a stack traceback of all return + * addresses up to @ref TRACE_SIZE will be printed from the point of execution. + * + * @{ + * + * @file + * @brief + * + * @author Martine Lenders + */ +#ifndef TRACE_H +#define TRACE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Maximum number of return addresses to print + */ +#ifndef TRACE_SIZE +#define TRACE_SIZE (4U) +#endif + +/** + * @brief Print the last @ref TRACE_SIZE return addresses from call of this + * function + */ +void trace_print(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TRACE_H */ +/** @} */ diff --git a/cpu/native/trace/Makefile b/cpu/native/trace/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/cpu/native/trace/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/cpu/native/trace/trace.c b/cpu/native/trace/trace.c new file mode 100644 index 0000000000..86a78c7c0b --- /dev/null +++ b/cpu/native/trace/trace.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2017 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * @author Martine Lenders + */ + +#include +#include +#include + +#include "trace.h" + +void trace_print(void) +{ + void *array[TRACE_SIZE + 1]; + size_t size; + + size = backtrace(array, TRACE_SIZE + 1); + + /* skip above line's return address and start with 1 */ + for (size_t i = 1; i < size; i++) { + printf("%p\n", array[i]); + } +} + +/** @} */