blob: 309bc4ecd4658f31a2b21cc96481713af72261d1 [file] [log] [blame]
Eric Christopherd7169e92012-10-16 23:46:21 +00001//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===//
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like "dwarfdump".
11//
12//===----------------------------------------------------------------------===//
13
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"
29#include <algorithm>
30#include <cstring>
31using namespace llvm;
32using namespace object;
33
34static cl::list<std::string>
35InputFilenames(cl::Positional, cl::desc("<input object files>"),
36 cl::ZeroOrMore);
37
Benjamin Kramer6b3ae462011-09-15 21:17:40 +000038static cl::opt<unsigned long long>
39Address("address", cl::init(-1ULL),
40 cl::desc("Print line information for a given address"));
41
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +000042static cl::opt<bool>
43PrintFunctions("functions", cl::init(false),
44 cl::desc("Print function names as well as line information "
45 "for a given address"));
46
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000047static cl::opt<bool>
48PrintInlining("inlining", cl::init(false),
49 cl::desc("Print all inlined frames for a given address"));
50
51static void PrintDILineInfo(DILineInfo dli) {
52 if (PrintFunctions)
53 outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>")
54 << "\n";
55 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
56 << dli.getLine() << ':' << dli.getColumn() << '\n';
57}
58
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000059static void DumpInput(const StringRef &Filename) {
60 OwningPtr<MemoryBuffer> Buff;
61
62 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
63 errs() << Filename << ": " << ec.message() << "\n";
64 return;
65 }
66
67 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
68
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000069 StringRef DebugInfoSection;
70 StringRef DebugAbbrevSection;
71 StringRef DebugLineSection;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000072 StringRef DebugArangesSection;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000073 StringRef DebugStringSection;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000074 StringRef DebugRangesSection;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000075
76 error_code ec;
Michael J. Spencer20990392011-10-07 19:52:41 +000077 for (section_iterator i = Obj->begin_sections(),
78 e = Obj->end_sections();
79 i != e; i.increment(ec)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000080 StringRef name;
81 i->getName(name);
82 StringRef data;
83 i->getContents(data);
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000084
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")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000089 DebugInfoSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000090 else if (name == "debug_abbrev")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000091 DebugAbbrevSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000092 else if (name == "debug_line")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000093 DebugLineSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000094 else if (name == "debug_aranges")
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000095 DebugArangesSection = data;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000096 else if (name == "debug_str")
97 DebugStringSection = data;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000098 else if (name == "debug_ranges")
99 DebugRangesSection = data;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000100 }
101
102 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
103 DebugInfoSection,
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000104 DebugAbbrevSection,
Benjamin Kramerb848e972011-09-15 02:12:05 +0000105 DebugArangesSection,
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000106 DebugLineSection,
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000107 DebugStringSection,
108 DebugRangesSection));
Benjamin Kramer6b3ae462011-09-15 21:17:40 +0000109 if (Address == -1ULL) {
110 outs() << Filename
111 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
112 // Dump the complete DWARF structure.
113 dictx->dump(outs());
114 } else {
115 // Print line info for the specified address.
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000116 int SpecFlags = DILineInfoSpecifier::FileLineInfo |
117 DILineInfoSpecifier::AbsoluteFilePath;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000118 if (PrintFunctions)
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000119 SpecFlags |= DILineInfoSpecifier::FunctionName;
120 if (PrintInlining) {
Eric Christopherd7169e92012-10-16 23:46:21 +0000121 DIInliningInfo InliningInfo =
122 dictx->getInliningInfoForAddress(Address, SpecFlags);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000123 uint32_t n = InliningInfo.getNumberOfFrames();
124 if (n == 0) {
125 // Print one empty debug line info in any case.
126 PrintDILineInfo(DILineInfo());
127 } else {
128 for (uint32_t i = 0; i < n; i++) {
129 DILineInfo dli = InliningInfo.getFrame(i);
130 PrintDILineInfo(dli);
131 }
132 }
133 } else {
134 DILineInfo dli = dictx->getLineInfoForAddress(Address, SpecFlags);
135 PrintDILineInfo(dli);
136 }
Benjamin Kramer6b3ae462011-09-15 21:17:40 +0000137 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000138}
139
140int 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.
145
146 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
147
148 // Defaults to a.out if no filenames specified.
149 if (InputFilenames.size() == 0)
150 InputFilenames.push_back("a.out");
151
152 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
153
154 return 0;
155}