Skip to content

Commit 178bcf7

Browse files
zhassan-awstedinski
authored andcommitted
Print location information for panic in codegen (#514)
1 parent 2446292 commit 178bcf7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

compiler/rustc_codegen_rmc/src/compiler_interface.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ impl CodegenBackend for GotocCodegenBackend {
6969
c.call_with_panic_debug_info(
7070
|ctx| ctx.declare_function(instance),
7171
format!("declare_function: {}", c.readable_instance_name(instance)),
72+
instance.def_id(),
7273
);
7374
}
7475
MonoItem::Static(def_id) => {
7576
c.call_with_panic_debug_info(
7677
|ctx| ctx.declare_static(def_id, item),
7778
format!("declare_static: {:?}", def_id),
79+
def_id,
7880
);
7981
}
8082
MonoItem::GlobalAsm(_) => {
@@ -100,12 +102,14 @@ impl CodegenBackend for GotocCodegenBackend {
100102
c.readable_instance_name(instance),
101103
c.symbol_name(instance)
102104
),
105+
instance.def_id(),
103106
);
104107
}
105108
MonoItem::Static(def_id) => {
106109
c.call_with_panic_debug_info(
107110
|ctx| ctx.codegen_static(def_id, item),
108111
format!("codegen_static: {:?}", def_id),
112+
def_id,
109113
);
110114
}
111115
MonoItem::GlobalAsm(_) => {} // We have already warned above

compiler/rustc_codegen_rmc/src/utils/debug.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
//! This file contains functionality that makes RMC easier to debug
55
66
use crate::GotocCtx;
7+
use cbmc::goto_program::Location;
78
use rustc_middle::mir::Body;
89
use rustc_middle::ty::print::with_no_trimmed_paths;
910
use rustc_middle::ty::Instance;
11+
use rustc_span::def_id::DefId;
1012
use std::cell::RefCell;
1113
use std::lazy::SyncLazy;
1214
use std::panic;
@@ -15,7 +17,7 @@ use tracing::debug;
1517
// Use a thread-local global variable to track the current codegen item for debugging.
1618
// If RMC panics during codegen, we can grab this item to include the problematic
1719
// codegen item in the panic trace.
18-
thread_local!(static CURRENT_CODEGEN_ITEM: RefCell<Option<String>> = RefCell::new(None));
20+
thread_local!(static CURRENT_CODEGEN_ITEM: RefCell<(Option<String>, Option<Location>)> = RefCell::new((None, None)));
1921

2022
// Include RMC's bug reporting URL in our panics.
2123
const BUG_REPORT_URL: &str =
@@ -44,11 +46,17 @@ static DEFAULT_HOOK: SyncLazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send +
4446

4547
// Print the current function if available
4648
CURRENT_CODEGEN_ITEM.with(|cell| {
47-
if let Some(current_item) = cell.borrow().clone() {
49+
let t = cell.borrow().clone();
50+
if let Some(current_item) = t.0 {
4851
eprintln!("[RMC] current codegen item: {}", current_item);
4952
} else {
5053
eprintln!("[RMC] no current codegen item.");
5154
}
55+
if let Some(current_loc) = t.1 {
56+
eprintln!("[RMC] current codegen location: {:?}", current_loc);
57+
} else {
58+
eprintln!("[RMC] no current codegen location.");
59+
}
5260
});
5361

5462
// Separate the output with an empty line
@@ -71,11 +79,13 @@ impl<'tcx> GotocCtx<'tcx> {
7179
&mut self,
7280
call: F,
7381
panic_debug: String,
82+
def_id: DefId,
7483
) {
7584
CURRENT_CODEGEN_ITEM.with(|odb_cell| {
76-
odb_cell.replace(Some(panic_debug));
85+
odb_cell
86+
.replace((Some(panic_debug), Some(self.codegen_span(&self.tcx.def_span(def_id)))));
7787
call(self);
78-
odb_cell.replace(None);
88+
odb_cell.replace((None, None));
7989
});
8090
}
8191

0 commit comments

Comments
 (0)