llvm-pdbutil.cpp 63 KB

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