blob: 59ecafaa778891fa75cb38ef1fcc3f0515c338c1 [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm -----------===//
2//
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
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000047static void DumpInput(const StringRef &Filename) {
48 OwningPtr<MemoryBuffer> Buff;
49
50 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
51 errs() << Filename << ": " << ec.message() << "\n";
52 return;
53 }
54
55 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
56
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000057 StringRef DebugInfoSection;
58 StringRef DebugAbbrevSection;
59 StringRef DebugLineSection;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000060 StringRef DebugArangesSection;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000061 StringRef DebugStringSection;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000062 StringRef DebugRangesSection;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000063
64 error_code ec;
Michael J. Spencer20990392011-10-07 19:52:41 +000065 for (section_iterator i = Obj->begin_sections(),
66 e = Obj->end_sections();
67 i != e; i.increment(ec)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000068 StringRef name;
69 i->getName(name);
70 StringRef data;
71 i->getContents(data);
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000072
73 if (name.startswith("__DWARF,"))
74 name = name.substr(8); // Skip "__DWARF," prefix.
75 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
76 if (name == "debug_info")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000077 DebugInfoSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000078 else if (name == "debug_abbrev")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000079 DebugAbbrevSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000080 else if (name == "debug_line")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000081 DebugLineSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000082 else if (name == "debug_aranges")
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000083 DebugArangesSection = data;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000084 else if (name == "debug_str")
85 DebugStringSection = data;
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000086 else if (name == "debug_ranges")
87 DebugRangesSection = data;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000088 }
89
90 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
91 DebugInfoSection,
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000092 DebugAbbrevSection,
Benjamin Kramerb848e972011-09-15 02:12:05 +000093 DebugArangesSection,
Benjamin Kramer34f864f2011-09-15 16:57:13 +000094 DebugLineSection,
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000095 DebugStringSection,
96 DebugRangesSection));
Benjamin Kramer6b3ae462011-09-15 21:17:40 +000097 if (Address == -1ULL) {
98 outs() << Filename
99 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
100 // Dump the complete DWARF structure.
101 dictx->dump(outs());
102 } else {
103 // Print line info for the specified address.
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000104 int spec_flags = DILineInfoSpecifier::FileLineInfo |
105 DILineInfoSpecifier::AbsoluteFilePath;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000106 if (PrintFunctions)
107 spec_flags |= DILineInfoSpecifier::FunctionName;
108 DILineInfo dli = dictx->getLineInfoForAddress(Address, spec_flags);
109 if (PrintFunctions)
110 outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>")
111 << "\n";
Benjamin Kramer6b3ae462011-09-15 21:17:40 +0000112 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
113 << dli.getLine() << ':' << dli.getColumn() << '\n';
114 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000115}
116
117int main(int argc, char **argv) {
118 // Print a stack trace if we signal out.
119 sys::PrintStackTraceOnErrorSignal();
120 PrettyStackTraceProgram X(argc, argv);
121 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
122
123 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
124
125 // Defaults to a.out if no filenames specified.
126 if (InputFilenames.size() == 0)
127 InputFilenames.push_back("a.out");
128
129 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
130
131 return 0;
132}