Skip to content

Fix NativeAOT debug info for statics, arrays, interfaces, enums and  this - #138164

Open
jbevain wants to merge 7 commits into
dotnet:mainfrom
jbevain:jbevain/naot-dwarf-fixes
Open

jbevain wants to merge 7 commits into
dotnet:mainfrom
jbevain:jbevain/naot-dwarf-fixes

Conversation

@jbevain

@jbevain jbevain commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Fix NativeAOT debug info for statics, arrays, interfaces, enums and this

While evaluating how lldb and gdb handle the DWARF that NativeAOT emits through their C++ support, I found several places where the debug info is inconsistent or wrong. Debuggers then show wrong values, fail to evaluate expressions, or trip assertions. This PR fixes them.

Changes

  1. Fix NativeAOT DWARF locations of static fields. On non-Windows targets, the static fields of a class are emitted grouped by storage region (non-GC, GC, then thread statics), but the descriptors with their locations were collected in declaration order, and the DWARF writer pairs both lists by index. Any class declaring a GC static before a non-GC static got the locations of its fields swapped. The location descriptors are now grouped the same way as the fields.

  2. Fix NativeAOT DWARF location of GC static fields. The GC static base symbol holds a reference to the object storing the statics, and the field offsets already include the MethodTable pointer, but the location expression dereferenced the symbol twice. Debuggers read GC statics relative to the MethodTable of the statics object. It now dereferences once, which matches the code the compiler generates.

  3. Don't describe a location for thread statics in NativeAOT DWARF. Thread statics that aren't inlined in the module TLS block were described relative to the __THREADSTATICS symbol of the type, which isn't their storage. The storage is looked up at runtime for the current thread (GetThreadStaticBaseForType), which a location expression can't describe. The fields are still declared, without a location, so debuggers report them as unavailable instead of reading an unrelated address.

  4. Remove duplicate element count from NativeAOT DWARF array types. Array types derive from System.Array, which describes the element count with _numComponents, and also declared their own m_NumComponents at the same offset. Clang-based debuggers reject the overlapping fields: an assertions-enabled lldb aborts in clang's record layout, and lldb main evaluates array.m_NumComponents to 0. The array types now only describe the bounds and the elements; the count is _numComponents of the base class, which NativeAOT.natvis already uses on Windows.

  5. Fix size of interfaces in NativeAOT debug info. Interfaces are described as classes deriving from Object, so debuggers can cast them to the actual type of the instance, but their size was their own instance byte count of 0. gdb fails to print variables of interface types with "access outside bounds of object". Interfaces now have the size of Object. This only changes DW_AT_byte_size; CodeView takes the size of classes from elsewhere.

  6. Describe boxed enums with their enumeration type in NativeAOT debug info. Every allocated MethodTable gets debug info, and a boxed enum was described as a class or struct named like the enum, with its value__ field, next to the enumeration type with the same name. This also affects CodeView. Debuggers then resolve the name to the wrong type:

    • lldb: Color::Green is "ambiguous".
    • gdb: there's "no field named Green".
    • WinDbg/cdb: dt Color shows the struct, and dx color == Color::Blue fails with "Unable to bind name 'Blue'".

    Boxed enums are now described by the enumeration type.

  7. Describe the this parameter as a pointer in NativeAOT DWARF. Member function declarations describe their object pointer as a pointer to the class, but the this parameter of the method definitions was a reference (or a byref for value types). gdb fails to evaluate member calls like Method() in an instance method with "Attempt to take contents of a non-pointer value". The definitions now use the type of the declaration's object pointer, which is also how the Visual Studio debugger shows this on Windows. CodeView is unchanged.

Visible behavior changes

  • The element count of arrays is only described as _numComponents (from System.Array); m_NumComponents no longer exists in DWARF.
  • this is a pointer in lldb and gdb, like the Visual Studio debugger already shows it on Windows: members are accessed with this->, and lldb's frame variable this prints the pointer (frame variable *this expands the object). Other object variables are still references, accessed with ., as in Visual Studio.
  • Thread statics that aren't inlined have no location in DWARF; debuggers show them as unavailable instead of a garbage value.

Not tested: other architectures and macOS.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 23, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The shared boxed-enum change affects CodeView and DWARF, but Linux dump tests do not establish Windows/macOS debugger name resolution or boxed-value inspection, leaving those contracts to human validation.

No open issues 🎉

What changed in this PR

This PR corrects NativeAOT debug metadata so debuggers can locate static fields and interpret arrays, interfaces, boxed enums, and this consistently. Most changes affect DWARF; the boxed-enum correction also affects CodeView.

Changes:

  • Align static-field locations with storage order, remove an extra GC-static dereference, and omit unsupported thread-static locations.
  • Correct array and interface layouts, boxed-enum descriptions, and DWARF this parameter types.
  • Extend the DWARF smoke tests with regression checks for these cases.
