blob: ca0493de5ddbe5451a34222f6b262b3ca1e95ec5 [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
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000042static void DumpInput(const StringRef &Filename) {
43 OwningPtr<MemoryBuffer> Buff;
44
45 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
46 errs() << Filename << ": " << ec.message() << "\n";
47 return;
48 }
49
50 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
51
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000052 StringRef DebugInfoSection;
53 StringRef DebugAbbrevSection;
54 StringRef DebugLineSection;
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000055 StringRef DebugArangesSection;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000056 StringRef DebugStringSection;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000057
58 error_code ec;
Michael J. Spencer20990392011-10-07 19:52:41 +000059 for (section_iterator i = Obj->begin_sections(),
60 e = Obj->end_sections();
61 i != e; i.increment(ec)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000062 StringRef name;
63 i->getName(name);
64 StringRef data;
65 i->getContents(data);
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000066
67 if (name.startswith("__DWARF,"))
68 name = name.substr(8); // Skip "__DWARF," prefix.
69 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
70 if (name == "debug_info")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000071 DebugInfoSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000072 else if (name == "debug_abbrev")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000073 DebugAbbrevSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000074 else if (name == "debug_line")
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000075 DebugLineSection = data;
Benjamin Kramer1c0b24f2011-09-14 17:28:13 +000076 else if (name == "debug_aranges")
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000077 DebugArangesSection = data;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000078 else if (name == "debug_str")
79 DebugStringSection = data;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000080 }
81
82 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
83 DebugInfoSection,
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000084 DebugAbbrevSection,
Benjamin Kramerb848e972011-09-15 02:12:05 +000085 DebugArangesSection,
Benjamin Kramer34f864f2011-09-15 16:57:13 +000086 DebugLineSection,
87 DebugStringSection));
Benjamin Kramer6b3ae462011-09-15 21:17:40 +000088 if (Address == -1ULL) {
89 outs() << Filename
90 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
91 // Dump the complete DWARF structure.
92 dictx->dump(outs());
93 } else {
94 // Print line info for the specified address.
95 DILineInfo dli = dictx->getLineInfoForAddress(Address);
96 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
97 << dli.getLine() << ':' << dli.getColumn() << '\n';
98 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000099}
100
101int main(int argc, char **argv) {
102 // Print a stack trace if we signal out.
103 sys::PrintStackTraceOnErrorSignal();
104 PrettyStackTraceProgram X(argc, argv);
105 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
106
107 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
108
109 // Defaults to a.out if no filenames specified.
110 if (InputFilenames.size() == 0)
111 InputFilenames.push_back("a.out");
112
113 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
114
115 return 0;
116}