llvm-pdbutil.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. //===- llvm-pdbutil.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Dumps debug information present in PDB files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm-pdbutil.h"
  13. #include "BytesOutputStyle.h"
  14. #include "DumpOutputStyle.h"
  15. #include "ExplainOutputStyle.h"
  16. #include "OutputStyle.h"
  17. #include "PrettyClassDefinitionDumper.h"
  18. #include "PrettyCompilandDumper.h"
  19. #include "PrettyEnumDumper.h"
  20. #include "PrettyExternalSymbolDumper.h"
  21. #include "PrettyFunctionDumper.h"
  22. #include "PrettyTypeDumper.h"
  23. #include "PrettyTypedefDumper.h"
  24. #include "PrettyVariableDumper.h"
  25. #include "YAMLOutputStyle.h"
  26. #include "llvm/ADT/ArrayRef.h"
  27. #include "llvm/ADT/BitVector.h"
  28. #include "llvm/ADT/DenseMap.h"
  29. #include "llvm/ADT/STLExtras.h"
  30. #include "llvm/ADT/StringExtras.h"
  31. #include "llvm/BinaryFormat/Magic.h"
  32. #include "llvm/Config/config.h"
  33. #include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
  34. #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
  35. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  36. #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
  37. #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
  38. #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
  39. #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
  40. #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
  41. #include "llvm/DebugInfo/MSF/MSFBuilder.h"
  42. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  43. #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
  44. #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
  45. #include "llvm/DebugInfo/PDB/IPDBInjectedSource.h"
  46. #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
  47. #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
  48. #include "llvm/DebugInfo/PDB/IPDBSession.h"
  49. #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
  50. #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
  51. #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
  52. #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
  53. #include "llvm/DebugInfo/PDB/Native/InputFile.h"
  54. #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
  55. #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
  56. #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
  57. #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
  58. #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
  59. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  60. #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
  61. #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
  62. #include "llvm/DebugInfo/PDB/PDB.h"
  63. #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
  64. #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
  65. #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
  66. #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
  67. #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
  68. #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
  69. #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
  70. #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
  71. #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
  72. #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
  73. #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
  74. #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
  75. #include "llvm/Support/BinaryByteStream.h"
  76. #include "llvm/Support/COM.h"
  77. #include "llvm/Support/CommandLine.h"
  78. #include "llvm/Support/ConvertUTF.h"
  79. #include "llvm/Support/FileOutputBuffer.h"
  80. #include "llvm/Support/FileSystem.h"
  81. #include "llvm/Support/Format.h"
  82. #include "llvm/Support/InitLLVM.h"
  83. #include "llvm/Support/LineIterator.h"
  84. #include "llvm/Support/MemoryBuffer.h"
  85. #include "llvm/Support/Path.h"
  86. #include "llvm/Support/PrettyStackTrace.h"
  87. #include "llvm/Support/Process.h"
  88. #include "llvm/Support/Regex.h"
  89. #include "llvm/Support/ScopedPrinter.h"
  90. #include "llvm/Support/Signals.h"
  91. #include "llvm/Support/raw_ostream.h"
  92. using namespace llvm;
  93. using namespace llvm::codeview;
  94. using namespace llvm::msf;
  95. using namespace llvm::pdb;
  96. namespace opts {
  97. cl::SubCommand DumpSubcommand("dump", "Dump MSF and CodeView debug info");
  98. cl::SubCommand BytesSubcommand("bytes", "Dump raw bytes from the PDB file");
  99. cl::SubCommand DiaDumpSubcommand("diadump",
  100. "Dump debug information using a DIA-like API");
  101. cl::SubCommand
  102. PrettySubcommand("pretty",
  103. "Dump semantic information about types and symbols");
  104. cl::SubCommand
  105. YamlToPdbSubcommand("yaml2pdb",
  106. "Generate a PDB file from a YAML description");
  107. cl::SubCommand
  108. PdbToYamlSubcommand("pdb2yaml",
  109. "Generate a detailed YAML description of a PDB File");
  110. cl::SubCommand MergeSubcommand("merge",
  111. "Merge multiple PDBs into a single PDB");
  112. cl::SubCommand ExplainSubcommand("explain",
  113. "Explain the meaning of a file offset");
  114. cl::SubCommand ExportSubcommand("export",
  115. "Write binary data from a stream to a file");
  116. cl::OptionCategory TypeCategory("Symbol Type Options");
  117. cl::OptionCategory FilterCategory("Filtering and Sorting Options");
  118. cl::OptionCategory OtherOptions("Other Options");
  119. cl::ValuesClass ChunkValues = cl::values(
  120. clEnumValN(ModuleSubsection::CrossScopeExports, "cme",
  121. "Cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
  122. clEnumValN(ModuleSubsection::CrossScopeImports, "cmi",
  123. "Cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
  124. clEnumValN(ModuleSubsection::FileChecksums, "fc",
  125. "File checksums (DEBUG_S_CHECKSUMS subsection)"),
  126. clEnumValN(ModuleSubsection::InlineeLines, "ilines",
  127. "Inlinee lines (DEBUG_S_INLINEELINES subsection)"),
  128. clEnumValN(ModuleSubsection::Lines, "lines",
  129. "Lines (DEBUG_S_LINES subsection)"),
  130. clEnumValN(ModuleSubsection::StringTable, "strings",
  131. "String Table (DEBUG_S_STRINGTABLE subsection) (not "
  132. "typically present in PDB file)"),
  133. clEnumValN(ModuleSubsection::FrameData, "frames",
  134. "Frame Data (DEBUG_S_FRAMEDATA subsection)"),
  135. clEnumValN(ModuleSubsection::Symbols, "symbols",
  136. "Symbols (DEBUG_S_SYMBOLS subsection) (not typically "
  137. "present in PDB file)"),
  138. clEnumValN(ModuleSubsection::CoffSymbolRVAs, "rvas",
  139. "COFF Symbol RVAs (DEBUG_S_COFF_SYMBOL_RVA subsection)"),
  140. clEnumValN(ModuleSubsection::Unknown, "unknown",
  141. "Any subsection not covered by another option"),
  142. clEnumValN(ModuleSubsection::All, "all", "All known subsections"));
  143. namespace diadump {
  144. cl::list<std::string> InputFilenames(cl::Positional,
  145. cl::desc("<input PDB files>"),
  146. cl::OneOrMore, cl::sub(DiaDumpSubcommand));
  147. cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
  148. cl::sub(DiaDumpSubcommand));
  149. static cl::opt<bool>
  150. ShowClassHierarchy("hierarchy", cl::desc("Show lexical and class parents"),
  151. cl::sub(DiaDumpSubcommand));
  152. static cl::opt<bool> NoSymIndexIds(
  153. "no-ids",
  154. cl::desc("Don't show any SymIndexId fields (overrides -hierarchy)"),
  155. cl::sub(DiaDumpSubcommand));
  156. static cl::opt<bool>
  157. Recurse("recurse",
  158. cl::desc("When dumping a SymIndexId, dump the full details of the "
  159. "corresponding record"),
  160. cl::sub(DiaDumpSubcommand));
  161. static cl::opt<bool> Enums("enums", cl::desc("Dump enum types"),
  162. cl::sub(DiaDumpSubcommand));
  163. static cl::opt<bool> Pointers("pointers", cl::desc("Dump enum types"),
  164. cl::sub(DiaDumpSubcommand));
  165. static cl::opt<bool> UDTs("udts", cl::desc("Dump udt types"),
  166. cl::sub(DiaDumpSubcommand));
  167. static cl::opt<bool> Compilands("compilands",
  168. cl::desc("Dump compiland information"),
  169. cl::sub(DiaDumpSubcommand));
  170. static cl::opt<bool> Funcsigs("funcsigs",
  171. cl::desc("Dump function signature information"),
  172. cl::sub(DiaDumpSubcommand));
  173. static cl::opt<bool> Arrays("arrays", cl::desc("Dump array types"),
  174. cl::sub(DiaDumpSubcommand));
  175. static cl::opt<bool> VTShapes("vtshapes", cl::desc("Dump virtual table shapes"),
  176. cl::sub(DiaDumpSubcommand));
  177. static cl::opt<bool> Typedefs("typedefs", cl::desc("Dump typedefs"),
  178. cl::sub(DiaDumpSubcommand));
  179. } // namespace diadump
  180. FilterOptions Filters;
  181. namespace pretty {
  182. cl::list<std::string> InputFilenames(cl::Positional,
  183. cl::desc("<input PDB files>"),
  184. cl::OneOrMore, cl::sub(PrettySubcommand));
  185. cl::opt<bool> InjectedSources("injected-sources",
  186. cl::desc("Display injected sources"),
  187. cl::cat(OtherOptions), cl::sub(PrettySubcommand));
  188. cl::opt<bool> ShowInjectedSourceContent(
  189. "injected-source-content",
  190. cl::desc("When displaying an injected source, display the file content"),
  191. cl::cat(OtherOptions), cl::sub(PrettySubcommand));
  192. cl::list<std::string> WithName(
  193. "with-name",
  194. cl::desc("Display any symbol or type with the specified exact name"),
  195. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  196. cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
  197. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  198. cl::opt<bool> Symbols("module-syms",
  199. cl::desc("Display symbols for each compiland"),
  200. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  201. cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
  202. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  203. cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
  204. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  205. cl::list<SymLevel> SymTypes(
  206. "sym-types", cl::desc("Type of symbols to dump (default all)"),
  207. cl::cat(TypeCategory), cl::sub(PrettySubcommand),
  208. cl::values(
  209. clEnumValN(SymLevel::Thunks, "thunks", "Display thunk symbols"),
  210. clEnumValN(SymLevel::Data, "data", "Display data symbols"),
  211. clEnumValN(SymLevel::Functions, "funcs", "Display function symbols"),
  212. clEnumValN(SymLevel::All, "all", "Display all symbols (default)")));
  213. cl::opt<bool>
  214. Types("types",
  215. cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
  216. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  217. cl::opt<bool> Classes("classes", cl::desc("Display class types"),
  218. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  219. cl::opt<bool> Enums("enums", cl::desc("Display enum types"),
  220. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  221. cl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
  222. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  223. cl::opt<bool> Funcsigs("funcsigs", cl::desc("Display function signatures"),
  224. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  225. cl::opt<bool> Pointers("pointers", cl::desc("Display pointer types"),
  226. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  227. cl::opt<bool> Arrays("arrays", cl::desc("Display arrays"),
  228. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  229. cl::opt<bool> VTShapes("vtshapes", cl::desc("Display vftable shapes"),
  230. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  231. cl::opt<SymbolSortMode> SymbolOrder(
  232. "symbol-order", cl::desc("symbol sort order"),
  233. cl::init(SymbolSortMode::None),
  234. cl::values(clEnumValN(SymbolSortMode::None, "none",
  235. "Undefined / no particular sort order"),
  236. clEnumValN(SymbolSortMode::Name, "name", "Sort symbols by name"),
  237. clEnumValN(SymbolSortMode::Size, "size",
  238. "Sort symbols by size")),
  239. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  240. cl::opt<ClassSortMode> ClassOrder(
  241. "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
  242. cl::values(
  243. clEnumValN(ClassSortMode::None, "none",
  244. "Undefined / no particular sort order"),
  245. clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
  246. clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
  247. clEnumValN(ClassSortMode::Padding, "padding",
  248. "Sort classes by amount of padding"),
  249. clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
  250. "Sort classes by percentage of space consumed by padding"),
  251. clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
  252. "Sort classes by amount of immediate padding"),
  253. clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
  254. "Sort classes by percentage of space consumed by immediate "
  255. "padding")),
  256. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  257. cl::opt<ClassDefinitionFormat> ClassFormat(
  258. "class-definitions", cl::desc("Class definition format"),
  259. cl::init(ClassDefinitionFormat::All),
  260. cl::values(
  261. clEnumValN(ClassDefinitionFormat::All, "all",
  262. "Display all class members including data, constants, "
  263. "typedefs, functions, etc"),
  264. clEnumValN(ClassDefinitionFormat::Layout, "layout",
  265. "Only display members that contribute to class size."),
  266. clEnumValN(ClassDefinitionFormat::None, "none",
  267. "Don't display class definitions")),
  268. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  269. cl::opt<uint32_t> ClassRecursionDepth(
  270. "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
  271. cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  272. cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
  273. cl::sub(PrettySubcommand));
  274. cl::opt<bool>
  275. All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
  276. cl::cat(TypeCategory), cl::sub(PrettySubcommand));
  277. cl::opt<uint64_t> LoadAddress(
  278. "load-address",
  279. cl::desc("Assume the module is loaded at the specified address"),
  280. cl::cat(OtherOptions), cl::sub(PrettySubcommand));
  281. cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
  282. cl::cat(OtherOptions), cl::sub(PrettySubcommand));
  283. cl::opt<cl::boolOrDefault>
  284. ColorOutput("color-output",
  285. cl::desc("Override use of color (default = isatty)"),
  286. cl::cat(OtherOptions), cl::sub(PrettySubcommand));
  287. cl::list<std::string>
  288. ExcludeTypes("exclude-types",
  289. cl::desc("Exclude types by regular expression"),
  290. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  291. cl::list<std::string>
  292. ExcludeSymbols("exclude-symbols",
  293. cl::desc("Exclude symbols by regular expression"),
  294. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  295. cl::list<std::string>
  296. ExcludeCompilands("exclude-compilands",
  297. cl::desc("Exclude compilands by regular expression"),
  298. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  299. cl::list<std::string> IncludeTypes(
  300. "include-types",
  301. cl::desc("Include only types which match a regular expression"),
  302. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  303. cl::list<std::string> IncludeSymbols(
  304. "include-symbols",
  305. cl::desc("Include only symbols which match a regular expression"),
  306. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  307. cl::list<std::string> IncludeCompilands(
  308. "include-compilands",
  309. cl::desc("Include only compilands those which match a regular expression"),
  310. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  311. cl::opt<uint32_t> SizeThreshold(
  312. "min-type-size", cl::desc("Displays only those types which are greater "
  313. "than or equal to the specified size."),
  314. cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  315. cl::opt<uint32_t> PaddingThreshold(
  316. "min-class-padding", cl::desc("Displays only those classes which have at "
  317. "least the specified amount of padding."),
  318. cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  319. cl::opt<uint32_t> ImmediatePaddingThreshold(
  320. "min-class-padding-imm",
  321. cl::desc("Displays only those classes which have at least the specified "
  322. "amount of immediate padding, ignoring padding internal to bases "
  323. "and aggregates."),
  324. cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  325. cl::opt<bool> ExcludeCompilerGenerated(
  326. "no-compiler-generated",
  327. cl::desc("Don't show compiler generated types and symbols"),
  328. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  329. cl::opt<bool>
  330. ExcludeSystemLibraries("no-system-libs",
  331. cl::desc("Don't show symbols from system libraries"),
  332. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  333. cl::opt<bool> NoEnumDefs("no-enum-definitions",
  334. cl::desc("Don't display full enum definitions"),
  335. cl::cat(FilterCategory), cl::sub(PrettySubcommand));
  336. }
  337. cl::OptionCategory FileOptions("Module & File Options");
  338. namespace bytes {
  339. cl::OptionCategory MsfBytes("MSF File Options");
  340. cl::OptionCategory DbiBytes("Dbi Stream Options");
  341. cl::OptionCategory PdbBytes("PDB Stream Options");
  342. cl::OptionCategory Types("Type Options");
  343. cl::OptionCategory ModuleCategory("Module Options");
  344. std::optional<NumberRange> DumpBlockRange;
  345. std::optional<NumberRange> DumpByteRange;
  346. cl::opt<std::string> DumpBlockRangeOpt(
  347. "block-range", cl::value_desc("start[-end]"),
  348. cl::desc("Dump binary data from specified range of blocks."),
  349. cl::sub(BytesSubcommand), cl::cat(MsfBytes));
  350. cl::opt<std::string>
  351. DumpByteRangeOpt("byte-range", cl::value_desc("start[-end]"),
  352. cl::desc("Dump binary data from specified range of bytes"),
  353. cl::sub(BytesSubcommand), cl::cat(MsfBytes));
  354. cl::list<std::string>
  355. DumpStreamData("stream-data", cl::CommaSeparated,
  356. cl::desc("Dump binary data from specified streams. Format "
  357. "is SN[:Start][@Size]"),
  358. cl::sub(BytesSubcommand), cl::cat(MsfBytes));
  359. cl::opt<bool> NameMap("name-map", cl::desc("Dump bytes of PDB Name Map"),
  360. cl::sub(BytesSubcommand), cl::cat(PdbBytes));
  361. cl::opt<bool> Fpm("fpm", cl::desc("Dump free page map"),
  362. cl::sub(BytesSubcommand), cl::cat(MsfBytes));
  363. cl::opt<bool> SectionContributions("sc", cl::desc("Dump section contributions"),
  364. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  365. cl::opt<bool> SectionMap("sm", cl::desc("Dump section map"),
  366. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  367. cl::opt<bool> ModuleInfos("modi", cl::desc("Dump module info"),
  368. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  369. cl::opt<bool> FileInfo("files", cl::desc("Dump source file info"),
  370. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  371. cl::opt<bool> TypeServerMap("type-server", cl::desc("Dump type server map"),
  372. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  373. cl::opt<bool> ECData("ec", cl::desc("Dump edit and continue map"),
  374. cl::sub(BytesSubcommand), cl::cat(DbiBytes));
  375. cl::list<uint32_t> TypeIndex(
  376. "type", cl::desc("Dump the type record with the given type index"),
  377. cl::CommaSeparated, cl::sub(BytesSubcommand), cl::cat(TypeCategory));
  378. cl::list<uint32_t>
  379. IdIndex("id", cl::desc("Dump the id record with the given type index"),
  380. cl::CommaSeparated, cl::sub(BytesSubcommand),
  381. cl::cat(TypeCategory));
  382. cl::opt<uint32_t> ModuleIndex(
  383. "mod",
  384. cl::desc(
  385. "Limit options in the Modules category to the specified module index"),
  386. cl::Optional, cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
  387. cl::opt<bool> ModuleSyms("syms", cl::desc("Dump symbol record substream"),
  388. cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
  389. cl::opt<bool> ModuleC11("c11-chunks", cl::Hidden,
  390. cl::desc("Dump C11 CodeView debug chunks"),
  391. cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
  392. cl::opt<bool> ModuleC13("chunks",
  393. cl::desc("Dump C13 CodeView debug chunk subsection"),
  394. cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
  395. cl::opt<bool> SplitChunks(
  396. "split-chunks",
  397. cl::desc(
  398. "When dumping debug chunks, show a different section for each chunk"),
  399. cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
  400. cl::list<std::string> InputFilenames(cl::Positional,
  401. cl::desc("<input PDB files>"),
  402. cl::OneOrMore, cl::sub(BytesSubcommand));
  403. } // namespace bytes
  404. namespace dump {
  405. cl::OptionCategory MsfOptions("MSF Container Options");
  406. cl::OptionCategory TypeOptions("Type Record Options");
  407. cl::OptionCategory SymbolOptions("Symbol Options");
  408. cl::OptionCategory MiscOptions("Miscellaneous Options");
  409. // MSF OPTIONS
  410. cl::opt<bool> DumpSummary("summary", cl::desc("dump file summary"),
  411. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  412. cl::opt<bool> DumpStreams("streams",
  413. cl::desc("dump summary of the PDB streams"),
  414. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  415. cl::opt<bool> DumpStreamBlocks(
  416. "stream-blocks",
  417. cl::desc("Add block information to the output of -streams"),
  418. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  419. cl::opt<bool> DumpSymbolStats(
  420. "sym-stats",
  421. cl::desc("Dump a detailed breakdown of symbol usage/size for each module"),
  422. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  423. cl::opt<bool> DumpTypeStats(
  424. "type-stats",
  425. cl::desc("Dump a detailed breakdown of type usage/size"),
  426. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  427. cl::opt<bool> DumpIDStats(
  428. "id-stats",
  429. cl::desc("Dump a detailed breakdown of IPI types usage/size"),
  430. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  431. cl::opt<bool> DumpUdtStats(
  432. "udt-stats",
  433. cl::desc("Dump a detailed breakdown of S_UDT record usage / stats"),
  434. cl::cat(MsfOptions), cl::sub(DumpSubcommand));
  435. // TYPE OPTIONS
  436. cl::opt<bool> DumpTypes("types",
  437. cl::desc("dump CodeView type records from TPI stream"),
  438. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  439. cl::opt<bool> DumpTypeData(
  440. "type-data",
  441. cl::desc("dump CodeView type record raw bytes from TPI stream"),
  442. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  443. cl::opt<bool>
  444. DumpTypeRefStats("type-ref-stats",
  445. cl::desc("dump statistics on the number and size of types "
  446. "transitively referenced by symbol records"),
  447. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  448. cl::opt<bool> DumpTypeExtras("type-extras",
  449. cl::desc("dump type hashes and index offsets"),
  450. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  451. cl::opt<bool> DontResolveForwardRefs(
  452. "dont-resolve-forward-refs",
  453. cl::desc("When dumping type records for classes, unions, enums, and "
  454. "structs, don't try to resolve forward references"),
  455. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  456. cl::list<uint32_t> DumpTypeIndex(
  457. "type-index", cl::CommaSeparated,
  458. cl::desc("only dump types with the specified hexadecimal type index"),
  459. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  460. cl::opt<bool> DumpIds("ids",
  461. cl::desc("dump CodeView type records from IPI stream"),
  462. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  463. cl::opt<bool>
  464. DumpIdData("id-data",
  465. cl::desc("dump CodeView type record raw bytes from IPI stream"),
  466. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  467. cl::opt<bool> DumpIdExtras("id-extras",
  468. cl::desc("dump id hashes and index offsets"),
  469. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  470. cl::list<uint32_t> DumpIdIndex(
  471. "id-index", cl::CommaSeparated,
  472. cl::desc("only dump ids with the specified hexadecimal type index"),
  473. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  474. cl::opt<bool> DumpTypeDependents(
  475. "dependents",
  476. cl::desc("In conjunection with -type-index and -id-index, dumps the entire "
  477. "dependency graph for the specified index instead of "
  478. "just the single record with the specified index"),
  479. cl::cat(TypeOptions), cl::sub(DumpSubcommand));
  480. // SYMBOL OPTIONS
  481. cl::opt<bool> DumpGlobals("globals", cl::desc("dump Globals symbol records"),
  482. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  483. cl::opt<bool> DumpGlobalExtras("global-extras", cl::desc("dump Globals hashes"),
  484. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  485. cl::list<std::string> DumpGlobalNames(
  486. "global-name",
  487. cl::desc(
  488. "With -globals, only dump globals whose name matches the given value"),
  489. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  490. cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
  491. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  492. cl::opt<bool> DumpPublicExtras("public-extras",
  493. cl::desc("dump Publics hashes and address maps"),
  494. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  495. cl::opt<bool>
  496. DumpGSIRecords("gsi-records",
  497. cl::desc("dump public / global common record stream"),
  498. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  499. cl::opt<bool> DumpSymbols("symbols", cl::desc("dump module symbols"),
  500. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  501. cl::opt<bool>
  502. DumpSymRecordBytes("sym-data",
  503. cl::desc("dump CodeView symbol record raw bytes"),
  504. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  505. cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"),
  506. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  507. cl::opt<uint32_t> DumpSymbolOffset(
  508. "symbol-offset", cl::Optional,
  509. cl::desc("only dump symbol record with the specified symbol offset"),
  510. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  511. cl::opt<bool> DumpParents("show-parents",
  512. cl::desc("dump the symbols record's all parents."),
  513. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  514. cl::opt<uint32_t>
  515. DumpParentDepth("parent-recurse-depth", cl::Optional, cl::init(-1U),
  516. cl::desc("only recurse to a depth of N when displaying "
  517. "parents of a symbol record."),
  518. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  519. cl::opt<bool> DumpChildren("show-children",
  520. cl::desc("dump the symbols record's all children."),
  521. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  522. cl::opt<uint32_t>
  523. DumpChildrenDepth("children-recurse-depth", cl::Optional, cl::init(-1U),
  524. cl::desc("only recurse to a depth of N when displaying "
  525. "children of a symbol record."),
  526. cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
  527. // MODULE & FILE OPTIONS
  528. cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
  529. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  530. cl::opt<bool> DumpModuleFiles(
  531. "files",
  532. cl::desc("Dump the source files that contribute to each module's."),
  533. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  534. cl::opt<bool> DumpLines(
  535. "l",
  536. cl::desc("dump source file/line information (DEBUG_S_LINES subsection)"),
  537. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  538. cl::opt<bool> DumpInlineeLines(
  539. "il",
  540. cl::desc("dump inlinee line information (DEBUG_S_INLINEELINES subsection)"),
  541. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  542. cl::opt<bool> DumpXmi(
  543. "xmi",
  544. cl::desc(
  545. "dump cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
  546. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  547. cl::opt<bool> DumpXme(
  548. "xme",
  549. cl::desc(
  550. "dump cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
  551. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  552. cl::opt<uint32_t> DumpModi("modi", cl::Optional,
  553. cl::desc("For all options that iterate over "
  554. "modules, limit to the specified module"),
  555. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  556. cl::opt<bool> JustMyCode("jmc", cl::Optional,
  557. cl::desc("For all options that iterate over modules, "
  558. "ignore modules from system libraries"),
  559. cl::cat(FileOptions), cl::sub(DumpSubcommand));
  560. // MISCELLANEOUS OPTIONS
  561. cl::opt<bool> DumpNamedStreams("named-streams",
  562. cl::desc("dump PDB named stream table"),
  563. cl::cat(MiscOptions), cl::sub(DumpSubcommand));
  564. cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
  565. cl::cat(MiscOptions), cl::sub(DumpSubcommand));
  566. cl::opt<bool> DumpStringTableDetails("string-table-details",
  567. cl::desc("dump PDB String Table Details"),
  568. cl::cat(MiscOptions),
  569. cl::sub(DumpSubcommand));
  570. cl::opt<bool> DumpSectionContribs("section-contribs",
  571. cl::desc("dump section contributions"),
  572. cl::cat(MiscOptions),
  573. cl::sub(DumpSubcommand));
  574. cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
  575. cl::cat(MiscOptions), cl::sub(DumpSubcommand));
  576. cl::opt<bool> DumpSectionHeaders("section-headers",
  577. cl::desc("Dump image section headers"),
  578. cl::cat(MiscOptions), cl::sub(DumpSubcommand));
  579. cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
  580. cl::cat(MiscOptions), cl::sub(DumpSubcommand));
  581. cl::list<std::string> InputFilenames(cl::Positional,
  582. cl::desc("<input PDB files>"),
  583. cl::OneOrMore, cl::sub(DumpSubcommand));
  584. }
  585. namespace yaml2pdb {
  586. cl::opt<std::string>
  587. YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
  588. cl::sub(YamlToPdbSubcommand));
  589. cl::opt<std::string> InputFilename(cl::Positional,
  590. cl::desc("<input YAML file>"), cl::Required,
  591. cl::sub(YamlToPdbSubcommand));
  592. }
  593. namespace pdb2yaml {
  594. cl::opt<bool> All("all",
  595. cl::desc("Dump everything we know how to dump."),
  596. cl::sub(PdbToYamlSubcommand), cl::init(false));
  597. cl::opt<bool> NoFileHeaders("no-file-headers",
  598. cl::desc("Do not dump MSF file headers"),
  599. cl::sub(PdbToYamlSubcommand), cl::init(false));
  600. cl::opt<bool> Minimal("minimal",
  601. cl::desc("Don't write fields with default values"),
  602. cl::sub(PdbToYamlSubcommand), cl::init(false));
  603. cl::opt<bool> StreamMetadata(
  604. "stream-metadata",
  605. cl::desc("Dump the number of streams and each stream's size"),
  606. cl::sub(PdbToYamlSubcommand), cl::init(false));
  607. cl::opt<bool> StreamDirectory(
  608. "stream-directory",
  609. cl::desc("Dump each stream's block map (implies -stream-metadata)"),
  610. cl::sub(PdbToYamlSubcommand), cl::init(false));
  611. cl::opt<bool> PdbStream("pdb-stream",
  612. cl::desc("Dump the PDB Stream (Stream 1)"),
  613. cl::sub(PdbToYamlSubcommand), cl::init(false));
  614. cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
  615. cl::sub(PdbToYamlSubcommand), cl::init(false));
  616. cl::opt<bool> DbiStream("dbi-stream",
  617. cl::desc("Dump the DBI Stream Headers (Stream 2)"),
  618. cl::sub(PdbToYamlSubcommand), cl::init(false));
  619. cl::opt<bool> TpiStream("tpi-stream",
  620. cl::desc("Dump the TPI Stream (Stream 3)"),
  621. cl::sub(PdbToYamlSubcommand), cl::init(false));
  622. cl::opt<bool> IpiStream("ipi-stream",
  623. cl::desc("Dump the IPI Stream (Stream 5)"),
  624. cl::sub(PdbToYamlSubcommand), cl::init(false));
  625. cl::opt<bool> PublicsStream("publics-stream",
  626. cl::desc("Dump the Publics Stream"),
  627. cl::sub(PdbToYamlSubcommand), cl::init(false));
  628. // MODULE & FILE OPTIONS
  629. cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
  630. cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
  631. cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
  632. cl::cat(FileOptions),
  633. cl::sub(PdbToYamlSubcommand));
  634. cl::list<ModuleSubsection> DumpModuleSubsections(
  635. "subsections", cl::CommaSeparated,
  636. cl::desc("dump subsections from each module's debug stream"), ChunkValues,
  637. cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
  638. cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
  639. cl::cat(FileOptions),
  640. cl::sub(PdbToYamlSubcommand));
  641. cl::list<std::string> InputFilename(cl::Positional,
  642. cl::desc("<input PDB file>"), cl::Required,
  643. cl::sub(PdbToYamlSubcommand));
  644. } // namespace pdb2yaml
  645. namespace merge {
  646. cl::list<std::string> InputFilenames(cl::Positional,
  647. cl::desc("<input PDB files>"),
  648. cl::OneOrMore, cl::sub(MergeSubcommand));
  649. cl::opt<std::string>
  650. PdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
  651. cl::sub(MergeSubcommand));
  652. }
  653. namespace explain {
  654. cl::list<std::string> InputFilename(cl::Positional,
  655. cl::desc("<input PDB file>"), cl::Required,
  656. cl::sub(ExplainSubcommand));
  657. cl::list<uint64_t> Offsets("offset", cl::desc("The file offset to explain"),
  658. cl::sub(ExplainSubcommand), cl::OneOrMore);
  659. cl::opt<InputFileType> InputType(
  660. "input-type", cl::desc("Specify how to interpret the input file"),
  661. cl::init(InputFileType::PDBFile), cl::Optional, cl::sub(ExplainSubcommand),
  662. cl::values(clEnumValN(InputFileType::PDBFile, "pdb-file",
  663. "Treat input as a PDB file (default)"),
  664. clEnumValN(InputFileType::PDBStream, "pdb-stream",
  665. "Treat input as raw contents of PDB stream"),
  666. clEnumValN(InputFileType::DBIStream, "dbi-stream",
  667. "Treat input as raw contents of DBI stream"),
  668. clEnumValN(InputFileType::Names, "names-stream",
  669. "Treat input as raw contents of /names named stream"),
  670. clEnumValN(InputFileType::ModuleStream, "mod-stream",
  671. "Treat input as raw contents of a module stream")));
  672. } // namespace explain
  673. namespace exportstream {
  674. cl::list<std::string> InputFilename(cl::Positional,
  675. cl::desc("<input PDB file>"), cl::Required,
  676. cl::sub(ExportSubcommand));
  677. cl::opt<std::string> OutputFile("out",
  678. cl::desc("The file to write the stream to"),
  679. cl::Required, cl::sub(ExportSubcommand));
  680. cl::opt<std::string>
  681. Stream("stream", cl::Required,
  682. cl::desc("The index or name of the stream whose contents to export"),
  683. cl::sub(ExportSubcommand));
  684. cl::opt<bool> ForceName("name",
  685. cl::desc("Force the interpretation of -stream as a "
  686. "string, even if it is a valid integer"),
  687. cl::sub(ExportSubcommand), cl::Optional,
  688. cl::init(false));
  689. } // namespace exportstream
  690. }
  691. static ExitOnError ExitOnErr;
  692. static void yamlToPdb(StringRef Path) {
  693. BumpPtrAllocator Allocator;
  694. ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
  695. MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
  696. /*RequiresNullTerminator=*/false);
  697. if (ErrorOrBuffer.getError()) {
  698. ExitOnErr(createFileError(Path, errorCodeToError(ErrorOrBuffer.getError())));
  699. }
  700. std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
  701. llvm::yaml::Input In(Buffer->getBuffer());
  702. pdb::yaml::PdbObject YamlObj(Allocator);
  703. In >> YamlObj;
  704. PDBFileBuilder Builder(Allocator);
  705. uint32_t BlockSize = 4096;
  706. if (YamlObj.Headers)
  707. BlockSize = YamlObj.Headers->SuperBlock.BlockSize;
  708. ExitOnErr(Builder.initialize(BlockSize));
  709. // Add each of the reserved streams. We ignore stream metadata in the
  710. // yaml, because we will reconstruct our own view of the streams. For
  711. // example, the YAML may say that there were 20 streams in the original
  712. // PDB, but maybe we only dump a subset of those 20 streams, so we will
  713. // have fewer, and the ones we do have may end up with different indices
  714. // than the ones in the original PDB. So we just start with a clean slate.
  715. for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
  716. ExitOnErr(Builder.getMsfBuilder().addStream(0));
  717. StringsAndChecksums Strings;
  718. Strings.setStrings(std::make_shared<DebugStringTableSubsection>());
  719. if (YamlObj.StringTable) {
  720. for (auto S : *YamlObj.StringTable)
  721. Strings.strings()->insert(S);
  722. }
  723. pdb::yaml::PdbInfoStream DefaultInfoStream;
  724. pdb::yaml::PdbDbiStream DefaultDbiStream;
  725. pdb::yaml::PdbTpiStream DefaultTpiStream;
  726. pdb::yaml::PdbTpiStream DefaultIpiStream;
  727. const auto &Info = YamlObj.PdbStream.value_or(DefaultInfoStream);
  728. auto &InfoBuilder = Builder.getInfoBuilder();
  729. InfoBuilder.setAge(Info.Age);
  730. InfoBuilder.setGuid(Info.Guid);
  731. InfoBuilder.setSignature(Info.Signature);
  732. InfoBuilder.setVersion(Info.Version);
  733. for (auto F : Info.Features)
  734. InfoBuilder.addFeature(F);
  735. const auto &Dbi = YamlObj.DbiStream.value_or(DefaultDbiStream);
  736. auto &DbiBuilder = Builder.getDbiBuilder();
  737. DbiBuilder.setAge(Dbi.Age);
  738. DbiBuilder.setBuildNumber(Dbi.BuildNumber);
  739. DbiBuilder.setFlags(Dbi.Flags);
  740. DbiBuilder.setMachineType(Dbi.MachineType);
  741. DbiBuilder.setPdbDllRbld(Dbi.PdbDllRbld);
  742. DbiBuilder.setPdbDllVersion(Dbi.PdbDllVersion);
  743. DbiBuilder.setVersionHeader(Dbi.VerHeader);
  744. for (const auto &MI : Dbi.ModInfos) {
  745. auto &ModiBuilder = ExitOnErr(DbiBuilder.addModuleInfo(MI.Mod));
  746. ModiBuilder.setObjFileName(MI.Obj);
  747. for (auto S : MI.SourceFiles)
  748. ExitOnErr(DbiBuilder.addModuleSourceFile(ModiBuilder, S));
  749. if (MI.Modi) {
  750. const auto &ModiStream = *MI.Modi;
  751. for (auto Symbol : ModiStream.Symbols) {
  752. ModiBuilder.addSymbol(
  753. Symbol.toCodeViewSymbol(Allocator, CodeViewContainer::Pdb));
  754. }
  755. }
  756. // Each module has its own checksum subsection, so scan for it every time.
  757. Strings.setChecksums(nullptr);
  758. CodeViewYAML::initializeStringsAndChecksums(MI.Subsections, Strings);
  759. auto CodeViewSubsections = ExitOnErr(CodeViewYAML::toCodeViewSubsectionList(
  760. Allocator, MI.Subsections, Strings));
  761. for (auto &SS : CodeViewSubsections) {
  762. ModiBuilder.addDebugSubsection(SS);
  763. }
  764. }
  765. auto &TpiBuilder = Builder.getTpiBuilder();
  766. const auto &Tpi = YamlObj.TpiStream.value_or(DefaultTpiStream);
  767. TpiBuilder.setVersionHeader(Tpi.Version);
  768. AppendingTypeTableBuilder TS(Allocator);
  769. for (const auto &R : Tpi.Records) {
  770. CVType Type = R.toCodeViewRecord(TS);
  771. TpiBuilder.addTypeRecord(Type.RecordData, std::nullopt);
  772. }
  773. const auto &Ipi = YamlObj.IpiStream.value_or(DefaultIpiStream);
  774. auto &IpiBuilder = Builder.getIpiBuilder();
  775. IpiBuilder.setVersionHeader(Ipi.Version);
  776. for (const auto &R : Ipi.Records) {
  777. CVType Type = R.toCodeViewRecord(TS);
  778. IpiBuilder.addTypeRecord(Type.RecordData, std::nullopt);
  779. }
  780. Builder.getStringTableBuilder().setStrings(*Strings.strings());
  781. codeview::GUID IgnoredOutGuid;
  782. ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile, &IgnoredOutGuid));
  783. }
  784. static PDBFile &loadPDB(StringRef Path, std::unique_ptr<IPDBSession> &Session) {
  785. ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
  786. NativeSession *NS = static_cast<NativeSession *>(Session.get());
  787. return NS->getPDBFile();
  788. }
  789. static void pdb2Yaml(StringRef Path) {
  790. std::unique_ptr<IPDBSession> Session;
  791. auto &File = loadPDB(Path, Session);
  792. auto O = std::make_unique<YAMLOutputStyle>(File);
  793. ExitOnErr(O->dump());
  794. }
  795. static void dumpRaw(StringRef Path) {
  796. InputFile IF = ExitOnErr(InputFile::open(Path));
  797. auto O = std::make_unique<DumpOutputStyle>(IF);
  798. ExitOnErr(O->dump());
  799. }
  800. static void dumpBytes(StringRef Path) {
  801. std::unique_ptr<IPDBSession> Session;
  802. auto &File = loadPDB(Path, Session);
  803. auto O = std::make_unique<BytesOutputStyle>(File);
  804. ExitOnErr(O->dump());
  805. }
  806. bool opts::pretty::shouldDumpSymLevel(SymLevel Search) {
  807. if (SymTypes.empty())
  808. return true;
  809. if (llvm::is_contained(SymTypes, Search))
  810. return true;
  811. if (llvm::is_contained(SymTypes, SymLevel::All))
  812. return true;
  813. return false;
  814. }
  815. uint32_t llvm::pdb::getTypeLength(const PDBSymbolData &Symbol) {
  816. auto SymbolType = Symbol.getType();
  817. const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
  818. return RawType.getLength();
  819. }
  820. bool opts::pretty::compareFunctionSymbols(
  821. const std::unique_ptr<PDBSymbolFunc> &F1,
  822. const std::unique_ptr<PDBSymbolFunc> &F2) {
  823. assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
  824. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
  825. return F1->getName() < F2->getName();
  826. // Note that we intentionally sort in descending order on length, since
  827. // long functions are more interesting than short functions.
  828. return F1->getLength() > F2->getLength();
  829. }
  830. bool opts::pretty::compareDataSymbols(
  831. const std::unique_ptr<PDBSymbolData> &F1,
  832. const std::unique_ptr<PDBSymbolData> &F2) {
  833. assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
  834. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
  835. return F1->getName() < F2->getName();
  836. // Note that we intentionally sort in descending order on length, since
  837. // large types are more interesting than short ones.
  838. return getTypeLength(*F1) > getTypeLength(*F2);
  839. }
  840. static std::string stringOr(std::string Str, std::string IfEmpty) {
  841. return (Str.empty()) ? IfEmpty : Str;
  842. }
  843. static void dumpInjectedSources(LinePrinter &Printer, IPDBSession &Session) {
  844. auto Sources = Session.getInjectedSources();
  845. if (!Sources || !Sources->getChildCount()) {
  846. Printer.printLine("There are no injected sources.");
  847. return;
  848. }
  849. while (auto IS = Sources->getNext()) {
  850. Printer.NewLine();
  851. std::string File = stringOr(IS->getFileName(), "<null>");
  852. uint64_t Size = IS->getCodeByteSize();
  853. std::string Obj = stringOr(IS->getObjectFileName(), "<null>");
  854. std::string VFName = stringOr(IS->getVirtualFileName(), "<null>");
  855. uint32_t CRC = IS->getCrc32();
  856. WithColor(Printer, PDB_ColorItem::Path).get() << File;
  857. Printer << " (";
  858. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Size;
  859. Printer << " bytes): ";
  860. WithColor(Printer, PDB_ColorItem::Keyword).get() << "obj";
  861. Printer << "=";
  862. WithColor(Printer, PDB_ColorItem::Path).get() << Obj;
  863. Printer << ", ";
  864. WithColor(Printer, PDB_ColorItem::Keyword).get() << "vname";
  865. Printer << "=";
  866. WithColor(Printer, PDB_ColorItem::Path).get() << VFName;
  867. Printer << ", ";
  868. WithColor(Printer, PDB_ColorItem::Keyword).get() << "crc";
  869. Printer << "=";
  870. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << CRC;
  871. Printer << ", ";
  872. WithColor(Printer, PDB_ColorItem::Keyword).get() << "compression";
  873. Printer << "=";
  874. dumpPDBSourceCompression(
  875. WithColor(Printer, PDB_ColorItem::LiteralValue).get(),
  876. IS->getCompression());
  877. if (!opts::pretty::ShowInjectedSourceContent)
  878. continue;
  879. // Set the indent level to 0 when printing file content.
  880. int Indent = Printer.getIndentLevel();
  881. Printer.Unindent(Indent);
  882. if (IS->getCompression() == PDB_SourceCompression::None)
  883. Printer.printLine(IS->getCode());
  884. else
  885. Printer.formatBinary("Compressed data",
  886. arrayRefFromStringRef(IS->getCode()),
  887. /*StartOffset=*/0);
  888. // Re-indent back to the original level.
  889. Printer.Indent(Indent);
  890. }
  891. }
  892. template <typename OuterT, typename ChildT>
  893. void diaDumpChildren(PDBSymbol &Outer, PdbSymbolIdField Ids,
  894. PdbSymbolIdField Recurse) {
  895. OuterT *ConcreteOuter = dyn_cast<OuterT>(&Outer);
  896. if (!ConcreteOuter)
  897. return;
  898. auto Children = ConcreteOuter->template findAllChildren<ChildT>();
  899. while (auto Child = Children->getNext()) {
  900. outs() << " {";
  901. Child->defaultDump(outs(), 4, Ids, Recurse);
  902. outs() << "\n }\n";
  903. }
  904. }
  905. static void dumpDia(StringRef Path) {
  906. std::unique_ptr<IPDBSession> Session;
  907. const auto ReaderType =
  908. opts::diadump::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
  909. ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
  910. auto GlobalScope = Session->getGlobalScope();
  911. std::vector<PDB_SymType> SymTypes;
  912. if (opts::diadump::Compilands)
  913. SymTypes.push_back(PDB_SymType::Compiland);
  914. if (opts::diadump::Enums)
  915. SymTypes.push_back(PDB_SymType::Enum);
  916. if (opts::diadump::Pointers)
  917. SymTypes.push_back(PDB_SymType::PointerType);
  918. if (opts::diadump::UDTs)
  919. SymTypes.push_back(PDB_SymType::UDT);
  920. if (opts::diadump::Funcsigs)
  921. SymTypes.push_back(PDB_SymType::FunctionSig);
  922. if (opts::diadump::Arrays)
  923. SymTypes.push_back(PDB_SymType::ArrayType);
  924. if (opts::diadump::VTShapes)
  925. SymTypes.push_back(PDB_SymType::VTableShape);
  926. if (opts::diadump::Typedefs)
  927. SymTypes.push_back(PDB_SymType::Typedef);
  928. PdbSymbolIdField Ids = opts::diadump::NoSymIndexIds ? PdbSymbolIdField::None
  929. : PdbSymbolIdField::All;
  930. PdbSymbolIdField Recurse = PdbSymbolIdField::None;
  931. if (opts::diadump::Recurse)
  932. Recurse = PdbSymbolIdField::All;
  933. if (!opts::diadump::ShowClassHierarchy)
  934. Ids &= ~(PdbSymbolIdField::ClassParent | PdbSymbolIdField::LexicalParent);
  935. for (PDB_SymType ST : SymTypes) {
  936. auto Children = GlobalScope->findAllChildren(ST);
  937. while (auto Child = Children->getNext()) {
  938. outs() << "{";
  939. Child->defaultDump(outs(), 2, Ids, Recurse);
  940. diaDumpChildren<PDBSymbolTypeEnum, PDBSymbolData>(*Child, Ids, Recurse);
  941. outs() << "\n}\n";
  942. }
  943. }
  944. }
  945. static void dumpPretty(StringRef Path) {
  946. std::unique_ptr<IPDBSession> Session;
  947. const auto ReaderType =
  948. opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
  949. ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
  950. if (opts::pretty::LoadAddress)
  951. Session->setLoadAddress(opts::pretty::LoadAddress);
  952. auto &Stream = outs();
  953. const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
  954. ? Stream.has_colors()
  955. : opts::pretty::ColorOutput == cl::BOU_TRUE;
  956. LinePrinter Printer(2, UseColor, Stream, opts::Filters);
  957. auto GlobalScope(Session->getGlobalScope());
  958. if (!GlobalScope)
  959. return;
  960. std::string FileName(GlobalScope->getSymbolsFileName());
  961. WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
  962. WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
  963. Printer.Indent();
  964. uint64_t FileSize = 0;
  965. Printer.NewLine();
  966. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
  967. if (!sys::fs::file_size(FileName, FileSize)) {
  968. Printer << ": " << FileSize << " bytes";
  969. } else {
  970. Printer << ": (Unable to obtain file size)";
  971. }
  972. Printer.NewLine();
  973. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
  974. Printer << ": " << GlobalScope->getGuid();
  975. Printer.NewLine();
  976. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
  977. Printer << ": " << GlobalScope->getAge();
  978. Printer.NewLine();
  979. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
  980. Printer << ": ";
  981. if (GlobalScope->hasCTypes())
  982. outs() << "HasCTypes ";
  983. if (GlobalScope->hasPrivateSymbols())
  984. outs() << "HasPrivateSymbols ";
  985. Printer.Unindent();
  986. if (!opts::pretty::WithName.empty()) {
  987. Printer.NewLine();
  988. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  989. << "---SYMBOLS & TYPES BY NAME---";
  990. for (StringRef Name : opts::pretty::WithName) {
  991. auto Symbols = GlobalScope->findChildren(
  992. PDB_SymType::None, Name, PDB_NameSearchFlags::NS_CaseSensitive);
  993. if (!Symbols || Symbols->getChildCount() == 0) {
  994. Printer.formatLine("[not found] - {0}", Name);
  995. continue;
  996. }
  997. Printer.formatLine("[{0} occurrences] - {1}", Symbols->getChildCount(),
  998. Name);
  999. AutoIndent Indent(Printer);
  1000. Printer.NewLine();
  1001. while (auto Symbol = Symbols->getNext()) {
  1002. switch (Symbol->getSymTag()) {
  1003. case PDB_SymType::Typedef: {
  1004. TypedefDumper TD(Printer);
  1005. std::unique_ptr<PDBSymbolTypeTypedef> T =
  1006. llvm::unique_dyn_cast<PDBSymbolTypeTypedef>(std::move(Symbol));
  1007. TD.start(*T);
  1008. break;
  1009. }
  1010. case PDB_SymType::Enum: {
  1011. EnumDumper ED(Printer);
  1012. std::unique_ptr<PDBSymbolTypeEnum> E =
  1013. llvm::unique_dyn_cast<PDBSymbolTypeEnum>(std::move(Symbol));
  1014. ED.start(*E);
  1015. break;
  1016. }
  1017. case PDB_SymType::UDT: {
  1018. ClassDefinitionDumper CD(Printer);
  1019. std::unique_ptr<PDBSymbolTypeUDT> C =
  1020. llvm::unique_dyn_cast<PDBSymbolTypeUDT>(std::move(Symbol));
  1021. CD.start(*C);
  1022. break;
  1023. }
  1024. case PDB_SymType::BaseClass:
  1025. case PDB_SymType::Friend: {
  1026. TypeDumper TD(Printer);
  1027. Symbol->dump(TD);
  1028. break;
  1029. }
  1030. case PDB_SymType::Function: {
  1031. FunctionDumper FD(Printer);
  1032. std::unique_ptr<PDBSymbolFunc> F =
  1033. llvm::unique_dyn_cast<PDBSymbolFunc>(std::move(Symbol));
  1034. FD.start(*F, FunctionDumper::PointerType::None);
  1035. break;
  1036. }
  1037. case PDB_SymType::Data: {
  1038. VariableDumper VD(Printer);
  1039. std::unique_ptr<PDBSymbolData> D =
  1040. llvm::unique_dyn_cast<PDBSymbolData>(std::move(Symbol));
  1041. VD.start(*D);
  1042. break;
  1043. }
  1044. case PDB_SymType::PublicSymbol: {
  1045. ExternalSymbolDumper ED(Printer);
  1046. std::unique_ptr<PDBSymbolPublicSymbol> PS =
  1047. llvm::unique_dyn_cast<PDBSymbolPublicSymbol>(std::move(Symbol));
  1048. ED.dump(*PS);
  1049. break;
  1050. }
  1051. default:
  1052. llvm_unreachable("Unexpected symbol tag!");
  1053. }
  1054. }
  1055. }
  1056. llvm::outs().flush();
  1057. }
  1058. if (opts::pretty::Compilands) {
  1059. Printer.NewLine();
  1060. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  1061. << "---COMPILANDS---";
  1062. auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
  1063. if (Compilands) {
  1064. Printer.Indent();
  1065. CompilandDumper Dumper(Printer);
  1066. CompilandDumpFlags options = CompilandDumper::Flags::None;
  1067. if (opts::pretty::Lines)
  1068. options = options | CompilandDumper::Flags::Lines;
  1069. while (auto Compiland = Compilands->getNext())
  1070. Dumper.start(*Compiland, options);
  1071. Printer.Unindent();
  1072. }
  1073. }
  1074. if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs ||
  1075. opts::pretty::Funcsigs || opts::pretty::Pointers ||
  1076. opts::pretty::Arrays || opts::pretty::VTShapes) {
  1077. Printer.NewLine();
  1078. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
  1079. Printer.Indent();
  1080. TypeDumper Dumper(Printer);
  1081. Dumper.start(*GlobalScope);
  1082. Printer.Unindent();
  1083. }
  1084. if (opts::pretty::Symbols) {
  1085. Printer.NewLine();
  1086. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
  1087. if (auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>()) {
  1088. Printer.Indent();
  1089. CompilandDumper Dumper(Printer);
  1090. while (auto Compiland = Compilands->getNext())
  1091. Dumper.start(*Compiland, true);
  1092. Printer.Unindent();
  1093. }
  1094. }
  1095. if (opts::pretty::Globals) {
  1096. Printer.NewLine();
  1097. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
  1098. Printer.Indent();
  1099. if (shouldDumpSymLevel(opts::pretty::SymLevel::Functions)) {
  1100. if (auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>()) {
  1101. FunctionDumper Dumper(Printer);
  1102. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
  1103. while (auto Function = Functions->getNext()) {
  1104. Printer.NewLine();
  1105. Dumper.start(*Function, FunctionDumper::PointerType::None);
  1106. }
  1107. } else {
  1108. std::vector<std::unique_ptr<PDBSymbolFunc>> Funcs;
  1109. while (auto Func = Functions->getNext())
  1110. Funcs.push_back(std::move(Func));
  1111. llvm::sort(Funcs, opts::pretty::compareFunctionSymbols);
  1112. for (const auto &Func : Funcs) {
  1113. Printer.NewLine();
  1114. Dumper.start(*Func, FunctionDumper::PointerType::None);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. if (shouldDumpSymLevel(opts::pretty::SymLevel::Data)) {
  1120. if (auto Vars = GlobalScope->findAllChildren<PDBSymbolData>()) {
  1121. VariableDumper Dumper(Printer);
  1122. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
  1123. while (auto Var = Vars->getNext())
  1124. Dumper.start(*Var);
  1125. } else {
  1126. std::vector<std::unique_ptr<PDBSymbolData>> Datas;
  1127. while (auto Var = Vars->getNext())
  1128. Datas.push_back(std::move(Var));
  1129. llvm::sort(Datas, opts::pretty::compareDataSymbols);
  1130. for (const auto &Var : Datas)
  1131. Dumper.start(*Var);
  1132. }
  1133. }
  1134. }
  1135. if (shouldDumpSymLevel(opts::pretty::SymLevel::Thunks)) {
  1136. if (auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>()) {
  1137. CompilandDumper Dumper(Printer);
  1138. while (auto Thunk = Thunks->getNext())
  1139. Dumper.dump(*Thunk);
  1140. }
  1141. }
  1142. Printer.Unindent();
  1143. }
  1144. if (opts::pretty::Externals) {
  1145. Printer.NewLine();
  1146. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
  1147. Printer.Indent();
  1148. ExternalSymbolDumper Dumper(Printer);
  1149. Dumper.start(*GlobalScope);
  1150. }
  1151. if (opts::pretty::Lines) {
  1152. Printer.NewLine();
  1153. }
  1154. if (opts::pretty::InjectedSources) {
  1155. Printer.NewLine();
  1156. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  1157. << "---INJECTED SOURCES---";
  1158. AutoIndent Indent1(Printer);
  1159. dumpInjectedSources(Printer, *Session);
  1160. }
  1161. Printer.NewLine();
  1162. outs().flush();
  1163. }
  1164. static void mergePdbs() {
  1165. BumpPtrAllocator Allocator;
  1166. MergingTypeTableBuilder MergedTpi(Allocator);
  1167. MergingTypeTableBuilder MergedIpi(Allocator);
  1168. // Create a Tpi and Ipi type table with all types from all input files.
  1169. for (const auto &Path : opts::merge::InputFilenames) {
  1170. std::unique_ptr<IPDBSession> Session;
  1171. auto &File = loadPDB(Path, Session);
  1172. SmallVector<TypeIndex, 128> TypeMap;
  1173. SmallVector<TypeIndex, 128> IdMap;
  1174. if (File.hasPDBTpiStream()) {
  1175. auto &Tpi = ExitOnErr(File.getPDBTpiStream());
  1176. ExitOnErr(
  1177. codeview::mergeTypeRecords(MergedTpi, TypeMap, Tpi.typeArray()));
  1178. }
  1179. if (File.hasPDBIpiStream()) {
  1180. auto &Ipi = ExitOnErr(File.getPDBIpiStream());
  1181. ExitOnErr(codeview::mergeIdRecords(MergedIpi, TypeMap, IdMap,
  1182. Ipi.typeArray()));
  1183. }
  1184. }
  1185. // Then write the PDB.
  1186. PDBFileBuilder Builder(Allocator);
  1187. ExitOnErr(Builder.initialize(4096));
  1188. // Add each of the reserved streams. We might not put any data in them,
  1189. // but at least they have to be present.
  1190. for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
  1191. ExitOnErr(Builder.getMsfBuilder().addStream(0));
  1192. auto &DestTpi = Builder.getTpiBuilder();
  1193. auto &DestIpi = Builder.getIpiBuilder();
  1194. MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
  1195. DestTpi.addTypeRecord(Type.RecordData, std::nullopt);
  1196. });
  1197. MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
  1198. DestIpi.addTypeRecord(Type.RecordData, std::nullopt);
  1199. });
  1200. Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
  1201. SmallString<64> OutFile(opts::merge::PdbOutputFile);
  1202. if (OutFile.empty()) {
  1203. OutFile = opts::merge::InputFilenames[0];
  1204. llvm::sys::path::replace_extension(OutFile, "merged.pdb");
  1205. }
  1206. codeview::GUID IgnoredOutGuid;
  1207. ExitOnErr(Builder.commit(OutFile, &IgnoredOutGuid));
  1208. }
  1209. static void explain() {
  1210. std::unique_ptr<IPDBSession> Session;
  1211. InputFile IF =
  1212. ExitOnErr(InputFile::open(opts::explain::InputFilename.front(), true));
  1213. for (uint64_t Off : opts::explain::Offsets) {
  1214. auto O = std::make_unique<ExplainOutputStyle>(IF, Off);
  1215. ExitOnErr(O->dump());
  1216. }
  1217. }
  1218. static void exportStream() {
  1219. std::unique_ptr<IPDBSession> Session;
  1220. PDBFile &File = loadPDB(opts::exportstream::InputFilename.front(), Session);
  1221. std::unique_ptr<MappedBlockStream> SourceStream;
  1222. uint32_t Index = 0;
  1223. bool Success = false;
  1224. std::string OutFileName = opts::exportstream::OutputFile;
  1225. if (!opts::exportstream::ForceName) {
  1226. // First try to parse it as an integer, if it fails fall back to treating it
  1227. // as a named stream.
  1228. if (to_integer(opts::exportstream::Stream, Index)) {
  1229. if (Index >= File.getNumStreams()) {
  1230. errs() << "Error: " << Index << " is not a valid stream index.\n";
  1231. exit(1);
  1232. }
  1233. Success = true;
  1234. outs() << "Dumping contents of stream index " << Index << " to file "
  1235. << OutFileName << ".\n";
  1236. }
  1237. }
  1238. if (!Success) {
  1239. InfoStream &IS = cantFail(File.getPDBInfoStream());
  1240. Index = ExitOnErr(IS.getNamedStreamIndex(opts::exportstream::Stream));
  1241. outs() << "Dumping contents of stream '" << opts::exportstream::Stream
  1242. << "' (index " << Index << ") to file " << OutFileName << ".\n";
  1243. }
  1244. SourceStream = File.createIndexedStream(Index);
  1245. auto OutFile = ExitOnErr(
  1246. FileOutputBuffer::create(OutFileName, SourceStream->getLength()));
  1247. FileBufferByteStream DestStream(std::move(OutFile), llvm::support::little);
  1248. BinaryStreamWriter Writer(DestStream);
  1249. ExitOnErr(Writer.writeStreamRef(*SourceStream));
  1250. ExitOnErr(DestStream.commit());
  1251. }
  1252. static bool parseRange(StringRef Str,
  1253. std::optional<opts::bytes::NumberRange> &Parsed) {
  1254. if (Str.empty())
  1255. return true;
  1256. llvm::Regex R("^([^-]+)(-([^-]+))?$");
  1257. llvm::SmallVector<llvm::StringRef, 2> Matches;
  1258. if (!R.match(Str, &Matches))
  1259. return false;
  1260. Parsed.emplace();
  1261. if (!to_integer(Matches[1], Parsed->Min))
  1262. return false;
  1263. if (!Matches[3].empty()) {
  1264. Parsed->Max.emplace();
  1265. if (!to_integer(Matches[3], *Parsed->Max))
  1266. return false;
  1267. }
  1268. return true;
  1269. }
  1270. static void simplifyChunkList(llvm::cl::list<opts::ModuleSubsection> &Chunks) {
  1271. // If this list contains "All" plus some other stuff, remove the other stuff
  1272. // and just keep "All" in the list.
  1273. if (!llvm::is_contained(Chunks, opts::ModuleSubsection::All))
  1274. return;
  1275. Chunks.reset();
  1276. Chunks.push_back(opts::ModuleSubsection::All);
  1277. }
  1278. int main(int Argc, const char **Argv) {
  1279. InitLLVM X(Argc, Argv);
  1280. ExitOnErr.setBanner("llvm-pdbutil: ");
  1281. cl::HideUnrelatedOptions(
  1282. {&opts::TypeCategory, &opts::FilterCategory, &opts::OtherOptions});
  1283. cl::ParseCommandLineOptions(Argc, Argv, "LLVM PDB Dumper\n");
  1284. if (opts::BytesSubcommand) {
  1285. if (!parseRange(opts::bytes::DumpBlockRangeOpt,
  1286. opts::bytes::DumpBlockRange)) {
  1287. errs() << "Argument '" << opts::bytes::DumpBlockRangeOpt
  1288. << "' invalid format.\n";
  1289. errs().flush();
  1290. exit(1);
  1291. }
  1292. if (!parseRange(opts::bytes::DumpByteRangeOpt,
  1293. opts::bytes::DumpByteRange)) {
  1294. errs() << "Argument '" << opts::bytes::DumpByteRangeOpt
  1295. << "' invalid format.\n";
  1296. errs().flush();
  1297. exit(1);
  1298. }
  1299. }
  1300. if (opts::DumpSubcommand) {
  1301. if (opts::dump::RawAll) {
  1302. opts::dump::DumpGlobals = true;
  1303. opts::dump::DumpFpo = true;
  1304. opts::dump::DumpInlineeLines = true;
  1305. opts::dump::DumpIds = true;
  1306. opts::dump::DumpIdExtras = true;
  1307. opts::dump::DumpLines = true;
  1308. opts::dump::DumpModules = true;
  1309. opts::dump::DumpModuleFiles = true;
  1310. opts::dump::DumpPublics = true;
  1311. opts::dump::DumpSectionContribs = true;
  1312. opts::dump::DumpSectionHeaders = true;
  1313. opts::dump::DumpSectionMap = true;
  1314. opts::dump::DumpStreams = true;
  1315. opts::dump::DumpStreamBlocks = true;
  1316. opts::dump::DumpStringTable = true;
  1317. opts::dump::DumpStringTableDetails = true;
  1318. opts::dump::DumpSummary = true;
  1319. opts::dump::DumpSymbols = true;
  1320. opts::dump::DumpSymbolStats = true;
  1321. opts::dump::DumpTypes = true;
  1322. opts::dump::DumpTypeExtras = true;
  1323. opts::dump::DumpUdtStats = true;
  1324. opts::dump::DumpXme = true;
  1325. opts::dump::DumpXmi = true;
  1326. }
  1327. }
  1328. if (opts::PdbToYamlSubcommand) {
  1329. if (opts::pdb2yaml::All) {
  1330. opts::pdb2yaml::StreamMetadata = true;
  1331. opts::pdb2yaml::StreamDirectory = true;
  1332. opts::pdb2yaml::PdbStream = true;
  1333. opts::pdb2yaml::StringTable = true;
  1334. opts::pdb2yaml::DbiStream = true;
  1335. opts::pdb2yaml::TpiStream = true;
  1336. opts::pdb2yaml::IpiStream = true;
  1337. opts::pdb2yaml::PublicsStream = true;
  1338. opts::pdb2yaml::DumpModules = true;
  1339. opts::pdb2yaml::DumpModuleFiles = true;
  1340. opts::pdb2yaml::DumpModuleSyms = true;
  1341. opts::pdb2yaml::DumpModuleSubsections.push_back(
  1342. opts::ModuleSubsection::All);
  1343. }
  1344. simplifyChunkList(opts::pdb2yaml::DumpModuleSubsections);
  1345. if (opts::pdb2yaml::DumpModuleSyms || opts::pdb2yaml::DumpModuleFiles)
  1346. opts::pdb2yaml::DumpModules = true;
  1347. if (opts::pdb2yaml::DumpModules)
  1348. opts::pdb2yaml::DbiStream = true;
  1349. }
  1350. llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
  1351. // Initialize the filters for LinePrinter.
  1352. auto propagate = [&](auto &Target, auto &Reference) {
  1353. for (std::string &Option : Reference)
  1354. Target.push_back(Option);
  1355. };
  1356. propagate(opts::Filters.ExcludeTypes, opts::pretty::ExcludeTypes);
  1357. propagate(opts::Filters.ExcludeTypes, opts::pretty::ExcludeTypes);
  1358. propagate(opts::Filters.ExcludeSymbols, opts::pretty::ExcludeSymbols);
  1359. propagate(opts::Filters.ExcludeCompilands, opts::pretty::ExcludeCompilands);
  1360. propagate(opts::Filters.IncludeTypes, opts::pretty::IncludeTypes);
  1361. propagate(opts::Filters.IncludeSymbols, opts::pretty::IncludeSymbols);
  1362. propagate(opts::Filters.IncludeCompilands, opts::pretty::IncludeCompilands);
  1363. opts::Filters.PaddingThreshold = opts::pretty::PaddingThreshold;
  1364. opts::Filters.SizeThreshold = opts::pretty::SizeThreshold;
  1365. opts::Filters.JustMyCode = opts::dump::JustMyCode;
  1366. if (opts::dump::DumpModi.getNumOccurrences() > 0) {
  1367. if (opts::dump::DumpModi.getNumOccurrences() != 1) {
  1368. errs() << "argument '-modi' specified more than once.\n";
  1369. errs().flush();
  1370. exit(1);
  1371. }
  1372. opts::Filters.DumpModi = opts::dump::DumpModi;
  1373. }
  1374. if (opts::dump::DumpSymbolOffset) {
  1375. if (opts::dump::DumpModi.getNumOccurrences() != 1) {
  1376. errs()
  1377. << "need to specify argument '-modi' when using '-symbol-offset'.\n";
  1378. errs().flush();
  1379. exit(1);
  1380. }
  1381. opts::Filters.SymbolOffset = opts::dump::DumpSymbolOffset;
  1382. if (opts::dump::DumpParents)
  1383. opts::Filters.ParentRecurseDepth = opts::dump::DumpParentDepth;
  1384. if (opts::dump::DumpChildren)
  1385. opts::Filters.ChildrenRecurseDepth = opts::dump::DumpChildrenDepth;
  1386. }
  1387. if (opts::PdbToYamlSubcommand) {
  1388. pdb2Yaml(opts::pdb2yaml::InputFilename.front());
  1389. } else if (opts::YamlToPdbSubcommand) {
  1390. if (opts::yaml2pdb::YamlPdbOutputFile.empty()) {
  1391. SmallString<16> OutputFilename(opts::yaml2pdb::InputFilename.getValue());
  1392. sys::path::replace_extension(OutputFilename, ".pdb");
  1393. opts::yaml2pdb::YamlPdbOutputFile = std::string(OutputFilename.str());
  1394. }
  1395. yamlToPdb(opts::yaml2pdb::InputFilename);
  1396. } else if (opts::DiaDumpSubcommand) {
  1397. llvm::for_each(opts::diadump::InputFilenames, dumpDia);
  1398. } else if (opts::PrettySubcommand) {
  1399. if (opts::pretty::Lines)
  1400. opts::pretty::Compilands = true;
  1401. if (opts::pretty::All) {
  1402. opts::pretty::Compilands = true;
  1403. opts::pretty::Symbols = true;
  1404. opts::pretty::Globals = true;
  1405. opts::pretty::Types = true;
  1406. opts::pretty::Externals = true;
  1407. opts::pretty::Lines = true;
  1408. }
  1409. if (opts::pretty::Types) {
  1410. opts::pretty::Classes = true;
  1411. opts::pretty::Typedefs = true;
  1412. opts::pretty::Enums = true;
  1413. opts::pretty::Pointers = true;
  1414. opts::pretty::Funcsigs = true;
  1415. }
  1416. // When adding filters for excluded compilands and types, we need to
  1417. // remember that these are regexes. So special characters such as * and \
  1418. // need to be escaped in the regex. In the case of a literal \, this means
  1419. // it needs to be escaped again in the C++. So matching a single \ in the
  1420. // input requires 4 \es in the C++.
  1421. if (opts::pretty::ExcludeCompilerGenerated) {
  1422. opts::Filters.ExcludeTypes.push_back("__vc_attributes");
  1423. opts::Filters.ExcludeCompilands.push_back("\\* Linker \\*");
  1424. }
  1425. if (opts::pretty::ExcludeSystemLibraries) {
  1426. opts::Filters.ExcludeCompilands.push_back(
  1427. "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
  1428. opts::Filters.ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
  1429. opts::Filters.ExcludeCompilands.push_back(
  1430. "d:\\\\th.obj.x86fre\\\\minkernel");
  1431. }
  1432. llvm::for_each(opts::pretty::InputFilenames, dumpPretty);
  1433. } else if (opts::DumpSubcommand) {
  1434. llvm::for_each(opts::dump::InputFilenames, dumpRaw);
  1435. } else if (opts::BytesSubcommand) {
  1436. llvm::for_each(opts::bytes::InputFilenames, dumpBytes);
  1437. } else if (opts::MergeSubcommand) {
  1438. if (opts::merge::InputFilenames.size() < 2) {
  1439. errs() << "merge subcommand requires at least 2 input files.\n";
  1440. exit(1);
  1441. }
  1442. mergePdbs();
  1443. } else if (opts::ExplainSubcommand) {
  1444. explain();
  1445. } else if (opts::ExportSubcommand) {
  1446. exportStream();
  1447. }
  1448. outs().flush();
  1449. return 0;
  1450. }