From ccffb95f03f1710ecfa2d1b9a20a28ba94d08290 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Fri, 10 Jan 2020 21:53:30 +0000 Subject: [PATCH] monkey: On assert failure, print a backtrace Signed-off-by: Daniel Silverstone --- frontends/monkey/main.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/frontends/monkey/main.c b/frontends/monkey/main.c index e1c2a382c..a89314440 100644 --- a/frontends/monkey/main.c +++ b/frontends/monkey/main.c @@ -316,6 +316,29 @@ static void monkey_run(void) } } +#ifndef NDEBUG +#include +static void *backtrace_buffer[4096]; + +void +__assert_fail(const char *__assertion, const char *__file, + unsigned int __line, const char *__function) +{ + int frames; + fprintf(stderr, "MONKEY: Assertion failure!\n%s:%d: %s: Assertion `%s` failed.\n", + __file, __line, __function, __assertion); + + frames = backtrace(&backtrace_buffer[0], 4096); + if (frames > 0 && frames < 4096) { + fprintf(stderr, "Backtrace:\n"); + fflush(stderr); + backtrace_symbols_fd(&backtrace_buffer[0], frames, 2); + } + + abort(); +} +#endif + int main(int argc, char **argv) {