Eric Christopher | 3680f88 | 2012-10-16 23:46:21 +0000 | [diff] [blame] | 1 | //===-- llvm-dwarfdump.cpp - Debug info dumping utility for llvm ----------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Triple.h" |
| 16 | #include "llvm/DebugInfo/DIContext.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 18 | #include "llvm/Object/RelocVisitor.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <cstring> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 32 | #include <system_error> |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 33 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | using namespace object; |
| 36 | |
| 37 | static cl::list<std::string> |
| 38 | InputFilenames(cl::Positional, cl::desc("<input object files>"), |
| 39 | cl::ZeroOrMore); |
| 40 | |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 41 | static cl::opt<DIDumpType> |
| 42 | DumpType("debug-dump", cl::init(DIDT_All), |
| 43 | cl::desc("Dump of debug sections:"), |
| 44 | cl::values( |
| 45 | clEnumValN(DIDT_All, "all", "Dump all debug sections"), |
| 46 | clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"), |
| 47 | clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"), |
| 48 | clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"), |
| 49 | clEnumValN(DIDT_Info, "info", ".debug_info"), |
| 50 | clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"), |
David Blaikie | 3af1442 | 2013-11-19 00:29:42 +0000 | [diff] [blame] | 51 | clEnumValN(DIDT_Types, "types", ".debug_types"), |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 52 | clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 53 | clEnumValN(DIDT_Line, "line", ".debug_line"), |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 54 | clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"), |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 55 | clEnumValN(DIDT_Loc, "loc", ".debug_loc"), |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 56 | clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"), |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 57 | clEnumValN(DIDT_Frames, "frames", ".debug_frame"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 58 | clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"), |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 59 | clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"), |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 60 | clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"), |
Eric Christopher | a9e303e | 2013-09-25 23:02:44 +0000 | [diff] [blame] | 61 | clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"), |
| 62 | clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 63 | clEnumValN(DIDT_Str, "str", ".debug_str"), |
| 64 | clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"), |
| 65 | clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"), |
| 66 | clEnumValEnd)); |
| 67 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame^] | 68 | static void DumpInput(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 69 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 70 | MemoryBuffer::getFileOrSTDIN(Filename); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 71 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 72 | if (std::error_code EC = BuffOrErr.getError()) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 73 | errs() << Filename << ": " << EC.message() << "\n"; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 74 | return; |
| 75 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 76 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 77 | |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 78 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 79 | ObjectFile::createObjectFile(Buff->getMemBufferRef()); |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 80 | if (std::error_code EC = ObjOrErr.getError()) { |
Rafael Espindola | 51cc360 | 2014-01-22 00:14:49 +0000 | [diff] [blame] | 81 | errs() << Filename << ": " << EC.message() << '\n'; |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 82 | return; |
| 83 | } |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 84 | ObjectFile &Obj = *ObjOrErr.get(); |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 85 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 86 | std::unique_ptr<DIContext> DICtx(DIContext::getDWARFContext(Obj)); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 87 | |
Alexey Samsonov | e321879 | 2014-05-19 18:45:32 +0000 | [diff] [blame] | 88 | outs() << Filename |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 89 | << ":\tfile format " << Obj.getFileFormatName() << "\n\n"; |
Alexey Samsonov | e321879 | 2014-05-19 18:45:32 +0000 | [diff] [blame] | 90 | // Dump the complete DWARF structure. |
| 91 | DICtx->dump(outs(), DumpType); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int main(int argc, char **argv) { |
| 95 | // Print a stack trace if we signal out. |
| 96 | sys::PrintStackTraceOnErrorSignal(); |
| 97 | PrettyStackTraceProgram X(argc, argv); |
| 98 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 99 | |
| 100 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 101 | |
| 102 | // Defaults to a.out if no filenames specified. |
| 103 | if (InputFilenames.size() == 0) |
| 104 | InputFilenames.push_back("a.out"); |
| 105 | |
| 106 | std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput); |
| 107 | |
| 108 | return 0; |
| 109 | } |