1 //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program is a utility that works like "dwarfdump".
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/OwningPtr.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/DebugInfo/DIContext.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/MemoryObject.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/system_error.h"
32 using namespace object;
34 static cl::list<std::string>
35 InputFilenames(cl::Positional, cl::desc("<input object files>"),
38 static cl::opt<unsigned long long>
39 Address("address", cl::init(-1ULL),
40 cl::desc("Print line information for a given address"));
43 PrintFunctions("functions", cl::init(false),
44 cl::desc("Print function names as well as line information "
45 "for a given address"));
48 PrintInlining("inlining", cl::init(false),
49 cl::desc("Print all inlined frames for a given address"));
51 static void PrintDILineInfo(DILineInfo dli) {
53 outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>")
55 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
56 << dli.getLine() << ':' << dli.getColumn() << '\n';
59 static void DumpInput(const StringRef &Filename) {
60 OwningPtr<MemoryBuffer> Buff;
62 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
63 errs() << Filename << ": " << ec.message() << "\n";
67 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
69 StringRef DebugInfoSection;
70 StringRef DebugAbbrevSection;
71 StringRef DebugLineSection;
72 StringRef DebugArangesSection;
73 StringRef DebugStringSection;
74 StringRef DebugRangesSection;
77 for (section_iterator i = Obj->begin_sections(),
78 e = Obj->end_sections();
79 i != e; i.increment(ec)) {
85 if (name.startswith("__DWARF,"))
86 name = name.substr(8); // Skip "__DWARF," prefix.
87 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
88 if (name == "debug_info")
89 DebugInfoSection = data;
90 else if (name == "debug_abbrev")
91 DebugAbbrevSection = data;
92 else if (name == "debug_line")
93 DebugLineSection = data;
94 else if (name == "debug_aranges")
95 DebugArangesSection = data;
96 else if (name == "debug_str")
97 DebugStringSection = data;
98 else if (name == "debug_ranges")
99 DebugRangesSection = data;
102 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
108 DebugRangesSection));
109 if (Address == -1ULL) {
111 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
112 // Dump the complete DWARF structure.
115 // Print line info for the specified address.
116 int SpecFlags = DILineInfoSpecifier::FileLineInfo |
117 DILineInfoSpecifier::AbsoluteFilePath;
119 SpecFlags |= DILineInfoSpecifier::FunctionName;
121 DIInliningInfo InliningInfo =
122 dictx->getInliningInfoForAddress(Address, SpecFlags);
123 uint32_t n = InliningInfo.getNumberOfFrames();
125 // Print one empty debug line info in any case.
126 PrintDILineInfo(DILineInfo());
128 for (uint32_t i = 0; i < n; i++) {
129 DILineInfo dli = InliningInfo.getFrame(i);
130 PrintDILineInfo(dli);
134 DILineInfo dli = dictx->getLineInfoForAddress(Address, SpecFlags);
135 PrintDILineInfo(dli);
140 int main(int argc, char **argv) {
141 // Print a stack trace if we signal out.
142 sys::PrintStackTraceOnErrorSignal();
143 PrettyStackTraceProgram X(argc, argv);
144 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
146 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
148 // Defaults to a.out if no filenames specified.
149 if (InputFilenames.size() == 0)
150 InputFilenames.push_back("a.out");
152 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);