Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -gdwarf CLI option to emit DWARF debuginfos for MSVC targets #3533

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ static cl::opt<unsigned char, true> debugInfo(
clEnumValN(3, "gline-tables-only", "Add line tables only")),
cl::location(global.params.symdebug), cl::init(0));

cl::opt<bool> emitDwarfDebugInfo(
"gdwarf", cl::ZeroOrMore,
cl::desc("Emit DWARF debuginfo (instead of CodeView) for MSVC targets"));

cl::opt<bool> noAsm("noasm", cl::desc("Disallow use of inline assembler"),
cl::ZeroOrMore);

Expand Down
1 change: 1 addition & 0 deletions driver/cl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern cl::list<std::string> fileList;
extern cl::list<std::string> runargs;
extern cl::opt<bool> invokedByLDMD;
extern cl::opt<bool> compileOnly;
extern cl::opt<bool> emitDwarfDebugInfo;
extern cl::opt<bool> noAsm;
extern cl::opt<bool> dontWriteObj;
extern cl::opt<std::string> objectFile;
Expand Down
12 changes: 11 additions & 1 deletion driver/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,17 @@ static std::vector<std::string> getDefaultLibNames() {

//////////////////////////////////////////////////////////////////////////////

bool useInternalLLDForLinking() { return linkInternally; }
bool useInternalLLDForLinking() {
return linkInternally
#if LDC_WITH_LLD
||
// DWARF debuginfos for MSVC require LLD
(opts::emitDwarfDebugInfo && linkInternally.getNumOccurrences() == 0 &&
opts::linker.empty() && !opts::isUsingLTO() &&
global.params.targetTriple->isWindowsMSVCEnvironment())
#endif
;
}

cl::boolOrDefault linkFullyStatic() { return staticFlag; }

Expand Down
5 changes: 5 additions & 0 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,11 @@ int cppmain() {
global.obj_ext = {3, "obj"};
}

// -gdwarf implies -g if not specified explicitly
if (opts::emitDwarfDebugInfo && global.params.symdebug == 0) {
global.params.symdebug = 1;
}

// allocate the target abi
gABI = TargetABI::getTarget();

Expand Down
41 changes: 26 additions & 15 deletions gen/dibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ bool DIBuilder::mustEmitLocationsDebugInfo() {

DIBuilder::DIBuilder(IRState *const IR)
: IR(IR), DBuilder(IR->module), CUNode(nullptr),
isTargetMSVC(global.params.targetTriple->isWindowsMSVCEnvironment()),
isTargetMSVCx64(isTargetMSVC &&
global.params.targetTriple->isArch64Bit()),
emitCodeView(!opts::emitDwarfDebugInfo &&
global.params.targetTriple->isWindowsMSVCEnvironment()),
// like clang, don't emit any column infos for CodeView by default
// (https://reviews.llvm.org/D23720)
emitColumnInfo(opts::getFlagOrDefault(::emitColumnInfo, !isTargetMSVC)) {}
emitColumnInfo(opts::getFlagOrDefault(::emitColumnInfo, !emitCodeView)) {}

llvm::LLVMContext &DIBuilder::getContext() { return IR->context(); }

Expand Down Expand Up @@ -244,7 +243,7 @@ DIType DIBuilder::CreateBasicType(Type *type) {
Encoding = DW_ATE_boolean;
break;
case Tchar:
if (isTargetMSVC) {
if (emitCodeView) {
// VS debugger does not support DW_ATE_UTF for char
Encoding = DW_ATE_unsigned_char;
break;
Expand All @@ -255,7 +254,7 @@ DIType DIBuilder::CreateBasicType(Type *type) {
Encoding = DW_ATE_UTF;
break;
case Tint8:
if (isTargetMSVC) {
if (emitCodeView) {
// VS debugger does not support DW_ATE_signed for 8-bit
Encoding = DW_ATE_signed_char;
break;
Expand All @@ -268,7 +267,7 @@ DIType DIBuilder::CreateBasicType(Type *type) {
Encoding = DW_ATE_signed;
break;
case Tuns8:
if (isTargetMSVC) {
if (emitCodeView) {
// VS debugger does not support DW_ATE_unsigned for 8-bit
Encoding = DW_ATE_unsigned_char;
break;
Expand All @@ -288,7 +287,7 @@ DIType DIBuilder::CreateBasicType(Type *type) {
case Timaginary32:
case Timaginary64:
case Timaginary80:
if (isTargetMSVC) {
if (emitCodeView) {
// DW_ATE_imaginary_float not supported by the LLVM DWARF->CodeView
// conversion
Encoding = DW_ATE_float;
Expand All @@ -299,7 +298,7 @@ DIType DIBuilder::CreateBasicType(Type *type) {
case Tcomplex32:
case Tcomplex64:
case Tcomplex80:
if (isTargetMSVC) {
if (emitCodeView) {
// DW_ATE_complex_float not supported by the LLVM DWARF->CodeView
// conversion
return CreateComplexType(t);
Expand Down Expand Up @@ -890,11 +889,22 @@ void DIBuilder::EmitCompileUnit(Module *m) {
auto producerName =
std::string("LDC ") + ldc_version + " (LLVM " + llvm_version + ")";

if (isTargetMSVC)
if (emitCodeView) {
IR->module.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
else if (global.params.dwarfVersion > 0)
IR->module.addModuleFlag(llvm::Module::Warning, "Dwarf Version",
global.params.dwarfVersion);
} else {
unsigned dwarfVersion = global.params.dwarfVersion;
if (dwarfVersion == 0 &&
global.params.targetTriple->isWindowsMSVCEnvironment()) {
// clang 10 defaults to v4
dwarfVersion = 4;
}

if (dwarfVersion > 0) {
IR->module.addModuleFlag(llvm::Module::Warning, "Dwarf Version",
dwarfVersion);
}
}

// Metadata without a correct version will be stripped by UpgradeDebugInfo.
IR->module.addModuleFlag(llvm::Module::Warning, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
Expand Down Expand Up @@ -974,7 +984,7 @@ DISubprogram DIBuilder::EmitSubProgram(FuncDeclaration *fd) {
DIScope scope = nullptr;
llvm::StringRef name;
// FIXME: work around apparent LLVM CodeView bug wrt. nested functions
if (isTargetMSVC && fd->toParent2()->isFuncDeclaration()) {
if (emitCodeView && fd->toParent2()->isFuncDeclaration()) {
// emit into module & use fully qualified name
scope = GetCU();
name = fd->toPrettyChars(true);
Expand Down Expand Up @@ -1241,7 +1251,8 @@ void DIBuilder::EmitLocalVariable(llvm::Value *ll, VarDeclaration *vd,

// For MSVC x64, some by-value parameters need to be declared as DI locals to
// work around garbage for both cdb and VS debuggers.
if (isParameter && !forceAsLocal && isTargetMSVCx64 && !isRefOrOut) {
if (emitCodeView && isParameter && !forceAsLocal && !isRefOrOut &&
global.params.targetTriple->isArch64Bit()) {
// 1) params rewritten by IndirectByvalRewrite
if (isaArgument(ll) && addr.empty()) {
forceAsLocal = true;
Expand Down
3 changes: 1 addition & 2 deletions gen/dibuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class DIBuilder {

DICompileUnit CUNode;

const bool isTargetMSVC;
const bool isTargetMSVCx64;
const bool emitCodeView;
const bool emitColumnInfo;

llvm::DenseMap<Declaration*, llvm::TypedTrackingMDRef<llvm::MDNode>> StaticDataMemberCache;
Expand Down
16 changes: 16 additions & 0 deletions tests/debuginfo/msvc_dwarf.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// REQUIRES: Windows

// RUN: %ldc -gdwarf -of=%t.exe %s 2> %t_stderr.log
// RUN: FileCheck %s < %t_stderr.log
// CHECK: lld-link: warning: section name .debug_info is longer than 8 characters and will use a non-standard string table

int foo(int p)
{
auto r = 2 * p;
return r;
}

void main()
{
foo(123);
}