Skip to content

Commit be5da3a

Browse files
committed
Auto merge of #3338 - RalfJung:more-tracking-and-threads, r=RalfJung
print thread name in miri error backtraces; add option to track read/write accesses This came up while debugging #121626. It didn't end up being useful there but still seems like good tools to have around.
2 parents 14e1628 + fe545d6 commit be5da3a

File tree

132 files changed

+257
-184
lines changed

Some content is hidden

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

132 files changed

+257
-184
lines changed

src/tools/miri/src/bin/miri.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ fn main() {
534534
),
535535
};
536536
miri_config.tracked_alloc_ids.extend(ids);
537+
} else if arg == "-Zmiri-track-alloc-accesses" {
538+
miri_config.track_alloc_accesses = true;
537539
} else if let Some(param) = arg.strip_prefix("-Zmiri-compare-exchange-weak-failure-rate=") {
538540
let rate = match param.parse::<f64>() {
539541
Ok(rate) if rate >= 0.0 && rate <= 1.0 => rate,

src/tools/miri/src/borrow_tracker/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ impl VisitProvenance for GlobalStateInner {
122122
/// We need interior mutable access to the global state.
123123
pub type GlobalState = RefCell<GlobalStateInner>;
124124

125-
/// Indicates which kind of access is being performed.
126-
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
127-
pub enum AccessKind {
128-
Read,
129-
Write,
130-
}
131-
132125
impl fmt::Display for AccessKind {
133126
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134127
match self {

src/tools/miri/src/borrow_tracker/stacked_borrows/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_span::{Span, SpanData};
66
use rustc_target::abi::Size;
77

8-
use crate::borrow_tracker::{AccessKind, GlobalStateInner, ProtectorKind};
8+
use crate::borrow_tracker::{GlobalStateInner, ProtectorKind};
99
use crate::*;
1010

1111
/// Error reporting

src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_target::abi::{Abi, Size};
1616

1717
use crate::borrow_tracker::{
1818
stacked_borrows::diagnostics::{AllocHistory, DiagnosticCx, DiagnosticCxBuilder},
19-
AccessKind, GlobalStateInner, ProtectorKind,
19+
GlobalStateInner, ProtectorKind,
2020
};
2121
use crate::*;
2222

src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::borrow_tracker::tree_borrows::{
99
tree::LocationState,
1010
unimap::UniIndex,
1111
};
12-
use crate::borrow_tracker::{AccessKind, ProtectorKind};
12+
use crate::borrow_tracker::ProtectorKind;
1313
use crate::*;
1414

1515
/// Cause of an access: either a real access or one

src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_target::abi::{Abi, Size};
22

3-
use crate::borrow_tracker::{AccessKind, GlobalState, GlobalStateInner, ProtectorKind};
3+
use crate::borrow_tracker::{GlobalState, GlobalStateInner, ProtectorKind};
44
use rustc_middle::{
55
mir::{Mutability, RetagKind},
66
ty::{

src/tools/miri/src/borrow_tracker/tree_borrows/perms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33

44
use crate::borrow_tracker::tree_borrows::diagnostics::TransitionError;
55
use crate::borrow_tracker::tree_borrows::tree::AccessRelatedness;
6-
use crate::borrow_tracker::AccessKind;
6+
use crate::AccessKind;
77

88
/// The activation states of a pointer.
99
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

src/tools/miri/src/borrow_tracker/tree_borrows/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::borrow_tracker::tree_borrows::{
2424
unimap::{UniEntry, UniIndex, UniKeyMap, UniValMap},
2525
Permission,
2626
};
27-
use crate::borrow_tracker::{AccessKind, GlobalState, ProtectorKind};
27+
use crate::borrow_tracker::{GlobalState, ProtectorKind};
2828
use crate::*;
2929

3030
mod tests;

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
{

0 commit comments

Comments
 (0)