Skip to content

Commit fe9dede

Browse files
committed
print thread name in miri error backtraces
1 parent 14e1628 commit fe9dede

File tree

117 files changed

+219
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+219
-170
lines changed

src/tools/miri/src/concurrency/data_race.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,8 +1673,8 @@ impl GlobalState {
16731673
vector: VectorIdx,
16741674
) -> String {
16751675
let thread = self.vector_info.borrow()[vector];
1676-
let thread_name = thread_mgr.get_thread_name(thread);
1677-
format!("thread `{}`", String::from_utf8_lossy(thread_name))
1676+
let thread_name = thread_mgr.get_thread_display_name(thread);
1677+
format!("thread `{thread_name}`")
16781678
}
16791679

16801680
/// Acquire a lock, express that the previous call of

src/tools/miri/src/concurrency/thread.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,18 @@ pub type StackEmptyCallback<'mir, 'tcx> =
160160
Box<dyn FnMut(&mut MiriInterpCx<'mir, 'tcx>) -> InterpResult<'tcx, Poll<()>>>;
161161

162162
impl<'mir, 'tcx> Thread<'mir, 'tcx> {
163-
/// Get the name of the current thread, or `<unnamed>` if it was not set.
164-
fn thread_name(&self) -> &[u8] {
165-
if let Some(ref thread_name) = self.thread_name { thread_name } else { b"<unnamed>" }
163+
/// Get the name of the current thread if it was set.
164+
fn thread_name(&self) -> Option<&[u8]> {
165+
self.thread_name.as_deref()
166+
}
167+
168+
/// Get the name of the current thread for display purposes; will include thread ID if not set.
169+
fn thread_display_name(&self, id: ThreadId) -> String {
170+
if let Some(ref thread_name) = self.thread_name {
171+
String::from_utf8_lossy(thread_name).into_owned()
172+
} else {
173+
format!("unnamed-{}", id.index())
174+
}
166175
}
167176

168177
/// Return the top user-relevant frame, if there is one.
@@ -205,7 +214,7 @@ impl<'mir, 'tcx> std::fmt::Debug for Thread<'mir, 'tcx> {
205214
write!(
206215
f,
207216
"{}({:?}, {:?})",
208-
String::from_utf8_lossy(self.thread_name()),
217+
String::from_utf8_lossy(self.thread_name().unwrap_or(b"<unnamed>")),
209218
self.state,
210219
self.join_status
211220
)
@@ -572,10 +581,14 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
572581
}
573582

574583
/// Get the name of the given thread.
575-
pub fn get_thread_name(&self, thread: ThreadId) -> &[u8] {
584+
pub fn get_thread_name(&self, thread: ThreadId) -> Option<&[u8]> {
576585
self.threads[thread].thread_name()
577586
}
578587

588+
pub fn get_thread_display_name(&self, thread: ThreadId) -> String {
589+
self.threads[thread].thread_display_name(thread)
590+
}
591+
579592
/// Put the thread into the blocked state.
580593
fn block_thread(&mut self, thread: ThreadId) {
581594
let state = &mut self.threads[thread].state;
@@ -980,7 +993,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
980993
}
981994

982995
#[inline]
983-
fn get_thread_name<'c>(&'c self, thread: ThreadId) -> &'c [u8]
996+
fn get_thread_name<'c>(&'c self, thread: ThreadId) -> Option<&[u8]>
984997
where
985998
'mir: 'c,
986999
{

src/tools/miri/src/diagnostics.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ pub fn report_msg<'tcx>(
477477

478478
// Show note and help messages.
479479
let mut extra_span = false;
480-
let notes_len = notes.len();
481480
for (span_data, note) in notes {
482481
if let Some(span_data) = span_data {
483482
err.span_note(span_data.span(), note);
@@ -486,7 +485,6 @@ pub fn report_msg<'tcx>(
486485
err.note(note);
487486
}
488487
}
489-
let helps_len = helps.len();
490488
for (span_data, help) in helps {
491489
if let Some(span_data) = span_data {
492490
err.span_help(span_data.span(), help);
@@ -495,12 +493,20 @@ pub fn report_msg<'tcx>(
495493
err.help(help);
496494
}
497495
}
498-
if notes_len + helps_len > 0 {
499-
// Add visual separator before backtrace.
500-
err.note(if extra_span { "BACKTRACE (of the first span):" } else { "BACKTRACE:" });
501-
}
502496

503497
// Add backtrace
498+
let mut backtrace_title = String::from("BACKTRACE");
499+
if extra_span {
500+
write!(backtrace_title, " (of the first span)").unwrap();
501+
}
502+
let thread_name =
503+
machine.threads.get_thread_display_name(machine.threads.get_active_thread_id());
504+
if thread_name != "main" {
505+
// Only print thread name if it is not `main`.
506+
write!(backtrace_title, " on thread `{thread_name}`").unwrap();
507+
};
508+
write!(backtrace_title, ":").unwrap();
509+
err.note(backtrace_title);
504510
for (idx, frame_info) in stacktrace.iter().enumerate() {
505511
let is_local = machine.is_local(frame_info);
506512
// No span for non-local frames and the first frame (which is the error site).

src/tools/miri/src/shims/unix/thread.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
104104
let name_out = name_out.to_pointer(this)?;
105105
let len = len.to_target_usize(this)?;
106106

107-
let name = this.get_thread_name(thread).to_owned();
107+
// FIXME: we should use the program name if the thread name is not set
108+
let name = this.get_thread_name(thread).unwrap_or(b"<unnamed>").to_owned();
108109
let (success, _written) = this.write_c_str(&name, name_out, len)?;
109110

110111
Ok(if success { Scalar::from_u32(0) } else { this.eval_libc("ERANGE") })

src/tools/miri/tests/compiletest.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ regexes! {
172172
r"\.rs:[0-9]+:[0-9]+(: [0-9]+:[0-9]+)?" => ".rs:LL:CC",
173173
// erase alloc ids
174174
"alloc[0-9]+" => "ALLOC",
175+
// erase thread ids
176+
r"unnamed-[0-9]+" => "unnamed-ID",
175177
// erase borrow tags
176178
"<[0-9]+>" => "<TAG>",
177179
"<[0-9]+=" => "<TAG=",

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_create_too_few_args.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | panic!()
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9-
= note: BACKTRACE:
9+
= note: BACKTRACE on thread `unnamed-ID`:
1010
= note: inside `thread_start` at RUSTLIB/core/src/panic.rs:LL:CC
1111
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1212

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_create_too_many_args.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | panic!()
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9-
= note: BACKTRACE:
9+
= note: BACKTRACE on thread `unnamed-ID`:
1010
= note: inside `thread_start` at RUSTLIB/core/src/panic.rs:LL:CC
1111
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1212

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_join_main.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | assert_eq!(libc::pthread_join(thread_id, ptr::null_mut()), 0);
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9-
= note: BACKTRACE:
9+
= note: BACKTRACE on thread `unnamed-ID`:
1010
= note: inside closure at $DIR/libc_pthread_join_main.rs:LL:CC
1111

1212
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_join_multiple.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | ... assert_eq!(libc::pthread_join(native_copy, ptr::null_mut()), 0);
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9-
= note: BACKTRACE:
9+
= note: BACKTRACE on thread `unnamed-ID`:
1010
= note: inside closure at $DIR/libc_pthread_join_multiple.rs:LL:CC
1111

1212
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

src/tools/miri/tests/fail-dep/concurrency/libc_pthread_join_self.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | assert_eq!(libc::pthread_join(native, ptr::null_mut()), 0);
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9-
= note: BACKTRACE:
9+
= note: BACKTRACE on thread `unnamed-ID`:
1010
= note: inside closure at $DIR/libc_pthread_join_self.rs:LL:CC
1111

1212
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

0 commit comments

Comments
 (0)