diff --git a/llvm/include/llvm/DebugInfo/DIContext.h b/llvm/include/llvm/DebugInfo/DIContext.h --- a/llvm/include/llvm/DebugInfo/DIContext.h +++ b/llvm/include/llvm/DebugInfo/DIContext.h @@ -28,6 +28,10 @@ /// A format-neutral container for source line information. struct DILineInfo { + // DILineInfo contains "" for function/filename it cannot fetch. + static constexpr const char *const BadString = ""; + // Use "??" instead of "" to make our output closer to addr2line. + static constexpr const char *const Addr2LineBadString = "??"; std::string FileName; std::string FunctionName; Optional Source; @@ -38,7 +42,8 @@ // DWARF-specific. uint32_t Discriminator = 0; - DILineInfo() : FileName(""), FunctionName("") {} + DILineInfo() + : FileName(BadString), FunctionName(BadString) {} bool operator==(const DILineInfo &RHS) const { return Line == RHS.Line && Column == RHS.Column && @@ -61,9 +66,9 @@ void dump(raw_ostream &OS) { OS << "Line info: "; - if (FileName != "") + if (FileName != BadString) OS << "file '" << FileName << "', "; - if (FunctionName != "") + if (FunctionName != BadString) OS << "function '" << FunctionName << "', "; OS << "line " << Line << ", "; OS << "column " << Column << ", "; @@ -109,7 +114,7 @@ uint64_t Start = 0; uint64_t Size = 0; - DIGlobal() : Name("") {} + DIGlobal() : Name(DILineInfo::BadString) {} }; struct DILocal { diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1118,8 +1118,8 @@ if (!CU) return Lines; - std::string FunctionName = ""; uint32_t StartLine = 0; + std::string FunctionName(DILineInfo::BadString); getFunctionNameAndStartLineForAddress(CU, Address.Address, Spec.FNKind, FunctionName, StartLine); diff --git a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp --- a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp +++ b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp @@ -30,11 +30,6 @@ namespace llvm { namespace symbolize { -// By default, DILineInfo contains "" for function/filename it -// cannot fetch. We replace it to "??" to make our output closer to addr2line. -static const char kDILineInfoBadString[] = ""; -static const char kBadString[] = "??"; - // Prints source code around in the FileName the Line. void DIPrinter::printContext(const std::string &FileName, int64_t Line) { if (PrintSourceContext <= 0) @@ -68,16 +63,16 @@ void DIPrinter::print(const DILineInfo &Info, bool Inlined) { if (PrintFunctionNames) { std::string FunctionName = Info.FunctionName; - if (FunctionName == kDILineInfoBadString) - FunctionName = kBadString; + if (FunctionName == DILineInfo::BadString) + FunctionName = DILineInfo::Addr2LineBadString; StringRef Delimiter = PrintPretty ? " at " : "\n"; StringRef Prefix = (PrintPretty && Inlined) ? " (inlined by) " : ""; OS << Prefix << FunctionName << Delimiter; } std::string Filename = Info.FileName; - if (Filename == kDILineInfoBadString) - Filename = kBadString; + if (Filename == DILineInfo::BadString) + Filename = DILineInfo::Addr2LineBadString; else if (Basenames) Filename = llvm::sys::path::filename(Filename); if (!Verbose) { @@ -115,8 +110,8 @@ DIPrinter &DIPrinter::operator<<(const DIGlobal &Global) { std::string Name = Global.Name; - if (Name == kDILineInfoBadString) - Name = kBadString; + if (Name == DILineInfo::BadString) + Name = DILineInfo::Addr2LineBadString; OS << Name << "\n"; OS << Global.Start << " " << Global.Size << "\n"; return *this; diff --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test --- a/llvm/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test +++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-invalid-source.test @@ -1,5 +1,5 @@ ## Test llvm-objdump's --source behaviour when a line number is greater than the -## file length. +## file length and ensure that we emit a warning. # RUN: sed -e "s,SRC_COMPDIR,%/p/Inputs,g" %p/Inputs/source-interleave.ll > %t.ll # RUN: sed -e "s,line: 7,line: 9999,g" %t.ll > %t2.ll @@ -8,9 +8,11 @@ # RUN: llc -o %t2.o -filetype=obj -mtriple=x86_64-pc-linux %t2.ll # RUN: llvm-objdump --source %t.o | FileCheck %s --check-prefixes=CHECK,GOOD -# RUN: llvm-objdump --source %t2.o | FileCheck %s --implicit-check-not="int *b = &a;" +# RUN: llvm-objdump --source %t2.o 2> %t2.e | FileCheck %s --check-prefixes=CHECK --implicit-check-not="int *b = &a;" +# RUN: FileCheck %s --input-file %t2.e --check-prefixes=WARN -DFILE=%p/Inputs/source-interleave-x86_64.c # CHECK: main: # CHECK-NEXT: ; int main() { +# WARN: warning: debug info line number 9999 exceeds the number of lines in [[FILE]] # GOOD: ; int *b = &a; # CHECK: ; return *b + foo(); diff --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-missing-source.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-missing-source.test --- a/llvm/test/tools/llvm-objdump/X86/source-interleave-missing-source.test +++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-missing-source.test @@ -1,5 +1,5 @@ ## Test that if the source cannot be found that disassembly is still printed, -## and that no source is printed. +## that no source is printed, and that we emit a warning. # RUN: sed -e "s,SRC_COMPDIR,%/t,g" %p/Inputs/source-interleave.ll > %t.ll # RUN: sed -e "s,SRC_COMPDIR,%/p/Inputs,g" %p/Inputs/source-interleave.ll > %t2.ll @@ -7,9 +7,11 @@ # RUN: llc -o %t.o -filetype=obj -mtriple=x86_64-pc-linux %t.ll # RUN: llc -o %t2.o -filetype=obj -mtriple=x86_64-pc-linux %t2.ll -# RUN: llvm-objdump --source %t.o | FileCheck %s --implicit-check-not='main()' +# RUN: llvm-objdump --source %t.o 2> %t.e | FileCheck %s --check-prefixes=CHECK --implicit-check-not='main()' # RUN: llvm-objdump --source %t2.o | FileCheck %s --check-prefixes=CHECK,SOURCE +# RUN: FileCheck %s --input-file %t.e --check-prefixes=WARN -DFILE=%t/source-interleave-x86_64.c +# WARN: warning: failed to find source [[FILE]] # CHECK: 0000000000000010 main: # SOURCE-NEXT: ; int main() { # CHECK-NEXT: 10: 55 pushq %rbp diff --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test --- a/llvm/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test +++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-no-debug-info.test @@ -1,13 +1,15 @@ ## Test that if an object has no debug information, only the disassembly is -## printed when --source is specified. +## printed when --source is specified, and that we emit a warning. # RUN: sed -e "s,SRC_COMPDIR,%/p/Inputs,g" %p/Inputs/source-interleave.ll > %t.ll # RUN: llc -o %t.o -filetype=obj -mtriple=x86_64-pc-linux %t.ll # RUN: llvm-objcopy --strip-debug %t.o %t2.o # RUN: llvm-objdump --source %t.o | FileCheck %s --check-prefixes=CHECK,SOURCE -# RUN: llvm-objdump --source %t2.o | FileCheck %s --implicit-check-not='main()' +# RUN: llvm-objdump --source %t2.o 2> %t2.e | FileCheck %s --check-prefixes=CHECK --implicit-check-not='main()' +# RUN: FileCheck %s --input-file %t2.e --check-prefixes=WARN +# WARN: warning: failed to parse debug info # CHECK: 0000000000000010 main: # SOURCE-NEXT: ; int main() { # CHECK-NEXT: 10: 55 pushq %rbp diff --git a/llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp b/llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp --- a/llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp +++ b/llvm/tools/llvm-cfi-verify/lib/FileAnalysis.cpp @@ -520,7 +520,7 @@ continue; } - if (LineInfo->FileName == "") + if (LineInfo->FileName == DILineInfo::BadString) continue; } diff --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h --- a/llvm/tools/llvm-objdump/llvm-objdump.h +++ b/llvm/tools/llvm-objdump/llvm-objdump.h @@ -129,7 +129,7 @@ void printSectionContents(const object::ObjectFile *O); void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); -void warn(StringRef Message); +void warn(Twine Message); LLVM_ATTRIBUTE_NORETURN void error(Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message); LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File); diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -51,6 +51,7 @@ #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/GraphWriter.h" #include "llvm/Support/Host.h" #include "llvm/Support/InitLLVM.h" @@ -381,12 +382,7 @@ exit(1); } -void warn(StringRef Message) { - WithColor::warning(errs(), ToolName) << Message << ".\n"; - errs().flush(); -} - -static void warn(Twine Message) { +void warn(Twine Message) { // Output order between errs() and outs() matters especially for archive // files where the output is per member object. outs().flush(); @@ -548,17 +544,22 @@ DILineInfo OldLineInfo; const ObjectFile *Obj = nullptr; std::unique_ptr Symbolizer; - // File name to file contents of source + // File name to file contents of source. std::unordered_map> SourceCache; - // Mark the line endings of the cached source + // Mark the line endings of the cached source. std::unordered_map> LineCache; + // Keep track of missing sources. + StringSet<> MissingSources; + // Only emit 'no debug info' warning once. + bool WarnedNoDebugInfo; private: bool cacheSource(const DILineInfo& LineInfoFile); public: SourcePrinter() = default; - SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) { + SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) + : Obj(Obj), WarnedNoDebugInfo(false) { symbolize::LLVMSymbolizer::Options SymbolizerOpts; SymbolizerOpts.PrintFunctions = DILineInfoSpecifier::FunctionNameKind::None; SymbolizerOpts.Demangle = false; @@ -577,8 +578,11 @@ Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source); } else { auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName); - if (!BufferOrError) + if (!BufferOrError) { + if (MissingSources.insert(LineInfo.FileName).second) + warn("failed to find source " + LineInfo.FileName); return false; + } Buffer = std::move(*BufferOrError); } // Chomp the file to get lines @@ -610,7 +614,15 @@ else LineInfo = *ExpectedLineInfo; - if ((LineInfo.FileName == "") || LineInfo.Line == 0 || + if (LineInfo.FileName == DILineInfo::BadString) { + if(!WarnedNoDebugInfo) { + warn("failed to parse debug info"); + WarnedNoDebugInfo = true; + } + return; + } + + if (LineInfo.Line == 0 || ((OldLineInfo.Line == LineInfo.Line) && (OldLineInfo.FileName == LineInfo.FileName))) return; @@ -623,8 +635,12 @@ return; auto LineBuffer = LineCache.find(LineInfo.FileName); if (LineBuffer != LineCache.end()) { - if (LineInfo.Line > LineBuffer->second.size()) + if (LineInfo.Line > LineBuffer->second.size()) { + warn(formatv( + "debug info line number {0} exceeds the number of lines in {1}", + LineInfo.Line, LineInfo.FileName)); return; + } // Vector begins at 0, line numbers are non-zero OS << Delimiter << LineBuffer->second[LineInfo.Line - 1] << '\n'; } diff --git a/llvm/tools/llvm-xray/func-id-helper.cpp b/llvm/tools/llvm-xray/func-id-helper.cpp --- a/llvm/tools/llvm-xray/func-id-helper.cpp +++ b/llvm/tools/llvm-xray/func-id-helper.cpp @@ -36,7 +36,7 @@ ModuleAddress.SectionIndex = object::SectionedAddress::UndefSection; if (auto ResOrErr = Symbolizer.symbolizeCode(BinaryInstrMap, ModuleAddress)) { auto &DI = *ResOrErr; - if (DI.FunctionName == "") + if (DI.FunctionName == DILineInfo::BadString) F << "@(" << std::hex << It->second << ")"; else F << DI.FunctionName;