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" |
Zachary Turner | 6489d7b | 2015-04-23 17:37:47 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DIContext.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 7c678de | 2012-11-07 23:22:07 +0000 | [diff] [blame] | 19 | #include "llvm/Object/RelocVisitor.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/Format.h" |
| 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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"), |
Frederic Riss | e837ec2 | 2014-11-14 16:15:53 +0000 | [diff] [blame] | 48 | clEnumValN(DIDT_AppleNames, "apple_names", ".apple_names"), |
| 49 | clEnumValN(DIDT_AppleTypes, "apple_types", ".apple_types"), |
| 50 | clEnumValN(DIDT_AppleNamespaces, "apple_namespaces", ".apple_namespaces"), |
| 51 | clEnumValN(DIDT_AppleObjC, "apple_objc", ".apple_objc"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 52 | clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"), |
| 53 | clEnumValN(DIDT_Info, "info", ".debug_info"), |
| 54 | clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"), |
David Blaikie | 3af1442 | 2013-11-19 00:29:42 +0000 | [diff] [blame] | 55 | clEnumValN(DIDT_Types, "types", ".debug_types"), |
David Blaikie | 92d9d62 | 2014-01-09 05:08:24 +0000 | [diff] [blame] | 56 | clEnumValN(DIDT_TypesDwo, "types.dwo", ".debug_types.dwo"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 57 | clEnumValN(DIDT_Line, "line", ".debug_line"), |
David Blaikie | 1d4736e | 2014-02-24 23:58:54 +0000 | [diff] [blame] | 58 | clEnumValN(DIDT_LineDwo, "line.dwo", ".debug_line.dwo"), |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 59 | clEnumValN(DIDT_Loc, "loc", ".debug_loc"), |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 60 | clEnumValN(DIDT_LocDwo, "loc.dwo", ".debug_loc.dwo"), |
Eli Bendersky | fd08bc1 | 2013-02-05 23:30:58 +0000 | [diff] [blame] | 61 | clEnumValN(DIDT_Frames, "frames", ".debug_frame"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 62 | clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"), |
Krzysztof Parzyszek | 97438dc | 2013-02-12 16:20:28 +0000 | [diff] [blame] | 63 | clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"), |
Eric Christopher | 4c7e6ba | 2013-09-25 23:02:41 +0000 | [diff] [blame] | 64 | clEnumValN(DIDT_Pubtypes, "pubtypes", ".debug_pubtypes"), |
Eric Christopher | a9e303e | 2013-09-25 23:02:44 +0000 | [diff] [blame] | 65 | clEnumValN(DIDT_GnuPubnames, "gnu_pubnames", ".debug_gnu_pubnames"), |
| 66 | clEnumValN(DIDT_GnuPubtypes, "gnu_pubtypes", ".debug_gnu_pubtypes"), |
Eli Bendersky | 7a94daa | 2013-01-25 20:26:43 +0000 | [diff] [blame] | 67 | clEnumValN(DIDT_Str, "str", ".debug_str"), |
| 68 | clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"), |
| 69 | clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"), |
| 70 | clEnumValEnd)); |
| 71 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 72 | static void error(StringRef Filename, std::error_code EC) { |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 73 | if (!EC) |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 74 | return; |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 75 | errs() << Filename << ": " << EC.message() << "\n"; |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 76 | exit(1); |
Alexey Samsonov | 85c7d66 | 2015-06-25 23:40:15 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame^] | 79 | static void DumpObjectFile(ObjectFile &Obj, StringRef Filename) { |
| 80 | std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(Obj)); |
| 81 | |
| 82 | outs() << Filename |
| 83 | << ":\tfile format " << Obj.getFileFormatName() << "\n\n"; |
| 84 | // Dump the complete DWARF structure. |
| 85 | DICtx->dump(outs(), DumpType); |
| 86 | } |
| 87 | |
Craig Topper | 6dc4a8bc | 2014-08-30 16:48:02 +0000 | [diff] [blame] | 88 | static void DumpInput(StringRef Filename) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 89 | ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 90 | MemoryBuffer::getFileOrSTDIN(Filename); |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 91 | error(Filename, BuffOrErr.getError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 92 | std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get()); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 93 | |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 94 | ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 95 | ObjectFile::createObjectFile(Buff->getMemBufferRef()); |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 96 | error(Filename, ObjOrErr.getError()); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 97 | ObjectFile &Obj = *ObjOrErr.get(); |
Eli Bendersky | a5a4ff5 | 2013-01-25 20:53:41 +0000 | [diff] [blame] | 98 | |
Frederic Riss | 40c01793 | 2015-08-03 00:10:25 +0000 | [diff] [blame^] | 99 | DumpObjectFile(Obj, Filename); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int main(int argc, char **argv) { |
| 103 | // Print a stack trace if we signal out. |
| 104 | sys::PrintStackTraceOnErrorSignal(); |
| 105 | PrettyStackTraceProgram X(argc, argv); |
| 106 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 107 | |
| 108 | cl::ParseCommandLineOptions(argc, argv, "llvm dwarf dumper\n"); |
| 109 | |
| 110 | // Defaults to a.out if no filenames specified. |
| 111 | if (InputFilenames.size() == 0) |
| 112 | InputFilenames.push_back("a.out"); |
| 113 | |
| 114 | std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput); |
| 115 | |
Davide Italiano | 4376ddb | 2015-07-26 05:35:59 +0000 | [diff] [blame] | 116 | return EXIT_SUCCESS; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 117 | } |