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