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