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 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame^] | 101 | if (auto EC = dumpBlockRanges()) |
| 102 | return EC; |
| 103 | |
| 104 | if (auto EC = dumpStreamBytes()) |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 105 | return EC; |
| 106 | |
| 107 | if (auto EC = dumpInfoStream()) |
| 108 | return EC; |
| 109 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame^] | 346 | Error LLVMOutputStyle::dumpBlockRanges() { |
| 347 | if (!opts::raw::DumpBlockRange.hasValue()) |
| 348 | return Error::success(); |
| 349 | auto &R = *opts::raw::DumpBlockRange; |
| 350 | uint32_t Max = R.Max.getValueOr(R.Min); |
| 351 | |
| 352 | if (Max < R.Min) |
| 353 | return make_error<StringError>( |
| 354 | "Invalid block range specified. Max < Min", |
| 355 | std::make_error_code(std::errc::bad_address)); |
| 356 | if (Max >= File.getBlockCount()) |
| 357 | return make_error<StringError>( |
| 358 | "Invalid block range specified. Requested block out of bounds", |
| 359 | std::make_error_code(std::errc::bad_address)); |
| 360 | |
| 361 | DictScope D(P, "Block Data"); |
| 362 | for (uint32_t I = R.Min; I <= Max; ++I) { |
| 363 | auto ExpectedData = File.getBlockData(I, File.getBlockSize()); |
| 364 | if (!ExpectedData) |
| 365 | return ExpectedData.takeError(); |
| 366 | std::string Label; |
| 367 | llvm::raw_string_ostream S(Label); |
| 368 | S << "Block " << I; |
| 369 | S.flush(); |
| 370 | P.printBinaryBlock(Label, *ExpectedData); |
| 371 | } |
| 372 | |
| 373 | return Error::success(); |
| 374 | } |
| 375 | |
| 376 | Error LLVMOutputStyle::dumpStreamBytes() { |
| 377 | if (opts::raw::DumpStreamData.empty()) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 378 | return Error::success(); |
| 379 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame^] | 380 | DictScope D(P, "Stream Data"); |
| 381 | for (uint32_t SI : opts::raw::DumpStreamData) { |
| 382 | if (SI >= File.getNumStreams()) |
| 383 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 384 | |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame^] | 385 | auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(), |
| 386 | File.getMsfBuffer(), SI); |
| 387 | if (!S) |
| 388 | continue; |
| 389 | StreamReader R(*S); |
| 390 | ArrayRef<uint8_t> StreamData; |
| 391 | if (auto EC = R.readBytes(StreamData, S->getLength())) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 392 | return EC; |
Zachary Turner | 72c5b64 | 2016-09-09 18:17:52 +0000 | [diff] [blame^] | 393 | std::string Label; |
| 394 | llvm::raw_string_ostream Stream(Label); |
| 395 | Stream << "Stream " << SI; |
| 396 | Stream.flush(); |
| 397 | P.printBinaryBlock(Label, StreamData); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 398 | } |
| 399 | return Error::success(); |
| 400 | } |
| 401 | |
| 402 | Error LLVMOutputStyle::dumpInfoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 403 | if (!opts::raw::DumpHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 404 | return Error::success(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 405 | auto IS = File.getPDBInfoStream(); |
| 406 | if (!IS) |
| 407 | return IS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 408 | |
| 409 | DictScope D(P, "PDB Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 410 | P.printNumber("Version", IS->getVersion()); |
| 411 | P.printHex("Signature", IS->getSignature()); |
| 412 | P.printNumber("Age", IS->getAge()); |
| 413 | P.printObject("Guid", IS->getGuid()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 414 | return Error::success(); |
| 415 | } |
| 416 | |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 417 | static void printTypeIndexOffset(raw_ostream &OS, |
| 418 | const TypeIndexOffset &TIOff) { |
| 419 | OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}"; |
| 420 | } |
| 421 | |
| 422 | static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 423 | if (!opts::raw::DumpTpiHash) |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 424 | return; |
| 425 | DictScope DD(P, "Hash"); |
Rui Ueyama | f14a74c | 2016-06-07 23:53:43 +0000 | [diff] [blame] | 426 | P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets()); |
Rui Ueyama | d833917 | 2016-06-07 23:44:27 +0000 | [diff] [blame] | 427 | P.printNumber("Hash Key Size", Tpi.getHashKeySize()); |
Rui Ueyama | fd97bf1 | 2016-06-03 20:48:51 +0000 | [diff] [blame] | 428 | P.printList("Values", Tpi.getHashValues()); |
| 429 | P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(), |
| 430 | printTypeIndexOffset); |
| 431 | P.printList("Hash Adjustments", Tpi.getHashAdjustments(), |
| 432 | printTypeIndexOffset); |
| 433 | } |
| 434 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 435 | Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) { |
| 436 | assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI); |
| 437 | |
| 438 | bool DumpRecordBytes = false; |
| 439 | bool DumpRecords = false; |
| 440 | StringRef Label; |
| 441 | StringRef VerLabel; |
| 442 | if (StreamIdx == StreamTPI) { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 443 | DumpRecordBytes = opts::raw::DumpTpiRecordBytes; |
| 444 | DumpRecords = opts::raw::DumpTpiRecords; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 445 | Label = "Type Info Stream (TPI)"; |
| 446 | VerLabel = "TPI Version"; |
| 447 | } else if (StreamIdx == StreamIPI) { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 448 | DumpRecordBytes = opts::raw::DumpIpiRecordBytes; |
| 449 | DumpRecords = opts::raw::DumpIpiRecords; |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 450 | Label = "Type Info Stream (IPI)"; |
| 451 | VerLabel = "IPI Version"; |
| 452 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 453 | if (!DumpRecordBytes && !DumpRecords && !opts::raw::DumpModuleSyms) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 454 | return Error::success(); |
| 455 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 456 | auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream() |
| 457 | : File.getPDBIpiStream(); |
| 458 | if (!Tpi) |
| 459 | return Tpi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 460 | |
| 461 | if (DumpRecords || DumpRecordBytes) { |
| 462 | DictScope D(P, Label); |
| 463 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 464 | P.printNumber(VerLabel, Tpi->getTpiVersion()); |
| 465 | P.printNumber("Record count", Tpi->NumTypeRecords()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 466 | |
| 467 | ListScope L(P, "Records"); |
| 468 | |
| 469 | bool HadError = false; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 470 | for (auto &Type : Tpi->types(&HadError)) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 471 | DictScope DD(P, ""); |
| 472 | |
Zachary Turner | 01ee3dae | 2016-06-16 18:22:27 +0000 | [diff] [blame] | 473 | if (DumpRecords) { |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 474 | if (auto EC = Dumper.dump(Type)) |
Zachary Turner | 01ee3dae | 2016-06-16 18:22:27 +0000 | [diff] [blame] | 475 | return EC; |
| 476 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 477 | |
| 478 | if (DumpRecordBytes) |
| 479 | P.printBinaryBlock("Bytes", Type.Data); |
| 480 | } |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 481 | dumpTpiHash(P, *Tpi); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 482 | if (HadError) |
| 483 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 484 | "TPI stream contained corrupt record"); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 485 | } else if (opts::raw::DumpModuleSyms) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 486 | // Even if the user doesn't want to dump type records, we still need to |
| 487 | // iterate them in order to build the list of types so that we can print |
| 488 | // them when dumping module symbols. So when they want to dump symbols |
| 489 | // but not types, use a null output stream. |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 490 | ScopedPrinter *OldP = Dumper.getPrinter(); |
| 491 | Dumper.setPrinter(nullptr); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 492 | |
| 493 | bool HadError = false; |
Zachary Turner | 01ee3dae | 2016-06-16 18:22:27 +0000 | [diff] [blame] | 494 | for (auto &Type : Tpi->types(&HadError)) { |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 495 | if (auto EC = Dumper.dump(Type)) |
Zachary Turner | 01ee3dae | 2016-06-16 18:22:27 +0000 | [diff] [blame] | 496 | return EC; |
| 497 | } |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 498 | |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 499 | Dumper.setPrinter(OldP); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 500 | dumpTpiHash(P, *Tpi); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 501 | if (HadError) |
| 502 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 503 | "TPI stream contained corrupt record"); |
| 504 | } |
| 505 | P.flush(); |
| 506 | return Error::success(); |
| 507 | } |
| 508 | |
| 509 | Error LLVMOutputStyle::dumpDbiStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 510 | bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms || |
| 511 | opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo; |
| 512 | if (!opts::raw::DumpHeaders && !DumpModules) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 513 | return Error::success(); |
| 514 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 515 | auto DS = File.getPDBDbiStream(); |
| 516 | if (!DS) |
| 517 | return DS.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 518 | |
| 519 | DictScope D(P, "DBI Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 520 | P.printNumber("Dbi Version", DS->getDbiVersion()); |
| 521 | P.printNumber("Age", DS->getAge()); |
| 522 | P.printBoolean("Incremental Linking", DS->isIncrementallyLinked()); |
| 523 | P.printBoolean("Has CTypes", DS->hasCTypes()); |
| 524 | P.printBoolean("Is Stripped", DS->isStripped()); |
| 525 | P.printObject("Machine Type", DS->getMachineType()); |
| 526 | P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex()); |
| 527 | P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex()); |
| 528 | P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 529 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 530 | uint16_t Major = DS->getBuildMajorVersion(); |
| 531 | uint16_t Minor = DS->getBuildMinorVersion(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 532 | P.printVersion("Toolchain Version", Major, Minor); |
| 533 | |
| 534 | std::string DllName; |
| 535 | raw_string_ostream DllStream(DllName); |
| 536 | DllStream << "mspdb" << Major << Minor << ".dll version"; |
| 537 | DllStream.flush(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 538 | P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 539 | |
| 540 | if (DumpModules) { |
| 541 | ListScope L(P, "Modules"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 542 | for (auto &Modi : DS->modules()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 543 | DictScope DD(P); |
| 544 | P.printString("Name", Modi.Info.getModuleName().str()); |
| 545 | P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex()); |
| 546 | P.printString("Object File Name", Modi.Info.getObjFileName().str()); |
| 547 | P.printNumber("Num Files", Modi.Info.getNumberOfFiles()); |
| 548 | P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex()); |
| 549 | P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex()); |
| 550 | P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize()); |
| 551 | P.printNumber("C13 Line Info Byte Size", |
| 552 | Modi.Info.getC13LineInfoByteSize()); |
| 553 | P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize()); |
| 554 | P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex()); |
| 555 | P.printBoolean("Has EC Info", Modi.Info.hasECInfo()); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 556 | if (opts::raw::DumpModuleFiles) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 557 | std::string FileListName = |
| 558 | to_string(Modi.SourceFiles.size()) + " Contributing Source Files"; |
| 559 | ListScope LL(P, FileListName); |
| 560 | for (auto File : Modi.SourceFiles) |
| 561 | P.printString(File.str()); |
| 562 | } |
| 563 | bool HasModuleDI = |
| 564 | (Modi.Info.getModuleStreamIndex() < File.getNumStreams()); |
| 565 | bool ShouldDumpSymbols = |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 566 | (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes); |
| 567 | if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 568 | auto ModStreamData = MappedBlockStream::createIndexedStream( |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 569 | File.getMsfLayout(), File.getMsfBuffer(), |
| 570 | Modi.Info.getModuleStreamIndex()); |
| 571 | |
| 572 | ModStream ModS(Modi.Info, std::move(ModStreamData)); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 573 | if (auto EC = ModS.reload()) |
| 574 | return EC; |
| 575 | |
| 576 | if (ShouldDumpSymbols) { |
| 577 | ListScope SS(P, "Symbols"); |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 578 | codeview::CVSymbolDumper SD(P, Dumper, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 579 | bool HadError = false; |
| 580 | for (const auto &S : ModS.symbols(&HadError)) { |
| 581 | DictScope DD(P, ""); |
| 582 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 583 | if (opts::raw::DumpModuleSyms) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 584 | SD.dump(S); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 585 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 586 | P.printBinaryBlock("Bytes", S.Data); |
| 587 | } |
| 588 | if (HadError) |
| 589 | return make_error<RawError>( |
| 590 | raw_error_code::corrupt_file, |
| 591 | "DBI stream contained corrupt symbol record"); |
| 592 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 593 | if (opts::raw::DumpLineInfo) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 594 | ListScope SS(P, "LineInfo"); |
| 595 | bool HadError = false; |
| 596 | // Define a locally scoped visitor to print the different |
| 597 | // substream types types. |
| 598 | class RecordVisitor : public codeview::IModuleSubstreamVisitor { |
| 599 | public: |
| 600 | RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {} |
| 601 | Error visitUnknown(ModuleSubstreamKind Kind, |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 602 | ReadableStreamRef Stream) override { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 603 | DictScope DD(P, "Unknown"); |
| 604 | ArrayRef<uint8_t> Data; |
| 605 | StreamReader R(Stream); |
| 606 | if (auto EC = R.readBytes(Data, R.bytesRemaining())) { |
| 607 | return make_error<RawError>( |
| 608 | raw_error_code::corrupt_file, |
| 609 | "DBI stream contained corrupt line info record"); |
| 610 | } |
| 611 | P.printBinaryBlock("Data", Data); |
| 612 | return Error::success(); |
| 613 | } |
| 614 | Error |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 615 | visitFileChecksums(ReadableStreamRef Data, |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 616 | const FileChecksumArray &Checksums) override { |
| 617 | DictScope DD(P, "FileChecksums"); |
| 618 | for (const auto &C : Checksums) { |
| 619 | DictScope DDD(P, "Checksum"); |
| 620 | if (auto Result = getFileNameForOffset(C.FileNameOffset)) |
| 621 | P.printString("FileName", Result.get()); |
| 622 | else |
| 623 | return Result.takeError(); |
| 624 | P.flush(); |
| 625 | P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames()); |
| 626 | P.printBinaryBlock("Checksum", C.Checksum); |
| 627 | } |
| 628 | return Error::success(); |
| 629 | } |
| 630 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 631 | Error visitLines(ReadableStreamRef Data, |
| 632 | const LineSubstreamHeader *Header, |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 633 | const LineInfoArray &Lines) override { |
| 634 | DictScope DD(P, "Lines"); |
| 635 | for (const auto &L : Lines) { |
| 636 | if (auto Result = getFileNameForOffset2(L.NameIndex)) |
| 637 | P.printString("FileName", Result.get()); |
| 638 | else |
| 639 | return Result.takeError(); |
| 640 | P.flush(); |
| 641 | for (const auto &N : L.LineNumbers) { |
| 642 | DictScope DDD(P, "Line"); |
| 643 | LineInfo LI(N.Flags); |
| 644 | P.printNumber("Offset", N.Offset); |
| 645 | if (LI.isAlwaysStepInto()) |
| 646 | P.printString("StepInto", StringRef("Always")); |
| 647 | else if (LI.isNeverStepInto()) |
| 648 | P.printString("StepInto", StringRef("Never")); |
| 649 | else |
| 650 | P.printNumber("LineNumberStart", LI.getStartLine()); |
| 651 | P.printNumber("EndDelta", LI.getLineDelta()); |
| 652 | P.printBoolean("IsStatement", LI.isStatement()); |
| 653 | } |
| 654 | for (const auto &C : L.Columns) { |
| 655 | DictScope DDD(P, "Column"); |
| 656 | P.printNumber("Start", C.StartColumn); |
| 657 | P.printNumber("End", C.EndColumn); |
| 658 | } |
| 659 | } |
| 660 | return Error::success(); |
| 661 | } |
| 662 | |
| 663 | private: |
| 664 | Expected<StringRef> getFileNameForOffset(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 665 | auto ST = F.getStringTable(); |
| 666 | if (!ST) |
| 667 | return ST.takeError(); |
| 668 | |
| 669 | return ST->getStringForID(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 670 | } |
| 671 | Expected<StringRef> getFileNameForOffset2(uint32_t Offset) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 672 | auto DS = F.getPDBDbiStream(); |
| 673 | if (!DS) |
| 674 | return DS.takeError(); |
| 675 | return DS->getFileNameForIndex(Offset); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 676 | } |
| 677 | ScopedPrinter &P; |
| 678 | PDBFile &F; |
| 679 | }; |
| 680 | |
| 681 | RecordVisitor V(P, File); |
| 682 | for (const auto &L : ModS.lines(&HadError)) { |
| 683 | if (auto EC = codeview::visitModuleSubstream(L, V)) |
| 684 | return EC; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | return Error::success(); |
| 691 | } |
| 692 | |
| 693 | Error LLVMOutputStyle::dumpSectionContribs() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 694 | if (!opts::raw::DumpSectionContribs) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 695 | return Error::success(); |
| 696 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 697 | auto Dbi = File.getPDBDbiStream(); |
| 698 | if (!Dbi) |
| 699 | return Dbi.takeError(); |
| 700 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 701 | ListScope L(P, "Section Contributions"); |
| 702 | class Visitor : public ISectionContribVisitor { |
| 703 | public: |
| 704 | Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {} |
| 705 | void visit(const SectionContrib &SC) override { |
| 706 | DictScope D(P, "Contribution"); |
| 707 | P.printNumber("ISect", SC.ISect); |
| 708 | P.printNumber("Off", SC.Off); |
| 709 | P.printNumber("Size", SC.Size); |
| 710 | P.printFlags("Characteristics", SC.Characteristics, |
| 711 | codeview::getImageSectionCharacteristicNames(), |
| 712 | COFF::SectionCharacteristics(0x00F00000)); |
| 713 | { |
| 714 | DictScope DD(P, "Module"); |
| 715 | P.printNumber("Index", SC.Imod); |
| 716 | auto M = DS.modules(); |
| 717 | if (M.size() > SC.Imod) { |
| 718 | P.printString("Name", M[SC.Imod].Info.getModuleName()); |
| 719 | } |
| 720 | } |
| 721 | P.printNumber("Data CRC", SC.DataCrc); |
| 722 | P.printNumber("Reloc CRC", SC.RelocCrc); |
| 723 | P.flush(); |
| 724 | } |
| 725 | void visit(const SectionContrib2 &SC) override { |
| 726 | visit(SC.Base); |
| 727 | P.printNumber("ISect Coff", SC.ISectCoff); |
| 728 | P.flush(); |
| 729 | } |
| 730 | |
| 731 | private: |
| 732 | ScopedPrinter &P; |
| 733 | DbiStream &DS; |
| 734 | }; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 735 | Visitor V(P, *Dbi); |
| 736 | Dbi->visitSectionContributions(V); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 737 | return Error::success(); |
| 738 | } |
| 739 | |
| 740 | Error LLVMOutputStyle::dumpSectionMap() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 741 | if (!opts::raw::DumpSectionMap) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 742 | return Error::success(); |
| 743 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 744 | auto Dbi = File.getPDBDbiStream(); |
| 745 | if (!Dbi) |
| 746 | return Dbi.takeError(); |
| 747 | |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 748 | ListScope L(P, "Section Map"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 749 | for (auto &M : Dbi->getSectionMap()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 750 | DictScope D(P, "Entry"); |
| 751 | P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames()); |
| 752 | P.printNumber("Flags", M.Flags); |
| 753 | P.printNumber("Ovl", M.Ovl); |
| 754 | P.printNumber("Group", M.Group); |
| 755 | P.printNumber("Frame", M.Frame); |
| 756 | P.printNumber("SecName", M.SecName); |
| 757 | P.printNumber("ClassName", M.ClassName); |
| 758 | P.printNumber("Offset", M.Offset); |
| 759 | P.printNumber("SecByteLength", M.SecByteLength); |
| 760 | P.flush(); |
| 761 | } |
| 762 | return Error::success(); |
| 763 | } |
| 764 | |
| 765 | Error LLVMOutputStyle::dumpPublicsStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 766 | if (!opts::raw::DumpPublics) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 767 | return Error::success(); |
| 768 | |
| 769 | DictScope D(P, "Publics Stream"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 770 | auto Publics = File.getPDBPublicsStream(); |
| 771 | if (!Publics) |
| 772 | return Publics.takeError(); |
| 773 | |
| 774 | auto Dbi = File.getPDBDbiStream(); |
| 775 | if (!Dbi) |
| 776 | return Dbi.takeError(); |
| 777 | |
| 778 | P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex()); |
| 779 | P.printNumber("SymHash", Publics->getSymHash()); |
| 780 | P.printNumber("AddrMap", Publics->getAddrMap()); |
| 781 | P.printNumber("Number of buckets", Publics->getNumBuckets()); |
| 782 | P.printList("Hash Buckets", Publics->getHashBuckets()); |
| 783 | P.printList("Address Map", Publics->getAddressMap()); |
| 784 | P.printList("Thunk Map", Publics->getThunkMap()); |
| 785 | P.printList("Section Offsets", Publics->getSectionOffsets(), |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 786 | printSectionOffset); |
| 787 | ListScope L(P, "Symbols"); |
Zachary Turner | 5e3e4bb | 2016-08-05 21:45:34 +0000 | [diff] [blame] | 788 | codeview::CVSymbolDumper SD(P, Dumper, nullptr, false); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 789 | bool HadError = false; |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 790 | for (auto S : Publics->getSymbols(&HadError)) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 791 | DictScope DD(P, ""); |
| 792 | |
| 793 | SD.dump(S); |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 794 | if (opts::raw::DumpSymRecordBytes) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 795 | P.printBinaryBlock("Bytes", S.Data); |
| 796 | } |
| 797 | if (HadError) |
| 798 | return make_error<RawError>( |
| 799 | raw_error_code::corrupt_file, |
| 800 | "Public symbol stream contained corrupt record"); |
| 801 | |
| 802 | return Error::success(); |
| 803 | } |
| 804 | |
| 805 | Error LLVMOutputStyle::dumpSectionHeaders() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 806 | if (!opts::raw::DumpSectionHeaders) |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 807 | return Error::success(); |
| 808 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 809 | auto Dbi = File.getPDBDbiStream(); |
| 810 | if (!Dbi) |
| 811 | return Dbi.takeError(); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 812 | |
| 813 | ListScope D(P, "Section Headers"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 814 | for (const object::coff_section &Section : Dbi->getSectionHeaders()) { |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 815 | DictScope DD(P, ""); |
| 816 | |
| 817 | // If a name is 8 characters long, there is no NUL character at end. |
| 818 | StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name))); |
| 819 | P.printString("Name", Name); |
| 820 | P.printNumber("Virtual Size", Section.VirtualSize); |
| 821 | P.printNumber("Virtual Address", Section.VirtualAddress); |
| 822 | P.printNumber("Size of Raw Data", Section.SizeOfRawData); |
| 823 | P.printNumber("File Pointer to Raw Data", Section.PointerToRawData); |
| 824 | P.printNumber("File Pointer to Relocations", Section.PointerToRelocations); |
| 825 | P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers); |
| 826 | P.printNumber("Number of Relocations", Section.NumberOfRelocations); |
| 827 | P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers); |
Rui Ueyama | 2c5384a | 2016-06-06 21:34:55 +0000 | [diff] [blame] | 828 | P.printFlags("Characteristics", Section.Characteristics, |
| 829 | getImageSectionCharacteristicNames()); |
Zachary Turner | d311739 | 2016-06-03 19:28:33 +0000 | [diff] [blame] | 830 | } |
| 831 | return Error::success(); |
| 832 | } |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 833 | |
| 834 | Error LLVMOutputStyle::dumpFpoStream() { |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 835 | if (!opts::raw::DumpFpo) |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 836 | return Error::success(); |
| 837 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 838 | auto Dbi = File.getPDBDbiStream(); |
| 839 | if (!Dbi) |
| 840 | return Dbi.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 841 | |
| 842 | ListScope D(P, "New FPO"); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 843 | for (const object::FpoData &Fpo : Dbi->getFpoRecords()) { |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 844 | DictScope DD(P, ""); |
| 845 | P.printNumber("Offset", Fpo.Offset); |
| 846 | P.printNumber("Size", Fpo.Size); |
| 847 | P.printNumber("Number of locals", Fpo.NumLocals); |
| 848 | P.printNumber("Number of params", Fpo.NumParams); |
| 849 | P.printNumber("Size of Prolog", Fpo.getPrologSize()); |
| 850 | P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs()); |
| 851 | P.printBoolean("Has SEH", Fpo.hasSEH()); |
| 852 | P.printBoolean("Use BP", Fpo.useBP()); |
| 853 | P.printNumber("Frame Pointer", Fpo.getFP()); |
| 854 | } |
| 855 | return Error::success(); |
| 856 | } |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 857 | |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 858 | void LLVMOutputStyle::flush() { P.flush(); } |