Skip to content

Commit 5f1bd9e

Browse files
committed
[LLDB] Add setting for native PDB reader
1 parent c10736a commit 5f1bd9e

File tree

6 files changed

+194
-27
lines changed

6 files changed

+194
-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 UseNativePDBReader {
76+
eUseNativePDBReaderDefault,
77+
eUseNativePDBReaderOn,
78+
eUseNativePDBReaderOff,
79+
};
80+
81+
constexpr OptionEnumValueElement g_native_pdb_reader_enums[] = {
82+
{
83+
eUseNativePDBReaderDefault,
84+
"default",
85+
"Use DIA PDB reader unless LLDB_USE_NATIVE_PDB_READER environment "
86+
"variable is set",
87+
},
88+
{
89+
eUseNativePDBReaderOn,
90+
"on",
91+
"Use native PDB reader",
92+
},
93+
{
94+
eUseNativePDBReaderOff,
95+
"off",
96+
"Use DIA 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 = GetPropertyAtIndexAs<UseNativePDBReader>(
140+
ePropertyUseNativeReader, eUseNativePDBReaderDefault);
141+
switch (value) {
142+
case eUseNativePDBReaderOn:
143+
return true;
144+
case eUseNativePDBReaderOff:
145+
return false;
146+
default:
147+
case eUseNativePDBReaderDefault:
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include "../../../../include/lldb/Core/PropertiesBase.td"
2+
3+
let Definition = "symbolfilepdb" in {
4+
def UseNativeReader: Property<"use-native-reader", "Enum">,
5+
Global,
6+
DefaultEnumValue<"eUseNativePDBReaderDefault">,
7+
EnumValues<"OptionEnumValues(g_native_pdb_reader_enums)">,
8+
Desc<"When 'on', use the native PDB reader based on LLVM's PDB support as opposed to the reader using Microsoft's DIA SDK. "
9+
"On Windows, the default is controlled by the LLDB_USE_NATIVE_PDB_READER environment variable. "
10+
"If this is set, then the native reader is used. "
11+
"Note that the setting value will always have priority and that it needs to be set before a target is created. "
12+
"On other platforms, the native reader is always used.">;
13+
}
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.use-native-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.use-native-reader off' \
9+
// RUN: -o 'target create %t.exe' \
10+
// RUN: -o 'target modules dump symfile' \
11+
// RUN: | FileCheck --check-prefix=ENV0-SET0 %s
12+
// RUN: env LLDB_USE_NATIVE_PDB_READER=1 %lldb \
13+
// RUN: -o 'settings set plugin.symbol-file.pdb.use-native-reader off' \
14+
// RUN: -o 'target create %t.exe' \
15+
// RUN: -o 'target modules dump symfile' \
16+
// RUN: | FileCheck --check-prefix=ENV1-SET0 %s
17+
// RUN: env LLDB_USE_NATIVE_PDB_READER=0 %lldb \
18+
// RUN: -o 'settings set plugin.symbol-file.pdb.use-native-reader on' \
19+
// RUN: -o 'target create %t.exe' \
20+
// RUN: -o 'target modules dump symfile' \
21+
// RUN: | FileCheck --check-prefix=ENV0-SET1 %s
22+
// RUN: env LLDB_USE_NATIVE_PDB_READER=1 %lldb \
23+
// RUN: -o 'settings set plugin.symbol-file.pdb.use-native-reader on' \
24+
// RUN: -o 'target create %t.exe' \
25+
// RUN: -o 'target modules dump symfile' \
26+
// RUN: | FileCheck --check-prefix=ENV1-SET1 %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-SET0: (lldb) target modules dump symfile
37+
// ENV0-SET0: Dumping debug symbols for 1 modules.
38+
// ENV0-SET0: SymbolFile pdb
39+
40+
// ENV1-SET0: (lldb) target modules dump symfile
41+
// ENV1-SET0: Dumping debug symbols for 1 modules.
42+
// ENV1-SET0: SymbolFile pdb
43+
44+
// ENV0-SET1: (lldb) target modules dump symfile
45+
// ENV0-SET1: Dumping debug symbols for 1 modules.
46+
// ENV0-SET1: SymbolFile native-pdb
47+
48+
// ENV1-SET1: (lldb) target modules dump symfile
49+
// ENV1-SET1: Dumping debug symbols for 1 modules.
50+
// ENV1-SET1: SymbolFile native-pdb
51+
52+
int main() {}

0 commit comments

Comments
 (0)