Skip to content

[lldb][Native PDB] don't omit 0-sized struct fields - #237688

Open
Walnut356 wants to merge 1 commit into
llvm:mainfrom
Walnut356:pdb_zst
Open

Walnut356 wants to merge 1 commit into
llvm:mainfrom
Walnut356:pdb_zst

Conversation

@Walnut356

Copy link
Copy Markdown
Contributor

Resolves #235686

TL;DR brings PDB handling in-line with DWARF handling, such that it no longer pretends 0-sized fields don't exist.

Worth noting that this change still has slightly different behavior than DWARF handling, though I'm not 100% sure if that's due to LLDB, LLVM, or the PDB format itself. For the same struct definition:

#[repr(C)]
struct A {
    x: u64,
    y: u16,
    data: [Sized],
}

the PDB type listing is as follows:

struct A {
    unsigned long long x;
    unsigned short y;
    temp::Sized data[0];
}

Where the type of data is considered to be 0-sized.

Conversely, the DWARF data is listed as:

struct A {
    unsigned long long x;
    unsigned short y;
    temp::Sized data[];
}

where the type of data is considered to be the size of 1 temp::Sized element.

Regardless, this change allows users to interact with all 0-sized fields, which is helpful for the visualizer scripts, particularly for Rust's dynamically sized types (e.g. the example struct above) and PhantomData<T>.

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-lldb

Author: Walnut (Walnut356)

Changes

Resolves #235686

TL;DR brings PDB handling in-line with DWARF handling, such that it no longer pretends 0-sized fields don't exist.

Worth noting that this change still has slightly different behavior than DWARF handling, though I'm not 100% sure if that's due to LLDB, LLVM, or the PDB format itself. For the same struct definition:

#[repr(C)]
struct A {
    x: u64,
    y: u16,
    data: [Sized],
}

the PDB type listing is as follows:

struct A {
    unsigned long long x;
    unsigned short y;
    temp::Sized data[0];
}

Where the type of data is considered to be 0-sized.

Conversely, the DWARF data is listed as:

struct A {
    unsigned long long x;
    unsigned short y;
    temp::Sized data[];
}

where the type of data is considered to be the size of 1 temp::Sized element.

Regardless, this change allows users to interact with all 0-sized fields, which is helpful for the visualizer scripts, particularly for Rust's dynamically sized types (e.g. the example struct above) and PhantomData&lt;T&gt;.


Full diff: https://github.com/llvm/llvm-project/pull/237688.diff

