Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(native): a crash record lands where the player can actually get it

yhh0106e5606f18
diff --git a/native/host/BootLog.cpp b/native/host/BootLog.cpp
index 1a332bc41..7168e3408 100644
--- a/native/host/BootLog.cpp
+++ b/native/host/BootLog.cpp
@@ -9,6 +9,8 @@
#include <ctime>
#include <string>
+#include <sys/stat.h>
+
#include <csignal>
#include <dlfcn.h>
#include <unistd.h>
@@ -19,6 +21,8 @@ namespace {
FILE* g_file = nullptr;
std::string g_path;
+/// The run before this one — read once, to see whether it ended in a crash.
+std::string g_prev;
const char* g_phase = "(none)";
/// The same file, as a descriptor — what the signal handler is allowed to use.
int g_fd = -1;
@@ -38,6 +42,29 @@ std::string stamp() {
return buf;
}
+/** The same clock as the header, in a form a filename can carry. */
+std::string fileStamp() {
+ std::time_t t = std::time(nullptr);
+ std::tm tm{};
+#if defined(_WIN32)
+ localtime_s(&tm, &t);
+#else
+ localtime_r(&t, &tm);
+#endif
+ char buf[32];
+ std::strftime(buf, sizeof(buf), "%Y%m%d-%H%M%S", &tm);
+ return buf;
+}
+
+/** mkdir -p, for a public directory this app has never written to before. */
+void mkdirs(const std::string& dir) {
+ for (size_t i = 1; i <= dir.size(); i++) {
+ if (i < dir.size() && dir[i] != '/') continue;
+ const std::string part = dir.substr(0, i);
+ if (!part.empty()) mkdir(part.c_str(), 0775);
+ }
+}
+
void writeLine(const char* prefix, const char* text) {
if (!g_file) return;
std::fprintf(g_file, "%s%s\n", prefix, text);
@@ -52,9 +79,9 @@ void openBootLog(const std::string& dir) {
if (dir.empty()) return;
g_path = dir + "/estella-boot.log";
- const std::string prev = dir + "/estella-boot.prev.log";
- std::remove(prev.c_str());
- std::rename(g_path.c_str(), prev.c_str());
+ g_prev = dir + "/estella-boot.prev.log";
+ std::remove(g_prev.c_str());
+ std::rename(g_path.c_str(), g_prev.c_str());
g_file = std::fopen(g_path.c_str(), "w");
if (!g_file) { g_path.clear(); return; }
@@ -99,6 +126,37 @@ void bootLogLine(bool error, const char* message) {
const std::string& bootLogPath() { return g_path; }
+std::string publishPreviousCrash(const std::vector<std::string>& dirs) {
+ if (g_prev.empty() || dirs.empty()) return {};
+
+ // Only a crash is worth publishing. A record that ends in "ready in" is the
+ // last healthy run, and copying that into someone's file manager every launch
+ // is litter.
+ std::string text;
+ {
+ FILE* f = std::fopen(g_prev.c_str(), "rb");
+ if (!f) return {};
+ char buf[4096];
+ size_t got = 0;
+ while ((got = std::fread(buf, 1, sizeof(buf), f)) > 0) text.append(buf, got);
+ std::fclose(f);
+ }
+ if (text.find("FATAL ") == std::string::npos) return {};
+
+ const std::string name = "estella-crash-" + fileStamp() + ".log";
+ for (const std::string& dir : dirs) {
+ mkdirs(dir);
+ const std::string dest = dir + "/" + name;
+ FILE* out = std::fopen(dest.c_str(), "wb");
+ if (!out) continue;
+ const bool wrote = std::fwrite(text.data(), 1, text.size(), out) == text.size();
+ std::fclose(out);
+ if (wrote) return dest;
+ std::remove(dest.c_str());
+ }
+ return {};
+}
+
// =============================================================================
// The crash handler
// =============================================================================
diff --git a/native/host/BootLog.hpp b/native/host/BootLog.hpp
index a14ab25bf..7449e6124 100644
--- a/native/host/BootLog.hpp
+++ b/native/host/BootLog.hpp
@@ -21,6 +21,7 @@
#pragma once
#include <string>
+#include <vector>
namespace eshost {
@@ -80,4 +81,18 @@ const std::string& bootLogPath();
*/
void installCrashHandler();
+/**
+ * If the run before this one ended in a crash, copy its record somewhere the
+ * player can reach, and return where it landed. Empty when the last run was
+ * fine, or when nowhere reachable could be written.
+ *
+ * Done HERE — on the next healthy launch, on the main thread — rather than in
+ * the handler, because copying a file is not something a signal handler may do.
+ * What a player experiences is: it closed itself, they opened it again, and now
+ * there is a file they can send.
+ *
+ * @param dirs Candidate directories, best first (see Platform::publicDirs).
+ */
+std::string publishPreviousCrash(const std::vector<std::string>& dirs);
+
} // namespace eshost
diff --git a/native/host/Host.cpp b/native/host/Host.cpp
index e735f39bd..39d399a40 100644
--- a/native/host/Host.cpp
+++ b/native/host/Host.cpp
@@ -192,6 +192,13 @@ bool boot(Platform& platform) {
openBootLog(platform.logDir());
installCrashHandler();
bootNote("device: %s", platform.describe().c_str());
+ // If the run before this one died, put its record where the player can find
+ // it — the copy is made now, on a healthy launch, because a signal handler
+ // may not copy files.
+ if (const std::string at = publishPreviousCrash(platform.publicDirs()); !at.empty()) {
+ ESHOST_LOGE("the previous run crashed — its record is at %s", at.c_str());
+ bootNote("previous run CRASHED; its record was copied to %s", at.c_str());
+ }
if (!bootLogPath().empty()) ESHOST_LOGI("boot record: %s", bootLogPath().c_str());
const double t0 = nowMs();
diff --git a/native/host/Host.hpp b/native/host/Host.hpp
index 2582d907d..7ae91accb 100644
--- a/native/host/Host.hpp
+++ b/native/host/Host.hpp
@@ -112,6 +112,17 @@ struct Platform {
* about a failure that happens on someone else's phone and not on yours. */
virtual std::string describe() = 0;
+ /**
+ * Directories a PLAYER can browse to, best first — where a crash record is
+ * copied so it can be sent.
+ *
+ * {@link logDir} is not one of them on Android: since Android 11 an app's
+ * `Android/data` is closed to file managers and to the system Files app, so
+ * the record a developer pulls over adb is one a player cannot reach at all.
+ * Empty means this platform has nowhere better, and the copy is skipped.
+ */
+ virtual std::vector<std::string> publicDirs() { return {}; }
+
/** The backend Dawn must pick: Vulkan on Android, Metal on iOS. */
virtual WGPUBackendType backend() const = 0;
diff --git a/native/host/platform/android.cpp b/native/host/platform/android.cpp
index 95f2f0c84..c11a2f831 100644
--- a/native/host/platform/android.cpp
+++ b/native/host/platform/android.cpp
@@ -168,6 +168,32 @@ struct AndroidPlatform final : eshost::Platform {
std::string logDir() override { return logs; }
+ /**
+ * `Android/media/<pkg>/` first, then Downloads.
+ *
+ * An app's own directory under `Android/media` belongs to the shared media
+ * collection rather than to app-private storage: a file manager lists it, and
+ * ordinary file IO can write it with no permission and no MediaStore round
+ * trip. `Android/data` — where the record itself lives — is none of those
+ * things since Android 11. Downloads is the fallback for devices that still
+ * allow a direct write there; both are tried and whichever works is recorded.
+ */
+ std::vector<std::string> publicDirs() override {
+ std::vector<std::string> out;
+ // externalDataPath is <root>/Android/data/<pkg>/files; its sibling under
+ // Android/media is the same package's public corner.
+ const std::string mark = "/Android/data/";
+ const size_t at = logs.find(mark);
+ if (at != std::string::npos) {
+ std::string pkg = logs.substr(at + mark.size());
+ const size_t slash = pkg.find('/');
+ if (slash != std::string::npos) pkg = pkg.substr(0, slash);
+ out.push_back(logs.substr(0, at) + "/Android/media/" + pkg);
+ out.push_back(logs.substr(0, at) + "/Download");
+ }
+ return out;
+ }
+
/** Model, Android release and ABI, from the system properties every device
* answers — no JNI round trip, so this works before anything else is up. */
std::string describe() override {
diff --git a/native/tools/bootlog_crash_test.cpp b/native/tools/bootlog_crash_test.cpp
index bb6a582d0..9f8718b67 100644
--- a/native/tools/bootlog_crash_test.cpp
+++ b/native/tools/bootlog_crash_test.cpp
@@ -32,6 +32,11 @@ int main(int argc, char** argv) {
}
eshost::installCrashHandler();
+ // Second run: the first one crashed, so its record should now be copied
+ // somewhere a player could browse to. Printed for the caller to check.
+ const std::string published = eshost::publishPreviousCrash({std::string(argv[1]) + "/public"});
+ std::printf("PUBLISHED=%s\n", published.c_str());
+
eshost::bootPhase("gpu device");
eshost::bootNote("device: a test, not a phone");
eshost::bootPhase("engine context");
diff --git a/tools/verify-bootlog-crash.mjs b/tools/verify-bootlog-crash.mjs
index a19a65a2a..563111488 100644
--- a/tools/verify-bootlog-crash.mjs
+++ b/tools/verify-bootlog-crash.mjs
@@ -96,12 +96,30 @@ try {
}
const record = sh(`cat ${DEVICE_DIR}/estella-boot.log`);
+// The launch AFTER the crash: the record of the death has moved aside, and its
+// copy should now be somewhere a player could reach.
+console.log('opening it again, the way a player would…');
+let second = '';
+try {
+ second = sh(`${DEVICE_DIR}/t ${DEVICE_DIR} --no-crash`);
+} catch (err) {
+ second = String(err.stdout ?? '');
+}
+const publishedTo = (/PUBLISHED=(\S+)/.exec(second) ?? [])[1] ?? '';
+const publishedText = publishedTo ? sh(`cat ${publishedTo}`) : '';
+
console.log('\n--- the file a player would send ---');
console.log(record.trimEnd());
console.log('------------------------------------\n');
+console.log(`the crash record was published to: ${publishedTo || '(nowhere)'}`);
+
const problems = [];
if (!/FATAL SIGSEGV/.test(record)) problems.push('no FATAL SIGSEGV line');
+if (!publishedTo) problems.push('the crash record was not published anywhere a player could reach');
+if (publishedTo && !/FATAL SIGSEGV/.test(publishedText)) {
+ problems.push('the published copy does not carry the crash');
+}
if (!/during phase: js runtime/.test(record)) problems.push('the phase it died in was not recorded');
if (!/backtrace/.test(record)) problems.push('no backtrace');
if (!/libc|bootlog_crash_test|\+0x/.test(record)) problems.push('no resolved frames in the backtrace');