Skip to content

Commit 7b208e0

Browse files
authored
[LLDB] Add setting for native PDB reader (#151490)
Initially suggested in #149305 (comment) - this PR adds the setting `plugin.symbol-file.pdb.use-native-reader`. It doesn't remove support for `LLDB_USE_NATIVE_PDB_READER` to allow some backwards compatibility. This was the suggested way to use the native reader - changing that would mean users who set this, now use the DIA reader. The setting has priority over the environment variable, though. If the default gets flipped on Windows, the environment variable could probably be removed as well. This would make it possible to test both native PDB and DIA PDB in the API tests (see linked PR).
1 parent a0db29d commit 7b208e0

File tree

6 files changed

+195
-27
lines changed

6 files changed

+195
-27
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
1212
#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
1313
#include "Plugins/ObjectFile/PDB/ObjectFilePDB.h"
14+
#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
1415
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
1516
#include "lldb/Core/Module.h"
1617
#include "lldb/Core/PluginManager.h"
@@ -298,6 +299,9 @@ llvm::StringRef SymbolFileNativePDB::GetPluginDescriptionStatic() {
298299
}
299300

300301
SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFileSP objfile_sp) {
302+
if (!SymbolFilePDB::UseNativePDB())
303+
return nullptr;
304+
301305
return new SymbolFileNativePDB(std::move(objfile_sp));
302306
}
303307