1 Files Affected:

  • (modified) lldb/source/Plugins/SymbolFile/NativePDB/UdtRecordCompleter.cpp (+1-2)
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/UdtRecordCompleter.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/UdtRecordCompleter.cpp
index 417d7630a93753..4d15e1c20d9635 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/UdtRecordCompleter.cpp
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/UdtRecordCompleter.cpp
@@ -294,8 +294,7 @@ Error UdtRecordCompleter::visitKnownMember(CVMemberRecord &cvr,
   lldb::AccessType access = TranslateMemberAccess(data_member.getAccess());
   size_t field_size =
       bitfield_width ? bitfield_width : GetSizeOfType(ti, m_index.tpi()) * 8;
-  if (field_size == 0)
-    return Error::success();
+
   m_record.CollectMember(data_member.Name, offset, field_size, member_qt,
                          access, bitfield_width, m_member_index);
   return Error::success();

@Walnut356

Copy link
Copy Markdown
Contributor Author

Hmm, I'll need to look more into that test failure I think. I don't have a great mental model of how LLDB's "incomplete types" work and whether or not the change from

error:{{.*}}incomplete type 'E' where a complete type is required

to

0x000000014000300d <incomplete type "E"> 

is a bad thing.

cc @Nerixyz

@Nerixyz

Nerixyz commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Off the top of my head: size=0 is sometimes used to detect incomplete types. I wonder why this works with dwarf in that case though.

@Walnut356

Copy link
Copy Markdown
Contributor Author

If I'm understanding this correctly, it looks like DWARF can represent the absence of a length, in which case DWARFASTParserClang passes nullopt when creating an array

if (array_info && array_info->element_orders.size() > 0) {
auto end = array_info->element_orders.rend();
for (auto pos = array_info->element_orders.rbegin(); pos != end; ++pos) {
clang_type = m_ast.CreateArrayType(
array_element_type, /*element_count=*/*pos, attrs.is_vector);
uint64_t num_elements = pos->value_or(0);
array_element_type = clang_type;
array_element_bit_stride = num_elements
? array_element_bit_stride * num_elements
: array_element_bit_stride;
}
} else {
clang_type = m_ast.CreateArrayType(
array_element_type, /*element_count=*/std::nullopt, attrs.is_vector);
}

PDB passes the length in directly instead of nullopt. Rust uses the same code to create the empty array types regardless of if it's generating for PDB or DWARF debug info:

https://github.com/rust-lang/rust/blob/2a01bd6f8ba2b81badc578a7fa927d4ff00f8c71/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs#L422-L437

so presumably LLVM is setting the length of an LF_ARRAY node to 0 when translating to CodeView, e.g.:

  0x1029 | LF_ARRAY [size = 16]
           size: 0, index type: 0x0023 (unsigned __int64), element type: 0x1028

Based on the codeview spec, it looks like maybe LF_BARRAY is the output we want from LLVM in this case instead? It only stores an element type index, rather than element type + index type + length + name (speaking of which, LLVM's DIBuilder doesn't have any param to specify a name when creating array types even though both DWARF and CodeView array nodes can store names).

@Walnut356

Walnut356 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

One other question, is "incomplete type" an LLDB/clang implementation detail, or is it just a synonym for "forward ref"?

@Walnut356

Copy link
Copy Markdown
Contributor Author

Ah, found the culprit

// Forward declarations of arrays without a size and VLAs use a count of -1.
// Emit a count of zero in these cases to match what MSVC does for arrays
// without a size. MSVC doesn't support VLAs, so it's not clear what we
// should do for them even if we could distinguish them.
if (Count == -1)
Count = 0;

I'll give it a test some time this week and see if MSVC's debugger freaks out when encountering LF_BARRAY

@Nerixyz

Nerixyz commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

One other question, is "incomplete type" an LLDB/clang implementation detail, or is it just a synonym for "forward ref"?

A forward reference is CodeView/PDB specific, because of the constraint that type records cannot contain type indices higher than their own.
But we do model forward references as incomplete if we haven't visited their members yet (e.g. because we only found a forward decl for now).


Conversely, the DWARF data is listed as:

struct A {
    unsigned long long x;
    unsigned short y;
    temp::Sized data[];
}

where the type of data is considered to be the size of 1 temp::Sized element.

In C, data would not take any space, since it's a flexible array member. The way PDB models the member as data[0] is not allowed by the standard, but it's a GCC and Clang extension. But in either case, it won't take any space.

@Walnut356

Copy link
Copy Markdown
Contributor Author

In C, data would not take any space, since it's a flexible array member

Whoops, i could have worded that better. When i said "is considered to be" i meant that if you ask LLDB what size it is via SBType.GetByteSize() it reports the size of 1 element.

@Walnut356

Walnut356 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll give it a test some time this week and see if MSVC's debugger freaks out when encountering LF_BARRAY

Yet another node that's in the spec, but that visual studio's debugger can't handle 🫩 Neither old nor new WinDbg handle it properly either

@Walnut356

Copy link
Copy Markdown
Contributor Author

I guess a simple (but imperfect) fix would be to skip the field_size == 1 check if the field type is an array type. That would allow our wide pointer visualizers to work, but wouldn't help for types that are transparent wrappers over array types (e.g. if the last field of your struct is CStr) or "load-bearing" empty types (PhantomData, which won't necessarily be as important after the nested-type-generics patch hits).

@Nerixyz

Nerixyz commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

I guess a simple (but imperfect) fix would be to skip the field_size == 1 check if the field type is an array type.

Sure. I think it's easier to judge if this is correct when there are tests.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[LLDB] Zero-length arrays and zero-sized fields are ignored in PDB debug info

2 participants