Merge the log file: a game run started without a redirected stderr writes logs\totk.log next to totk.exe, keeps the four runs before it and stops a file at 64 MiB

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-29 12:47:27 +03:00
commit 0dbfffe7df
10 files changed
+146 -32

No files matched your search

+6 -6
View File
@@ -78,7 +78,7 @@ static void cb_module_inits(Cpu *c) {
rt_log("boot: ENTER %s DT_INIT (%llu init_array entries)", m->name, (unsigned long long)entries);
rt_call(c, m->init, 0);
uint32_t state = ctor_state(m);
printf("boot: DT_INIT %s state=%u (%llu)\n", m->name, state, (unsigned long long)entries);
rt_logf("boot: DT_INIT %s state=%u (%llu)\n", m->name, state, (unsigned long long)entries);
if (state != CTOR_DONE) rt_fatal("boot: %s DT_INIT left its constructor flag at %u, not 2", m->name, state);
if (entries != expected_inits[k].entries)
rt_fatal("boot: %s has %llu init_array entries, this game version has %llu", m->name, (unsigned long long)entries,
@@ -95,7 +95,7 @@ static void cb_module_finis(Cpu *c) {
// ---- checkpoints (host.h)
static void stats_line(void) {
RtStats s = rt_stats();
printf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs,
rt_logf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs,
(unsigned long long)s.threads, (unsigned long long)s.host_calls);
}
@@ -114,7 +114,7 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
rt_fatal("boot: checkpoint %s: %s constructor flag reads %u", detail, ldr_module(i)->name,
ctor_state(ldr_module(i)));
InterlockedExchange(&checkpoint_seen, 1);
printf("boot: checkpoint %s from main+%#llx\n", detail, (unsigned long long)(ret - main_mod->base));
rt_logf("boot: checkpoint %s from main+%#llx\n", detail, (unsigned long long)(ret - main_mod->base));
return;
}
if (!strcmp(what, "nvn")) {
@@ -123,7 +123,7 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
(unsigned long long)NVN_RET);
if (!InterlockedCompareExchange(&checkpoint_seen, 0, 0))
rt_fatal("boot: first NVN call before the SetFocusHandlingMode checkpoint");
printf("boot: first NVN call nvnBootstrapLoader(\"%s\") from main+%#llx\n", detail,
rt_logf("boot: first NVN call nvnBootstrapLoader(\"%s\") from main+%#llx\n", detail,
(unsigned long long)(ret - main_mod->base));
if (rt_config()->seconds) return; // --seconds: the run goes on into the world
if (rt_trace & RT_TRACE_HOSTCALL) hostfn_report();
@@ -219,7 +219,7 @@ static void save_shot(const tex_t *t, const char *path) {
}
rt_write_png(path, px, w, h);
free(px);
printf("boot: shot %s (%ux%u)\n", path, w, h);
rt_logf("boot: shot %s (%ux%u)\n", path, w, h);
}
// TOTK_SHOTS=lo-hi:step [TOTK_SHOTS_DIR=dir]: every step-th present in [lo, hi] saved as <dir>/f<n>.png (n the
@@ -291,7 +291,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) {
if (w < 0 || (double)w < cfg->seconds * 1e6) return;
if (cfg->shot) save_shot(t, cfg->shot);
GfxStats g = gfx_stats();
printf("boot: frames %llu presented (nvn calls %llu, distinct %llu, unimplemented %llu, validation errors %llu, "
rt_logf("boot: frames %llu presented (nvn calls %llu, distinct %llu, unimplemented %llu, validation errors %llu, "
"shader failures %llu, draws skipped %llu)\n", (unsigned long long)g.frames,
(unsigned long long)g.nvn_calls, (unsigned long long)g.nvn_distinct, (unsigned long long)g.unimplemented,
(unsigned long long)g.validation_errors, (unsigned long long)g.shader_failures,
+3 -3
View File
@@ -47,9 +47,9 @@ static uint64_t g_nhost;
void rt_fatal(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fputs("FATAL: ", stderr);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
rt_log_raw("FATAL: ", 7);
rt_vlogf(fmt, ap);
rt_log_raw("\n", 1);
va_end(ap);
fflush(stdout);
fflush(stderr);
+7 -1
View File
@@ -16,7 +16,13 @@
#include "dispatch.h"
// ---- logging and results
void rt_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); // "[host] <msg>\n" to stderr, one write
void rt_log(const char *fmt, ...) __attribute__((format(printf, 1, 2))); // "[host] <msg>\n" to the log, one write
// The log is stderr plus, in a game run whose stderr is not a file, logs\totk.log next to totk.exe (main.c log_open).
// rt_logf and rt_log_raw write to both without the prefix and the newline: crash reports, fatal errors, guest output.
void rt_logf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
void rt_vlogf(const char *fmt, va_list ap);
void rt_log_raw(const char *p, size_t n);
const char *rt_log_path(void); // the log file, NULL when this run writes none
enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32,
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 };
extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all)
+6 -6
View File
@@ -75,11 +75,11 @@ void ipc_backtrace(void) {
const Cpu *c = t_req;
if (!c) return;
char d[160];
fprintf(stderr, " ipc request from %s\n", ldr_describe(c->x[30], d, sizeof d));
rt_logf(" ipc request from %s\n", ldr_describe(c->x[30], d, sizeof d));
uint64_t fp = c->x[29];
for (int n = 0; n < 24 && fp && !(fp & 7) && readable(fp, 16); n++) {
uint64_t next = LD64(fp), lr = LD64(fp + 8);
fprintf(stderr, " guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
rt_logf(" guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
if (next <= fp || next - fp > (64u << 20)) break;
fp = next;
}
@@ -109,7 +109,7 @@ void ipc_out(IpcResponse *rs, const void *p, uint32_t n) {
void ipc_unimplemented(const IpcSession *s, const IpcRequest *rq) {
fflush(stdout);
fprintf(stderr, "ipc: %s command %u (type %u, data %u bytes, x%u a%u b%u w%u c%u, copy %u move %u)\n", s->name,
rt_logf("ipc: %s command %u (type %u, data %u bytes, x%u a%u b%u w%u c%u, copy %u move %u)\n", s->name,
rq->cmd, rq->type, rq->data_size, rq->nx, rq->na, rq->nb, rq->nw, rq->nc, rq->ncopy, rq->nmove);
ipc_backtrace();
rt_fatal("ipc: %s command %u is not implemented", s->name, rq->cmd);
@@ -235,9 +235,9 @@ static void dispatch(IpcSession *s, uint32_t *m) {
rq.data_size = payload + 16 <= end ? (uint32_t)(end - payload - 16) : 0;
} else {
fflush(stdout);
fprintf(stderr, "ipc: %s message type %u:", s->name, rq.type);
for (unsigned i = 0; i < 16; i++) fprintf(stderr, " %08x", m[i]);
fputc('\n', stderr);
rt_logf("ipc: %s message type %u:", s->name, rq.type);
for (unsigned i = 0; i < 16; i++) rt_logf(" %08x", m[i]);
rt_log_raw("\n", 1);
ipc_backtrace();
rt_fatal("ipc: %s message type %u is not implemented", s->name, rq.type);
}
+94 -10
View File
@@ -35,6 +35,51 @@
// ---- logging, tracing, hashing
unsigned rt_trace;
// Log file. A game run whose stderr is not a file already (started from Explorer, a shortcut or a console) also
// writes its log to logs\totk.log next to totk.exe, so a player has one to attach to a report; run_game.py and
// `2> file` keep their own file. Each start moves the earlier logs to totk.1.log .. totk.4.log. A file stops at 64 MiB
// (TOTK_TRACE can write that much). Lines from before log_open wait in a static buffer: mem_init runs first so that
// nothing else lands in the guest address space, and an earlier open would put the CRT's heap there.
#define LOG_KEEP 5
#define LOG_CAP (64ull << 20)
enum { LOG_EARLY, LOG_FILE, LOG_OFF };
static int g_log_state;
static PlatAppend *g_log_file;
static char g_log_path[MAX_PATH * 3];
static char g_log_early[32768];
static size_t g_log_early_n;
static uint64_t g_log_bytes;
void rt_log_raw(const char *p, size_t n) {
fwrite(p, 1, n, stderr);
int state = __atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE);
if (state == LOG_EARLY) {
size_t at = __atomic_fetch_add(&g_log_early_n, n, __ATOMIC_RELAXED);
if (at + n <= sizeof g_log_early) memcpy(g_log_early + at, p, n);
} else if (state == LOG_FILE) {
uint64_t at = __atomic_fetch_add(&g_log_bytes, n, __ATOMIC_RELAXED);
if (at + n <= LOG_CAP) {
plat_append_write(g_log_file, p, n);
} else if (at <= LOG_CAP) { // the first write past the cap says so, once
static const char cut[] = "[host] log: the file reached 64 MiB, the rest of this run goes to stderr only\n";
plat_append_write(g_log_file, cut, sizeof cut - 1);
}
}
}
void rt_vlogf(const char *fmt, va_list ap) {
char buf[2048];
int n = vsnprintf(buf, sizeof buf, fmt, ap);
if (n > 0) rt_log_raw(buf, n < (int)sizeof buf ? (size_t)n : sizeof buf - 1);
}
void rt_logf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
rt_vlogf(fmt, ap);
va_end(ap);
}
void rt_log(const char *fmt, ...) {
char buf[2048];
int n = snprintf(buf, sizeof buf, "[host] ");
@@ -44,7 +89,45 @@ void rt_log(const char *fmt, ...) {
va_end(ap);
n = k < 0 ? n : n + k < (int)sizeof buf - 1 ? n + k : (int)sizeof buf - 2;
buf[n++] = '\n';
fwrite(buf, 1, n, stderr);
rt_log_raw(buf, n);
}
const char *rt_log_path(void) {
return __atomic_load_n(&g_log_state, __ATOMIC_ACQUIRE) == LOG_FILE ? g_log_path : NULL;
}
// Opens the log file for a game run (game = 1) and writes the buffered lines to it; anything else drops them. Runs on
// the main thread before the boot starts other threads.
static void log_open(int game) {
char dir[MAX_PATH * 3], from[MAX_PATH * 3 + 16], to[MAX_PATH * 3 + 16];
char *cut = NULL;
if (game && !plat_stderr_path(dir, sizeof dir) && plat_exe_path(dir, sizeof dir)) {
cut = strrchr(dir, '\\');
char *f = strrchr(dir, '/');
if (f > cut) cut = f;
}
if (cut && (size_t)(cut - dir) + sizeof "\\logs" <= sizeof dir) {
strcpy(cut, "\\logs");
plat_file_mkdir_p(dir);
for (int i = LOG_KEEP - 1; i > 0; i--) { // totk.3.log -> totk.4.log, ..., totk.log -> totk.1.log
if (i > 1) snprintf(from, sizeof from, "%s\\totk.%d.log", dir, i - 1);
else snprintf(from, sizeof from, "%s\\totk.log", dir);
snprintf(to, sizeof to, "%s\\totk.%d.log", dir, i);
if (plat_file_exists(from)) plat_file_replace(from, to);
}
snprintf(g_log_path, sizeof g_log_path, "%s\\totk.log", dir);
g_log_file = plat_append_open(g_log_path);
}
if (!g_log_file) {
__atomic_store_n(&g_log_state, LOG_OFF, __ATOMIC_RELEASE);
return;
}
size_t n = __atomic_load_n(&g_log_early_n, __ATOMIC_RELAXED);
if (n > sizeof g_log_early) n = sizeof g_log_early;
plat_append_write(g_log_file, g_log_early, n);
g_log_bytes = n;
__atomic_store_n(&g_log_state, LOG_FILE, __ATOMIC_RELEASE);
rt_log("log: %s", g_log_path);
}
static void parse_trace(void) {
@@ -176,30 +259,30 @@ static LONG CALLBACK crash_handler(EXCEPTION_POINTERS *ep) {
return EXCEPTION_CONTINUE_SEARCH; // not our code: leave it to the handler that expects it
if (InterlockedExchange(&busy, 1)) ExitProcess(71);
char d[160];
fprintf(stderr, "FATAL: exception %#lx at host %p (totk.exe+%#llx)", code, (void *)rip,
rt_logf("FATAL: exception %#lx at host %p (totk.exe+%#llx)", code, (void *)rip,
(unsigned long long)(rip - (uintptr_t)mi.lpBaseOfDll));
if (code == EXCEPTION_ACCESS_VIOLATION && ep->ExceptionRecord->NumberParameters >= 2)
fprintf(stderr, ", %s address %#llx",
rt_logf(", %s address %#llx",
ep->ExceptionRecord->ExceptionInformation[0] == 1 ? "writing" : ep->ExceptionRecord->ExceptionInformation[0] == 8 ? "executing" : "reading",
(unsigned long long)ep->ExceptionRecord->ExceptionInformation[1]);
fputc('\n', stderr);
rt_log_raw("\n", 1);
uint64_t f = lifted_function(rip);
if (f) fprintf(stderr, " in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d));
if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d));
Cpu *c = kern_cpu();
if (c) {
for (int i = 0; i < 31; i++)
fprintf(stderr, " x%-2d %016llx%s", i, (unsigned long long)c->x[i], i % 4 == 3 ? "\n" : "");
fprintf(stderr, " sp %016llx\n pc %016llx lr %s\n", (unsigned long long)c->sp, (unsigned long long)c->pc,
rt_logf(" x%-2d %016llx%s", i, (unsigned long long)c->x[i], i % 4 == 3 ? "\n" : "");
rt_logf(" sp %016llx\n pc %016llx lr %s\n", (unsigned long long)c->sp, (unsigned long long)c->pc,
ldr_describe(c->x[30], d, sizeof d));
uint64_t fp = c->x[29];
for (int n = 0; n < 32 && fp && !(fp & 7) && readable(fp, 16); n++) {
uint64_t next = LD64(fp), lr = LD64(fp + 8);
fprintf(stderr, " guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
rt_logf(" guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
if (next <= fp || next - fp > (64u << 20)) break;
fp = next;
}
} else {
fprintf(stderr, " not on a guest thread\n");
rt_logf(" not on a guest thread\n");
}
fflush(stderr);
ExitProcess(71);
@@ -440,7 +523,7 @@ int main(int argc, char **argv) {
.gpu = cfg_get("TOTK_GPU")};
portable_paths(&cfg); // command-line paths below still win
if (cfg.gpu && strcmp(cfg.gpu, "null") && strcmp(cfg.gpu, "vulkan")) {
fprintf(stderr, "TOTK_GPU must be null or vulkan, not '%s'\n", cfg.gpu);
rt_logf("TOTK_GPU must be null or vulkan, not '%s'\n", cfg.gpu);
return 2;
}
const char *mode = NULL;
@@ -467,6 +550,7 @@ int main(int argc, char **argv) {
}
if (cfg.shot && !cfg.seconds) return usage(argv[0]);
if (cfg.seconds && mode) return usage(argv[0]);
log_open(!mode);
if (!mode) {
cfg.boot = cfg.seconds > 0; // --seconds starts the game like --boot, with its checkpoints
rt_boot();
+5
View File
@@ -70,6 +70,11 @@ void plat_file_delete(const char *path);
int plat_file_truncate(const char *path, uint64_t size); // 1 on success
int64_t plat_file_size(const char *path); // bytes of a file, -1 when missing or unreadable
int plat_file_set_readonly(const char *path, int on); // sets or clears the read-only attribute; 1 on success
// A file that only grows and other processes may read meanwhile: each write lands whole at its end, from any thread,
// and reaches the file even if the process dies right after (no user-space buffer, no flush to the disk).
typedef struct PlatAppend PlatAppend;
PlatAppend *plat_append_open(const char *path); // creates or truncates path; NULL on failure
int plat_append_write(PlatAppend *f, const void *data, size_t n); // 1 when all n bytes landed
// Calls cb(name, ctx) for each file in dir whose name ends in ext (".in"); name has no directory part. cb returns 0 to
// stop. Returns 0 when dir cannot be listed.
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx);
+15
View File
@@ -199,6 +199,21 @@ int plat_file_write(const char *path, const void *data, size_t n) {
return ok;
}
PlatAppend *plat_append_open(const char *path) {
wchar_t w[WPATH];
if (!widen(path, w, WPATH)) return NULL;
HANDLE h = CreateFileW(w, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
return h == INVALID_HANDLE_VALUE ? NULL : (PlatAppend *)h;
}
int plat_append_write(PlatAppend *f, const void *data, size_t n) {
// Offset and OffsetHigh all ones: WriteFile appends at the end of the file, atomically against other writers.
OVERLAPPED o = {.Offset = 0xffffffff, .OffsetHigh = 0xffffffff};
DWORD put = 0;
return WriteFile((HANDLE)f, data, (DWORD)n, &put, &o) && put == n;
}
int plat_file_replace(const char *from, const char *to) {
wchar_t a[WPATH], b[WPATH];
if (!widen(from, a, WPATH) || !widen(to, b, WPATH)) return 0;
+4 -4
View File
@@ -35,11 +35,11 @@ static int readable(uint64_t a, uint64_t n) {
void kern_backtrace(const Cpu *c) {
char d[160];
fprintf(stderr, " lr %s\n", ldr_describe(c->x[30], d, sizeof d));
rt_logf(" lr %s\n", ldr_describe(c->x[30], d, sizeof d));
uint64_t fp = c->x[29];
for (int n = 0; n < 32 && fp && !(fp & 7) && readable(fp, 16); n++) {
uint64_t next = LD64(fp), lr = LD64(fp + 8);
fprintf(stderr, " guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
rt_logf(" guest frame %2d: %s\n", n, ldr_describe(lr, d, sizeof d));
if (next <= fp || next - fp > (64u << 20)) break;
fp = next;
}
@@ -61,7 +61,7 @@ static __attribute__((noreturn)) void svc_fatal(Cpu *c, uint32_t imm, uint64_t p
if (unimplemented[i].imm == imm) name = unimplemented[i].name;
char at[160];
fflush(stdout);
fprintf(stderr, "FATAL: svc #%#x %s at %s: %s\n x0 %#llx x1 %#llx x2 %#llx x3 %#llx\n", imm, name,
rt_logf("FATAL: svc #%#x %s at %s: %s\n x0 %#llx x1 %#llx x2 %#llx x3 %#llx\n", imm, name,
ldr_describe(pc, at, sizeof at), why, (unsigned long long)c->x[0], (unsigned long long)c->x[1],
(unsigned long long)c->x[2], (unsigned long long)c->x[3]);
kern_backtrace(c);
@@ -211,7 +211,7 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) {
if (n > 65536) svc_fatal(c, imm, pc, "debug string longer than 64 KiB");
if (n) {
fflush(stdout);
fwrite((const void *)(uintptr_t)x[0], 1, n, stderr);
rt_log_raw((const char *)(uintptr_t)x[0], n);
}
x[0] = R_OK;
return;
+4 -1
View File
@@ -8,6 +8,7 @@
#endif
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
#include "sysinfo.h"
#include "vk.h"
@@ -29,7 +30,9 @@ void sysinfo_cpu_name(char *out, size_t n) {
}
int sysinfo_log_folder(char *out, size_t n) {
if (!plat_stderr_path(out, n) && !plat_exe_path(out, n)) return 0;
const char *log = rt_log_path();
if (log) snprintf(out, n, "%s", log);
else if (!plat_stderr_path(out, n) && !plat_exe_path(out, n)) return 0;
char *s = strrchr(out, '\\'), *f = strrchr(out, '/');
if (f > s) s = f;
if (!s) return 0;
+2 -1
View File
@@ -12,7 +12,8 @@ extern "C" {
int sysinfo_text(char *buf, size_t n);
// The CPU's brand string ("AMD Ryzen 7 7800X3D 8-Core Processor"); "" when the CPU has none.
void sysinfo_cpu_name(char *out, size_t n);
// The folder with the log: the folder of the file stderr goes to, else the folder of totk.exe. 0 on failure.
// The folder with the log: the folder of logs\totk.log when this run writes it, else of the file stderr goes to, else
// of totk.exe. 0 on failure.
int sysinfo_log_folder(char *out, size_t n);
#ifdef __cplusplus