llvm-pdbutil.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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, /*IsText=*/false,
  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. ExitOnErr(O->dump());
  770. }
  771. static void dumpRaw(StringRef Path) {
  772. InputFile IF = ExitOnErr(InputFile::open(Path));
  773. auto O = std::make_unique<DumpOutputStyle>(IF);
  774. ExitOnErr(O->dump());
  775. }
  776. static void dumpBytes(StringRef Path) {
  777. std::unique_ptr<IPDBSession> Session;
  778. auto &File = loadPDB(Path, Session);
  779. auto O = std::make_unique<BytesOutputStyle>(File);
  780. ExitOnErr(O->dump());
  781. }
  782. bool opts::pretty::shouldDumpSymLevel(SymLevel Search) {
  783. if (SymTypes.empty())
  784. return true;
  785. if (llvm::is_contained(SymTypes, Search))
  786. return true;
  787. if (llvm::is_contained(SymTypes, SymLevel::All))
  788. return true;
  789. return false;
  790. }
  791. uint32_t llvm::pdb::getTypeLength(const PDBSymbolData &Symbol) {
  792. auto SymbolType = Symbol.getType();
  793. const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
  794. return RawType.getLength();
  795. }
  796. bool opts::pretty::compareFunctionSymbols(
  797. const std::unique_ptr<PDBSymbolFunc> &F1,
  798. const std::unique_ptr<PDBSymbolFunc> &F2) {
  799. assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
  800. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
  801. return F1->getName() < F2->getName();
  802. // Note that we intentionally sort in descending order on length, since
  803. // long functions are more interesting than short functions.
  804. return F1->getLength() > F2->getLength();
  805. }
  806. bool opts::pretty::compareDataSymbols(
  807. const std::unique_ptr<PDBSymbolData> &F1,
  808. const std::unique_ptr<PDBSymbolData> &F2) {
  809. assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
  810. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
  811. return F1->getName() < F2->getName();
  812. // Note that we intentionally sort in descending order on length, since
  813. // large types are more interesting than short ones.
  814. return getTypeLength(*F1) > getTypeLength(*F2);
  815. }
  816. static std::string stringOr(std::string Str, std::string IfEmpty) {
  817. return (Str.empty()) ? IfEmpty : Str;
  818. }
  819. static void dumpInjectedSources(LinePrinter &Printer, IPDBSession &Session) {
  820. auto Sources = Session.getInjectedSources();
  821. if (!Sources || !Sources->getChildCount()) {
  822. Printer.printLine("There are no injected sources.");
  823. return;
  824. }
  825. while (auto IS = Sources->getNext()) {
  826. Printer.NewLine();
  827. std::string File = stringOr(IS->getFileName(), "<null>");
  828. uint64_t Size = IS->getCodeByteSize();
  829. std::string Obj = stringOr(IS->getObjectFileName(), "<null>");
  830. std::string VFName = stringOr(IS->getVirtualFileName(), "<null>");
  831. uint32_t CRC = IS->getCrc32();
  832. WithColor(Printer, PDB_ColorItem::Path).get() << File;
  833. Printer << " (";
  834. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Size;
  835. Printer << " bytes): ";
  836. WithColor(Printer, PDB_ColorItem::Keyword).get() << "obj";
  837. Printer << "=";
  838. WithColor(Printer, PDB_ColorItem::Path).get() << Obj;
  839. Printer << ", ";
  840. WithColor(Printer, PDB_ColorItem::Keyword).get() << "vname";
  841. Printer << "=";
  842. WithColor(Printer, PDB_ColorItem::Path).get() << VFName;
  843. Printer << ", ";
  844. WithColor(Printer, PDB_ColorItem::Keyword).get() << "crc";
  845. Printer << "=";
  846. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << CRC;
  847. Printer << ", ";
  848. WithColor(Printer, PDB_ColorItem::Keyword).get() << "compression";
  849. Printer << "=";
  850. dumpPDBSourceCompression(
  851. WithColor(Printer, PDB_ColorItem::LiteralValue).get(),
  852. IS->getCompression());
  853. if (!opts::pretty::ShowInjectedSourceContent)
  854. continue;
  855. // Set the indent level to 0 when printing file content.
  856. int Indent = Printer.getIndentLevel();
  857. Printer.Unindent(Indent);
  858. if (IS->getCompression() == PDB_SourceCompression::None)
  859. Printer.printLine(IS->getCode());
  860. else
  861. Printer.formatBinary("Compressed data",
  862. arrayRefFromStringRef(IS->getCode()),
  863. /*StartOffset=*/0);
  864. // Re-indent back to the original level.
  865. Printer.Indent(Indent);
  866. }
  867. }
  868. template <typename OuterT, typename ChildT>
  869. void diaDumpChildren(PDBSymbol &Outer, PdbSymbolIdField Ids,
  870. PdbSymbolIdField Recurse) {
  871. OuterT *ConcreteOuter = dyn_cast<OuterT>(&Outer);
  872. if (!ConcreteOuter)
  873. return;
  874. auto Children = ConcreteOuter->template findAllChildren<ChildT>();
  875. while (auto Child = Children->getNext()) {
  876. outs() << " {";
  877. Child->defaultDump(outs(), 4, Ids, Recurse);
  878. outs() << "\n }\n";
  879. }
  880. }
  881. static void dumpDia(StringRef Path) {
  882. std::unique_ptr<IPDBSession> Session;
  883. const auto ReaderType =
  884. opts::diadump::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
  885. ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
  886. auto GlobalScope = Session->getGlobalScope();
  887. std::vector<PDB_SymType> SymTypes;
  888. if (opts::diadump::Compilands)
  889. SymTypes.push_back(PDB_SymType::Compiland);
  890. if (opts::diadump::Enums)
  891. SymTypes.push_back(PDB_SymType::Enum);
  892. if (opts::diadump::Pointers)
  893. SymTypes.push_back(PDB_SymType::PointerType);
  894. if (opts::diadump::UDTs)
  895. SymTypes.push_back(PDB_SymType::UDT);
  896. if (opts::diadump::Funcsigs)
  897. SymTypes.push_back(PDB_SymType::FunctionSig);
  898. if (opts::diadump::Arrays)
  899. SymTypes.push_back(PDB_SymType::ArrayType);
  900. if (opts::diadump::VTShapes)
  901. SymTypes.push_back(PDB_SymType::VTableShape);
  902. if (opts::diadump::Typedefs)
  903. SymTypes.push_back(PDB_SymType::Typedef);
  904. PdbSymbolIdField Ids = opts::diadump::NoSymIndexIds ? PdbSymbolIdField::None
  905. : PdbSymbolIdField::All;
  906. PdbSymbolIdField Recurse = PdbSymbolIdField::None;
  907. if (opts::diadump::Recurse)
  908. Recurse = PdbSymbolIdField::All;
  909. if (!opts::diadump::ShowClassHierarchy)
  910. Ids &= ~(PdbSymbolIdField::ClassParent | PdbSymbolIdField::LexicalParent);
  911. for (PDB_SymType ST : SymTypes) {
  912. auto Children = GlobalScope->findAllChildren(ST);
  913. while (auto Child = Children->getNext()) {
  914. outs() << "{";
  915. Child->defaultDump(outs(), 2, Ids, Recurse);
  916. diaDumpChildren<PDBSymbolTypeEnum, PDBSymbolData>(*Child, Ids, Recurse);
  917. outs() << "\n}\n";
  918. }
  919. }
  920. }
  921. static void dumpPretty(StringRef Path) {
  922. std::unique_ptr<IPDBSession> Session;
  923. const auto ReaderType =
  924. opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
  925. ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
  926. if (opts::pretty::LoadAddress)
  927. Session->setLoadAddress(opts::pretty::LoadAddress);
  928. auto &Stream = outs();
  929. const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
  930. ? Stream.has_colors()
  931. : opts::pretty::ColorOutput == cl::BOU_TRUE;
  932. LinePrinter Printer(2, UseColor, Stream);
  933. auto GlobalScope(Session->getGlobalScope());
  934. if (!GlobalScope)
  935. return;
  936. std::string FileName(GlobalScope->getSymbolsFileName());
  937. WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
  938. WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
  939. Printer.Indent();
  940. uint64_t FileSize = 0;
  941. Printer.NewLine();
  942. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
  943. if (!sys::fs::file_size(FileName, FileSize)) {
  944. Printer << ": " << FileSize << " bytes";
  945. } else {
  946. Printer << ": (Unable to obtain file size)";
  947. }
  948. Printer.NewLine();
  949. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
  950. Printer << ": " << GlobalScope->getGuid();
  951. Printer.NewLine();
  952. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
  953. Printer << ": " << GlobalScope->getAge();
  954. Printer.NewLine();
  955. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
  956. Printer << ": ";
  957. if (GlobalScope->hasCTypes())
  958. outs() << "HasCTypes ";
  959. if (GlobalScope->hasPrivateSymbols())
  960. outs() << "HasPrivateSymbols ";
  961. Printer.Unindent();
  962. if (!opts::pretty::WithName.empty()) {
  963. Printer.NewLine();
  964. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  965. << "---SYMBOLS & TYPES BY NAME---";
  966. for (StringRef Name : opts::pretty::WithName) {
  967. auto Symbols = GlobalScope->findChildren(
  968. PDB_SymType::None, Name, PDB_NameSearchFlags::NS_CaseSensitive);
  969. if (!Symbols || Symbols->getChildCount() == 0) {
  970. Printer.formatLine("[not found] - {0}", Name);
  971. continue;
  972. }
  973. Printer.formatLine("[{0} occurrences] - {1}", Symbols->getChildCount(),
  974. Name);
  975. AutoIndent Indent(Printer);
  976. Printer.NewLine();
  977. while (auto Symbol = Symbols->getNext()) {
  978. switch (Symbol->getSymTag()) {
  979. case PDB_SymType::Typedef: {
  980. TypedefDumper TD(Printer);
  981. std::unique_ptr<PDBSymbolTypeTypedef> T =
  982. llvm::unique_dyn_cast<PDBSymbolTypeTypedef>(std::move(Symbol));
  983. TD.start(*T);
  984. break;
  985. }
  986. case PDB_SymType::Enum: {
  987. EnumDumper ED(Printer);
  988. std::unique_ptr<PDBSymbolTypeEnum> E =
  989. llvm::unique_dyn_cast<PDBSymbolTypeEnum>(std::move(Symbol));
  990. ED.start(*E);
  991. break;
  992. }
  993. case PDB_SymType::UDT: {
  994. ClassDefinitionDumper CD(Printer);
  995. std::unique_ptr<PDBSymbolTypeUDT> C =
  996. llvm::unique_dyn_cast<PDBSymbolTypeUDT>(std::move(Symbol));
  997. CD.start(*C);
  998. break;
  999. }
  1000. case PDB_SymType::BaseClass:
  1001. case PDB_SymType::Friend: {
  1002. TypeDumper TD(Printer);
  1003. Symbol->dump(TD);
  1004. break;
  1005. }
  1006. case PDB_SymType::Function: {
  1007. FunctionDumper FD(Printer);
  1008. std::unique_ptr<PDBSymbolFunc> F =
  1009. llvm::unique_dyn_cast<PDBSymbolFunc>(std::move(Symbol));
  1010. FD.start(*F, FunctionDumper::PointerType::None);
  1011. break;
  1012. }
  1013. case PDB_SymType::Data: {
  1014. VariableDumper VD(Printer);
  1015. std::unique_ptr<PDBSymbolData> D =
  1016. llvm::unique_dyn_cast<PDBSymbolData>(std::move(Symbol));
  1017. VD.start(*D);
  1018. break;
  1019. }
  1020. case PDB_SymType::PublicSymbol: {
  1021. ExternalSymbolDumper ED(Printer);
  1022. std::unique_ptr<PDBSymbolPublicSymbol> PS =
  1023. llvm::unique_dyn_cast<PDBSymbolPublicSymbol>(std::move(Symbol));
  1024. ED.dump(*PS);
  1025. break;
  1026. }
  1027. default:
  1028. llvm_unreachable("Unexpected symbol tag!");
  1029. }
  1030. }
  1031. }
  1032. llvm::outs().flush();
  1033. }
  1034. if (opts::pretty::Compilands) {
  1035. Printer.NewLine();
  1036. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  1037. << "---COMPILANDS---";
  1038. auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
  1039. if (Compilands) {
  1040. Printer.Indent();
  1041. CompilandDumper Dumper(Printer);
  1042. CompilandDumpFlags options = CompilandDumper::Flags::None;
  1043. if (opts::pretty::Lines)
  1044. options = options | CompilandDumper::Flags::Lines;
  1045. while (auto Compiland = Compilands->getNext())
  1046. Dumper.start(*Compiland, options);
  1047. Printer.Unindent();
  1048. }
  1049. }
  1050. if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs ||
  1051. opts::pretty::Funcsigs || opts::pretty::Pointers ||
  1052. opts::pretty::Arrays || opts::pretty::VTShapes) {
  1053. Printer.NewLine();
  1054. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
  1055. Printer.Indent();
  1056. TypeDumper Dumper(Printer);
  1057. Dumper.start(*GlobalScope);
  1058. Printer.Unindent();
  1059. }
  1060. if (opts::pretty::Symbols) {
  1061. Printer.NewLine();
  1062. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
  1063. if (auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>()) {
  1064. Printer.Indent();
  1065. CompilandDumper Dumper(Printer);
  1066. while (auto Compiland = Compilands->getNext())
  1067. Dumper.start(*Compiland, true);
  1068. Printer.Unindent();
  1069. }
  1070. }
  1071. if (opts::pretty::Globals) {
  1072. Printer.NewLine();
  1073. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
  1074. Printer.Indent();
  1075. if (shouldDumpSymLevel(opts::pretty::SymLevel::Functions)) {
  1076. if (auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>()) {
  1077. FunctionDumper Dumper(Printer);
  1078. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
  1079. while (auto Function = Functions->getNext()) {
  1080. Printer.NewLine();
  1081. Dumper.start(*Function, FunctionDumper::PointerType::None);
  1082. }
  1083. } else {
  1084. std::vector<std::unique_ptr<PDBSymbolFunc>> Funcs;
  1085. while (auto Func = Functions->getNext())
  1086. Funcs.push_back(std::move(Func));
  1087. llvm::sort(Funcs, opts::pretty::compareFunctionSymbols);
  1088. for (const auto &Func : Funcs) {
  1089. Printer.NewLine();
  1090. Dumper.start(*Func, FunctionDumper::PointerType::None);
  1091. }
  1092. }
  1093. }
  1094. }
  1095. if (shouldDumpSymLevel(opts::pretty::SymLevel::Data)) {
  1096. if (auto Vars = GlobalScope->findAllChildren<PDBSymbolData>()) {
  1097. VariableDumper Dumper(Printer);
  1098. if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
  1099. while (auto Var = Vars->getNext())
  1100. Dumper.start(*Var);
  1101. } else {
  1102. std::vector<std::unique_ptr<PDBSymbolData>> Datas;
  1103. while (auto Var = Vars->getNext())
  1104. Datas.push_back(std::move(Var));
  1105. llvm::sort(Datas, opts::pretty::compareDataSymbols);
  1106. for (const auto &Var : Datas)
  1107. Dumper.start(*Var);
  1108. }
  1109. }
  1110. }
  1111. if (shouldDumpSymLevel(opts::pretty::SymLevel::Thunks)) {
  1112. if (auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>()) {
  1113. CompilandDumper Dumper(Printer);
  1114. while (auto Thunk = Thunks->getNext())
  1115. Dumper.dump(*Thunk);
  1116. }
  1117. }
  1118. Printer.Unindent();
  1119. }
  1120. if (opts::pretty::Externals) {
  1121. Printer.NewLine();
  1122. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
  1123. Printer.Indent();
  1124. ExternalSymbolDumper Dumper(Printer);
  1125. Dumper.start(*GlobalScope);
  1126. }
  1127. if (opts::pretty::Lines) {
  1128. Printer.NewLine();
  1129. }
  1130. if (opts::pretty::InjectedSources) {
  1131. Printer.NewLine();
  1132. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  1133. << "---INJECTED SOURCES---";
  1134. AutoIndent Indent1(Printer);
  1135. dumpInjectedSources(Printer, *Session);
  1136. }
  1137. Printer.NewLine();
  1138. outs().flush();
  1139. }
  1140. static void mergePdbs() {
  1141. BumpPtrAllocator Allocator;
  1142. MergingTypeTableBuilder MergedTpi(Allocator);
  1143. MergingTypeTableBuilder MergedIpi(Allocator);
  1144. // Create a Tpi and Ipi type table with all types from all input files.
  1145. for (const auto &Path : opts::merge::InputFilenames) {
  1146. std::unique_ptr<IPDBSession> Session;
  1147. auto &File = loadPDB(Path, Session);
  1148. SmallVector<TypeIndex, 128> TypeMap;
  1149. SmallVector<TypeIndex, 128> IdMap;
  1150. if (File.hasPDBTpiStream()) {
  1151. auto &Tpi = ExitOnErr(File.getPDBTpiStream());
  1152. ExitOnErr(
  1153. codeview::mergeTypeRecords(MergedTpi, TypeMap, Tpi.typeArray()));
  1154. }
  1155. if (File.hasPDBIpiStream()) {
  1156. auto &Ipi = ExitOnErr(File.getPDBIpiStream());
  1157. ExitOnErr(codeview::mergeIdRecords(MergedIpi, TypeMap, IdMap,
  1158. Ipi.typeArray()));
  1159. }
  1160. }
  1161. // Then write the PDB.
  1162. PDBFileBuilder Builder(Allocator);
  1163. ExitOnErr(Builder.initialize(4096));
  1164. // Add each of the reserved streams. We might not put any data in them,
  1165. // but at least they have to be present.
  1166. for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
  1167. ExitOnErr(Builder.getMsfBuilder().addStream(0));
  1168. auto &DestTpi = Builder.getTpiBuilder();
  1169. auto &DestIpi = Builder.getIpiBuilder();
  1170. MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
  1171. DestTpi.addTypeRecord(Type.RecordData, None);
  1172. });
  1173. MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
  1174. DestIpi.addTypeRecord(Type.RecordData, None);
  1175. });
  1176. Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
  1177. SmallString<64> OutFile(opts::merge::PdbOutputFile);
  1178. if (OutFile.empty()) {
  1179. OutFile = opts::merge::InputFilenames[0];
  1180. llvm::sys::path::replace_extension(OutFile, "merged.pdb");
  1181. }
  1182. codeview::GUID IgnoredOutGuid;
  1183. ExitOnErr(Builder.commit(OutFile, &IgnoredOutGuid));
  1184. }
  1185. static void explain() {
  1186. std::unique_ptr<IPDBSession> Session;
  1187. InputFile IF =
  1188. ExitOnErr(InputFile::open(opts::explain::InputFilename.front(), true));
  1189. for (uint64_t Off : opts::explain::Offsets) {
  1190. auto O = std::make_unique<ExplainOutputStyle>(IF, Off);
  1191. ExitOnErr(O->dump());
  1192. }
  1193. }
  1194. static void exportStream() {
  1195. std::unique_ptr<IPDBSession> Session;
  1196. PDBFile &File = loadPDB(opts::exportstream::InputFilename.front(), Session);
  1197. std::unique_ptr<MappedBlockStream> SourceStream;
  1198. uint32_t Index = 0;
  1199. bool Success = false;
  1200. std::string OutFileName = opts::exportstream::OutputFile;
  1201. if (!opts::exportstream::ForceName) {
  1202. // First try to parse it as an integer, if it fails fall back to treating it
  1203. // as a named stream.
  1204. if (to_integer(opts::exportstream::Stream, Index)) {
  1205. if (Index >= File.getNumStreams()) {
  1206. errs() << "Error: " << Index << " is not a valid stream index.\n";
  1207. exit(1);
  1208. }
  1209. Success = true;
  1210. outs() << "Dumping contents of stream index " << Index << " to file "
  1211. << OutFileName << ".\n";
  1212. }
  1213. }
  1214. if (!Success) {
  1215. InfoStream &IS = cantFail(File.getPDBInfoStream());
  1216. Index = ExitOnErr(IS.getNamedStreamIndex(opts::exportstream::Stream));
  1217. outs() << "Dumping contents of stream '" << opts::exportstream::Stream
  1218. << "' (index " << Index << ") to file " << OutFileName << ".\n";
  1219. }
  1220. SourceStream = File.createIndexedStream(Index);
  1221. auto OutFile = ExitOnErr(
  1222. FileOutputBuffer::create(OutFileName, SourceStream->getLength()));
  1223. FileBufferByteStream DestStream(std::move(OutFile), llvm::support::little);
  1224. BinaryStreamWriter Writer(DestStream);
  1225. ExitOnErr(Writer.writeStreamRef(*SourceStream));
  1226. ExitOnErr(DestStream.commit());
  1227. }
  1228. static bool parseRange(StringRef Str,
  1229. Optional<opts::bytes::NumberRange> &Parsed) {
  1230. if (Str.empty())
  1231. return true;
  1232. llvm::Regex R("^([^-]+)(-([^-]+))?$");
  1233. llvm::SmallVector<llvm::StringRef, 2> Matches;
  1234. if (!R.match(Str, &Matches))
  1235. return false;
  1236. Parsed.emplace();
  1237. if (!to_integer(Matches[1], Parsed->Min))
  1238. return false;
  1239. if (!Matches[3].empty()) {
  1240. Parsed->Max.emplace();
  1241. if (!to_integer(Matches[3], *Parsed->Max))
  1242. return false;
  1243. }
  1244. return true;
  1245. }
  1246. static void simplifyChunkList(llvm::cl::list<opts::ModuleSubsection> &Chunks) {
  1247. // If this list contains "All" plus some other stuff, remove the other stuff
  1248. // and just keep "All" in the list.
  1249. if (!llvm::is_contained(Chunks, opts::ModuleSubsection::All))
  1250. return;
  1251. Chunks.reset();
  1252. Chunks.push_back(opts::ModuleSubsection::All);
  1253. }
  1254. int main(int Argc, const char **Argv) {
  1255. InitLLVM X(Argc, Argv);
  1256. ExitOnErr.setBanner("llvm-pdbutil: ");
  1257. cl::HideUnrelatedOptions(
  1258. {&opts::TypeCategory, &opts::FilterCategory, &opts::OtherOptions});
  1259. cl::ParseCommandLineOptions(Argc, Argv, "LLVM PDB Dumper\n");
  1260. if (opts::BytesSubcommand) {
  1261. if (!parseRange(opts::bytes::DumpBlockRangeOpt,
  1262. opts::bytes::DumpBlockRange)) {
  1263. errs() << "Argument '" << opts::bytes::DumpBlockRangeOpt
  1264. << "' invalid format.\n";
  1265. errs().flush();
  1266. exit(1);
  1267. }
  1268. if (!parseRange(opts::bytes::DumpByteRangeOpt,
  1269. opts::bytes::DumpByteRange)) {
  1270. errs() << "Argument '" << opts::bytes::DumpByteRangeOpt
  1271. << "' invalid format.\n";
  1272. errs().flush();
  1273. exit(1);
  1274. }
  1275. }
  1276. if (opts::DumpSubcommand) {
  1277. if (opts::dump::RawAll) {
  1278. opts::dump::DumpGlobals = true;
  1279. opts::dump::DumpFpo = true;
  1280. opts::dump::DumpInlineeLines = true;
  1281. opts::dump::DumpIds = true;
  1282. opts::dump::DumpIdExtras = true;
  1283. opts::dump::DumpLines = true;
  1284. opts::dump::DumpModules = true;
  1285. opts::dump::DumpModuleFiles = true;
  1286. opts::dump::DumpPublics = true;
  1287. opts::dump::DumpSectionContribs = true;
  1288. opts::dump::DumpSectionHeaders = true;
  1289. opts::dump::DumpSectionMap = true;
  1290. opts::dump::DumpStreams = true;
  1291. opts::dump::DumpStreamBlocks = true;
  1292. opts::dump::DumpStringTable = true;
  1293. opts::dump::DumpStringTableDetails = true;
  1294. opts::dump::DumpSummary = true;
  1295. opts::dump::DumpSymbols = true;
  1296. opts::dump::DumpSymbolStats = true;
  1297. opts::dump::DumpTypes = true;
  1298. opts::dump::DumpTypeExtras = true;
  1299. opts::dump::DumpUdtStats = true;
  1300. opts::dump::DumpXme = true;
  1301. opts::dump::DumpXmi = true;
  1302. }
  1303. }
  1304. if (opts::PdbToYamlSubcommand) {
  1305. if (opts::pdb2yaml::All) {
  1306. opts::pdb2yaml::StreamMetadata = true;
  1307. opts::pdb2yaml::StreamDirectory = true;
  1308. opts::pdb2yaml::PdbStream = true;
  1309. opts::pdb2yaml::StringTable = true;
  1310. opts::pdb2yaml::DbiStream = true;
  1311. opts::pdb2yaml::TpiStream = true;
  1312. opts::pdb2yaml::IpiStream = true;
  1313. opts::pdb2yaml::PublicsStream = true;
  1314. opts::pdb2yaml::DumpModules = true;
  1315. opts::pdb2yaml::DumpModuleFiles = true;
  1316. opts::pdb2yaml::DumpModuleSyms = true;
  1317. opts::pdb2yaml::DumpModuleSubsections.push_back(
  1318. opts::ModuleSubsection::All);
  1319. }
  1320. simplifyChunkList(opts::pdb2yaml::DumpModuleSubsections);
  1321. if (opts::pdb2yaml::DumpModuleSyms || opts::pdb2yaml::DumpModuleFiles)
  1322. opts::pdb2yaml::DumpModules = true;
  1323. if (opts::pdb2yaml::DumpModules)
  1324. opts::pdb2yaml::DbiStream = true;
  1325. }
  1326. llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
  1327. if (opts::PdbToYamlSubcommand) {
  1328. pdb2Yaml(opts::pdb2yaml::InputFilename.front());
  1329. } else if (opts::YamlToPdbSubcommand) {
  1330. if (opts::yaml2pdb::YamlPdbOutputFile.empty()) {
  1331. SmallString<16> OutputFilename(opts::yaml2pdb::InputFilename.getValue());
  1332. sys::path::replace_extension(OutputFilename, ".pdb");
  1333. opts::yaml2pdb::YamlPdbOutputFile = std::string(OutputFilename.str());
  1334. }
  1335. yamlToPdb(opts::yaml2pdb::InputFilename);
  1336. } else if (opts::DiaDumpSubcommand) {
  1337. llvm::for_each(opts::diadump::InputFilenames, dumpDia);
  1338. } else if (opts::PrettySubcommand) {
  1339. if (opts::pretty::Lines)
  1340. opts::pretty::Compilands = true;
  1341. if (opts::pretty::All) {
  1342. opts::pretty::Compilands = true;
  1343. opts::pretty::Symbols = true;
  1344. opts::pretty::Globals = true;
  1345. opts::pretty::Types = true;
  1346. opts::pretty::Externals = true;
  1347. opts::pretty::Lines = true;
  1348. }
  1349. if (opts::pretty::Types) {
  1350. opts::pretty::Classes = true;
  1351. opts::pretty::Typedefs = true;
  1352. opts::pretty::Enums = true;
  1353. opts::pretty::Pointers = true;
  1354. opts::pretty::Funcsigs = true;
  1355. }
  1356. // When adding filters for excluded compilands and types, we need to
  1357. // remember that these are regexes. So special characters such as * and \
  1358. // need to be escaped in the regex. In the case of a literal \, this means
  1359. // it needs to be escaped again in the C++. So matching a single \ in the
  1360. // input requires 4 \es in the C++.
  1361. if (opts::pretty::ExcludeCompilerGenerated) {
  1362. opts::pretty::ExcludeTypes.push_back("__vc_attributes");
  1363. opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
  1364. }
  1365. if (opts::pretty::ExcludeSystemLibraries) {
  1366. opts::pretty::ExcludeCompilands.push_back(
  1367. "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
  1368. opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
  1369. opts::pretty::ExcludeCompilands.push_back(
  1370. "d:\\\\th.obj.x86fre\\\\minkernel");
  1371. }
  1372. llvm::for_each(opts::pretty::InputFilenames, dumpPretty);
  1373. } else if (opts::DumpSubcommand) {
  1374. llvm::for_each(opts::dump::InputFilenames, dumpRaw);
  1375. } else if (opts::BytesSubcommand) {
  1376. llvm::for_each(opts::bytes::InputFilenames, dumpBytes);
  1377. } else if (opts::MergeSubcommand) {
  1378. if (opts::merge::InputFilenames.size() < 2) {
  1379. errs() << "merge subcommand requires at least 2 input files.\n";
  1380. exit(1);
  1381. }
  1382. mergePdbs();
  1383. } else if (opts::ExplainSubcommand) {
  1384. explain();
  1385. } else if (opts::ExportSubcommand) {
  1386. exportStream();
  1387. }
  1388. outs().flush();
  1389. return 0;
  1390. }