F3 + C writes a crash report

The debug crash from #90 printed a stand-in report; it now ends the game
with a DebugCrash error that main reports as "Manually triggered debug
crash" with the stack where it was raised. The game keeps the error that
ends it as an error, not a string, so main can tell. Leading std backtrace
frames are dropped (macOS keeps them).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-14 17:15:20 +02:00
parent 744b61efc3
commit 66c6bd6b13
4 changed files with 86 additions and 25 deletions
+71 -8
View File
@@ -296,10 +296,10 @@ fn panic_message(info: &std::panic::PanicHookInfo) -> String {
}
}
/// A `std::backtrace` as one line a frame, `function (file:line:col)`, from the frame that
/// panicked: the hook's and the panic machinery's frames go, and paths inside the workspace
/// lose the build machine's prefix.
pub fn stack_lines(backtrace: &str) -> String {
/// A `std::backtrace` as one line a frame, `function (file:line:col)`. Captured in a panic hook
/// (`from_panic`), it starts at the frame that panicked: the hook's and the panic machinery's
/// frames go. Paths inside the workspace lose the build machine's prefix.
pub fn stack_lines(backtrace: &str, from_panic: bool) -> String {
// The workspace as it was on the machine that built this binary (it need not exist here).
let manifest = env!("CARGO_MANIFEST_DIR");
let root = manifest
@@ -330,7 +330,8 @@ pub fn stack_lines(backtrace: &str) -> String {
|| name.contains("rust_begin_unwind")
};
// The hook's frames, then the panic machinery's, then the code that panicked.
let first = match frames.iter().take(30).position(|(name, _)| machinery(name)) {
let top = frames.iter().take(30).position(|(name, _)| machinery(name));
let first = match top.filter(|_| from_panic) {
Some(start) => {
start
+ frames[start..]
@@ -340,6 +341,12 @@ pub fn stack_lines(backtrace: &str) -> String {
}
None => 0,
};
// Some platforms keep std's own capture frames (`<std::backtrace::Backtrace>::create`).
let first = first
+ frames[first..]
.iter()
.take_while(|(name, _)| name.trim_start_matches('<').starts_with("std::backtrace"))
.count();
frames[first..]
.iter()
.map(|(name, at)| match at {
@@ -370,7 +377,10 @@ pub fn panic_report(info: &std::panic::PanicHookInfo, side: &'static str) -> Rep
time: Local::now(),
description: "Unexpected error".into(),
error: panic_message(info),
stack: stack_lines(&std::backtrace::Backtrace::force_capture().to_string()),
stack: stack_lines(
&std::backtrace::Backtrace::force_capture().to_string(),
true,
),
thread: thread_name(),
side,
details: system_details(),
@@ -398,12 +408,55 @@ pub fn install(home: PathBuf, side: &'static str) {
}));
}
/// Vanilla's debug crash (`key.debug.crash` held for ten seconds): the error `main` gets back,
/// with the stack where it was raised.
#[derive(Debug)]
pub struct DebugCrash {
stack: String,
}
impl DebugCrash {
pub const MESSAGE: &str = "Manually triggered debug crash";
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
stack: stack_lines(
&std::backtrace::Backtrace::force_capture().to_string(),
false,
),
}
}
}
impl std::fmt::Display for DebugCrash {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(Self::MESSAGE)
}
}
impl std::error::Error for DebugCrash {}
/// A report for an error `main` got back: the game ends with it.
pub fn report_error(
home: &Path,
side: &'static str,
error: &dyn std::error::Error,
error: &(dyn std::error::Error + 'static),
) -> Option<PathBuf> {
if let Some(debug) = error.downcast_ref::<DebugCrash>() {
return deliver(
home,
Report {
time: Local::now(),
description: DebugCrash::MESSAGE.into(),
error: DebugCrash::MESSAGE.into(),
stack: debug.stack.clone(),
thread: thread_name(),
side,
details: system_details(),
log: crate::logging::recent_lines(),
},
);
}
let mut message = error.to_string();
let mut source = error.source();
while let Some(e) = source {
@@ -507,11 +560,21 @@ mod tests {
root = env!("CARGO_MANIFEST_DIR")
);
assert_eq!(
stack_lines(&bt),
stack_lines(&bt, true),
"horizon_client::crash::debug_crash_tick (crates/horizon-client/src/crash.rs:359:9)\nwinit::dispatch (winit-0.30.13/src/event_loop.rs:642:56)\nmain\n"
);
}
#[test]
fn the_debug_crash_is_reported_as_vanillas() {
let error: Box<dyn std::error::Error> = DebugCrash::new().into();
assert_eq!(error.to_string(), "Manually triggered debug crash");
let debug = error.downcast_ref::<DebugCrash>().unwrap();
// The stack starts where the crash was raised (DebugCrash::new, in this file).
let first = debug.stack.lines().next().unwrap();
assert!(first.contains("crash.rs"), "{}", debug.stack);
}
#[test]
fn a_panic_becomes_a_report() {
// A real panic through a hook, as the game's would be.
+8 -10
View File
@@ -650,7 +650,8 @@ struct Game<'a> {
fps_at: Instant,
ticks: u64,
health_hud: hud_layout::HealthHud,
error: Option<String>,
/// What ends the game with an error; kept as it is, so main can tell a debug crash.
error: Option<Box<dyn std::error::Error>>,
message: Option<(String, Instant)>,
save: Arc<SaveDir>,
level: Level,
@@ -5418,10 +5419,7 @@ impl Game<'_> {
..Level::default()
});
let mut level = level;
horizon_client::crash::set_detail(
"World",
format!("{} (saves/{folder}, {})", level.name, level.dimension),
);
horizon_client::crash::set_detail("World", format!("{} (saves/{folder})", level.name));
if let Some(d) = &self.options.dimension {
level.dimension = d.clone();
level.position = None;
@@ -8058,7 +8056,7 @@ impl ApplicationHandler for Game<'_> {
) {
Ok(w) => Arc::new(w),
Err(e) => {
self.error = Some(e.to_string());
self.error = Some(e.into());
event_loop.exit();
return;
}
@@ -8066,7 +8064,7 @@ impl ApplicationHandler for Game<'_> {
match self.new_renderer(window.clone()) {
Ok(r) => self.renderer = Some(r),
Err(e) => {
self.error = Some(e.to_string());
self.error = Some(e);
event_loop.exit();
return;
}
@@ -8617,7 +8615,7 @@ impl ApplicationHandler for Game<'_> {
Ok(())
})();
if let Err(e) = result {
self.error = Some(e.to_string());
self.error = Some(e);
event_loop.exit();
}
}
@@ -9123,12 +9121,12 @@ pub fn run() -> Result<()> {
"generated": game.streaming.generated_total,
"meshed_sections": game.streaming.meshed_total,
"player": game.player.as_ref().map(|p| [p.position.x, p.position.y, p.position.z]),
"error": game.error,
"error": game.error.as_ref().map(|e| e.to_string()),
});
std::fs::write(path, serde_json::to_vec_pretty(&report)?)?;
}
if let Some(e) = game.error {
return Err(e.into());
return Err(e);
}
Ok(())
}
+3 -5
View File
@@ -151,12 +151,10 @@ impl Game<'_> {
)),
Some(CrashNotice::Crash) => {
// Minecraft.emergencySaveAndCrash: the world is saved, then the
// game stops with the report.
eprintln!(
"---- Minecraft Crash Report ----\n\nDescription: Manually triggered debug crash\n\njava.lang.Throwable: Manually triggered debug crash"
);
// game stops; main writes the crash report.
let crash = horizon_client::crash::DebugCrash::new();
let _ = self.save_game();
return Err("Manually triggered debug crash".into());
return Err(crash.into());
}
}
Ok(())
+4 -2
View File
@@ -146,8 +146,10 @@ stderr as before. A panic on the main thread or a fatal error writes
the machine, the GPU, render settings, resource packs, the open world and the
last 50 log lines) and the game exits with a non-zero code. `horizon-ctl logs
[--lines N]` prints the end of latest.log and `horizon-ctl logs --crash` the
newest report (the `logs` MCP tool returns the same). Development builds crash
on purpose with `HORIZON_DEBUG_CRASH=SECONDS`, that long after the first frame.
newest report (the `logs` MCP tool returns the same). F3 + C held for ten
seconds crashes the game on purpose, as in vanilla (the world is saved first);
development builds also crash with `HORIZON_DEBUG_CRASH=SECONDS`, that long
after the first frame.
**MCP.** `horizon-ctl mcp` serves the Model Context Protocol over stdin and
stdout (JSON-RPC 2.0, one message per line), with one tool per command and a