lldb/source/Plugins/SymbolFile/PDB/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
lldb_tablegen(SymbolFilePDBProperties.inc -gen-lldb-property-defs
2+
SOURCE SymbolFilePDBProperties.td
3+
TARGET LLDBPluginSymbolFilePDBPropertiesGen)
4+
5+
lldb_tablegen(SymbolFilePDBPropertiesEnum.inc -gen-lldb-property-enum-defs
6+
SOURCE SymbolFilePDBProperties.td
7+
TARGET LLDBPluginSymbolFilePDBPropertiesEnumGen)
8+
19
add_lldb_library(lldbPluginSymbolFilePDB PLUGIN
210
PDBASTParser.cpp
311
PDBLocationToDWARFExpression.cpp
@@ -16,3 +24,7 @@ add_lldb_library(lldbPluginSymbolFilePDB PLUGIN
1624
clangAST
1725
clangLex
1826
)
27+
28+
add_dependencies(lldbPluginSymbolFilePDB
29+
LLDBPluginSymbolFilePDBPropertiesGen
30+
LLDBPluginSymbolFilePDBPropertiesEnumGen)

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp

Lines changed: 111 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,93 @@ LLDB_PLUGIN_DEFINE(SymbolFilePDB)
7171
char SymbolFilePDB::ID;
7272

7373
namespace {
74+
75+
enum PDBReader {
76+
ePDBReaderDefault,
77+
ePDBReaderDIA,
78+
ePDBReaderNative,
79+
};
80+
81+
constexpr OptionEnumValueElement g_pdb_reader_enums[] = {
82+
{
83+
ePDBReaderDefault,
84+
"default",
85+
"Use DIA PDB reader unless LLDB_USE_NATIVE_PDB_READER environment "
86+
"variable is set",
87+
},
88+
{
89+
ePDBReaderDIA,
90+
"dia",
91+
"Use DIA PDB reader",
92+
},
93+
{
94+
ePDBReaderNative,
95+
"native",
96+
"Use native PDB reader",
97+
},
98+
};
99+
100+
#define LLDB_PROPERTIES_symbolfilepdb
101+
#include "SymbolFilePDBProperties.inc"
102+
103+
enum {
104+
#define LLDB_PROPERTIES_symbolfilepdb
105+
#include "SymbolFilePDBPropertiesEnum.inc"
106+
};
107+
108+
#if LLVM_ENABLE_DIA_SDK && defined(_WIN32)
109+
bool ShouldUseNativeReaderByDefault() {
110+
static bool g_use_native_by_default = true;
111+
112+
static llvm::once_flag g_initialize;
113+
llvm::call_once(g_initialize, [] {
114+
llvm::StringRef env_value = ::getenv("LLDB_USE_NATIVE_PDB_READER");
115+
if (!env_value.equals_insensitive("on") &&
116+
!env_value.equals_insensitive("yes") &&
117+
!env_value.equals_insensitive("1") &&
118+
!env_value.equals_insensitive("true"))
119+
g_use_native_by_default = false;
120+
});
121+
122+
return g_use_native_by_default;
123+
}
124+
#endif
125+
126+
class PluginProperties : public Properties {
127+
public:
128+
static llvm::StringRef GetSettingName() {
129+
return SymbolFilePDB::GetPluginNameStatic();
130+
}
131+
132+
PluginProperties() {
133+
m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
134+
m_collection_sp->Initialize(g_symbolfilepdb_properties);
135+
}
136+
137+
bool UseNativeReader() const {
138+
#if LLVM_ENABLE_DIA_SDK && defined(_WIN32)
139+
auto value =
140+
GetPropertyAtIndexAs<PDBReader>(ePropertyReader, ePDBReaderDefault);
141+
switch (value) {
142+
case ePDBReaderNative:
143+
return true;
144+
case ePDBReaderDIA:
145+
return false;
146+
default:
147+
case ePDBReaderDefault:
148+
return ShouldUseNativeReaderByDefault();
149+
}
150+
#else
151+
return true;
152+
#endif
153+
}
154+
};
155+
156+
PluginProperties &GetGlobalPluginProperties() {
157+
static PluginProperties g_settings;
158+
return g_settings;
159+
}
160+
74161
lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
75162
switch (lang) {
76163
case PDB_Lang::Cpp:
@@ -97,46 +184,43 @@ bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
97184
}
98185
} // namespace
99186

100-
static bool ShouldUseNativeReader() {
101-
#if defined(_WIN32)
102-
#if LLVM_ENABLE_DIA_SDK
103-
llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
104-
if (!use_native.equals_insensitive("on") &&
105-
!use_native.equals_insensitive("yes") &&
106-
!use_native.equals_insensitive("1") &&
107-
!use_native.equals_insensitive("true"))
108-
return false;
109-
#endif
110-
#endif
111-
return true;
112-
}
113-
114187
void SymbolFilePDB::Initialize() {
115-
if (ShouldUseNativeReader()) {
116-
npdb::SymbolFileNativePDB::Initialize();
117-
} else {
118-
PluginManager::RegisterPlugin(GetPluginNameStatic(),
119-
GetPluginDescriptionStatic(), CreateInstance,
120-
DebuggerInitialize);
121-
}
188+
// Initialize both but check in CreateInstance for the desired plugin
189+
npdb::SymbolFileNativePDB::Initialize();
190+
191+
PluginManager::RegisterPlugin(GetPluginNameStatic(),
192+
GetPluginDescriptionStatic(), CreateInstance,
193+
DebuggerInitialize);
122194
}
123195

124196
void SymbolFilePDB::Terminate() {
125-
if (ShouldUseNativeReader()) {
126-
npdb::SymbolFileNativePDB::Terminate();
127-
} else {
128-
PluginManager::UnregisterPlugin(CreateInstance);
129-
}
197+
npdb::SymbolFileNativePDB::Terminate();
198+
199+
PluginManager::UnregisterPlugin(CreateInstance);
200+
}
201+
202+
bool SymbolFilePDB::UseNativePDB() {
203+
return GetGlobalPluginProperties().UseNativeReader();
130204
}
131205

132-
void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
206+
void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {
207+
if (!PluginManager::GetSettingForSymbolFilePlugin(
208+
debugger, PluginProperties::GetSettingName())) {
209+
PluginManager::CreateSettingForSymbolFilePlugin(
210+
debugger, GetGlobalPluginProperties().GetValueProperties(),
211+
"Properties for the PDB symbol-file plug-in.", true);
212+
}
213+
}
133214

134215
llvm::StringRef SymbolFilePDB::GetPluginDescriptionStatic() {
135216
return "Microsoft PDB debug symbol file reader.";
136217
}
137218

138219
lldb_private::SymbolFile *
139220
SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) {
221+
if (UseNativePDB())
222+
return nullptr;
223+
140224
return new SymbolFilePDB(std::move(objfile_sp));
141225
}
142226

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class SymbolFilePDB : public lldb_private::SymbolFileCommon {
4949
static lldb_private::SymbolFile *
5050
CreateInstance(lldb::ObjectFileSP objfile_sp);
5151

52+
static bool UseNativePDB();
53+
5254
// Constructors and Destructors
5355
SymbolFilePDB(lldb::ObjectFileSP objfile_sp);
5456

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include "../../../../include/lldb/Core/PropertiesBase.td"
2+
3+
let Definition = "symbolfilepdb" in {
4+
def Reader: Property<"reader", "Enum">,
5+
Global,
6+
DefaultEnumValue<"ePDBReaderDefault">,
7+
EnumValues<"OptionEnumValues(g_pdb_reader_enums)">,
8+
Desc<"Selects the reader for PDB symbol files. "
9+
"The native PDB reader that uses LLVM's PDB support is always available (value: 'native'). "
10+
"Secondly, the DIA PDB reader is only available if LLVM was comppiled with Microsoft's DIA SDK on Windows (value: 'DIA'). "
11+
"By default, the DIA PDB reader is used if available. "
12+
"The LLDB_USE_NATIVE_PDB_READER environment variable can be used to switch to the native reader when this setting has the default value. "
13+
"Otherwise, the setting always has priority.">;
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// REQUIRES: target-windows
2+
3+
// Test plugin.symbol-file.pdb.reader setting
4+
// RUN: %build -o %t.exe -- %s
5+
// RUN: env LLDB_USE_NATIVE_PDB_READER=0 %lldb %t.exe -o 'target modules dump symfile' | FileCheck --check-prefix=ENV0 %s
6+
// RUN: env LLDB_USE_NATIVE_PDB_READER=1 %lldb %t.exe -o 'target modules dump symfile' | FileCheck --check-prefix=ENV1 %s
7+
// RUN: env LLDB_USE_NATIVE_PDB_READER=0 %lldb \
8+
// RUN: -o 'settings set plugin.symbol-file.pdb.reader dia' \
9+
// RUN: -o 'target create %t.exe' \
10+
// RUN: -o 'target modules dump symfile' \
11+
// RUN: | FileCheck --check-prefix=ENV0-SET-DIA %s
12+
// RUN: env LLDB_USE_NATIVE_PDB_READER=1 %lldb \
13+
// RUN: -o 'settings set plugin.symbol-file.pdb.reader dia' \
14+
// RUN: -o 'target create %t.exe' \
15+
// RUN: -o 'target modules dump symfile' \
16+
// RUN: | FileCheck --check-prefix=ENV1-SET-DIA %s
17+
// RUN: env LLDB_USE_NATIVE_PDB_READER=0 %lldb \
18+
// RUN: -o 'settings set plugin.symbol-file.pdb.reader native' \
19+
// RUN: -o 'target create %t.exe' \
20+
// RUN: -o 'target modules dump symfile' \
21+
// RUN: | FileCheck --check-prefix=ENV0-SET-NATIVE %s
22+
// RUN: env LLDB_USE_NATIVE_PDB_READER=1 %lldb \
23+
// RUN: -o 'settings set plugin.symbol-file.pdb.reader native' \
24+
// RUN: -o 'target create %t.exe' \
25+
// RUN: -o 'target modules dump symfile' \
26+
// RUN: | FileCheck --check-prefix=ENV1-SET-NATIVE %s
27+
28+
// ENV0: (lldb) target modules dump symfile
29+
// ENV0: Dumping debug symbols for 1 modules.
30+
// ENV0: SymbolFile pdb
31+
32+
// ENV1: (lldb) target modules dump symfile
33+
// ENV1: Dumping debug symbols for 1 modules.
34+
// ENV1: SymbolFile native-pdb
35+
36+
// ENV0-SET-DIA: (lldb) target modules dump symfile
37+
// ENV0-SET-DIA: Dumping debug symbols for 1 modules.
38+
// ENV0-SET-DIA: SymbolFile pdb
39+
40+
// ENV1-SET-DIA: (lldb) target modules dump symfile
41+
// ENV1-SET-DIA: Dumping debug symbols for 1 modules.
42+
// ENV1-SET-DIA: SymbolFile pdb
43+
44+
// ENV0-SET-NATIVE: (lldb) target modules dump symfile
45+
// ENV0-SET-NATIVE: Dumping debug symbols for 1 modules.
46+
// ENV0-SET-NATIVE: SymbolFile native-pdb
47+
48+
// ENV1-SET-NATIVE: (lldb) target modules dump symfile
49+
// ENV1-SET-NATIVE: Dumping debug symbols for 1 modules.
50+
// ENV1-SET-NATIVE: SymbolFile native-pdb
51+
52+
int main() {}

0 commit comments

Comments
 (0)