Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 1 | //===- LLVMOutputStyle.cpp ------------------------------------ *- C++ --*-===// |
| 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 | #include "LLVMOutputStyle.h" |
| 11 | |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 12 | #include "CompactTypeDumpVisitor.h" |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 13 | #include "StreamUtil.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 14 | #include "llvm-pdbdump.h" |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 15 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/CVTypeDumper.h" |
| 17 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/EnumTables.h" |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/ModuleDebugFragmentVisitor.h" |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h" |
| 22 | #include "llvm/DebugInfo/CodeView/ModuleDebugUnknownFragment.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeDatabaseVisitor.h" |
| 25 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
| 26 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
| 27 | #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/EnumTables.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 37 | #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" |
| 38 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 39 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 40 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 41 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 42 | #include "llvm/Support/BinaryStreamReader.h" |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 43 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 44 | |
| 45 | #include <unordered_map> |
| 46 | |
| 47 | using namespace llvm; |
| 48 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 49 | using namespace llvm::msf; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 50 | using namespace llvm::pdb; |
| 51 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | struct PageStats { |
| 54 | explicit PageStats(const BitVector &FreePages) |
| 55 | : Upm(FreePages), ActualUsedPages(FreePages.size()), |
| 56 | MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) { |
| 57 | const_cast<BitVector &>(Upm).flip(); |
| 58 | // To calculate orphaned pages, we start with the set of pages that the |
| 59 | // MSF thinks are used. Each time we find one that actually *is* used, |
| 60 | // we unset it. Whichever bits remain set at the end are orphaned. |
| 61 | OrphanedPages = Upm; |
| 62 | } |
| 63 | |
| 64 | // The inverse of the MSF File's copy of the Fpm. The basis for which we |
| 65 | // determine the allocation status of each page. |
| 66 | const BitVector Upm; |
| 67 | |
| 68 | // Pages which are marked as used in the FPM and are used at least once. |
| 69 | BitVector ActualUsedPages; |
| 70 | |
| 71 | // Pages which are marked as used in the FPM but are used more than once. |
| 72 | BitVector MultiUsePages; |
| 73 | |
| 74 | // Pages which are marked as used in the FPM but are not used at all. |
| 75 | BitVector OrphanedPages; |
| 76 | |
| 77 | // Pages which are marked free in the FPM but are used. |
| 78 | BitVector UseAfterFreePages; |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) { |
| 83 | if (Stats.Upm.test(UsedIndex)) { |
| 84 | if (Stats.ActualUsedPages.test(UsedIndex)) |
| 85 | Stats.MultiUsePages.set(UsedIndex); |
| 86 | Stats.ActualUsedPages.set(UsedIndex); |
| 87 | Stats.OrphanedPages.reset(UsedIndex); |
| 88 | } else { |
| 89 | // The MSF doesn't think this page is used, but it is. |
| 90 | Stats.UseAfterFreePages.set(UsedIndex); |
| 91 | } |
| 92 | } |
| 93 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 94 | static void printSectionOffset(llvm::raw_ostream &OS, |
| 95 | const SectionOffset &Off) { |
| 96 | OS << Off.Off << ", " << Off.Isect; |
| 97 | } |
| 98 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 99 | LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) : File(File), P(outs()) {} |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 100 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 101 | Error LLVMOutputStyle::dump() { |
| 102 | if (auto EC = dumpFileHeaders()) |
| 103 | return EC; |
| 104 | |
| 105 | if (auto EC = dumpStreamSummary()) |
| 106 | return EC; |
| 107 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 108 | if (auto EC = dumpFreePageMap()) |
| 109 | return EC; |
| 110 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 111 | if (auto EC = dumpStreamBlocks()) |
| 112 | return EC; |
| 113 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 114 | if (auto EC = dumpBlockRanges()) |
| 115 | return EC; |
| 116 | |
| 117 | if (auto EC = dumpStreamBytes()) |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 118 | return EC; |
| 119 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 120 | if (auto EC = dumpStringTable()) |
| 121 | return EC; |
| 122 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 123 | if (auto EC = dumpInfoStream()) |
| 124 | return EC; |
| 125 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 126 | if (auto EC = dumpTpiStream(StreamTPI)) |
| 127 | return EC; |
| 128 | |
| 129 | if (auto EC = dumpTpiStream(StreamIPI)) |
| 130 | return EC; |
| 131 | |
| 132 | if (auto EC = dumpDbiStream()) |
| 133 | return EC; |
| 134 | |
| 135 | if (auto EC = dumpSectionContribs()) |
| 136 | return EC; |
| 137 | |
| 138 | if (auto EC = dumpSectionMap()) |
| 139 | return EC; |
| 140 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 141 | if (auto EC = dumpGlobalsStream()) |
| 142 | return EC; |
| 143 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 144 | if (auto EC = dumpPublicsStream()) |
| 145 | return EC; |
| 146 | |
| 147 | if (auto EC = dumpSectionHeaders()) |
| 148 | return EC; |
| 149 | |
| 150 | if (auto EC = dumpFpoStream()) |
| 151 | return EC; |
| 152 | |
| 153 | flush(); |
| 154 | |
| 155 | return Error::success(); |
| 156 | } |
| 157 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 158 | Error LLVMOutputStyle::dumpFileHeaders() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 159 | if (!opts::raw::DumpHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 160 | return Error::success(); |
| 161 | |
| 162 | DictScope D(P, "FileHeaders"); |
| 163 | P.printNumber("BlockSize", File.getBlockSize()); |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 164 | P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 165 | P.printNumber("NumBlocks", File.getBlockCount()); |
| 166 | P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes()); |
| 167 | P.printNumber("Unknown1", File.getUnknown1()); |
| 168 | P.printNumber("BlockMapAddr", File.getBlockMapIndex()); |
| 169 | P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 170 | |
| 171 | // The directory is not contiguous. Instead, the block map contains a |
| 172 | // contiguous list of block numbers whose contents, when concatenated in |
| 173 | // order, make up the directory. |
| 174 | P.printList("DirectoryBlocks", File.getDirectoryBlockArray()); |
| 175 | P.printNumber("NumStreams", File.getNumStreams()); |
| 176 | return Error::success(); |
| 177 | } |
| 178 | |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 179 | Error LLVMOutputStyle::dumpStreamSummary() { |
| 180 | if (!opts::raw::DumpStreamSummary) |
| 181 | return Error::success(); |
| 182 | |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 183 | if (StreamPurposes.empty()) |
| 184 | discoverStreamPurposes(File, StreamPurposes); |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 185 | |
| 186 | uint32_t StreamCount = File.getNumStreams(); |
| 187 | |
| 188 | ListScope L(P, "Streams"); |
| 189 | for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 190 | std::string Label("Stream "); |
| 191 | Label += to_string(StreamIdx); |
| 192 | |
| 193 | std::string Value = "[" + StreamPurposes[StreamIdx] + "] ("; |
| 194 | Value += to_string(File.getStreamByteSize(StreamIdx)); |
| 195 | Value += " bytes)"; |
| 196 | |
| 197 | P.printString(Label, Value); |
| 198 | } |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 199 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 200 | P.flush(); |
| 201 | return Error::success(); |
| 202 | } |
| 203 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 204 | Error LLVMOutputStyle::dumpFreePageMap() { |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 205 | if (!opts::raw::DumpPageStats) |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 206 | return Error::success(); |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 207 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 208 | // Start with used pages instead of free pages because |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 209 | // the number of free pages is far larger than used pages. |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 210 | BitVector FPM = File.getMsfLayout().FreePageMap; |
| 211 | |
| 212 | PageStats PS(FPM); |
| 213 | |
| 214 | recordKnownUsedPage(PS, 0); // MSF Super Block |
| 215 | |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 216 | uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout()); |
| 217 | uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout()); |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 218 | for (uint32_t I = 0; I < NumSections; ++I) { |
| 219 | uint32_t Fpm0 = 1 + BlocksPerSection * I; |
| 220 | // 2 Fpm blocks spaced at `getBlockSize()` block intervals |
| 221 | recordKnownUsedPage(PS, Fpm0); |
| 222 | recordKnownUsedPage(PS, Fpm0 + 1); |
| 223 | } |
| 224 | |
| 225 | recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table |
| 226 | |
Rui Ueyama | 22e6738 | 2016-08-02 23:22:46 +0000 | [diff] [blame] | 227 | for (auto DB : File.getDirectoryBlockArray()) |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 228 | recordKnownUsedPage(PS, DB); |
Rui Ueyama | 22e6738 | 2016-08-02 23:22:46 +0000 | [diff] [blame] | 229 | |
| 230 | // Record pages used by streams. Note that pages for stream 0 |
| 231 | // are considered being unused because that's what MSVC tools do. |
| 232 | // Stream 0 doesn't contain actual data, so it makes some sense, |
| 233 | // though it's a bit confusing to us. |
| 234 | for (auto &SE : File.getStreamMap().drop_front(1)) |
| 235 | for (auto &S : SE) |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 236 | recordKnownUsedPage(PS, S); |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 237 | |
| 238 | dumpBitVector("Msf Free Pages", FPM); |
| 239 | dumpBitVector("Orphaned Pages", PS.OrphanedPages); |
| 240 | dumpBitVector("Multiply Used Pages", PS.MultiUsePages); |
| 241 | dumpBitVector("Use After Free Pages", PS.UseAfterFreePages); |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 242 | return Error::success(); |
| 243 | } |
| 244 | |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 245 | void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) { |
| 246 | std::vector<uint32_t> Vec; |
| 247 | for (uint32_t I = 0, E = V.size(); I != E; ++I) |
| 248 | if (V[I]) |
| 249 | Vec.push_back(I); |
| 250 | P.printList(Name, Vec); |
| 251 | } |
| 252 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 253 | Error LLVMOutputStyle::dumpGlobalsStream() { |
| 254 | if (!opts::raw::DumpGlobals) |
| 255 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 256 | if (!File.hasPDBGlobalsStream()) { |
| 257 | P.printString("Globals Stream not present"); |
| 258 | return Error::success(); |
| 259 | } |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 260 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 261 | auto Globals = File.getPDBGlobalsStream(); |
| 262 | if (!Globals) |
Bob Haarman | 312fd0e | 2016-12-06 00:55:55 +0000 | [diff] [blame] | 263 | return Globals.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 264 | DictScope D(P, "Globals Stream"); |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 265 | |
| 266 | auto Dbi = File.getPDBDbiStream(); |
| 267 | if (!Dbi) |
| 268 | return Dbi.takeError(); |
| 269 | |
| 270 | P.printNumber("Stream number", Dbi->getGlobalSymbolStreamIndex()); |
| 271 | P.printNumber("Number of buckets", Globals->getNumBuckets()); |
| 272 | P.printList("Hash Buckets", Globals->getHashBuckets()); |
| 273 | |
| 274 | return Error::success(); |
| 275 | } |
| 276 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 277 | Error LLVMOutputStyle::dumpStreamBlocks() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 278 | if (!opts::raw::DumpStreamBlocks) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 279 | return Error::success(); |
| 280 | |
| 281 | ListScope L(P, "StreamBlocks"); |
| 282 | uint32_t StreamCount = File.getNumStreams(); |
| 283 | for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) { |
| 284 | std::string Name("Stream "); |
| 285 | Name += to_string(StreamIdx); |
| 286 | auto StreamBlocks = File.getStreamBlockList(StreamIdx); |
| 287 | P.printList(Name, StreamBlocks); |
| 288 | } |
| 289 | return Error::success(); |
| 290 | } |
| 291 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 292 | Error LLVMOutputStyle::dumpBlockRanges() { |
| 293 | if (!opts::raw::DumpBlockRange.hasValue()) |
| 294 | return Error::success(); |
| 295 | auto &R = *opts::raw::DumpBlockRange; |
| 296 | uint32_t Max = R.Max.getValueOr(R.Min); |
| 297 | |
| 298 | if (Max < R.Min) |
| 299 | return make_error<StringError>( |
| 300 | "Invalid block range specified. Max < Min", |
| 301 | std::make_error_code(std::errc::bad_address)); |
| 302 | if (Max >= File.getBlockCount()) |
| 303 | return make_error<StringError>( |
| 304 | "Invalid block range specified. Requested block out of bounds", |
| 305 | std::make_error_code(std::errc::bad_address)); |
| 306 | |
| 307 | DictScope D(P, "Block Data"); |
| 308 | for (uint32_t I = R.Min; I <= Max; ++I) { |
| 309 | auto ExpectedData = File.getBlockData(I, File.getBlockSize()); |
| 310 | if (!ExpectedData) |
| 311 | return ExpectedData.takeError(); |
| 312 | std::string Label; |
| 313 | llvm::raw_string_ostream S(Label); |
| 314 | S << "Block " << I; |
| 315 | S.flush(); |
| 316 | P.printBinaryBlock(Label, *ExpectedData); |
| 317 | } |
| 318 | |
| 319 | return Error::success(); |
| 320 | } |
| 321 | |
Zachary Turner | 7159ab9 | 2017-04-28 00:43:38 +0000 | [diff] [blame] | 322 | static Error parseStreamSpec(StringRef Str, uint32_t &SI, uint32_t &Offset, |
| 323 | uint32_t &Size) { |
| 324 | if (Str.consumeInteger(0, SI)) |
| 325 | return make_error<RawError>(raw_error_code::invalid_format, |
| 326 | "Invalid Stream Specification"); |
| 327 | if (Str.consume_front(":")) { |
| 328 | if (Str.consumeInteger(0, Offset)) |
| 329 | return make_error<RawError>(raw_error_code::invalid_format, |
| 330 | "Invalid Stream Specification"); |
| 331 | } |
| 332 | if (Str.consume_front("@")) { |
| 333 | if (Str.consumeInteger(0, Size)) |
| 334 | return make_error<RawError>(raw_error_code::invalid_format, |
| 335 | "Invalid Stream Specification"); |
| 336 | } |
| 337 | if (!Str.empty()) |
| 338 | return make_error<RawError>(raw_error_code::invalid_format, |
| 339 | "Invalid Stream Specification"); |
| 340 | return Error::success(); |
| 341 | } |
| 342 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 343 | Error LLVMOutputStyle::dumpStreamBytes() { |
| 344 | if (opts::raw::DumpStreamData.empty()) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 345 | return Error::success(); |
| 346 | |
Zachary Turner | 6ac232c | 2017-03-13 23:28:25 +0000 | [diff] [blame] | 347 | if (StreamPurposes.empty()) |
| 348 | discoverStreamPurposes(File, StreamPurposes); |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 349 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 350 | DictScope D(P, "Stream Data"); |
Zachary Turner | 7159ab9 | 2017-04-28 00:43:38 +0000 | [diff] [blame] | 351 | for (auto &Str : opts::raw::DumpStreamData) { |
| 352 | uint32_t SI = 0; |
| 353 | uint32_t Begin = 0; |
| 354 | uint32_t Size = 0; |
| 355 | uint32_t End = 0; |
| 356 | |
| 357 | if (auto EC = parseStreamSpec(Str, SI, Begin, Size)) |
| 358 | return EC; |
| 359 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 360 | if (SI >= File.getNumStreams()) |
| 361 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 362 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 363 | auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(), |
| 364 | File.getMsfBuffer(), SI); |
| 365 | if (!S) |
| 366 | continue; |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 367 | DictScope DD(P, "Stream"); |
Zachary Turner | 7159ab9 | 2017-04-28 00:43:38 +0000 | [diff] [blame] | 368 | if (Size == 0) |
| 369 | End = S->getLength(); |
| 370 | else { |
| 371 | End = Begin + Size; |
| 372 | if (End >= S->getLength()) |
| 373 | return make_error<RawError>(raw_error_code::index_out_of_bounds, |
| 374 | "Stream is not long enough!"); |
| 375 | } |
Zachary Turner | 36efbfa | 2016-09-09 19:00:49 +0000 | [diff] [blame] | 376 | |
| 377 | P.printNumber("Index", SI); |
| 378 | P.printString("Type", StreamPurposes[SI]); |
| 379 | P.printNumber("Size", S->getLength()); |
| 380 | auto Blocks = File.getMsfLayout().StreamMap[SI]; |
| 381 | P.printList("Blocks", Blocks); |
| 382 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 383 | BinaryStreamReader R(*S); |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame] | 384 | ArrayRef<uint8_t> StreamData; |
| 385 | if (auto EC = R.readBytes(StreamData, S->getLength())) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 386 | return EC; |
Zachary Turner | 7159ab9 | 2017-04-28 00:43:38 +0000 | [diff] [blame] | 387 | Size = End - Begin; |
| 388 | StreamData = StreamData.slice(Begin, Size); |
| 389 | P.printBinaryBlock("Data", StreamData, Begin); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 390 | } |
| 391 | return Error::success(); |
| 392 | } |
| 393 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 394 | Error LLVMOutputStyle::dumpStringTable() { |
| 395 | if (!opts::raw::DumpStringTable) |
| 396 | return Error::success(); |
| 397 | |
| 398 | auto IS = File.getStringTable(); |
| 399 | if (!IS) |
| 400 | return IS.takeError(); |
| 401 | |
| 402 | DictScope D(P, "String Table"); |
| 403 | for (uint32_t I : IS->name_ids()) { |
| 404 | StringRef S = IS->getStringForID(I); |
| 405 | if (!S.empty()) { |
| 406 | llvm::SmallString<32> Str; |
| 407 | Str.append("'"); |
| 408 | Str.append(S); |
| 409 | Str.append("'"); |
| 410 | P.printString(Str); |
| 411 | } |
| 412 | } |
| 413 | return Error::success(); |
| 414 | } |
| 415 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 416 | Error LLVMOutputStyle::dumpInfoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 417 | if (!opts::raw::DumpHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 418 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 419 | if (!File.hasPDBInfoStream()) { |
| 420 | P.printString("PDB Stream not present"); |
| 421 | return Error::success(); |
| 422 | } |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 423 | auto IS = File.getPDBInfoStream(); |
| 424 | if (!IS) |
| 425 | return IS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 426 | |
| 427 | DictScope D(P, "PDB Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 428 | P.printNumber("Version", IS->getVersion()); |
| 429 | P.printHex("Signature", IS->getSignature()); |
| 430 | P.printNumber("Age", IS->getAge()); |
| 431 | P.printObject("Guid", IS->getGuid()); |
Zachary Turner | 05d5e61 | 2017-03-16 20:19:11 +0000 | [diff] [blame] | 432 | P.printHex("Features", IS->getFeatures()); |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 433 | { |
| 434 | DictScope DD(P, "Named Streams"); |
| 435 | for (const auto &S : IS->getNamedStreams().entries()) |
| 436 | P.printObject(S.getKey(), S.getValue()); |
| 437 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 438 | return Error::success(); |
| 439 | } |
| 440 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 441 | namespace { |
| 442 | class RecordBytesVisitor : public TypeVisitorCallbacks { |
| 443 | public: |
| 444 | explicit RecordBytesVisitor(ScopedPrinter &P) : P(P) {} |
| 445 | |
| 446 | Error visitTypeEnd(CVType &Record) override { |
| 447 | P.printBinaryBlock("Bytes", Record.content()); |
| 448 | return Error::success(); |
| 449 | } |
| 450 | |
| 451 | private: |
| 452 | ScopedPrinter &P; |
| 453 | }; |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 456 | Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) { |
| 457 | assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI); |
| 458 | |
| 459 | bool DumpRecordBytes = false; |
| 460 | bool DumpRecords = false; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 461 | bool DumpTpiHash = false; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 462 | StringRef Label; |
| 463 | StringRef VerLabel; |
| 464 | if (StreamIdx == StreamTPI) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 465 | if (!File.hasPDBTpiStream()) { |
| 466 | P.printString("Type Info Stream (TPI) not present"); |
| 467 | return Error::success(); |
| 468 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 469 | DumpRecordBytes = opts::raw::DumpTpiRecordBytes; |
| 470 | DumpRecords = opts::raw::DumpTpiRecords; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 471 | DumpTpiHash = opts::raw::DumpTpiHash; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 472 | Label = "Type Info Stream (TPI)"; |
| 473 | VerLabel = "TPI Version"; |
| 474 | } else if (StreamIdx == StreamIPI) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 475 | if (!File.hasPDBIpiStream()) { |
| 476 | P.printString("Type Info Stream (IPI) not present"); |
| 477 | return Error::success(); |
| 478 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 479 | DumpRecordBytes = opts::raw::DumpIpiRecordBytes; |
| 480 | DumpRecords = opts::raw::DumpIpiRecords; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 481 | Label = "Type Info Stream (IPI)"; |
| 482 | VerLabel = "IPI Version"; |
| 483 | } |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 484 | if (!DumpRecordBytes && !DumpRecords && !DumpTpiHash && |
| 485 | !opts::raw::DumpModuleSyms) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 486 | return Error::success(); |
| 487 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 488 | bool IsSilentDatabaseBuild = !DumpRecordBytes && !DumpRecords && !DumpTpiHash; |
| 489 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 490 | auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream() |
| 491 | : File.getPDBIpiStream(); |
| 492 | if (!Tpi) |
| 493 | return Tpi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 494 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 495 | std::unique_ptr<DictScope> StreamScope; |
| 496 | std::unique_ptr<ListScope> RecordScope; |
| 497 | |
| 498 | if (!IsSilentDatabaseBuild) { |
| 499 | StreamScope = llvm::make_unique<DictScope>(P, Label); |
| 500 | P.printNumber(VerLabel, Tpi->getTpiVersion()); |
| 501 | P.printNumber("Record count", Tpi->NumTypeRecords()); |
| 502 | } |
| 503 | |
Reid Kleckner | a5d187b | 2017-03-23 21:36:25 +0000 | [diff] [blame] | 504 | TypeDatabase &StreamDB = (StreamIdx == StreamTPI) ? TypeDB : ItemDB; |
| 505 | |
| 506 | TypeDatabaseVisitor DBV(StreamDB); |
| 507 | CompactTypeDumpVisitor CTDV(StreamDB, &P); |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 508 | TypeDumpVisitor TDV(TypeDB, &P, false); |
Reid Kleckner | a5d187b | 2017-03-23 21:36:25 +0000 | [diff] [blame] | 509 | if (StreamIdx == StreamIPI) |
| 510 | TDV.setItemDB(ItemDB); |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 511 | RecordBytesVisitor RBV(P); |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 512 | TypeDeserializer Deserializer; |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 513 | |
| 514 | // We always need to deserialize and add it to the type database. This is |
| 515 | // true if even if we're not dumping anything, because we could need the |
| 516 | // type database for the purposes of dumping symbols. |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 517 | TypeVisitorCallbackPipeline Pipeline; |
| 518 | Pipeline.addCallbackToPipeline(Deserializer); |
| 519 | Pipeline.addCallbackToPipeline(DBV); |
| 520 | |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 521 | // If we're in dump mode, add a dumper with the appropriate detail level. |
| 522 | if (DumpRecords) { |
Zachary Turner | 44a643c | 2017-01-12 22:28:15 +0000 | [diff] [blame] | 523 | if (opts::raw::CompactRecords) |
| 524 | Pipeline.addCallbackToPipeline(CTDV); |
| 525 | else |
| 526 | Pipeline.addCallbackToPipeline(TDV); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 527 | } |
Zachary Turner | 29da5db | 2017-01-25 21:17:40 +0000 | [diff] [blame] | 528 | if (DumpRecordBytes) |
| 529 | Pipeline.addCallbackToPipeline(RBV); |
| 530 | |
| 531 | CVTypeVisitor Visitor(Pipeline); |
| 532 | |
| 533 | if (DumpRecords || DumpRecordBytes) |
| 534 | RecordScope = llvm::make_unique<ListScope>(P, "Records"); |
| 535 | |
| 536 | bool HadError = false; |
| 537 | |
| 538 | TypeIndex T(TypeIndex::FirstNonSimpleIndex); |
| 539 | for (auto Type : Tpi->types(&HadError)) { |
| 540 | std::unique_ptr<DictScope> OneRecordScope; |
| 541 | |
| 542 | if ((DumpRecords || DumpRecordBytes) && !opts::raw::CompactRecords) |
| 543 | OneRecordScope = llvm::make_unique<DictScope>(P, ""); |
| 544 | |
| 545 | if (auto EC = Visitor.visitTypeRecord(Type)) |
| 546 | return EC; |
| 547 | } |
| 548 | if (HadError) |
| 549 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 550 | "TPI stream contained corrupt record"); |
| 551 | |
| 552 | if (DumpTpiHash) { |
| 553 | DictScope DD(P, "Hash"); |
| 554 | P.printNumber("Number of Hash Buckets", Tpi->NumHashBuckets()); |
| 555 | P.printNumber("Hash Key Size", Tpi->getHashKeySize()); |
| 556 | P.printList("Values", Tpi->getHashValues()); |
| 557 | |
| 558 | ListScope LHA(P, "Adjusters"); |
| 559 | auto ExpectedST = File.getStringTable(); |
| 560 | if (!ExpectedST) |
| 561 | return ExpectedST.takeError(); |
| 562 | const auto &ST = *ExpectedST; |
| 563 | for (const auto &E : Tpi->getHashAdjusters()) { |
| 564 | DictScope DHA(P); |
| 565 | StringRef Name = ST.getStringForID(E.first); |
| 566 | P.printString("Type", Name); |
| 567 | P.printHex("TI", E.second); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (!IsSilentDatabaseBuild) { |
| 572 | ListScope L(P, "TypeIndexOffsets"); |
| 573 | for (const auto &IO : Tpi->getTypeIndexOffsets()) { |
| 574 | P.printString(formatv("Index: {0:x}, Offset: {1:N}", IO.Type.getIndex(), |
| 575 | (uint32_t)IO.Offset) |
| 576 | .str()); |
| 577 | } |
| 578 | } |
| 579 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 580 | P.flush(); |
| 581 | return Error::success(); |
| 582 | } |
| 583 | |
| 584 | Error LLVMOutputStyle::dumpDbiStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 585 | bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms || |
| 586 | opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo; |
| 587 | if (!opts::raw::DumpHeaders && !DumpModules) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 588 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 589 | if (!File.hasPDBDbiStream()) { |
| 590 | P.printString("DBI Stream not present"); |
| 591 | return Error::success(); |
| 592 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 593 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 594 | auto DS = File.getPDBDbiStream(); |
| 595 | if (!DS) |
| 596 | return DS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 597 | |
| 598 | DictScope D(P, "DBI Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 599 | P.printNumber("Dbi Version", DS->getDbiVersion()); |
| 600 | P.printNumber("Age", DS->getAge()); |
| 601 | P.printBoolean("Incremental Linking", DS->isIncrementallyLinked()); |
| 602 | P.printBoolean("Has CTypes", DS->hasCTypes()); |
| 603 | P.printBoolean("Is Stripped", DS->isStripped()); |
| 604 | P.printObject("Machine Type", DS->getMachineType()); |
| 605 | P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex()); |
| 606 | P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex()); |
| 607 | P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 608 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 609 | uint16_t Major = DS->getBuildMajorVersion(); |
| 610 | uint16_t Minor = DS->getBuildMinorVersion(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 611 | P.printVersion("Toolchain Version", Major, Minor); |
| 612 | |
| 613 | std::string DllName; |
| 614 | raw_string_ostream DllStream(DllName); |
| 615 | DllStream << "mspdb" << Major << Minor << ".dll version"; |
| 616 | DllStream.flush(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 617 | P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 618 | |
| 619 | if (DumpModules) { |
| 620 | ListScope L(P, "Modules"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 621 | for (auto &Modi : DS->modules()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 622 | DictScope DD(P); |
| 623 | P.printString("Name", Modi.Info.getModuleName().str()); |
| 624 | P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex()); |
| 625 | P.printString("Object File Name", Modi.Info.getObjFileName().str()); |
| 626 | P.printNumber("Num Files", Modi.Info.getNumberOfFiles()); |
| 627 | P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex()); |
| 628 | P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex()); |
| 629 | P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize()); |
| 630 | P.printNumber("C13 Line Info Byte Size", |
| 631 | Modi.Info.getC13LineInfoByteSize()); |
| 632 | P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize()); |
| 633 | P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex()); |
| 634 | P.printBoolean("Has EC Info", Modi.Info.hasECInfo()); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 635 | if (opts::raw::DumpModuleFiles) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 636 | std::string FileListName = |
| 637 | to_string(Modi.SourceFiles.size()) + " Contributing Source Files"; |
| 638 | ListScope LL(P, FileListName); |
| 639 | for (auto File : Modi.SourceFiles) |
| 640 | P.printString(File.str()); |
| 641 | } |
| 642 | bool HasModuleDI = |
| 643 | (Modi.Info.getModuleStreamIndex() < File.getNumStreams()); |
| 644 | bool ShouldDumpSymbols = |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 645 | (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes); |
| 646 | if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 647 | auto ModStreamData = MappedBlockStream::createIndexedStream( |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 648 | File.getMsfLayout(), File.getMsfBuffer(), |
| 649 | Modi.Info.getModuleStreamIndex()); |
| 650 | |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 651 | ModuleDebugStream ModS(Modi.Info, std::move(ModStreamData)); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 652 | if (auto EC = ModS.reload()) |
| 653 | return EC; |
| 654 | |
| 655 | if (ShouldDumpSymbols) { |
| 656 | ListScope SS(P, "Symbols"); |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 657 | codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 658 | bool HadError = false; |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 659 | for (auto S : ModS.symbols(&HadError)) { |
| 660 | DictScope LL(P, ""); |
| 661 | if (opts::raw::DumpModuleSyms) { |
| 662 | if (auto EC = SD.dump(S)) { |
| 663 | llvm::consumeError(std::move(EC)); |
| 664 | HadError = true; |
| 665 | break; |
| 666 | } |
| 667 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 668 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | c67b00c | 2016-09-14 23:00:16 +0000 | [diff] [blame] | 669 | P.printBinaryBlock("Bytes", S.content()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 670 | } |
| 671 | if (HadError) |
| 672 | return make_error<RawError>( |
| 673 | raw_error_code::corrupt_file, |
| 674 | "DBI stream contained corrupt symbol record"); |
| 675 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 676 | if (opts::raw::DumpLineInfo) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 677 | ListScope SS(P, "LineInfo"); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 678 | // Define a locally scoped visitor to print the different |
| 679 | // substream types types. |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 680 | class RecordVisitor : public codeview::ModuleDebugFragmentVisitor { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 681 | public: |
| 682 | RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {} |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 683 | Error visitUnknown(ModuleDebugUnknownFragment &Fragment) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 684 | DictScope DD(P, "Unknown"); |
| 685 | ArrayRef<uint8_t> Data; |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 686 | BinaryStreamReader R(Fragment.getData()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 687 | if (auto EC = R.readBytes(Data, R.bytesRemaining())) { |
| 688 | return make_error<RawError>( |
| 689 | raw_error_code::corrupt_file, |
| 690 | "DBI stream contained corrupt line info record"); |
| 691 | } |
| 692 | P.printBinaryBlock("Data", Data); |
| 693 | return Error::success(); |
| 694 | } |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 695 | Error visitFileChecksums( |
| 696 | ModuleDebugFileChecksumFragment &Checksums) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 697 | DictScope DD(P, "FileChecksums"); |
| 698 | for (const auto &C : Checksums) { |
| 699 | DictScope DDD(P, "Checksum"); |
| 700 | if (auto Result = getFileNameForOffset(C.FileNameOffset)) |
| 701 | P.printString("FileName", Result.get()); |
| 702 | else |
| 703 | return Result.takeError(); |
| 704 | P.flush(); |
| 705 | P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames()); |
| 706 | P.printBinaryBlock("Checksum", C.Checksum); |
| 707 | } |
| 708 | return Error::success(); |
| 709 | } |
| 710 | |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 711 | Error visitLines(ModuleDebugLineFragment &Lines) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 712 | DictScope DD(P, "Lines"); |
| 713 | for (const auto &L : Lines) { |
| 714 | if (auto Result = getFileNameForOffset2(L.NameIndex)) |
| 715 | P.printString("FileName", Result.get()); |
| 716 | else |
| 717 | return Result.takeError(); |
| 718 | P.flush(); |
| 719 | for (const auto &N : L.LineNumbers) { |
| 720 | DictScope DDD(P, "Line"); |
| 721 | LineInfo LI(N.Flags); |
| 722 | P.printNumber("Offset", N.Offset); |
| 723 | if (LI.isAlwaysStepInto()) |
| 724 | P.printString("StepInto", StringRef("Always")); |
| 725 | else if (LI.isNeverStepInto()) |
| 726 | P.printString("StepInto", StringRef("Never")); |
| 727 | else |
| 728 | P.printNumber("LineNumberStart", LI.getStartLine()); |
| 729 | P.printNumber("EndDelta", LI.getLineDelta()); |
| 730 | P.printBoolean("IsStatement", LI.isStatement()); |
| 731 | } |
| 732 | for (const auto &C : L.Columns) { |
| 733 | DictScope DDD(P, "Column"); |
| 734 | P.printNumber("Start", C.StartColumn); |
| 735 | P.printNumber("End", C.EndColumn); |
| 736 | } |
| 737 | } |
| 738 | return Error::success(); |
| 739 | } |
| 740 | |
| 741 | private: |
| 742 | Expected<StringRef> getFileNameForOffset(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 743 | auto ST = F.getStringTable(); |
| 744 | if (!ST) |
| 745 | return ST.takeError(); |
| 746 | |
| 747 | return ST->getStringForID(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 748 | } |
| 749 | Expected<StringRef> getFileNameForOffset2(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 750 | auto DS = F.getPDBDbiStream(); |
| 751 | if (!DS) |
| 752 | return DS.takeError(); |
| 753 | return DS->getFileNameForIndex(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 754 | } |
| 755 | ScopedPrinter &P; |
| 756 | PDBFile &F; |
| 757 | }; |
| 758 | |
| 759 | RecordVisitor V(P, File); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 760 | for (const auto &L : ModS.linesAndChecksums()) { |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 761 | if (auto EC = codeview::visitModuleDebugFragment(L, V)) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 762 | return EC; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | return Error::success(); |
| 769 | } |
| 770 | |
| 771 | Error LLVMOutputStyle::dumpSectionContribs() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 772 | if (!opts::raw::DumpSectionContribs) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 773 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 774 | if (!File.hasPDBDbiStream()) { |
| 775 | P.printString("DBI Stream not present"); |
| 776 | return Error::success(); |
| 777 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 778 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 779 | auto Dbi = File.getPDBDbiStream(); |
| 780 | if (!Dbi) |
| 781 | return Dbi.takeError(); |
| 782 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 783 | ListScope L(P, "Section Contributions"); |
| 784 | class Visitor : public ISectionContribVisitor { |
| 785 | public: |
| 786 | Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {} |
| 787 | void visit(const SectionContrib &SC) override { |
| 788 | DictScope D(P, "Contribution"); |
| 789 | P.printNumber("ISect", SC.ISect); |
| 790 | P.printNumber("Off", SC.Off); |
| 791 | P.printNumber("Size", SC.Size); |
| 792 | P.printFlags("Characteristics", SC.Characteristics, |
| 793 | codeview::getImageSectionCharacteristicNames(), |
| 794 | COFF::SectionCharacteristics(0x00F00000)); |
| 795 | { |
| 796 | DictScope DD(P, "Module"); |
| 797 | P.printNumber("Index", SC.Imod); |
| 798 | auto M = DS.modules(); |
| 799 | if (M.size() > SC.Imod) { |
| 800 | P.printString("Name", M[SC.Imod].Info.getModuleName()); |
| 801 | } |
| 802 | } |
| 803 | P.printNumber("Data CRC", SC.DataCrc); |
| 804 | P.printNumber("Reloc CRC", SC.RelocCrc); |
| 805 | P.flush(); |
| 806 | } |
| 807 | void visit(const SectionContrib2 &SC) override { |
| 808 | visit(SC.Base); |
| 809 | P.printNumber("ISect Coff", SC.ISectCoff); |
| 810 | P.flush(); |
| 811 | } |
| 812 | |
| 813 | private: |
| 814 | ScopedPrinter &P; |
| 815 | DbiStream &DS; |
| 816 | }; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 817 | Visitor V(P, *Dbi); |
| 818 | Dbi->visitSectionContributions(V); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 819 | return Error::success(); |
| 820 | } |
| 821 | |
| 822 | Error LLVMOutputStyle::dumpSectionMap() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 823 | if (!opts::raw::DumpSectionMap) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 824 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 825 | if (!File.hasPDBDbiStream()) { |
| 826 | P.printString("DBI Stream not present"); |
| 827 | return Error::success(); |
| 828 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 829 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 830 | auto Dbi = File.getPDBDbiStream(); |
| 831 | if (!Dbi) |
| 832 | return Dbi.takeError(); |
| 833 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 834 | ListScope L(P, "Section Map"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 835 | for (auto &M : Dbi->getSectionMap()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 836 | DictScope D(P, "Entry"); |
| 837 | P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 838 | P.printNumber("Ovl", M.Ovl); |
| 839 | P.printNumber("Group", M.Group); |
| 840 | P.printNumber("Frame", M.Frame); |
| 841 | P.printNumber("SecName", M.SecName); |
| 842 | P.printNumber("ClassName", M.ClassName); |
| 843 | P.printNumber("Offset", M.Offset); |
| 844 | P.printNumber("SecByteLength", M.SecByteLength); |
| 845 | P.flush(); |
| 846 | } |
| 847 | return Error::success(); |
| 848 | } |
| 849 | |
| 850 | Error LLVMOutputStyle::dumpPublicsStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 851 | if (!opts::raw::DumpPublics) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 852 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 853 | if (!File.hasPDBPublicsStream()) { |
| 854 | P.printString("Publics Stream not present"); |
| 855 | return Error::success(); |
| 856 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 857 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 858 | auto Publics = File.getPDBPublicsStream(); |
| 859 | if (!Publics) |
| 860 | return Publics.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 861 | DictScope D(P, "Publics Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 862 | |
| 863 | auto Dbi = File.getPDBDbiStream(); |
| 864 | if (!Dbi) |
| 865 | return Dbi.takeError(); |
| 866 | |
| 867 | P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex()); |
| 868 | P.printNumber("SymHash", Publics->getSymHash()); |
| 869 | P.printNumber("AddrMap", Publics->getAddrMap()); |
| 870 | P.printNumber("Number of buckets", Publics->getNumBuckets()); |
| 871 | P.printList("Hash Buckets", Publics->getHashBuckets()); |
| 872 | P.printList("Address Map", Publics->getAddressMap()); |
| 873 | P.printList("Thunk Map", Publics->getThunkMap()); |
| 874 | P.printList("Section Offsets", Publics->getSectionOffsets(), |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 875 | printSectionOffset); |
| 876 | ListScope L(P, "Symbols"); |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 877 | codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 878 | bool HadError = false; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 879 | for (auto S : Publics->getSymbols(&HadError)) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 880 | DictScope DD(P, ""); |
| 881 | |
Zachary Turner | 0d84074 | 2016-10-07 21:34:46 +0000 | [diff] [blame] | 882 | if (auto EC = SD.dump(S)) { |
| 883 | HadError = true; |
| 884 | break; |
| 885 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 886 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | c67b00c | 2016-09-14 23:00:16 +0000 | [diff] [blame] | 887 | P.printBinaryBlock("Bytes", S.content()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 888 | } |
| 889 | if (HadError) |
| 890 | return make_error<RawError>( |
| 891 | raw_error_code::corrupt_file, |
| 892 | "Public symbol stream contained corrupt record"); |
| 893 | |
| 894 | return Error::success(); |
| 895 | } |
| 896 | |
| 897 | Error LLVMOutputStyle::dumpSectionHeaders() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 898 | if (!opts::raw::DumpSectionHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 899 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 900 | if (!File.hasPDBDbiStream()) { |
| 901 | P.printString("DBI Stream not present"); |
| 902 | return Error::success(); |
| 903 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 904 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 905 | auto Dbi = File.getPDBDbiStream(); |
| 906 | if (!Dbi) |
| 907 | return Dbi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 908 | |
| 909 | ListScope D(P, "Section Headers"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 910 | for (const object::coff_section &Section : Dbi->getSectionHeaders()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 911 | DictScope DD(P, ""); |
| 912 | |
| 913 | // If a name is 8 characters long, there is no NUL character at end. |
| 914 | StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name))); |
| 915 | P.printString("Name", Name); |
| 916 | P.printNumber("Virtual Size", Section.VirtualSize); |
| 917 | P.printNumber("Virtual Address", Section.VirtualAddress); |
| 918 | P.printNumber("Size of Raw Data", Section.SizeOfRawData); |
| 919 | P.printNumber("File Pointer to Raw Data", Section.PointerToRawData); |
| 920 | P.printNumber("File Pointer to Relocations", Section.PointerToRelocations); |
| 921 | P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers); |
| 922 | P.printNumber("Number of Relocations", Section.NumberOfRelocations); |
| 923 | P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers); |
Rui Ueyama | 2c5384a | 2016-06-06 21:34:55 +0000 | [diff] [blame] | 924 | P.printFlags("Characteristics", Section.Characteristics, |
| 925 | getImageSectionCharacteristicNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 926 | } |
| 927 | return Error::success(); |
| 928 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 929 | |
| 930 | Error LLVMOutputStyle::dumpFpoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 931 | if (!opts::raw::DumpFpo) |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 932 | return Error::success(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 933 | if (!File.hasPDBDbiStream()) { |
| 934 | P.printString("DBI Stream not present"); |
| 935 | return Error::success(); |
| 936 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 937 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 938 | auto Dbi = File.getPDBDbiStream(); |
| 939 | if (!Dbi) |
| 940 | return Dbi.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 941 | |
| 942 | ListScope D(P, "New FPO"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 943 | for (const object::FpoData &Fpo : Dbi->getFpoRecords()) { |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 944 | DictScope DD(P, ""); |
| 945 | P.printNumber("Offset", Fpo.Offset); |
| 946 | P.printNumber("Size", Fpo.Size); |
| 947 | P.printNumber("Number of locals", Fpo.NumLocals); |
| 948 | P.printNumber("Number of params", Fpo.NumParams); |
| 949 | P.printNumber("Size of Prolog", Fpo.getPrologSize()); |
| 950 | P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs()); |
| 951 | P.printBoolean("Has SEH", Fpo.hasSEH()); |
| 952 | P.printBoolean("Use BP", Fpo.useBP()); |
| 953 | P.printNumber("Frame Pointer", Fpo.getFP()); |
| 954 | } |
| 955 | return Error::success(); |
| 956 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 957 | |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 958 | void LLVMOutputStyle::flush() { P.flush(); } |