FormatUtil.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //===- FormatUtil.cpp ----------------------------------------- *- 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. #include "FormatUtil.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/BinaryFormat/COFF.h"
  12. #include "llvm/DebugInfo/CodeView/CodeView.h"
  13. #include "llvm/Support/FormatAdapters.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. using namespace llvm;
  16. using namespace llvm::codeview;
  17. using namespace llvm::pdb;
  18. std::string llvm::pdb::truncateStringBack(StringRef S, uint32_t MaxLen) {
  19. if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
  20. return std::string(S);
  21. assert(MaxLen >= 3);
  22. uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3);
  23. S = S.take_front(FinalLen);
  24. return std::string(S) + std::string("...");
  25. }
  26. std::string llvm::pdb::truncateStringMiddle(StringRef S, uint32_t MaxLen) {
  27. if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
  28. return std::string(S);
  29. assert(MaxLen >= 3);
  30. uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3);
  31. StringRef Front = S.take_front(FinalLen / 2);
  32. StringRef Back = S.take_back(Front.size());
  33. return std::string(Front) + std::string("...") + std::string(Back);
  34. }
  35. std::string llvm::pdb::truncateStringFront(StringRef S, uint32_t MaxLen) {
  36. if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
  37. return std::string(S);
  38. assert(MaxLen >= 3);
  39. S = S.take_back(MaxLen - 3);
  40. return std::string("...") + std::string(S);
  41. }
  42. std::string llvm::pdb::truncateQuotedNameFront(StringRef Label, StringRef Name,
  43. uint32_t MaxLen) {
  44. uint32_t RequiredExtraChars = Label.size() + 1 + 2;
  45. if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen)
  46. return formatv("{0} \"{1}\"", Label, Name).str();
  47. assert(MaxLen >= RequiredExtraChars);
  48. std::string TN = truncateStringFront(Name, MaxLen - RequiredExtraChars);
  49. return formatv("{0} \"{1}\"", Label, TN).str();
  50. }
  51. std::string llvm::pdb::truncateQuotedNameBack(StringRef Label, StringRef Name,
  52. uint32_t MaxLen) {
  53. uint32_t RequiredExtraChars = Label.size() + 1 + 2;
  54. if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen)
  55. return formatv("{0} \"{1}\"", Label, Name).str();
  56. assert(MaxLen >= RequiredExtraChars);
  57. std::string TN = truncateStringBack(Name, MaxLen - RequiredExtraChars);
  58. return formatv("{0} \"{1}\"", Label, TN).str();
  59. }
  60. std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts,
  61. uint32_t IndentLevel, uint32_t GroupSize,
  62. StringRef Sep) {
  63. std::string Result;
  64. while (!Opts.empty()) {
  65. ArrayRef<std::string> ThisGroup;
  66. ThisGroup = Opts.take_front(GroupSize);
  67. Opts = Opts.drop_front(ThisGroup.size());
  68. Result += join(ThisGroup, Sep);
  69. if (!Opts.empty()) {
  70. Result += Sep;
  71. Result += "\n";
  72. Result += std::string(formatv("{0}", fmt_repeat(' ', IndentLevel)));
  73. }
  74. }
  75. return Result;
  76. }
  77. std::string llvm::pdb::typesetStringList(uint32_t IndentLevel,
  78. ArrayRef<StringRef> Strings) {
  79. std::string Result = "[";
  80. for (const auto &S : Strings) {
  81. Result += std::string(formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S));
  82. }
  83. Result += "]";
  84. return Result;
  85. }
  86. std::string llvm::pdb::formatChunkKind(DebugSubsectionKind Kind,
  87. bool Friendly) {
  88. if (Friendly) {
  89. switch (Kind) {
  90. RETURN_CASE(DebugSubsectionKind, None, "none");
  91. RETURN_CASE(DebugSubsectionKind, Symbols, "symbols");
  92. RETURN_CASE(DebugSubsectionKind, Lines, "lines");
  93. RETURN_CASE(DebugSubsectionKind, StringTable, "strings");
  94. RETURN_CASE(DebugSubsectionKind, FileChecksums, "checksums");
  95. RETURN_CASE(DebugSubsectionKind, FrameData, "frames");
  96. RETURN_CASE(DebugSubsectionKind, InlineeLines, "inlinee lines");
  97. RETURN_CASE(DebugSubsectionKind, CrossScopeImports, "xmi");
  98. RETURN_CASE(DebugSubsectionKind, CrossScopeExports, "xme");
  99. RETURN_CASE(DebugSubsectionKind, ILLines, "il lines");
  100. RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap, "func md token map");
  101. RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap, "type md token map");
  102. RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
  103. "merged assembly input");
  104. RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA, "coff symbol rva");
  105. }
  106. } else {
  107. switch (Kind) {
  108. RETURN_CASE(DebugSubsectionKind, None, "none");
  109. RETURN_CASE(DebugSubsectionKind, Symbols, "DEBUG_S_SYMBOLS");
  110. RETURN_CASE(DebugSubsectionKind, Lines, "DEBUG_S_LINES");
  111. RETURN_CASE(DebugSubsectionKind, StringTable, "DEBUG_S_STRINGTABLE");
  112. RETURN_CASE(DebugSubsectionKind, FileChecksums, "DEBUG_S_FILECHKSMS");
  113. RETURN_CASE(DebugSubsectionKind, FrameData, "DEBUG_S_FRAMEDATA");
  114. RETURN_CASE(DebugSubsectionKind, InlineeLines, "DEBUG_S_INLINEELINES");
  115. RETURN_CASE(DebugSubsectionKind, CrossScopeImports,
  116. "DEBUG_S_CROSSSCOPEIMPORTS");
  117. RETURN_CASE(DebugSubsectionKind, CrossScopeExports,
  118. "DEBUG_S_CROSSSCOPEEXPORTS");
  119. RETURN_CASE(DebugSubsectionKind, ILLines, "DEBUG_S_IL_LINES");
  120. RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap,
  121. "DEBUG_S_FUNC_MDTOKEN_MAP");
  122. RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap,
  123. "DEBUG_S_TYPE_MDTOKEN_MAP");
  124. RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
  125. "DEBUG_S_MERGED_ASSEMBLYINPUT");
  126. RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA,
  127. "DEBUG_S_COFF_SYMBOL_RVA");
  128. }
  129. }
  130. return formatUnknownEnum(Kind);
  131. }
  132. std::string llvm::pdb::formatSymbolKind(SymbolKind K) {
  133. switch (uint32_t(K)) {
  134. #define SYMBOL_RECORD(EnumName, value, name) \
  135. case EnumName: \
  136. return #EnumName;
  137. #define CV_SYMBOL(EnumName, value) SYMBOL_RECORD(EnumName, value, EnumName)
  138. #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
  139. }
  140. return formatUnknownEnum(K);
  141. }
  142. std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
  143. switch (K) {
  144. #define TYPE_RECORD(EnumName, value, name) \
  145. case EnumName: \
  146. return #EnumName;
  147. #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
  148. default:
  149. return formatv("UNKNOWN RECORD ({0:X})",
  150. static_cast<std::underlying_type_t<TypeLeafKind>>(K))
  151. .str();
  152. }
  153. }
  154. std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {
  155. return std::string(formatv("{0:4}:{1:4}", Segment, Offset));
  156. }
  157. #define PUSH_CHARACTERISTIC_FLAG(Enum, TheOpt, Value, Style, Descriptive) \
  158. PUSH_FLAG(Enum, TheOpt, Value, \
  159. ((Style == CharacteristicStyle::HeaderDefinition) ? #TheOpt \
  160. : Descriptive))
  161. #define PUSH_MASKED_CHARACTERISTIC_FLAG(Enum, Mask, TheOpt, Value, Style, \
  162. Descriptive) \
  163. PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value, \
  164. ((Style == CharacteristicStyle::HeaderDefinition) \
  165. ? #TheOpt \
  166. : Descriptive))
  167. std::string llvm::pdb::formatSectionCharacteristics(uint32_t IndentLevel,
  168. uint32_t C,
  169. uint32_t FlagsPerLine,
  170. StringRef Separator,
  171. CharacteristicStyle Style) {
  172. using SC = COFF::SectionCharacteristics;
  173. std::vector<std::string> Opts;
  174. if (C == COFF::SC_Invalid)
  175. return "invalid";
  176. if (C == 0)
  177. return "none";
  178. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NOLOAD, C, Style, "noload");
  179. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NO_PAD, C, Style, "no padding");
  180. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_CODE, C, Style, "code");
  181. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_INITIALIZED_DATA, C, Style,
  182. "initialized data");
  183. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_UNINITIALIZED_DATA, C, Style,
  184. "uninitialized data");
  185. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_OTHER, C, Style, "other");
  186. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_INFO, C, Style, "info");
  187. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_REMOVE, C, Style, "remove");
  188. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_COMDAT, C, Style, "comdat");
  189. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_GPREL, C, Style, "gp rel");
  190. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PURGEABLE, C, Style, "purgeable");
  191. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_16BIT, C, Style, "16-bit");
  192. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_LOCKED, C, Style, "locked");
  193. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PRELOAD, C, Style, "preload");
  194. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1BYTES, C,
  195. Style, "1 byte align");
  196. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2BYTES, C,
  197. Style, "2 byte align");
  198. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4BYTES, C,
  199. Style, "4 byte align");
  200. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8BYTES, C,
  201. Style, "8 byte align");
  202. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_16BYTES, C,
  203. Style, "16 byte align");
  204. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_32BYTES, C,
  205. Style, "32 byte align");
  206. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_64BYTES, C,
  207. Style, "64 byte align");
  208. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_128BYTES, C,
  209. Style, "128 byte align");
  210. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_256BYTES, C,
  211. Style, "256 byte align");
  212. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_512BYTES, C,
  213. Style, "512 byte align");
  214. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1024BYTES, C,
  215. Style, "1024 byte align");
  216. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2048BYTES, C,
  217. Style, "2048 byte align");
  218. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4096BYTES, C,
  219. Style, "4096 byte align");
  220. PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8192BYTES, C,
  221. Style, "8192 byte align");
  222. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_NRELOC_OVFL, C, Style,
  223. "noreloc overflow");
  224. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_DISCARDABLE, C, Style,
  225. "discardable");
  226. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_CACHED, C, Style,
  227. "not cached");
  228. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_PAGED, C, Style, "not paged");
  229. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_SHARED, C, Style, "shared");
  230. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_EXECUTE, C, Style,
  231. "execute permissions");
  232. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_READ, C, Style,
  233. "read permissions");
  234. PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_WRITE, C, Style,
  235. "write permissions");
  236. return typesetItemList(Opts, IndentLevel, FlagsPerLine, Separator);
  237. }