pw_assert_basic: abort() on assert failure
This makes pw_assert_basic trigger C's standard abort() function by
default, unless PW_ASSERT_BASIC_ABORT is defined as 0.
Below is an example session in pw watch with the abort functionality:
▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌
▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌
▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌
▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
Watching for changes. Ctrl-C to exit; enter to rebuild
20200415 21:17:32 INF Change detected: None
20200415 21:17:32 INF Starting build with 1 directories
20200415 21:17:32 INF [1/1] Starting build: out/host
ninja: Entering directory `out/host'
[1/5] ACTION //pw_assert:assert_backend_compile_test_run(//pw_toolchain:host_clang_og)
FAILED: gen/pw_assert/assert_backend_compile_test_run.pw_pystamp
python ../../pw_build/py/python_runner.py --gn-root ../../ --out-dir //out/host --touch //out/host/gen/pw_assert/assert_backend_compile_test_run.pw_pystamp --capture-output -- //pw_unit_test/py/pw_unit_test/test_runner.py --runner //targets/host/run_test --test //out/host/obj/pw_assert:assert_backend_compile_test
INF Test 1/1: [ RUN] assert_backend_compile_test
ERR /Users/keir/wrk/pigweed/targets/host/run_test exited with status 134
OUT [15726]
INF [==========] Running all tests.
INF [ RUN ] Crash.WithAndWithoutMessageArguments
INF [ OK ] Crash.WithAndWithoutMessageArguments
INF [ RUN ] Check.NoMessage
INF [ OK ] Check.NoMessage
INF [ RUN ] Check.WithMessageAndArgs
INF [ OK ] Check.WithMessageAndArgs
INF [ RUN ] Check.IntComparison
▄████▄ ██▀███ ▄▄▄ ██████ ██░ ██
▒██▀ ▀█ ▓██ ▒ ██▒ ▒████▄ ▒██ ▒ ▓██░ ██▒
▒▓█ 💥 ▄ ▓██ ░▄█ ▒ ▒██ ▀█▄ ░ ▓██▄ ▒██▀▀██░
▒▓▓▄ ▄██▒ ▒██▀▀█▄ ░██▄▄▄▄██ ▒ ██▒ ░▓█ ░██
▒ ▓███▀ ░ ░██▓ ▒██▒ ▓█ ▓██▒ ▒██████▒▒ ░▓█▒░██▓
░ ░▒ ▒ ░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░ ▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒
░ ▒ ░▒ ░ ▒░ ▒ ▒▒ ░ ░ ░▒ ░ ░ ▒ ░▒░ ░
░ ░░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░
░
Welp, that didn't go as planned. It seems we crashed. Terribly sorry!
CRASH MESSAGE
Check failed: x_int (=50) >= y_int (=66). Oh dear. In state: TEST
CRASH FILE & LINE
../../pw_assert/assert_backend_compile_test.cc:92
CRASH FUNCTION
PigweedTestBody
/Users/keir/wrk/pigweed/targets/host/run_test: line 18: 15727 Abort trap: 6 "$@"
INF Test 1/1: [FAIL] assert_backend_compile_test
ninja: build stopped: subcommand failed.
20200415 21:17:32 ERR [1/1] Finished build: out/host (FAIL)
20200415 21:17:32 INF Finished; some builds failed
.------------------------------------
|
| FAIL out/host
|
'------------------------------------
▄██████▒░▄▄▄ ██▓ ░██▓
▓█▓ ░▒████▄ ▓██▒ ░▓██▒
▒████▒ ░▒█▀ ▀█▄ ▒██▒ ▒██░
░▓█▒ ░░██▄▄▄▄██ ░██░ ▒██░
░▒█░ ▓█ ▓██▒░██░░ ████████▒
▒█░ ▒▒ ▓▒█░░▓ ░ ▒░▓ ░
░▒ ▒ ▒▒ ░ ▒ ░░ ░ ▒ ░
░ ░ ░ ▒ ▒ ░ ░ ░
░ ░ ░ ░ ░
Change-Id: Iaf69455c5f108b2d4d9ae1b8dc4cd6bb5902fbed
diff --git a/pw_assert_basic/assert_basic.cc b/pw_assert_basic/assert_basic.cc
index 3046ba6..837ce52 100644
--- a/pw_assert_basic/assert_basic.cc
+++ b/pw_assert_basic/assert_basic.cc
@@ -25,6 +25,11 @@
#include "pw_string/string_builder.h"
#include "pw_sys_io/sys_io.h"
+// If 1, call C's standard abort() function on assert failure.
+#ifndef PW_ASSERT_BASIC_ABORT
+#define PW_ASSERT_BASIC_ABORT 1
+#endif // PW_ASSERT_BASIC_ABORT
+
// TODO(pwbug/17): Expose these through the config system.
#define PW_ASSERT_BASIC_SHOW_BANNER 1
#define PW_ASSERT_BASIC_USE_COLORS 1
@@ -129,11 +134,15 @@
// device. At some point we'll have a reboot BSP function or similar, but for
// now this is acceptable since no one is using this basic backend.
if (!PW_ASSERT_BASIC_DISABLE_NORETURN) {
- WriteLine(MAGENTA " HANG TIME" RESET);
- WriteLine("");
- WriteLine(
- " ... until a debugger joins. System is waiting in a while(1)");
- while (1) {
+ if (PW_ASSERT_BASIC_ABORT) {
+ abort();
+ } else {
+ WriteLine(MAGENTA " HANG TIME" RESET);
+ WriteLine("");
+ WriteLine(
+ " ... until a debugger joins. System is waiting in a while(1)");
+ while (1) {
+ }
}
PW_UNREACHABLE;
} else {