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