Deleted Added
full compact
llvm-dwarfdump.cpp (243789) llvm-dwarfdump.cpp (249259)
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"
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"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/Triple.h"
17#include "llvm/DebugInfo/DIContext.h"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Object/RelocVisitor.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Object/RelocVisitor.h"
19#include "llvm/DebugInfo/DIContext.h"
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"
25#include "llvm/Support/MemoryObject.h"
26#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"

--- 19 unchanged lines hidden (view full) ---

47PrintFunctions("functions", cl::init(false),
48 cl::desc("Print function names as well as line information "
49 "for a given address"));
50
51static cl::opt<bool>
52PrintInlining("inlining", cl::init(false),
53 cl::desc("Print all inlined frames for a given address"));
54
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"
25#include "llvm/Support/MemoryObject.h"
26#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"

--- 19 unchanged lines hidden (view full) ---

47PrintFunctions("functions", cl::init(false),
48 cl::desc("Print function names as well as line information "
49 "for a given address"));
50
51static cl::opt<bool>
52PrintInlining("inlining", cl::init(false),
53 cl::desc("Print all inlined frames for a given address"));
54
55static cl::opt<DIDumpType>
56DumpType("debug-dump", cl::init(DIDT_All),
57 cl::desc("Dump of debug sections:"),
58 cl::values(
59 clEnumValN(DIDT_All, "all", "Dump all debug sections"),
60 clEnumValN(DIDT_Abbrev, "abbrev", ".debug_abbrev"),
61 clEnumValN(DIDT_AbbrevDwo, "abbrev.dwo", ".debug_abbrev.dwo"),
62 clEnumValN(DIDT_Aranges, "aranges", ".debug_aranges"),
63 clEnumValN(DIDT_Info, "info", ".debug_info"),
64 clEnumValN(DIDT_InfoDwo, "info.dwo", ".debug_info.dwo"),
65 clEnumValN(DIDT_Line, "line", ".debug_line"),
66 clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
67 clEnumValN(DIDT_Ranges, "ranges", ".debug_ranges"),
68 clEnumValN(DIDT_Pubnames, "pubnames", ".debug_pubnames"),
69 clEnumValN(DIDT_Str, "str", ".debug_str"),
70 clEnumValN(DIDT_StrDwo, "str.dwo", ".debug_str.dwo"),
71 clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"),
72 clEnumValEnd));
73
55static void PrintDILineInfo(DILineInfo dli) {
56 if (PrintFunctions)
57 outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>")
58 << "\n";
59 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
60 << dli.getLine() << ':' << dli.getColumn() << '\n';
61}
62
63static void DumpInput(const StringRef &Filename) {
64 OwningPtr<MemoryBuffer> Buff;
65
66 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
67 errs() << Filename << ": " << ec.message() << "\n";
68 return;
69 }
70
71 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
74static void PrintDILineInfo(DILineInfo dli) {
75 if (PrintFunctions)
76 outs() << (dli.getFunctionName() ? dli.getFunctionName() : "<unknown>")
77 << "\n";
78 outs() << (dli.getFileName() ? dli.getFileName() : "<unknown>") << ':'
79 << dli.getLine() << ':' << dli.getColumn() << '\n';
80}
81
82static void DumpInput(const StringRef &Filename) {
83 OwningPtr<MemoryBuffer> Buff;
84
85 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
86 errs() << Filename << ": " << ec.message() << "\n";
87 return;
88 }
89
90 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
72
73 StringRef DebugInfoSection;
74 RelocAddrMap RelocMap;
75 StringRef DebugAbbrevSection;
76 StringRef DebugLineSection;
77 StringRef DebugArangesSection;
78 StringRef DebugStringSection;
79 StringRef DebugRangesSection;
80
81 error_code ec;
82 for (section_iterator i = Obj->begin_sections(),
83 e = Obj->end_sections();
84 i != e; i.increment(ec)) {
85 StringRef name;
86 i->getName(name);
87 StringRef data;
88 i->getContents(data);
89
90 if (name.startswith("__DWARF,"))
91 name = name.substr(8); // Skip "__DWARF," prefix.
92 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
93 if (name == "debug_info")
94 DebugInfoSection = data;
95 else if (name == "debug_abbrev")
96 DebugAbbrevSection = data;
97 else if (name == "debug_line")
98 DebugLineSection = data;
99 else if (name == "debug_aranges")
100 DebugArangesSection = data;
101 else if (name == "debug_str")
102 DebugStringSection = data;
103 else if (name == "debug_ranges")
104 DebugRangesSection = data;
105 // Any more debug info sections go here.
106 else
107 continue;
108
109 // TODO: For now only handle relocations for the debug_info section.
110 if (name != "debug_info")
111 continue;
112
113 if (i->begin_relocations() != i->end_relocations()) {
114 uint64_t SectionSize;
115 i->getSize(SectionSize);
116 for (relocation_iterator reloc_i = i->begin_relocations(),
117 reloc_e = i->end_relocations();
118 reloc_i != reloc_e; reloc_i.increment(ec)) {
119 uint64_t Address;
120 reloc_i->getAddress(Address);
121 uint64_t Type;
122 reloc_i->getType(Type);
123
124 RelocVisitor V(Obj->getFileFormatName());
125 // The section address is always 0 for debug sections.
126 RelocToApply R(V.visit(Type, *reloc_i));
127 if (V.error()) {
128 SmallString<32> Name;
129 error_code ec(reloc_i->getTypeName(Name));
130 if (ec) {
131 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
132 }
133 errs() << "error: failed to compute relocation: "
134 << Name << "\n";
135 continue;
136 }
137
138 if (Address + R.Width > SectionSize) {
139 errs() << "error: " << R.Width << "-byte relocation starting "
140 << Address << " bytes into section " << name << " which is "
141 << SectionSize << " bytes long.\n";
142 continue;
143 }
144 if (R.Width > 8) {
145 errs() << "error: can't handle a relocation of more than 8 bytes at "
146 "a time.\n";
147 continue;
148 }
149 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
150 << " at " << format("%p", Address)
151 << " with width " << format("%d", R.Width)
152 << "\n");
153 RelocMap[Address] = std::make_pair(R.Width, R.Value);
154 }
155 }
91 if (!Obj) {
92 errs() << Filename << ": Unknown object file format\n";
93 return;
156 }
157
94 }
95
158 OwningPtr<DIContext> dictx(DIContext::getDWARFContext(/*FIXME*/true,
159 DebugInfoSection,
160 DebugAbbrevSection,
161 DebugArangesSection,
162 DebugLineSection,
163 DebugStringSection,
164 DebugRangesSection,
165 RelocMap));
96 OwningPtr<DIContext> DICtx(DIContext::getDWARFContext(Obj.get()));
97
166 if (Address == -1ULL) {
167 outs() << Filename
168 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
169 // Dump the complete DWARF structure.
98 if (Address == -1ULL) {
99 outs() << Filename
100 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
101 // Dump the complete DWARF structure.
170 dictx->dump(outs());
102 DICtx->dump(outs(), DumpType);
171 } else {
172 // Print line info for the specified address.
173 int SpecFlags = DILineInfoSpecifier::FileLineInfo |
174 DILineInfoSpecifier::AbsoluteFilePath;
175 if (PrintFunctions)
176 SpecFlags |= DILineInfoSpecifier::FunctionName;
177 if (PrintInlining) {
178 DIInliningInfo InliningInfo =
103 } else {
104 // Print line info for the specified address.
105 int SpecFlags = DILineInfoSpecifier::FileLineInfo |
106 DILineInfoSpecifier::AbsoluteFilePath;
107 if (PrintFunctions)
108 SpecFlags |= DILineInfoSpecifier::FunctionName;
109 if (PrintInlining) {
110 DIInliningInfo InliningInfo =
179 dictx->getInliningInfoForAddress(Address, SpecFlags);
111 DICtx->getInliningInfoForAddress(Address, SpecFlags);
180 uint32_t n = InliningInfo.getNumberOfFrames();
181 if (n == 0) {
182 // Print one empty debug line info in any case.
183 PrintDILineInfo(DILineInfo());
184 } else {
185 for (uint32_t i = 0; i < n; i++) {
186 DILineInfo dli = InliningInfo.getFrame(i);
187 PrintDILineInfo(dli);
188 }
189 }
190 } else {
112 uint32_t n = InliningInfo.getNumberOfFrames();
113 if (n == 0) {
114 // Print one empty debug line info in any case.
115 PrintDILineInfo(DILineInfo());
116 } else {
117 for (uint32_t i = 0; i < n; i++) {
118 DILineInfo dli = InliningInfo.getFrame(i);
119 PrintDILineInfo(dli);
120 }
121 }
122 } else {
191 DILineInfo dli = dictx->getLineInfoForAddress(Address, SpecFlags);
123 DILineInfo dli = DICtx->getLineInfoForAddress(Address, SpecFlags);
192 PrintDILineInfo(dli);
193 }
194 }
195}
196
197int main(int argc, char **argv) {
198 // Print a stack trace if we signal out.
199 sys::PrintStackTraceOnErrorSignal();

--- 13 unchanged lines hidden ---
124 PrintDILineInfo(dli);
125 }
126 }
127}
128
129int main(int argc, char **argv) {
130 // Print a stack trace if we signal out.
131 sys::PrintStackTraceOnErrorSignal();

--- 13 unchanged lines hidden ---