File Description
src/​tests/​nativeaot/​SmokeTests/​DwarfDump/​Program.cs Adds test types and DWARF assertions for the corrected metadata.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​UserDefinedTypeDescriptor.cs Fixes interface size and static-location descriptor ordering; removes thread-static locations.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​UnixObjectWriter.Aot.cs Emits ordinary instance-method this parameters as pointers, preserving state-machine handling.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​ObjectWriter.Aot.cs Shares declaration-compatible this typing and uses enumeration types for boxed-enum MethodTables.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​Dwarf/​DwarfInfo.cs Uses a single dereference for GC-static locations.
src/​coreclr/​tools/​aot/​ILCompiler.Compiler/​Compiler/​ObjectWriter/​Dwarf/​DwarfBuilder.cs Skips unavailable static locations and removes the duplicate array element-count field.

Review effort: Balanced

@jbevain

jbevain commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

The shared boxed-enum change affects CodeView and DWARF, but Linux dump tests do not establish Windows/macOS debugger name resolution or boxed-value inspection, leaving those contracts to human validation.

This has been validated on macOS as well. I've found issues related to how dsymutil processes the DWARF symbols, but I'm addressing this in a separate PR as they existed before and after this change: #138224.

@jbevain

jbevain commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Windows (CodeView) validation of the boxed enum change

The commit that describes boxed enums with their enumeration type also changes the Windows debug info, so I checked it on Windows x64. I built the same NativeAOT probe with the ILC from before and after that commit. The probe uses enum Color : byte { Red = 1, Green = 2, Blue = 4 } and boxes a Color.

PDB type records, from llvm-pdbutil dump -types:

Before After
Records named Probe_Probe_Color LF_ENUM and LF_STRUCTURE (with value__) LF_ENUM
Enums sharing their name with a struct, whole PDB 13 of 375 0

cdb/WinDbg, stopped in a method with a Color color parameter set to Blue:

// Before
0:000> dt Probe!Probe_Probe_Color
   +0x000 value__          : UChar
0:000> dx color == Probe_Probe_Color::Blue
Error: Unable to bind name 'Blue'
0:000> dx circle.Color == Probe_Probe_Color::Green
Error: Unable to bind name 'Green'

// After
0:000> dt Probe!Probe_Probe_Color
   Red = 0n1
   Green = 0n2
   Blue = 0n4
0:000> dx color == Probe_Probe_Color::Blue
color == Probe_Probe_Color::Blue : true
0:000> dx circle.Color == Probe_Probe_Color::Green
circle.Color == Probe_Probe_Color::Green : true

Visual Studio 2026: circle.Color == Probe_Probe_Color::Green evaluates to true both before and after, and Probe_Probe_Color::Green shows Green (2). So the change fixes WinDbg without changing the Visual Studio behavior.

@jbevain
jbevain force-pushed the jbevain/naot-dwarf-fixes branch from a3ecd55 to 45ba752 Compare November 25, 2026 18:48
jbevain and others added 7 commits November 25, 2026 10:54
On non-Windows targets, the debug info of a class emits its static fields
grouped by storage region (non-GC, GC, then thread statics), but the
descriptors with their locations were collected in field declaration order.
The DWARF writer pairs both lists by index, so any class declaring a GC
static before a non-GC one got the locations of its static fields swapped.

Collect the location descriptors per region and concatenate them in the
same order as the fields.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
The static base symbol of a type with GC statics holds a reference to the
object storing those statics, and the offset of each GC static field already
includes the MethodTable pointer at the start of that object. The location
expression dereferenced the symbol twice, which made debuggers read the
fields relative to the MethodTable of the statics object instead of the
object itself.

Dereference the static base once.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
The location of thread static fields was described like the location of GC
statics, relative to the __THREADSTATICS symbol of the type. That symbol is
not the storage of the thread statics: the storage is looked up at runtime
for the current thread from the index of the type, which a DWARF location
expression can't describe. Debuggers read thread static fields from an
unrelated address.

Keep declaring thread static fields, but don't describe their location.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
The DWARF description of array types derives from System.Array, which
already describes the number of elements with its _numComponents field, and
then declared its own m_NumComponents member at the same offset. Debuggers
building a C++ record from this see two overlapping fields: clang asserts
on the layout, and lldb can evaluate m_NumComponents to a wrong value in
expressions.

Only describe the bounds and the elements in the array types.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
Interfaces are described as classes deriving from Object, so debuggers can
cast them to the actual type of the instance, but their size was their own
instance byte count, which is 0. A class smaller than its base class is
invalid; gdb fails to print variables of interface types with "access
outside bounds of object".

Use the size of Object for interfaces.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
The debug info of every allocated MethodTable was created through
GetTypeIndex, which describes a boxed enum as a class named like the enum
with its value__ field. The same name was also used by the enumeration type
describing variables and fields of the enum, which makes the name ambiguous
for debuggers: lldb fails to evaluate Color::Green with "reference to
'Color' is ambiguous", and gdb looks up the enumerators in the class.

Use the enumeration type for boxed enums.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f
Member function declarations describe their object pointer as a pointer to
the class, but the this parameter of the method definitions had the type of
a variable of the class: a reference for reference types and value type
byrefs. Debuggers use the this parameter to resolve the members of the class
in expressions; gdb fails to evaluate a call like Method() inside an
instance method with "Attempt to take contents of a non-pointer value".

Give the this parameter of the method definitions the type of the object
pointer of their declaration.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: bf34413c-9243-495c-acf3-59e4b5f38a9f

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants