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