Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 1 | //===- YAMLOutputStyle.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 "YAMLOutputStyle.h" |
| 11 | |
| 12 | #include "PdbYaml.h" |
| 13 | #include "llvm-pdbdump.h" |
| 14 | |
Zachary Turner | 3b14764 | 2016-10-08 01:12:01 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame^] | 16 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 18 | #include "llvm/DebugInfo/PDB/Native/ModStream.h" |
| 19 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/RawConstants.h" |
| 21 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace llvm::pdb; |
| 25 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 26 | YAMLOutputStyle::YAMLOutputStyle(PDBFile &File) |
| 27 | : File(File), Out(outs()), Obj(File.getAllocator()) {} |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 28 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 29 | Error YAMLOutputStyle::dump() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 30 | if (opts::pdb2yaml::StreamDirectory) |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 31 | opts::pdb2yaml::StreamMetadata = true; |
Zachary Turner | 3b14764 | 2016-10-08 01:12:01 +0000 | [diff] [blame] | 32 | if (opts::pdb2yaml::DbiModuleSyms) |
| 33 | opts::pdb2yaml::DbiModuleInfo = true; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 34 | if (opts::pdb2yaml::DbiModuleSourceFileInfo) |
| 35 | opts::pdb2yaml::DbiModuleInfo = true; |
| 36 | if (opts::pdb2yaml::DbiModuleInfo) |
| 37 | opts::pdb2yaml::DbiStream = true; |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 38 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 39 | if (auto EC = dumpFileHeaders()) |
| 40 | return EC; |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 41 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 42 | if (auto EC = dumpStreamMetadata()) |
| 43 | return EC; |
| 44 | |
| 45 | if (auto EC = dumpStreamDirectory()) |
| 46 | return EC; |
| 47 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 48 | if (auto EC = dumpStringTable()) |
| 49 | return EC; |
| 50 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 51 | if (auto EC = dumpPDBStream()) |
| 52 | return EC; |
| 53 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 54 | if (auto EC = dumpDbiStream()) |
| 55 | return EC; |
| 56 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 57 | if (auto EC = dumpTpiStream()) |
| 58 | return EC; |
| 59 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 60 | if (auto EC = dumpIpiStream()) |
| 61 | return EC; |
| 62 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 63 | flush(); |
| 64 | return Error::success(); |
| 65 | } |
| 66 | |
| 67 | Error YAMLOutputStyle::dumpFileHeaders() { |
Zachary Turner | f6b9382 | 2016-07-11 21:45:09 +0000 | [diff] [blame] | 68 | if (opts::pdb2yaml::NoFileHeaders) |
| 69 | return Error::success(); |
| 70 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 71 | yaml::MSFHeaders Headers; |
Zachary Turner | f6b9382 | 2016-07-11 21:45:09 +0000 | [diff] [blame] | 72 | Obj.Headers.emplace(); |
| 73 | Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount(); |
| 74 | Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex(); |
Zachary Turner | f6b9382 | 2016-07-11 21:45:09 +0000 | [diff] [blame] | 75 | Obj.Headers->SuperBlock.BlockSize = File.getBlockSize(); |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 76 | auto Blocks = File.getDirectoryBlockArray(); |
Zachary Turner | f6b9382 | 2016-07-11 21:45:09 +0000 | [diff] [blame] | 77 | Obj.Headers->DirectoryBlocks.assign(Blocks.begin(), Blocks.end()); |
| 78 | Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks(); |
| 79 | Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes(); |
| 80 | Obj.Headers->NumStreams = |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 81 | opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0; |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 82 | Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock(); |
Zachary Turner | f6b9382 | 2016-07-11 21:45:09 +0000 | [diff] [blame] | 83 | Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1(); |
| 84 | Obj.Headers->FileSize = File.getFileSize(); |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 85 | |
| 86 | return Error::success(); |
| 87 | } |
| 88 | |
Zachary Turner | 760ad4d | 2017-01-20 22:42:09 +0000 | [diff] [blame] | 89 | Error YAMLOutputStyle::dumpStringTable() { |
| 90 | if (!opts::pdb2yaml::StringTable) |
| 91 | return Error::success(); |
| 92 | |
| 93 | Obj.StringTable.emplace(); |
| 94 | auto ExpectedST = File.getStringTable(); |
| 95 | if (!ExpectedST) |
| 96 | return ExpectedST.takeError(); |
| 97 | |
| 98 | const auto &ST = ExpectedST.get(); |
| 99 | for (auto ID : ST.name_ids()) { |
| 100 | StringRef S = ST.getStringForID(ID); |
| 101 | if (!S.empty()) |
| 102 | Obj.StringTable->push_back(S); |
| 103 | } |
| 104 | return Error::success(); |
| 105 | } |
| 106 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 107 | Error YAMLOutputStyle::dumpStreamMetadata() { |
| 108 | if (!opts::pdb2yaml::StreamMetadata) |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 109 | return Error::success(); |
| 110 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 111 | Obj.StreamSizes.emplace(); |
| 112 | Obj.StreamSizes->assign(File.getStreamSizes().begin(), |
| 113 | File.getStreamSizes().end()); |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 114 | return Error::success(); |
| 115 | } |
| 116 | |
Zachary Turner | a30bd1a | 2016-06-30 17:42:48 +0000 | [diff] [blame] | 117 | Error YAMLOutputStyle::dumpStreamDirectory() { |
| 118 | if (!opts::pdb2yaml::StreamDirectory) |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 119 | return Error::success(); |
| 120 | |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 121 | auto StreamMap = File.getStreamMap(); |
| 122 | Obj.StreamMap.emplace(); |
| 123 | for (auto &Stream : StreamMap) { |
| 124 | pdb::yaml::StreamBlockList BlockList; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 125 | BlockList.Blocks.assign(Stream.begin(), Stream.end()); |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 126 | Obj.StreamMap->push_back(BlockList); |
Zachary Turner | 25e8b05 | 2016-06-06 20:37:17 +0000 | [diff] [blame] | 127 | } |
Zachary Turner | 25e8b05 | 2016-06-06 20:37:17 +0000 | [diff] [blame] | 128 | |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 129 | return Error::success(); |
| 130 | } |
| 131 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 132 | Error YAMLOutputStyle::dumpPDBStream() { |
| 133 | if (!opts::pdb2yaml::PdbStream) |
| 134 | return Error::success(); |
| 135 | |
| 136 | auto IS = File.getPDBInfoStream(); |
| 137 | if (!IS) |
| 138 | return IS.takeError(); |
| 139 | |
| 140 | auto &InfoS = IS.get(); |
| 141 | Obj.PdbStream.emplace(); |
| 142 | Obj.PdbStream->Age = InfoS.getAge(); |
| 143 | Obj.PdbStream->Guid = InfoS.getGuid(); |
| 144 | Obj.PdbStream->Signature = InfoS.getSignature(); |
| 145 | Obj.PdbStream->Version = InfoS.getVersion(); |
| 146 | |
| 147 | return Error::success(); |
| 148 | } |
| 149 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 150 | Error YAMLOutputStyle::dumpDbiStream() { |
| 151 | if (!opts::pdb2yaml::DbiStream) |
| 152 | return Error::success(); |
| 153 | |
| 154 | auto DbiS = File.getPDBDbiStream(); |
| 155 | if (!DbiS) |
| 156 | return DbiS.takeError(); |
| 157 | |
| 158 | auto &DS = DbiS.get(); |
| 159 | Obj.DbiStream.emplace(); |
| 160 | Obj.DbiStream->Age = DS.getAge(); |
| 161 | Obj.DbiStream->BuildNumber = DS.getBuildNumber(); |
| 162 | Obj.DbiStream->Flags = DS.getFlags(); |
| 163 | Obj.DbiStream->MachineType = DS.getMachineType(); |
| 164 | Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld(); |
| 165 | Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion(); |
| 166 | Obj.DbiStream->VerHeader = DS.getDbiVersion(); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 167 | if (opts::pdb2yaml::DbiModuleInfo) { |
| 168 | for (const auto &MI : DS.modules()) { |
| 169 | yaml::PdbDbiModuleInfo DMI; |
| 170 | DMI.Mod = MI.Info.getModuleName(); |
| 171 | DMI.Obj = MI.Info.getObjFileName(); |
| 172 | if (opts::pdb2yaml::DbiModuleSourceFileInfo) |
| 173 | DMI.SourceFiles = MI.SourceFiles; |
Zachary Turner | 3b14764 | 2016-10-08 01:12:01 +0000 | [diff] [blame] | 174 | |
| 175 | if (opts::pdb2yaml::DbiModuleSyms && |
| 176 | MI.Info.getModuleStreamIndex() != kInvalidStreamIndex) { |
| 177 | DMI.Modi.emplace(); |
| 178 | auto ModStreamData = msf::MappedBlockStream::createIndexedStream( |
| 179 | File.getMsfLayout(), File.getMsfBuffer(), |
| 180 | MI.Info.getModuleStreamIndex()); |
| 181 | |
| 182 | pdb::ModStream ModS(MI.Info, std::move(ModStreamData)); |
| 183 | if (auto EC = ModS.reload()) |
| 184 | return EC; |
| 185 | |
| 186 | DMI.Modi->Signature = ModS.signature(); |
| 187 | bool HadError = false; |
| 188 | for (auto &Sym : ModS.symbols(&HadError)) { |
| 189 | pdb::yaml::PdbSymbolRecord Record{Sym}; |
| 190 | DMI.Modi->Symbols.push_back(Record); |
| 191 | } |
| 192 | } |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 193 | Obj.DbiStream->ModInfos.push_back(DMI); |
| 194 | } |
| 195 | } |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 196 | return Error::success(); |
| 197 | } |
| 198 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 199 | Error YAMLOutputStyle::dumpTpiStream() { |
| 200 | if (!opts::pdb2yaml::TpiStream) |
| 201 | return Error::success(); |
| 202 | |
| 203 | auto TpiS = File.getPDBTpiStream(); |
| 204 | if (!TpiS) |
| 205 | return TpiS.takeError(); |
| 206 | |
| 207 | auto &TS = TpiS.get(); |
| 208 | Obj.TpiStream.emplace(); |
| 209 | Obj.TpiStream->Version = TS.getTpiVersion(); |
| 210 | for (auto &Record : TS.types(nullptr)) { |
| 211 | yaml::PdbTpiRecord R; |
| 212 | // It's not necessary to set R.RecordData here. That only exists as a |
| 213 | // way to have the `PdbTpiRecord` structure own the memory that `R.Record` |
| 214 | // references. In the case of reading an existing PDB though, that memory |
| 215 | // is owned by the backing stream. |
| 216 | R.Record = Record; |
| 217 | Obj.TpiStream->Records.push_back(R); |
| 218 | } |
| 219 | |
| 220 | return Error::success(); |
| 221 | } |
| 222 | |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame] | 223 | Error YAMLOutputStyle::dumpIpiStream() { |
| 224 | if (!opts::pdb2yaml::IpiStream) |
| 225 | return Error::success(); |
| 226 | |
| 227 | auto IpiS = File.getPDBIpiStream(); |
| 228 | if (!IpiS) |
| 229 | return IpiS.takeError(); |
| 230 | |
| 231 | auto &IS = IpiS.get(); |
| 232 | Obj.IpiStream.emplace(); |
| 233 | Obj.IpiStream->Version = IS.getTpiVersion(); |
| 234 | for (auto &Record : IS.types(nullptr)) { |
| 235 | yaml::PdbTpiRecord R; |
| 236 | R.Record = Record; |
| 237 | Obj.IpiStream->Records.push_back(R); |
| 238 | } |
| 239 | |
| 240 | return Error::success(); |
| 241 | } |
| 242 | |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 243 | void YAMLOutputStyle::flush() { |
| 244 | Out << Obj; |
| 245 | outs().flush(); |
| 246 | } |