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"
|
|
|
|
#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;
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
2015-12-24 05:51:13 +08:00
|
|
|
InputFilenames(cl::Positional, cl::desc("<input object files or .dSYM bundles>"),
|
2011-09-14 03:42:23 +08:00
|
|
|
cl::ZeroOrMore);
|
|
|
|
|
2015-11-12 03:30:47 +08:00
|
|
|
static cl::opt<DIDumpType> DumpType(
|
|
|
|
"debug-dump", cl::init(DIDT_All), cl::desc("Dump of debug sections:"),
|
|
|
|
cl::values(
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_All, "all", "Dump all debug sections"),
|
|
|
|
clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"),
|
|
|
|
clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"),
|
2014-11-15 00:15:53 +08:00
|
|
|
clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"),
|
|
|
|
clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"),
|
2015-11-12 03:30:47 +08:00
|
|
|
clEnumValN(DIDT_AppleNamespaces, "apple_namespaces",
|
|
|
|
".apple_namespaces"),
|
2014-11-15 00:15:53 +08:00
|
|
|
clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"),
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
|
|
|
|
clEnumValN(DIDT_Info, "info", ".debug_info"),
|
|
|
|
clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
|
2013-11-19 08:29:42 +08:00
|
|
|
clEnumValN(DIDT_Types, "types", ".debug_types"),
|
2014-01-09 13:08:24 +08:00
|
|
|
clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"),
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_Line, "line", ".debug_line"),
|
2014-02-25 07:58:54 +08:00
|
|
|
clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"),
|
2013-06-20 05:37:13 +08:00
|
|
|
clEnumValN(DIDT_Loc, "loc", ".debug_loc"),
|
2014-03-25 09:44:02 +08:00
|
|
|
clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"),
|
2013-02-06 07:30:58 +08:00
|
|
|
clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
|
2015-11-12 17:38:54 +08:00
|
|
|
clEnumValN(DIDT_Macro, "macro", ".debug_macinfo"),
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
|
2013-02-13 00:20:28 +08:00
|
|
|
clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
|
2013-09-26 07:02:41 +08:00
|
|
|
clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"),
|
2013-09-26 07:02:44 +08:00
|
|
|
clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"),
|
|
|
|
clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"),
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_Str, "str", ".debug_str"),
|
2017-06-06 09:22:34 +08:00
|
|
|
clEnumValN(DIDT_StrOffsets, "str_offsets", ".debug_str_offsets"),
|
2013-01-26 04:26:43 +08:00
|
|
|
clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
|
2015-11-12 03:30:47 +08:00
|
|
|
clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo",
|
|
|
|
".debug_str_offsets.dwo"),
|
2015-11-12 08:42:49 +08:00
|
|
|
clEnumValN(DIDT_CUIndex, "cu_index", ".debug_cu_index"),
|
2016-09-23 19:01:53 +08:00
|
|
|
clEnumValN(DIDT_GdbIndex, "gdb_index", ".gdb_index"),
|
2016-10-09 03:41:06 +08:00
|
|
|
clEnumValN(DIDT_TUIndex, "tu_index", ".debug_tu_index")));
|
2013-01-26 04:26:43 +08:00
|
|
|
|
2016-10-19 05:09:48 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
SummarizeTypes("summarize-types",
|
|
|
|
cl::desc("Abbreviate the description of type unit entries"));
|
|
|
|
|
2017-05-02 06:07:02 +08:00
|
|
|
static cl::opt<bool> Verify("verify", cl::desc("Verify the DWARF debug info"));
|
|
|
|
|
|
|
|
static cl::opt<bool> Quiet("quiet",
|
|
|
|
cl::desc("Use with -verify to not emit to STDOUT."));
|
|
|
|
|
2017-06-07 07:28:45 +08:00
|
|
|
static cl::opt<bool> Brief("brief", cl::desc("Print fewer low-level details"));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-03 08:10:31 +08:00
|
|
|
static void DumpObjectFile(ObjectFile &Obj, Twine Filename) {
|
2015-08-03 08:10:25 +08:00
|
|
|
std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj));
|
|
|
|
|
2015-08-03 08:10:31 +08:00
|
|
|
outs() << Filename.str() << ":\tfile format " << Obj.getFileFormatName()
|
|
|
|
<< "\n\n";
|
2017-06-02 02:18:23 +08:00
|
|
|
|
2017-06-13 03:04:28 +08:00
|
|
|
|
2015-08-03 08:10:25 +08:00
|
|
|
// Dump the complete DWARF structure.
|
2017-06-02 02:18:23 +08:00
|
|
|
DIDumpOptions DumpOpts;
|
|
|
|
DumpOpts.DumpType = DumpType;
|
|
|
|
DumpOpts.SummarizeTypes = SummarizeTypes;
|
2017-06-07 07:28:45 +08:00
|
|
|
DumpOpts.Brief = Brief;
|
2017-06-02 02:18:23 +08:00
|
|
|
DICtx->dump(outs(), DumpOpts);
|
2015-08-03 08:10:25 +08:00
|
|
|
}
|
|
|
|
|
2014-08-31 00:48:02 +08:00
|
|
|
static void DumpInput(StringRef Filename) {
|
2014-08-20 02:44:46 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
|
2014-07-07 01:43:13 +08:00
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
2015-07-26 13:35:59 +08:00
|
|
|
error(Filename, BuffOrErr.getError());
|
2014-08-20 02:44:46 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
|
2011-09-14 03:42:23 +08:00
|
|
|
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<Binary>> BinOrErr =
|
2015-08-03 08:10:31 +08:00
|
|
|
object::createBinary(Buff->getMemBufferRef());
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!BinOrErr)
|
|
|
|
error(Filename, errorToErrorCode(BinOrErr.takeError()));
|
2013-01-26 04:53:41 +08:00
|
|
|
|
2015-08-03 08:10:31 +08:00
|
|
|
if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
|
|
|
|
DumpObjectFile(*Obj, Filename);
|
|
|
|
else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
|
|
|
|
for (auto &ObjForArch : Fat->objects()) {
|
|
|
|
auto MachOOrErr = ObjForArch.getAsObjectFile();
|
2016-06-01 04:35:34 +08:00
|
|
|
error(Filename, errorToErrorCode(MachOOrErr.takeError()));
|
2015-08-03 08:10:31 +08:00
|
|
|
DumpObjectFile(**MachOOrErr,
|
2016-12-17 06:54:02 +08:00
|
|
|
Filename + " (" + ObjForArch.getArchFlagName() + ")");
|
2015-08-03 08:10:31 +08:00
|
|
|
}
|
2011-09-14 03:42:23 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 06:07:02 +08:00
|
|
|
static bool VerifyObjectFile(ObjectFile &Obj, Twine Filename) {
|
|
|
|
std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj));
|
|
|
|
|
|
|
|
// 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";
|
|
|
|
bool Result = DICtx->verify(stream, DumpType);
|
|
|
|
if (Result)
|
|
|
|
stream << "No errors.\n";
|
|
|
|
else
|
|
|
|
stream << "Errors detected.\n";
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool VerifyInput(StringRef Filename) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
error(Filename, BuffOrErr.getError());
|
|
|
|
std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<Binary>> BinOrErr =
|
|
|
|
object::createBinary(Buff->getMemBufferRef());
|
|
|
|
if (!BinOrErr)
|
|
|
|
error(Filename, errorToErrorCode(BinOrErr.takeError()));
|
|
|
|
|
|
|
|
bool Result = true;
|
|
|
|
if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
|
|
|
|
Result = VerifyObjectFile(*Obj, Filename);
|
|
|
|
else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
|
|
|
|
for (auto &ObjForArch : Fat->objects()) {
|
|
|
|
auto MachOOrErr = ObjForArch.getAsObjectFile();
|
|
|
|
error(Filename, errorToErrorCode(MachOOrErr.takeError()));
|
|
|
|
if (!VerifyObjectFile(**MachOOrErr, Filename + " (" + ObjForArch.getArchFlagName() + ")"))
|
|
|
|
Result = false;
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!std::all_of(Objects.begin(), Objects.end(), VerifyInput))
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
std::for_each(Objects.begin(), Objects.end(), DumpInput);
|
|
|
|
}
|
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
|
|
|
}
|