aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorqmuntal <quimmuntal@gmail.com>2026-02-17 14:43:58 +0100
committerQuim Muntal <quimmuntal@gmail.com>2026-02-18 22:04:02 -0800
commita3b5ce2234dfadff260fca8197ab68ea55fd2654 (patch)
tree8e57c6095f88218cf0d662c46bdd6a5ffe7dff5f /src/runtime
parent0b395557234c0013b1795fe5d90ddadd1d6302a3 (diff)
downloadgo-a3b5ce2234dfadff260fca8197ab68ea55fd2654.tar.xz
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Quim Muntal <quimmuntal@gmail.com>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/cgo/gcc_android.c26
-rw-r--r--src/runtime/cgo/gcc_fatalf.c17
2 files changed, 14 insertions, 29 deletions
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 <stdarg.h>
-#include <android/log.h>
#include <pthread.h>
#include <dlfcn.h>
#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 <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
+#ifdef __ANDROID__
+#include <android/log.h>
+#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();
}