Skip to content

Commit 0cfcd38

Browse files
authored
[lldb][NativePDB] Parse global variables. (llvm#114303)
This doesn't parse S_CONSTANT case yet, because I found that the global variable `std::strong_ordering::equal` is a S_CONSTANT and has type of LF_STRUCTURE which is not currently handled when creating dwarf expression for the variable. Left a TODO for it to finish later. This makes `lldb/test/Shell/SymbolFile/PDB/ast-restore.test` and `lldb/test/Shell/SymbolFile/PDB/calling-conventions-x86.test` pass on windows with native pdb plugin only.
1 parent 8634e35 commit 0cfcd38

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,9 @@ VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
888888

889889
CompUnitSP comp_unit;
890890
std::optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr);
891-
if (!modi) {
891+
// Some globals has modi points to the linker module, ignore them.
892+
if (!modi || modi >= GetNumCompileUnits())
892893
return nullptr;
893-
}
894894

895895
CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi);
896896
comp_unit = GetOrCreateCompileUnit(cci);
@@ -1810,7 +1810,27 @@ SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit,
18101810
VariableList &variables) {
18111811
PdbSymUid sym_uid(comp_unit.GetID());
18121812
lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland);
1813-
return 0;
1813+
for (const uint32_t gid : m_index->globals().getGlobalsTable()) {
1814+
PdbGlobalSymId global{gid, false};
1815+
CVSymbol sym = m_index->ReadSymbolRecord(global);
1816+
// TODO: S_CONSTANT is not handled here to prevent a possible crash in
1817+
// lldb_private::npdb::MakeConstantLocationExpression when it's a record
1818+
// type (e.g. std::strong_ordering::equal). That function needs to be
1819+
// updated to handle this case when we add S_CONSTANT case here.
1820+
switch (sym.kind()) {
1821+
case SymbolKind::S_GDATA32:
1822+
case SymbolKind::S_LDATA32:
1823+
case SymbolKind::S_GTHREAD32:
1824+
case SymbolKind::S_LTHREAD32: {
1825+
if (VariableSP var = GetOrCreateGlobalVariable(global))
1826+
variables.AddVariable(var);
1827+
break;
1828+
}
1829+
default:
1830+
break;
1831+
}
1832+
}
1833+
return variables.GetSize();
18141834
}
18151835

18161836
VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,

lldb/test/Shell/SymbolFile/NativePDB/ast-methods.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ int main(int argc, char **argv) {
4444
// AST: | |-ParmVarDecl {{.*}} 'char'
4545
// AST: | `-ParmVarDecl {{.*}} 'int'
4646

47-
// SYMBOL: int main(int argc, char **argv);
48-
// SYMBOL-NEXT: struct Struct {
47+
// SYMBOL: struct Struct {
4948
// SYMBOL-NEXT: void simple_method();
5049
// SYMBOL-NEXT: static void static_method();
5150
// SYMBOL-NEXT: virtual void virtual_method();
5251
// SYMBOL-NEXT: int overloaded_method();
5352
// SYMBOL-NEXT: int overloaded_method(char);
5453
// SYMBOL-NEXT: int overloaded_method(char, int, ...);
5554
// SYMBOL-NEXT: };
55+
// SYMBOL-NEXT: Struct s;
56+
// SYMBOL-NEXT: int main(int argc, char **argv);

lldb/test/Shell/SymbolFile/NativePDB/global-ctor-dtor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ int main() {
1818
return 0;
1919
}
2020

21-
// CHECK: static void B::`dynamic initializer for 'glob'();
21+
// CHECK: struct A {
22+
// CHECK-NEXT: ~A();
23+
// CHECK-NEXT: };
24+
// CHECK-NEXT: A B::glob;
25+
// CHECK-NEXT: static void B::`dynamic initializer for 'glob'();
2226
// CHECK-NEXT: static void B::`dynamic atexit destructor for 'glob'();
2327
// CHECK-NEXT: int main();
2428
// CHECK-NEXT: static void _GLOBAL__sub_I_global_ctor_dtor.cpp();
25-
// CHECK-NEXT: struct A {
26-
// CHECK-NEXT: ~A();
27-
// CHECK-NEXT: };
2829
// CHECK-NEXT: struct B {
2930
// CHECK-NEXT: static A glob;
3031
// CHECK-NEXT: };

lldb/test/Shell/SymbolFile/PDB/ast-restore.test

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ ENUM: }
2525
ENUM: }
2626
ENUM: }
2727

28-
GLOBAL: Module: {{.*}}
29-
GLOBAL: namespace N0 {
30-
GLOBAL: namespace N1 {
31-
GLOBAL: N0::N1::(anonymous namespace)::Enum Global;
32-
GLOBAL: }
33-
GLOBAL: }
28+
GLOBAL: N0::N1::(anonymous namespace)::Enum {{.*}}Global;
3429

3530
BASE: Module: {{.*}}
3631
BASE: namespace N0 {
@@ -77,7 +72,7 @@ INNER: }
7772

7873
TEMPLATE: Module: {{.*}}
7974
TEMPLATE: struct Template<N0::N1::Class> {
80-
TEMPLATE: inline void TemplateFunc<1>();
75+
TEMPLATE: void TemplateFunc<1>();
8176
TEMPLATE: };
8277

8378
FOO: Module: {{.*}}

0 commit comments

Comments
 (0)