Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 1 | //===-- 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> |
| 31 | using namespace llvm; |
| 32 | using namespace object; |
| 33 | |
| 34 | static cl::list<std::string> |
| 35 | InputFilenames(cl::Positional, cl::desc("<input object files>"), |
| 36 | cl::ZeroOrMore); |
| 37 | |
Benjamin Kramer | 6b3ae46 | 2011-09-15 21:17:40 +0000 | [diff] [blame] | 38 | static cl::opt<unsigned long long> |
| 39 | Address("address", cl::init(-1ULL), |
| 40 | cl::desc("Print line information for a given address")); |
| 41 | |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame^] | 42 | static cl::opt<bool> |
| 43 | PrintFunctions("functions", cl::init(false), |
| 44 | cl::desc("Print function names as well as line information " |
| 45 | "for a given address")); |
| 46 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 47 | static 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 Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 57 | StringRef DebugInfoSection; |
| 58 | StringRef DebugAbbrevSection; |
| 59 | StringRef DebugLineSection; |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 60 | StringRef DebugArangesSection; |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 61 | StringRef DebugStringSection; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 62 | |
| 63 | error_code ec; |
Michael J. Spencer | 2099039 | 2011-10-07 19:52:41 +0000 | [diff] [blame] | 64 | for (section_iterator i = Obj->begin_sections(), |
| 65 | e = Obj->end_sections(); |
| 66 | i != e; i.increment(ec)) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 67 | StringRef name; |
| 68 | i->getName(name); |
| 69 | StringRef data; |
| 70 | i->getContents(data); |
Benjamin Kramer | 1c0b24f | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 71 | |
| 72 | if (name.startswith("__DWARF,")) |
| 73 | name = name.substr(8); // Skip "__DWARF," prefix. |
| 74 | name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes. |
| 75 | if (name == "debug_info") |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 76 | DebugInfoSection = data; |
Benjamin Kramer | 1c0b24f | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 77 | else if (name == "debug_abbrev") |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 78 | DebugAbbrevSection = data; |
Benjamin Kramer | 1c0b24f | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 79 | else if (name == "debug_line") |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 80 | DebugLineSection = data; |
Benjamin Kramer | 1c0b24f | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 81 | else if (name == "debug_aranges") |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 82 | DebugArangesSection = data; |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 83 | else if (name == "debug_str") |
| 84 | DebugStringSection = data; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true, |
| 88 | DebugInfoSection, |
Benjamin Kramer | 358f4fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 89 | DebugAbbrevSection, |
Benjamin Kramer | b848e97 | 2011-09-15 02:12:05 +0000 | [diff] [blame] | 90 | DebugArangesSection, |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 91 | DebugLineSection, |
| 92 | DebugStringSection)); |
Benjamin Kramer | 6b3ae46 | 2011-09-15 21:17:40 +0000 | [diff] [blame] | 93 | if (Address == -1ULL) { |
| 94 | outs() << Filename |
| 95 | << ":\tfile format " << Obj->getFileFormatName() << "\n\n"; |
| 96 | // Dump the complete DWARF structure. |
| 97 | dictx->dump(outs()); |
| 98 | } else { |
| 99 | // Print line info for the specified address. |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame^] | 100 | int spec_flags = DILineInfoSpecifier::FileLineInfo; |
| 101 | if (PrintFunctions) |
| 102 | spec_flags |= DILineInfoSpecifier::FunctionName; |
| 103 | DILineInfo dli = dictx->getLineInfoForAddress(Address, spec_flags); |
| 104 | if (PrintFunctions) |
| 105 | outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>") |
| 106 | << "\n"; |
Benjamin Kramer | 6b3ae46 | 2011-09-15 21:17:40 +0000 | [diff] [blame] | 107 | outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':' |
| 108 | << dli.getLine() << ':' << dli.getColumn() << '\n'; |
| 109 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int main(int argc, char **argv) { |
| 113 | // Print a stack trace if we signal out. |
| 114 | sys::PrintStackTraceOnErrorSignal(); |
| 115 | PrettyStackTraceProgram X(argc, argv); |
| 116 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 117 | |
| 118 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 119 | |
| 120 | // Defaults to a.out if no filenames specified. |
| 121 | if (InputFilenames.size() == 0) |
| 122 | InputFilenames.push_back("a.out"); |
| 123 | |
| 124 | std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput); |
| 125 | |
| 126 | return 0; |
| 127 | } |