[cDAC] Implement GetPartialUserState for cDAC#127848
[cDAC] Implement GetPartialUserState for cDAC#127848barosiak wants to merge 1 commit intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag |
There was a problem hiding this comment.
Pull request overview
Implements DacDbiImpl.GetPartialUserState in the managed cDAC reader by mapping thread state flags from the IThread contract (including new Interruptible and StateNC/ThreadStateNC) to a typed CorDebugUserState, and adds unit + dump-based tests to validate the mapping.
Changes:
- Extend the Thread contract surface to expose
Interruptibleplus a newStateNCfield (ThreadStateNC) and plumb it through descriptors, data model, and contract implementation. - Replace the legacy-delegation stub for
GetPartialUserStatewith a managed implementation (plus DEBUG cross-validation against legacy DAC). - Add unit and dump tests validating state-flag mapping and
GetPartialUserStateresults.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/native/managed/cdac/tests/ThreadTests.cs | Adds a unit test ensuring raw state fields map to ThreadState / ThreadStateNC. |
| src/native/managed/cdac/tests/MockDescriptors/MockDescriptors.Thread.cs | Extends mock Thread layout to include StateNC and exposes setters. |
| src/native/managed/cdac/tests/DumpTests/DacDbi/DacDbiThreadDumpTests.cs | Adds dump test cross-validating GetPartialUserState against IThread contract data. |
| src/native/managed/cdac/tests/ClrDataExceptionStateTests.cs | Updates mocks constructing ThreadData to include the new StateNC field. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/Dbi/IDacDbiInterface.cs | Introduces typed CorDebugUserState and updates GetPartialUserState signature. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/Dbi/DacDbiImpl.cs | Implements GetPartialUserState using thread contract data and DEBUG cross-validation. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Data/Thread.cs | Reads new StateNC field from the Thread data descriptor. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/Thread_1.cs | Maps StateNC into ThreadData and adds Interruptible mapping. |
| src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Abstractions/Contracts/IThread.cs | Extends ThreadState, adds ThreadStateNC, and adds StateNC to ThreadData. |
| src/coreclr/vm/threads.h | Annotates thread state bits and adds m_StateNC to the cDAC data offsets. |
| src/coreclr/vm/datadescriptor/datadescriptor.inc | Adds Thread.StateNC field to the cDAC data descriptor. |
| docs/design/datacontracts/Thread.md | Documents new ThreadState.Interruptible, ThreadStateNC, and ThreadData.StateNC. |
| Id = target.ReadField<uint>(address, type, nameof(Id)); | ||
| OSId = target.ReadNUIntField(address, type, nameof(OSId)); | ||
| State = target.ReadField<uint>(address, type, nameof(State)); | ||
| StateNC = target.ReadField<uint>(address, type, nameof(StateNC)); |
| [Flags] | ||
| public enum CorDebugUserState | ||
| { | ||
| USER_BACKGROUND = 0x04, | ||
| USER_UNSTARTED = 0x08, | ||
| USER_STOPPED = 0x10, | ||
| USER_WAIT_SLEEP_JOIN = 0x20, | ||
| USER_THREADPOOL = 0x100, | ||
| } |
| TSNC_DebuggerSleepWaitJoin = 0x04000000, // Indicates to the debugger that this thread is in a sleep wait or join state | ||
| TSNC_DebuggerSleepWaitJoin = 0x04000000, // Indicates to the debugger that this thread is in a sleep wait or join state. [cDAC] [Thread]: Contract depends on this value. | ||
| // This almost mirrors the TS_Interruptible state however that flag can change | ||
| // during GC-preemptive mode whereas this one cannot. |
There was a problem hiding this comment.
Do we really need this flag?
These flags seem to be always set together:
runtime/src/coreclr/vm/comsynchronizable.cpp
Lines 438 to 454 in 1f84bc9
runtime/src/coreclr/vm/threads.cpp
Line 4466 in 1f84bc9
TS_Interruptible, but leave TSNC_DebuggerSleepWaitJoin set. That looks like a bug.
I would suggest we delete TSNC_DebuggerSleepWaitJoin and just depend on TS_Interruptible
There was a problem hiding this comment.
@jkoritzinsky, Do you have any more context on why we have these separated? It looks like you modified this relatively recently in #117788
There is some old comment about this being used to address a race condition.
runtime/src/coreclr/debug/daccess/dacdbiimpl.cpp
Lines 5694 to 5703 in 4da638d
There was a problem hiding this comment.
Historically, the managed threading APIs had deep roots into the rest of the runtime. I think this is left-over of that. We have been working on untangling it for a while now.
The core (unmanaged) runtime does not actually care about the SleepWaitJoin state at all. This state just tracks whether you are inside Thread.Sleep or one of the managed Wait APIs. It would be perfectly fine to move this bit to the managed Thread (like it is done in Native AOT). I am not asking for that to be done as part of this PR- I am just trying to explain what this state really is.
| TS_TPWorkerThread = 0x01000000, // is this a threadpool worker thread? [cDAC] [Thread]: Contract depends on this value. | ||
|
|
||
| TS_Interruptible = 0x02000000, // sitting in a Sleep(), Wait(), Join() | ||
| TS_Interruptible = 0x02000000, // sitting in a Sleep(), Wait(), Join(). [cDAC] [Thread]: Contract depends on this value. |
There was a problem hiding this comment.
We should rename this to TS_WaitSleepJoin since that's what the same thing is called in public APIs: https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadstate
Summary
Replaces the legacy-delegation stub in DacDbiImpl.GetPartialUserState with a managed implementation using the IThread contract, mirroring the native C++ logic in dacdbiimpl.cpp that maps thread state flags to CorDebugUserState.
Changes