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