From 2b4b2018475ae9dad9eb467a2cf9e19b99a54acf Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 22 Jan 2019 22:02:59 -0500 Subject: [PATCH] libroot: Call abort() only if a signal handler is installed. Preserving the assert failure message in debug reports is desirable, so if possible we should do so, not just print it to stderr. So now we reuse the same trick from abort() directly. Sorry for the extra noise; I should have combined these commits. --- src/system/libroot/posix/Jamfile | 2 +- .../libroot/posix/{assert.c => assert.cpp} | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) rename src/system/libroot/posix/{assert.c => assert.cpp} (55%) diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile index 2079067016..6aa077ab8a 100644 --- a/src/system/libroot/posix/Jamfile +++ b/src/system/libroot/posix/Jamfile @@ -18,7 +18,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { PWD_BACKEND = pwd.cpp grp.cpp shadow.cpp user_group_common.cpp ; } MergeObject <$(architecture)>posix_main.o : - assert.c + assert.cpp dlfcn.c dirent.c errno.c diff --git a/src/system/libroot/posix/assert.c b/src/system/libroot/posix/assert.cpp similarity index 55% rename from src/system/libroot/posix/assert.c rename to src/system/libroot/posix/assert.cpp index c840b54bdd..851ca17bf7 100644 --- a/src/system/libroot/posix/assert.c +++ b/src/system/libroot/posix/assert.cpp @@ -1,5 +1,6 @@ /* * Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2019, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. */ @@ -10,6 +11,7 @@ #include #include +#include #include #include @@ -18,11 +20,19 @@ extern char* __progname; void -__assert_fail(const char *assertion, const char* file, unsigned int line, +__assert_fail(const char* assertion, const char* file, unsigned int line, const char* function) { fprintf(stderr, "%s: %s:%d:%s: %s\n", __progname, file, line, function, assertion); + + // If there's no handler installed for SIGABRT, call debugger(). + struct sigaction signalAction; + if (sigaction(SIGABRT, NULL, &signalAction) == 0 + && signalAction.sa_handler == SIG_DFL) { + debugger(assertion); + } + abort(); } @@ -31,7 +41,5 @@ void __assert_perror_fail(int error, const char* file, unsigned int line, const char* function) { - fprintf(stderr, "%s: %s:%d:%s: %s\n", __progname, file, line, function, - strerror(error)); - abort(); + __assert_fail(strerror(error), file, line, function); }