Teach the symbolizer lib symbolize objects directly.

Currently, the symbolizer lib can only symbolize a file on disk.
This patch teaches the symbolizer lib to symbolize objects.
llvm-objdump needs this to support archive disassembly with source info.

https://bugs.llvm.org/show_bug.cgi?id=41871

Reviewed by: jhenderson, grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D63521

llvm-svn: 365376
This commit is contained in:
Yuanfang Chen 2019-07-08 19:28:57 +00:00
parent 611c122045
commit 5de4692cc7
6 changed files with 113 additions and 26 deletions

View File

@ -52,6 +52,8 @@ public:
flush();
}
Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
object::SectionedAddress ModuleOffset);
Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
object::SectionedAddress ModuleOffset);
Expected<DIInliningInfo>
@ -71,7 +73,11 @@ public:
private:
// Bundles together object file with code/data and object file with
// corresponding debug info. These objects can be the same.
using ObjectPair = std::pair<ObjectFile *, ObjectFile *>;
using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
Expected<DILineInfo>
symbolizeCodeCommon(SymbolizableModule *Info,
object::SectionedAddress ModuleOffset);
/// Returns a SymbolizableModule or an error if loading debug info failed.
/// Only one attempt is made to load a module, and errors during loading are
@ -80,6 +86,11 @@ private:
Expected<SymbolizableModule *>
getOrCreateModuleInfo(const std::string &ModuleName);
Expected<SymbolizableModule *>
createModuleInfo(const ObjectFile *Obj,
std::unique_ptr<DIContext> Context,
StringRef ModuleName);
ObjectFile *lookUpDsymFile(const std::string &Path,
const MachOObjectFile *ExeObj,
const std::string &ArchName);

View File

