blob: 90225deaeefb34fe2e8571b659b0718363e28f44 [file] [log] [blame]
Benjamin Krameraa2f78f2011-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
38static void DumpInput(const StringRef &Filename) {
39 OwningPtr<MemoryBuffer> Buff;
40
41 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
42 errs() << Filename << ": " << ec.message() << "\n";
43 return;
44 }
45
46 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
47
48 outs() << '\n';
49 outs() << Filename
50 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
51
52 StringRef DebugInfoSection;
53 StringRef DebugAbbrevSection;
54 StringRef DebugLineSection;
Benjamin Kramera6002fd2011-09-14 01:09:52 +000055 StringRef DebugArangesSection;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000056
57 error_code ec;
58 for (ObjectFile::section_iterator i = Obj->begin_sections(),
59 e = Obj->end_sections();
60 i != e; i.increment(ec)) {
61 StringRef name;
62 i->getName(name);
63 StringRef data;
64 i->getContents(data);
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000065
66 if (name.startswith("__DWARF,"))
67 name = name.substr(8); // Skip "__DWARF," prefix.
68 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
69 if (name == "debug_info")
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000070 DebugInfoSection = data;
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000071 else if (name == "debug_abbrev")
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000072 DebugAbbrevSection = data;
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000073 else if (name == "debug_line")
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000074 DebugLineSection = data;
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000075 else if (name == "debug_aranges")
Benjamin Kramera6002fd2011-09-14 01:09:52 +000076 DebugArangesSection = data;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000077 }
78
79 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
80 DebugInfoSection,
Benjamin Kramera6002fd2011-09-14 01:09:52 +000081 DebugAbbrevSection,
Benjamin Kramer5acab502011-09-15 02:12:05 +000082 DebugArangesSection,
83 DebugLineSection));
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000084 dictx->dump(outs());
85}
86
87int main(int argc, char **argv) {
88 // Print a stack trace if we signal out.
89 sys::PrintStackTraceOnErrorSignal();
90 PrettyStackTraceProgram X(argc, argv);
91 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
92
93 cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n");
94
95 // Defaults to a.out if no filenames specified.
96 if (InputFilenames.size() == 0)
97 InputFilenames.push_back("a.out");
98
99 std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput);
100
101 return 0;
102}