From a3b5ce2234dfadff260fca8197ab68ea55fd2654 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Tue, 17 Feb 2026 14:43:58 +0100 Subject: runtime/cgo: deduplicate Android's fatalf Most of the fatalf code is shared between Android and other Unix-like platforms, but Android has some special handling for the case where the fatalf is called from an .apk file. Put the Android-specific code in the generic fatalf code, and implement the Android-specific handling using a #ifdef guard. Change-Id: Ic7a49792648260c83b213380b77c0c352e9486f0 Reviewed-on: https://go-review.googlesource.com/c/go/+/745604 LUCI-TryBot-Result: Go LUCI Reviewed-by: Cherry Mui Reviewed-by: Michael Pratt Auto-Submit: Quim Muntal --- src/runtime/cgo/gcc_android.c | 26 -------------------------- src/runtime/cgo/gcc_fatalf.c | 17 ++++++++++++++--- 2 files changed, 14 insertions(+), 29 deletions(-) (limited to 'src/runtime') diff --git a/src/runtime/cgo/gcc_android.c b/src/runtime/cgo/gcc_android.c index 7ea213599d..1373feb47a 100644 --- a/src/runtime/cgo/gcc_android.c +++ b/src/runtime/cgo/gcc_android.c @@ -2,36 +2,10 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -#include -#include #include #include #include "libcgo.h" -void -fatalf(const char* format, ...) -{ - va_list ap; - - // Write to both stderr and logcat. - // - // When running from an .apk, /dev/stderr and /dev/stdout - // redirect to /dev/null. And when running a test binary - // via adb shell, it's easy to miss logcat. - - fprintf(stderr, "runtime/cgo: "); - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - fprintf(stderr, "\n"); - - va_start(ap, format); - __android_log_vprint(ANDROID_LOG_FATAL, "runtime/cgo", format, ap); - va_end(ap); - - abort(); -} - // Truncated to a different magic value on 32-bit; that's ok. #define magic1 (0x23581321345589ULL) diff --git a/src/runtime/cgo/gcc_fatalf.c b/src/runtime/cgo/gcc_fatalf.c index 8d46e9187d..2754414fc1 100644 --- a/src/runtime/cgo/gcc_fatalf.c +++ b/src/runtime/cgo/gcc_fatalf.c @@ -2,11 +2,12 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build unix && !android +//go:build unix #include -#include -#include +#ifdef __ANDROID__ +#include +#endif #include "libcgo.h" void @@ -19,5 +20,15 @@ fatalf(const char* format, ...) vfprintf(stderr, format, ap); va_end(ap); fprintf(stderr, "\n"); + +#ifdef __ANDROID__ + // When running from an Android .apk, /dev/stderr and /dev/stdout + // redirect to /dev/null. And when running a test binary + // via adb shell, it's easy to miss logcat. So write to both. + va_start(ap, format); + __android_log_vprint(ANDROID_LOG_FATAL, "runtime/cgo", format, ap); + va_end(ap); +#endif + abort(); } -- cgit v1.3