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