2012-10-17 07:46:21 +08:00
|
|
|
//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
|
2011-09-14 03:42:23 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program is a utility that works like "dwarfdump".
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-04-24 01:37:47 +08:00
|
|
|
#include "llvm/DebugInfo/DIContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
2015-08-03 08:10:31 +08:00
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2012-11-08 07:22:07 +08:00
|
|
|
#include "llvm/Object/RelocVisitor.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-12-24 05:51:13 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
[dwarfdump] Pretty print location expressions and location lists
Summary:
Based on Fred's patch here: https://reviews.llvm.org/D6771
I can't seem to commandeer the old review, so I'm creating a new one.
With that change the locations exrpessions are pretty printed inline in the
DIE tree. The output looks like this for debug_loc entries:
DW_AT_location [DW_FORM_data4] (0x00000000
0x0000000000000001 - 0x000000000000000b: DW_OP_consts +3
0x000000000000000b - 0x0000000000000012: DW_OP_consts +7
0x0000000000000012 - 0x000000000000001b: DW_OP_reg0 RAX, DW_OP_piece 0x4
0x000000000000001b - 0x0000000000000024: DW_OP_breg5 RDI+0)
And like this for debug_loc.dwo entries:
DW_AT_location [DW_FORM_sec_offset] (0x00000000
Addr idx 2 (w/ length 190): DW_OP_consts +0, DW_OP_stack_value
Addr idx 3 (w/ length 23): DW_OP_reg0 RAX, DW_OP_piece 0x4)
Simple locations without ranges are printed inline:
DW_AT_location [DW_FORM_block1] (DW_OP_reg4 RSI, DW_OP_piece 0x4, DW_OP_bit_piece 0x20 0x0)
The debug_loc(.dwo) dumping in changed accordingly to factor the code.
Reviewers: dblaikie, aprantl, friss
Subscribers: mgorny, javed.absar, hiraditya, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D37123
llvm-svn: 312042
2017-08-30 05:41:21 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2011-09-14 03:42:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2012-11-08 07:22:07 +08:00
|
|
|
#include <string>
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2012-11-08 07:22:07 +08:00
|
|
|
|
2011-09-14 03:42:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2017-09-13 06:32:53 +08:00
|
|
|
namespace {
|
|
|
|
using namespace llvm::cl;
|
|
|
|
|
|
|
|
OptionCategory DwarfDumpCategory("Specific Options");
|
|
|
|
static opt<bool> Help("h", desc("Alias for -help"), Hidden,
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
static list<std::string>
|
|
|
|
InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
|
|
|
|
ZeroOrMore, cat(DwarfDumpCategory));
|
|
|
|
|
|
|
|
cl::OptionCategory
|
|
|
|
SectionCategory("Section-specific Dump Options",
|
|
|
|
"These control which sections are dumped.");
|
|
|
|
static opt<bool> DumpAll("all", desc("Dump all debug info sections"),
|
|
|
|
cat(SectionCategory));
|
|
|
|
static alias DumpAllAlias("a", desc("Alias for -all"), aliasopt(DumpAll));
|
2017-09-12 06:59:45 +08:00
|
|
|
|
2017-09-14 06:09:01 +08:00
|
|
|
static unsigned DumpType = DIDT_Null;
|
2017-09-12 06:59:45 +08:00
|
|
|
#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
|
2017-09-13 06:32:53 +08:00
|
|
|
static opt<bool> Dump##ENUM_NAME(CMDLINE_NAME, \
|
|
|
|
desc("Dump the " ELF_NAME " section"), \
|
|
|
|
cat(SectionCategory));
|
2017-09-12 06:59:45 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.def"
|
|
|
|
#undef HANDLE_DWARF_SECTION
|
2017-09-14 02:22:59 +08:00
|
|
|
static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"),
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID));
|
2013-01-26 04:26:43 +08:00
|
|
|
|
2017-09-13 06:32:53 +08:00
|
|
|
static opt<bool>
|
2016-10-19 05:09:48 +08:00
|
|
|
SummarizeTypes("summarize-types",
|
2017-09-13 06:32:53 +08:00
|
|
|
desc("Abbreviate the description of type unit entries"));
|
|
|
|
static opt<bool> Verify("verify", desc("Verify the DWARF debug info"),
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
static opt<bool> Verbose("verbose",
|
|
|
|
desc("Print more low-level encoding details"),
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
static alias VerboseAlias("v", desc("Alias for -verbose"), aliasopt(Verbose),
|
|
|
|
cat(DwarfDumpCategory));
|
|
|
|
} // namespace
|
2017-06-07 07:28:45 +08:00
|
|
|
|
2015-07-26 13:35:59 +08:00
|
|
|
static void error(StringRef Filename, std::error_code EC) {
|
2015-06-26 07:40:15 +08:00
|
|
|
if (!EC)
|
2015-07-26 13:35:59 +08:00
|
|
|
return;
|
2015-06-26 07:40:15 +08:00
|
|
|
errs() << Filename << ": " << EC.message() << "\n";
|
2015-07-26 13:35:59 +08:00
|
|
|
exit(1);
|
2015-06-26 07:40:15 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:07:24 +08:00
|
|
|
static DIDumpOptions getDumpOpts() {
|
2017-09-13 17:43:05 +08:00
|
|
|
DIDumpOptions DumpOpts;
|
|
|
|
DumpOpts.DumpType = DumpType;
|
|
|
|
DumpOpts.SummarizeTypes = SummarizeTypes;
|
|
|
|
DumpOpts.Verbose = Verbose;
|
|
|
|
return DumpOpts;
|
|
|
|
}
|
|
|
|
|
2017-09-14 07:07:24 +08:00
|
|
|
static bool dumpObjectFile(ObjectFile &Obj, Twine Filename) {
|
[dwarfdump] Pretty print location expressions and location lists
Summary:
Based on Fred's patch here: https://reviews.llvm.org/D6771
I can't seem to commandeer the old review, so I'm creating a new one.
With that change the locations exrpessions are pretty printed inline in the
DIE tree. The output looks like this for debug_loc entries:
DW_AT_location [DW_FORM_data4] (0x00000000
0x0000000000000001 - 0x000000000000000b: DW_OP_consts +3
0x000000000000000b - 0x0000000000000012: DW_OP_consts +7
0x0000000000000012 - 0x000000000000001b: DW_OP_reg0 RAX, DW_OP_piece 0x4
0x000000000000001b - 0x0000000000000024: DW_OP_breg5 RDI+0)
And like this for debug_loc.dwo entries:
DW_AT_location [DW_FORM_sec_offset] (0x00000000
Addr idx 2 (w/ length 190): DW_OP_consts +0, DW_OP_stack_value
Addr idx 3 (w/ length 23): DW_OP_reg0 RAX, DW_OP_piece 0x4)
Simple locations without ranges are printed inline:
DW_AT_location [DW_FORM_block1] (DW_OP_reg4 RSI, DW_OP_piece 0x4, DW_OP_bit_piece 0x20 0x0)
The debug_loc(.dwo) dumping in changed accordingly to factor the code.
Reviewers: dblaikie, aprantl, friss
Subscribers: mgorny, javed.absar, hiraditya, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D37123
llvm-svn: 312042
2017-08-30 05:41:21 +08:00
|
|
|
std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
|
|
|
|
logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(),
|
|
|
|
Filename.str() + ": ");
|
2017-09-14 02:22:59 +08:00
|
|
|
// The UUID dump already contains all the same information.
|
|
|
|
if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
|
2017-09-14 06:09:01 +08:00
|
|
|
outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
|
2017-06-13 03:04:28 +08:00
|
|
|
|
2015-08-03 08:10:25 +08:00
|
|
|
// Dump the complete DWARF structure.
|
2017-09-14 07:07:24 +08:00
|
|
|
DICtx->dump(outs(), getDumpOpts());
|
|
|
|
return true;
|
2015-08-03 08:10:25 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:07:24 +08:00
|
|
|
static bool verifyObjectFile(ObjectFile &Obj, Twine Filename) {
|
2017-07-20 06:27:28 +08:00
|
|
|
std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj);
|
|
|
|
|
2017-05-02 06:07:02 +08:00
|
|
|
// Verify the DWARF and exit with non-zero exit status if verification
|
|
|
|
// fails.
|
|
|
|
raw_ostream &stream = Quiet ? nulls() : outs();
|
|
|
|
stream << "Verifying " << Filename.str() << ":\tfile format "
|
|
|
|
<< Obj.getFileFormatName() << "\n";
|
2017-09-14 07:07:24 +08:00
|
|
|
bool Result = DICtx->verify(stream, DumpType, getDumpOpts());
|
2017-05-02 06:07:02 +08:00
|
|
|
if (Result)
|
|
|
|
stream << "No errors.\n";
|
|
|
|
else
|
|
|
|
stream << "Errors detected.\n";
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-14 07:07:24 +08:00
|
|
|
static bool handleBuffer(StringRef Filename, MemoryBuffer &Buffer,
|
|
|
|
std::function<bool(ObjectFile &, Twine)> HandleObj) {
|
2017-05-02 06:07:02 +08:00
|
|
|
Expected<std::unique_ptr<Binary>> BinOrErr =
|
2017-09-14 07:07:24 +08:00
|
|
|
object::createBinary(Buffer.getMemBufferRef());
|
2017-05-02 06:07:02 +08:00
|
|
|
if (!BinOrErr)
|
|
|
|
error(Filename, errorToErrorCode(BinOrErr.takeError()));
|
2017-09-01 00:44:47 +08:00
|
|
|
|
2017-05-02 06:07:02 +08:00
|
|
|
bool Result = true;
|
|
|
|
if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
|
2017-09-14 07:07:24 +08:00
|
|
|
Result = HandleObj(*Obj, Filename);
|
2017-05-02 06:07:02 +08:00
|
|
|
else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
|
|
|
|
for (auto &ObjForArch : Fat->objects()) {
|
|
|
|
auto MachOOrErr = ObjForArch.getAsObjectFile();
|
|
|
|
error(Filename, errorToErrorCode(MachOOrErr.takeError()));
|
2017-09-14 07:07:24 +08:00
|
|
|
if (!HandleObj(**MachOOrErr,
|
|
|
|
Filename + " (" + ObjForArch.getArchFlagName() + ")"))
|
2017-05-02 06:07:02 +08:00
|
|
|
Result = false;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2017-09-14 07:07:24 +08:00
|
|
|
static bool handleFile(StringRef Filename,
|
|
|
|
std::function<bool(ObjectFile &, Twine)> HandleObj) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
error(Filename, BuffOrErr.getError());
|
|
|
|
std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
|
|
|
|
return handleBuffer(Filename, *Buffer, HandleObj);
|
|
|
|
}
|
|
|
|
|
2015-12-24 05:51:13 +08:00
|
|
|
/// If the input path is a .dSYM bundle (as created by the dsymutil tool),
|
|
|
|
/// replace it with individual entries for each of the object files inside the
|
|
|
|
/// bundle otherwise return the input path.
|
2016-06-09 03:09:22 +08:00
|
|
|
static std::vector<std::string> expandBundle(const std::string &InputPath) {
|
2015-12-24 05:51:13 +08:00
|
|
|
std::vector<std::string> BundlePaths;
|
|
|
|
SmallString<256> BundlePath(InputPath);
|
|
|
|
// Manually open up the bundle to avoid introducing additional dependencies.
|
|
|
|
if (sys::fs::is_directory(BundlePath) &&
|
|
|
|
sys::path::extension(BundlePath) == ".dSYM") {
|
|
|
|
std::error_code EC;
|
|
|
|
sys::path::append(BundlePath, "Contents", "Resources", "DWARF");
|
|
|
|
for (sys::fs::directory_iterator Dir(BundlePath, EC), DirEnd;
|
|
|
|
Dir != DirEnd && !EC; Dir.increment(EC)) {
|
|
|
|
const std::string &Path = Dir->path();
|
|
|
|
sys::fs::file_status Status;
|
|
|
|
EC = sys::fs::status(Path, Status);
|
|
|
|
error(Path, EC);
|
|
|
|
switch (Status.type()) {
|
|
|
|
case sys::fs::file_type::regular_file:
|
|
|
|
case sys::fs::file_type::symlink_file:
|
|
|
|
case sys::fs::file_type::type_unknown:
|
|
|
|
BundlePaths.push_back(Path);
|
|
|
|
break;
|
|
|
|
default: /*ignore*/;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error(BundlePath, EC);
|
|
|
|
}
|
|
|
|
if (!BundlePaths.size())
|
|
|
|
BundlePaths.push_back(InputPath);
|
|
|
|
return BundlePaths;
|
|
|
|
}
|
|
|
|
|
2011-09-14 03:42:23 +08:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2011-09-14 03:42:23 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
[dwarfdump] Pretty print location expressions and location lists
Summary:
Based on Fred's patch here: https://reviews.llvm.org/D6771
I can't seem to commandeer the old review, so I'm creating a new one.
With that change the locations exrpessions are pretty printed inline in the
DIE tree. The output looks like this for debug_loc entries:
DW_AT_location [DW_FORM_data4] (0x00000000
0x0000000000000001 - 0x000000000000000b: DW_OP_consts +3
0x000000000000000b - 0x0000000000000012: DW_OP_consts +7
0x0000000000000012 - 0x000000000000001b: DW_OP_reg0 RAX, DW_OP_piece 0x4
0x000000000000001b - 0x0000000000000024: DW_OP_breg5 RDI+0)
And like this for debug_loc.dwo entries:
DW_AT_location [DW_FORM_sec_offset] (0x00000000
Addr idx 2 (w/ length 190): DW_OP_consts +0, DW_OP_stack_value
Addr idx 3 (w/ length 23): DW_OP_reg0 RAX, DW_OP_piece 0x4)
Simple locations without ranges are printed inline:
DW_AT_location [DW_FORM_block1] (DW_OP_reg4 RSI, DW_OP_piece 0x4, DW_OP_bit_piece 0x20 0x0)
The debug_loc(.dwo) dumping in changed accordingly to factor the code.
Reviewers: dblaikie, aprantl, friss
Subscribers: mgorny, javed.absar, hiraditya, llvm-commits, JDevlieghere
Differential Revision: https://reviews.llvm.org/D37123
llvm-svn: 312042
2017-08-30 05:41:21 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
|
2017-09-13 06:32:53 +08:00
|
|
|
HideUnrelatedOptions({&DwarfDumpCategory, &SectionCategory});
|
|
|
|
cl::ParseCommandLineOptions(
|
|
|
|
argc, argv,
|
|
|
|
"pretty-print DWARF debug information in object files"
|
|
|
|
" and debug info archives.\n");
|
|
|
|
|
|
|
|
if (Help) {
|
|
|
|
PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2017-09-01 00:44:47 +08:00
|
|
|
// Defaults to dumping all sections, unless brief mode is specified in which
|
|
|
|
// case only the .debug_info section in dumped.
|
2017-09-12 06:59:45 +08:00
|
|
|
#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
|
|
|
|
if (Dump##ENUM_NAME) \
|
|
|
|
DumpType |= DIDT_##ENUM_NAME;
|
|
|
|
#include "llvm/BinaryFormat/Dwarf.def"
|
|
|
|
#undef HANDLE_DWARF_SECTION
|
2017-09-14 02:22:59 +08:00
|
|
|
if (DumpUUID)
|
|
|
|
DumpType |= DIDT_UUID;
|
2017-09-12 06:59:45 +08:00
|
|
|
if (DumpAll)
|
|
|
|
DumpType = DIDT_All;
|
2017-09-01 00:44:47 +08:00
|
|
|
if (DumpType == DIDT_Null) {
|
2017-09-12 07:05:20 +08:00
|
|
|
if (Verbose)
|
2017-09-01 00:44:47 +08:00
|
|
|
DumpType = DIDT_All;
|
2017-09-12 07:05:20 +08:00
|
|
|
else
|
|
|
|
DumpType = DIDT_DebugInfo;
|
2017-09-01 00:44:47 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 03:42:23 +08:00
|
|
|
// Defaults to a.out if no filenames specified.
|
|
|
|
if (InputFilenames.size() == 0)
|
|
|
|
InputFilenames.push_back("a.out");
|
|
|
|
|
2015-12-24 05:51:13 +08:00
|
|
|
// Expand any .dSYM bundles to the individual object files contained therein.
|
|
|
|
std::vector<std::string> Objects;
|
2016-05-27 20:30:51 +08:00
|
|
|
for (const auto &F : InputFilenames) {
|
2015-12-24 05:51:13 +08:00
|
|
|
auto Objs = expandBundle(F);
|
|
|
|
Objects.insert(Objects.end(), Objs.begin(), Objs.end());
|
|
|
|
}
|
|
|
|
|
2017-05-02 06:07:02 +08:00
|
|
|
if (Verify) {
|
|
|
|
// If we encountered errors during verify, exit with a non-zero exit status.
|
2017-09-14 07:07:24 +08:00
|
|
|
if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) {
|
|
|
|
return handleFile(Object, verifyObjectFile);
|
|
|
|
}))
|
2017-05-02 06:07:02 +08:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2017-09-14 07:07:24 +08:00
|
|
|
std::for_each(Objects.begin(), Objects.end(), [](std::string Object) {
|
|
|
|
handleFile(Object, dumpObjectFile);
|
|
|
|
});
|
2017-05-02 06:07:02 +08:00
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2015-07-26 13:35:59 +08:00
|
|
|
return EXIT_SUCCESS;
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|