summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2021-12-29 14:46:37 +0100
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2021-12-29 14:46:37 +0100
commitac29bb14011d77fc6042a8455179231450a79b9c (patch)
tree372fee9a83493f17eb08c419bbf49a4e173a62a8
parent4fbbf561a41be515a96b34afce1fd40fec9d3182 (diff)
libshouldbeinlibc: Add backtrace_stderr and backtrace_mach
as convenience for printing a backtrace without erroring out.
-rw-r--r--libshouldbeinlibc/assert-backtrace.c37
-rw-r--r--libshouldbeinlibc/assert-backtrace.h6
2 files changed, 41 insertions, 2 deletions
diff --git a/libshouldbeinlibc/assert-backtrace.c b/libshouldbeinlibc/assert-backtrace.c
index 66ce47c1..7c6fac51 100644
--- a/libshouldbeinlibc/assert-backtrace.c
+++ b/libshouldbeinlibc/assert-backtrace.c
@@ -26,9 +26,13 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <mach/mach_traps.h>
#include "assert-backtrace.h"
+static const size_t size = 128;
+static const size_t skip = 2;
+
static void __attribute__ ((noreturn))
__assert_fail_base_backtrace (const char *fmt,
const char *assertion,
@@ -36,8 +40,6 @@ __assert_fail_base_backtrace (const char *fmt,
unsigned int line,
const char *function)
{
- const size_t size = 128;
- const size_t skip = 2;
int nptrs;
void *buffer[size];
@@ -79,4 +81,35 @@ __assert_perror_fail_backtrace (int errnum,
}
+void
+backtrace_stderr (void)
+{
+ int nptrs;
+ void *buffer[size];
+
+ nptrs = backtrace (buffer, size);
+ backtrace_symbols_fd (buffer, nptrs, STDERR_FILENO);
+ fflush (stderr);
+}
+
+void
+backtrace_mach (void)
+{
+ int nptrs;
+ void *buffer[size];
+ char **symbols;
+ int i;
+
+ nptrs = backtrace (buffer, size);
+ symbols = backtrace_symbols (buffer, nptrs);
+
+ for (i = 0; i < nptrs; i++)
+ {
+ mach_print(symbols[i]);
+ mach_print("\n");
+ }
+
+ free(symbols);
+}
+
#endif /* ! defined NDEBUG */
diff --git a/libshouldbeinlibc/assert-backtrace.h b/libshouldbeinlibc/assert-backtrace.h
index c1e51594..84a9e7c1 100644
--- a/libshouldbeinlibc/assert-backtrace.h
+++ b/libshouldbeinlibc/assert-backtrace.h
@@ -58,5 +58,11 @@ void __assert_perror_fail_backtrace (int errnum,
__FILE__, __LINE__, \
__PRETTY_FUNCTION__))
+/* Print a stack trace on stderr. */
+void backtrace_stderr (void);
+
+/* Print a stack trace on the mach console. */
+void backtrace_mach (void);
+
#endif /* NDEBUG */
#endif /* __ASSERT_BACKTRACE__ */