@ -42,7 +42,7 @@ getDILineInfoSpecifier(FunctionNameKind FNKind) {
}
ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
SymbolizableObjectFile::create(object::ObjectFile *Obj,
SymbolizableObjectFile::create(const object::ObjectFile *Obj,
std::unique_ptr<DIContext> DICtx) {
assert(DICtx);
std::unique_ptr<SymbolizableObjectFile> res(
@ -102,7 +102,7 @@ SymbolizableObjectFile::create(object::ObjectFile *Obj,
return std::move(res);
}
SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj,
SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj,
std::unique_ptr<DIContext> DICtx)
: Module(Obj), DebugInfoContext(std::move(DICtx)) {}

View File

@ -31,7 +31,7 @@ namespace symbolize {
class SymbolizableObjectFile : public SymbolizableModule {
public:
static ErrorOr<std::unique_ptr<SymbolizableObjectFile>>
create(object::ObjectFile *Obj, std::unique_ptr<DIContext> DICtx);
create(const object::ObjectFile *Obj, std::unique_ptr<DIContext> DICtx);
DILineInfo symbolizeCode(object::SectionedAddress ModuleOffset,
FunctionNameKind FNKind,
@ -68,7 +68,7 @@ private:
/// Search for the first occurence of specified Address in ObjectFile.
uint64_t getModuleSectionIndexForAddress(uint64_t Address) const;
object::ObjectFile *Module;
const object::ObjectFile *Module;
std::unique_ptr<DIContext> DebugInfoContext;
struct SymbolDesc {
@ -84,7 +84,7 @@ private:
std::vector<std::pair<SymbolDesc, StringRef>> Functions;
std::vector<std::pair<SymbolDesc, StringRef>> Objects;
SymbolizableObjectFile(object::ObjectFile *Obj,
SymbolizableObjectFile(const object::ObjectFile *Obj,
std::unique_ptr<DIContext> DICtx);
};

View File

@ -52,14 +52,8 @@ namespace llvm {
namespace symbolize {
Expected<DILineInfo>
LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
object::SectionedAddress ModuleOffset) {
SymbolizableModule *Info;
if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName))
Info = InfoOrErr.get();
else
return InfoOrErr.takeError();
LLVMSymbolizer::symbolizeCodeCommon(SymbolizableModule *Info,
object::SectionedAddress ModuleOffset) {
// A null module means an error has already been reported. Return an empty
// result.
if (!Info)
@ -77,6 +71,32 @@ LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
return LineInfo;
}
Expected<DILineInfo>
LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj,
object::SectionedAddress ModuleOffset) {
StringRef ModuleName = Obj.getFileName();
auto I = Modules.find(ModuleName);
if (I != Modules.end())
return symbolizeCodeCommon(I->second.get(), ModuleOffset);
std::unique_ptr<DIContext> Context =
DWARFContext::create(Obj, nullptr, DWARFContext::defaultErrorHandler);
Expected<SymbolizableModule *> InfoOrErr =
createModuleInfo(&Obj, std::move(Context), ModuleName);
if (!InfoOrErr)
return InfoOrErr.takeError();
return symbolizeCodeCommon(*InfoOrErr, ModuleOffset);
}
Expected<DILineInfo>
LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
object::SectionedAddress ModuleOffset) {
Expected<SymbolizableModule *> InfoOrErr = getOrCreateModuleInfo(ModuleName);
if (!InfoOrErr)
return InfoOrErr.takeError();
return symbolizeCodeCommon(*InfoOrErr, ModuleOffset);
}
Expected<DIInliningInfo>
LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName,
object::SectionedAddress ModuleOffset) {
@ -394,6 +414,23 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path,
return errorCodeToError(object_error::arch_not_found);
}
Expected<SymbolizableModule *>
LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj,
std::unique_ptr<DIContext> Context,
StringRef ModuleName) {
auto InfoOrErr =
SymbolizableObjectFile::create(Obj, std::move(Context));
std::unique_ptr<SymbolizableModule> SymMod;
if (InfoOrErr)
SymMod = std::move(*InfoOrErr);
auto InsertResult =
Modules.insert(std::make_pair(ModuleName, std::move(SymMod)));
assert(InsertResult.second);
if (std::error_code EC = InfoOrErr.getError())
return errorCodeToError(EC);
return InsertResult.first->second.get();
}
Expected<SymbolizableModule *>
LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
auto I = Modules.find(ModuleName);
@ -442,16 +479,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Context =
DWARFContext::create(*Objects.second, nullptr,
DWARFContext::defaultErrorHandler, Opts.DWPName);
auto InfoOrErr =
SymbolizableObjectFile::create(Objects.first, std::move(Context));
std::unique_ptr<SymbolizableModule> SymMod;
if (InfoOrErr)
SymMod = std::move(InfoOrErr.get());
auto InsertResult = Modules.emplace(ModuleName, std::move(SymMod));
assert(InsertResult.second);
if (auto EC = InfoOrErr.getError())
return errorCodeToError(EC);
return InsertResult.first->second.get();
return createModuleInfo(Objects.first, std::move(Context), ModuleName);
}
namespace {

View File

@ -0,0 +1,49 @@
; This test checks that 'llvm-objdump --source' works with archive files.
; The reason this test is written in .ll is that showing source code
; requires the debug data to have the source file path. Since we create
; the source file ad-hoc in this test, we don't know the path beforehand to
; create the object and archive.
; RUN: rm -rf %t && mkdir -p %t/subdir && cd %t
; RUN: echo -e "int foo(int a)\n\n{ return a+1; }" > subdir/a.c
; RUN: sed -e "s,DIRNAME,%/t/subdir," %s | llc --filetype=obj -mtriple=x86_64-pc-linux -o a.o
; RUN: llvm-ar rc a.a a.o
; RUN: rm a.o
; RUN: llvm-objdump --source a.a | FileCheck %s
; CHECK: { return a+1; }
define i32 @foo(i32 %a) #0 !dbg !8 {
entry:
%a.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
call void @llvm.dbg.declare(metadata i32* %a.addr, metadata !12, metadata !DIExpression()), !dbg !13
%0 = load i32, i32* %a.addr, align 4, !dbg !14
%add = add nsw i32 %0, 1, !dbg !14
ret i32 %add, !dbg !14
}
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone speculatable }
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5, !6}
!llvm.ident = !{!7}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
!1 = !DIFile(filename: "a.c", directory: "DIRNAME")
!2 = !{}
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 2}
!6 = !{i32 7, !"PIC Level", i32 2}
!7 = !{!"clang version 9.0.0"}
!8 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !9, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!9 = !DISubroutineType(types: !10)
!10 = !{!11, !11}
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !DILocalVariable(name: "a", arg: 1, scope: !8, file: !1, line: 1, type: !11)
!13 = !DILocation(line: 1, scope: !8)
!14 = !DILocation(line: 3, scope: !8)

View File

@ -600,8 +600,7 @@ void SourcePrinter::printSourceLine(raw_ostream &OS,
return;
DILineInfo LineInfo = DILineInfo();
auto ExpectedLineInfo =
Symbolizer->symbolizeCode(Obj->getFileName(), Address);
auto ExpectedLineInfo = Symbolizer->symbolizeCode(*Obj, Address);
if (!ExpectedLineInfo)
consumeError(ExpectedLineInfo.takeError());
else