Benjamin Kramer | aa2f78f | 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 | |
| 38 | static 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; |
| 55 | |
| 56 | error_code ec; |
| 57 | for (ObjectFile::section_iterator i = Obj->begin_sections(), |
| 58 | e = Obj->end_sections(); |
| 59 | i != e; i.increment(ec)) { |
| 60 | StringRef name; |
| 61 | i->getName(name); |
| 62 | StringRef data; |
| 63 | i->getContents(data); |
| 64 | if (name.endswith("debug_info")) |
| 65 | DebugInfoSection = data; |
| 66 | else if (name.endswith("debug_abbrev")) |
| 67 | DebugAbbrevSection = data; |
| 68 | else if (name.endswith("debug_line")) |
| 69 | DebugLineSection = data; |
| 70 | } |
| 71 | |
| 72 | OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true, |
| 73 | DebugInfoSection, |
| 74 | DebugAbbrevSection)); |
| 75 | dictx->dump(outs()); |
| 76 | } |
| 77 | |
| 78 | int main(int argc, char **argv) { |
| 79 | // Print a stack trace if we signal out. |
| 80 | sys::PrintStackTraceOnErrorSignal(); |
| 81 | PrettyStackTraceProgram X(argc, argv); |
| 82 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 83 | |
| 84 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 85 | |
| 86 | // Defaults to a.out if no filenames specified. |
| 87 | if (InputFilenames.size() == 0) |
| 88 | InputFilenames.push_back("a.out"); |
| 89 | |
| 90 | std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput); |
| 91 | |
| 92 | return 0; |
| 93 | } |