Skip to content

Commit 919c11a

Browse files
committed
f provide more debug information in panics
1 parent 1d0b473 commit 919c11a

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

lightning/src/debug_sync.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ impl std::hash::Hash for LockDep {
9494
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64(self.lock.lock_idx); }
9595
}
9696

97+
#[cfg(feature = "backtrace")]
98+
fn get_construction_symbol<'a>(backtrace: &'a Backtrace) -> (&'a str, u64) {
99+
// Find the first frame which is after `debug_sync` (or which is in our tests) and use
100+
// that as the mutex construction site. Note that the first few frames may be in
101+
// `backtrace`, so we have to ignore those.
102+
let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync.*new").unwrap();
103+
let mut found_debug_sync = false;
104+
for frame in backtrace.frames() {
105+
for symbol in frame.symbols() {
106+
let symbol_name = symbol.name().unwrap().as_str().unwrap();
107+
if !sync_mutex_constr_regex.is_match(symbol_name) {
108+
if found_debug_sync {
109+
return (symbol_name, symbol.addr().unwrap() as usize as u64);
110+
}
111+
} else { found_debug_sync = true; }
112+
}
113+
}
114+
panic!("Couldn't find mutex construction callsite");
115+
}
116+
97117
impl LockMetadata {
98118
fn new() -> Arc<LockMetadata> {
99119
let lock_idx;
@@ -104,24 +124,7 @@ impl LockMetadata {
104124

105125
#[cfg(feature = "backtrace")]
106126
{
107-
let mut symbol_ptr = None;
108-
// Find the first frame which is after `debug_sync` (or which is in our tests) and use
109-
// that as the mutex construction site. Note that the first few frames may be in
110-
// `backtrace`, so we have to ignore those.
111-
let sync_mutex_constr_regex = regex::Regex::new(r"lightning.*debug_sync.*new").unwrap();
112-
let mut found_debug_sync = false;
113-
'trace_walk: for frame in backtrace.frames() {
114-
for symbol in frame.symbols() {
115-
let symbol_name = symbol.name().unwrap().as_str().unwrap();
116-
if !sync_mutex_constr_regex.is_match(symbol_name) {
117-
if found_debug_sync {
118-
symbol_ptr = Some(symbol.addr().unwrap() as usize as u64);
119-
break 'trace_walk;
120-
}
121-
} else { found_debug_sync = true; }
122-
}
123-
}
124-
lock_idx = symbol_ptr.unwrap();
127+
lock_idx = get_construction_symbol(&backtrace).1;
125128
LOCKS_INIT.call_once(|| { unsafe { LOCKS = Some(StdMutex::new(HashMap::new())); } });
126129
if let Some(metadata) = unsafe { LOCKS.as_ref() }.unwrap().lock().unwrap().get(&lock_idx) {
127130
return Arc::clone(&metadata);
@@ -167,7 +170,10 @@ impl LockMetadata {
167170
for locked_dep in locked.locked_before.lock().unwrap().iter() {
168171
if locked_dep.lock == *this && locked_dep.lock != *locked {
169172
#[cfg(feature = "backtrace")]
170-
panic!("Tried to violate existing lockorder.\nMutex that should be locked after the current lock was created at the following backtrace.\nNote that to get a backtrace for the lockorder violation, you should set RUST_BACKTRACE=1\nLock constructed at:\n{:?}\n\nLock dep created at:\n{:?}\n\n", locked._lock_construction_bt, locked_dep.lockdep_trace);
173+
panic!("Tried to violate existing lockorder.\nMutex that should be locked after the current lock was created at the following backtrace.\nNote that to get a backtrace for the lockorder violation, you should set RUST_BACKTRACE=1\nLock being taken constructed at: {}:\n{:?}\nLock constructed at: {}:\n{:?}\n\nLock dep created at:\n{:?}\n\n",
174+
get_construction_symbol(&this._lock_construction_bt).0, this._lock_construction_bt,
175+
get_construction_symbol(&locked._lock_construction_bt).0, locked._lock_construction_bt,
176+
locked_dep.lockdep_trace);
171177
#[cfg(not(feature = "backtrace"))]
172178
panic!("Tried to violate existing lockorder. Build with the backtrace feature for more info.");
173179
}

0 commit comments

Comments
 (0)