1 //===-- BreakpadRecords.cpp ----------------------------------- -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/Support/Endian.h"
13 #include "llvm/Support/FormatVariadic.h"
15 using namespace lldb_private;
16 using namespace lldb_private::breakpad;
35 static T stringTo(llvm::StringRef Str);
37 template <> Token stringTo<Token>(llvm::StringRef Str) {
38 return llvm::StringSwitch<Token>(Str)
39 .Case("MODULE", Token::Module)
40 .Case("INFO", Token::Info)
41 .Case("CODE_ID", Token::CodeID)
42 .Case("FILE", Token::File)
43 .Case("FUNC", Token::Func)
44 .Case("PUBLIC", Token::Public)
45 .Case("STACK", Token::Stack)
46 .Case("CFI", Token::CFI)
47 .Case("INIT", Token::Init)
48 .Case("WIN", Token::Win)
49 .Default(Token::Unknown);
53 llvm::Triple::OSType stringTo<llvm::Triple::OSType>(llvm::StringRef Str) {
55 return llvm::StringSwitch<Triple::OSType>(Str)
56 .Case("Linux", Triple::Linux)
57 .Case("mac", Triple::MacOSX)
58 .Case("windows", Triple::Win32)
59 .Default(Triple::UnknownOS);
63 llvm::Triple::ArchType stringTo<llvm::Triple::ArchType>(llvm::StringRef Str) {
65 return llvm::StringSwitch<Triple::ArchType>(Str)
66 .Case("arm", Triple::arm)
67 .Cases("arm64", "arm64e", Triple::aarch64)
68 .Case("mips", Triple::mips)
69 .Case("ppc", Triple::ppc)
70 .Case("ppc64", Triple::ppc64)
71 .Case("s390", Triple::systemz)
72 .Case("sparc", Triple::sparc)
73 .Case("sparcv9", Triple::sparcv9)
74 .Case("x86", Triple::x86)
75 .Cases("x86_64", "x86_64h", Triple::x86_64)
76 .Default(Triple::UnknownArch);
80 static T consume(llvm::StringRef &Str) {
81 llvm::StringRef Token;
82 std::tie(Token, Str) = getToken(Str);
83 return stringTo<T>(Token);
86 /// Return the number of hex digits needed to encode an (POD) object of a given
88 template <typename T> static constexpr size_t hex_digits() {
92 static UUID parseModuleId(llvm::Triple::OSType os, llvm::StringRef str) {
94 using uuid_t = uint8_t[16];
96 llvm::support::ubig32_t age;
98 static_assert(sizeof(data) == 20, "");
99 // The textual module id encoding should be between 33 and 40 bytes long,
100 // depending on the size of the age field, which is of variable length.
101 // The first three chunks of the id are encoded in big endian, so we need to
103 if (str.size() <= hex_digits<data_t::uuid_t>() ||
104 str.size() > hex_digits<data_t>())
106 if (!all_of(str, llvm::isHexDigit))
109 llvm::StringRef uuid_str = str.take_front(hex_digits<data_t::uuid_t>());
110 llvm::StringRef age_str = str.drop_front(hex_digits<data_t::uuid_t>());
112 llvm::copy(fromHex(uuid_str), data.uuid);
114 bool success = to_integer(age_str, age, 16);
119 // On non-windows, the age field should always be zero, so we don't include to
120 // match the native uuid format of these platforms.
121 return UUID::fromData(&data, os == llvm::Triple::Win32 ? sizeof(data)
122 : sizeof(data.uuid));
125 llvm::Optional<Record::Kind> Record::classify(llvm::StringRef Line) {
126 Token Tok = consume<Token>(Line);
129 return Record::Module;
137 return Record::Public;
139 Tok = consume<Token>(Line);
142 return Record::StackCFI;
144 return Record::StackWin;
150 // Optimistically assume that any unrecognised token means this is a line
151 // record, those don't have a special keyword and start directly with a
159 // These should never appear at the start of a valid record.
162 llvm_unreachable("Fully covered switch above!");
165 llvm::Optional<ModuleRecord> ModuleRecord::parse(llvm::StringRef Line) {
166 // MODULE Linux x86_64 E5894855C35DCCCCCCCCCCCCCCCCCCCC0 a.out
167 if (consume<Token>(Line) != Token::Module)
170 llvm::Triple::OSType OS = consume<llvm::Triple::OSType>(Line);
171 if (OS == llvm::Triple::UnknownOS)
174 llvm::Triple::ArchType Arch = consume<llvm::Triple::ArchType>(Line);
175 if (Arch == llvm::Triple::UnknownArch)
179 std::tie(Str, Line) = getToken(Line);
180 UUID ID = parseModuleId(OS, Str);
184 return ModuleRecord(OS, Arch, std::move(ID));
187 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
188 const ModuleRecord &R) {
189 return OS << "MODULE " << llvm::Triple::getOSTypeName(R.OS) << " "
190 << llvm::Triple::getArchTypeName(R.Arch) << " "
191 << R.ID.GetAsString();
194 llvm::Optional<InfoRecord> InfoRecord::parse(llvm::StringRef Line) {
195 // INFO CODE_ID 554889E55DC3CCCCCCCCCCCCCCCCCCCC [a.exe]
196 if (consume<Token>(Line) != Token::Info)
199 if (consume<Token>(Line) != Token::CodeID)
203 std::tie(Str, Line) = getToken(Line);
204 // If we don't have any text following the code ID (e.g. on linux), we should
205 // use this as the UUID. Otherwise, we should revert back to the module ID.
207 if (Line.trim().empty()) {
208 if (Str.empty() || ID.SetFromStringRef(Str, Str.size() / 2) != Str.size())
211 return InfoRecord(std::move(ID));
214 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
215 const InfoRecord &R) {
216 return OS << "INFO CODE_ID " << R.ID.GetAsString();
219 llvm::Optional<FileRecord> FileRecord::parse(llvm::StringRef Line) {
221 if (consume<Token>(Line) != Token::File)
226 std::tie(Str, Line) = getToken(Line);
227 if (!to_integer(Str, Number))
230 llvm::StringRef Name = Line.trim();
234 return FileRecord(Number, Name);
237 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
238 const FileRecord &R) {
239 return OS << "FILE " << R.Number << " " << R.Name;
242 static bool parsePublicOrFunc(llvm::StringRef Line, bool &Multiple,
243 lldb::addr_t &Address, lldb::addr_t *Size,
244 lldb::addr_t &ParamSize, llvm::StringRef &Name) {
245 // PUBLIC [m] address param_size name
247 // FUNC [m] address size param_size name
249 Token Tok = Size ? Token::Func : Token::Public;
251 if (consume<Token>(Line) != Tok)
255 std::tie(Str, Line) = getToken(Line);
256 Multiple = Str == "m";
259 std::tie(Str, Line) = getToken(Line);
260 if (!to_integer(Str, Address, 16))
263 if (Tok == Token::Func) {
264 std::tie(Str, Line) = getToken(Line);
265 if (!to_integer(Str, *Size, 16))
269 std::tie(Str, Line) = getToken(Line);
270 if (!to_integer(Str, ParamSize, 16))
280 llvm::Optional<FuncRecord> FuncRecord::parse(llvm::StringRef Line) {
282 lldb::addr_t Address, Size, ParamSize;
283 llvm::StringRef Name;
285 if (parsePublicOrFunc(Line, Multiple, Address, &Size, ParamSize, Name))
286 return FuncRecord(Multiple, Address, Size, ParamSize, Name);
291 bool breakpad::operator==(const FuncRecord &L, const FuncRecord &R) {
292 return L.Multiple == R.Multiple && L.Address == R.Address &&
293 L.Size == R.Size && L.ParamSize == R.ParamSize && L.Name == R.Name;
295 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
296 const FuncRecord &R) {
297 return OS << llvm::formatv("FUNC {0}{1:x-} {2:x-} {3:x-} {4}",
298 R.Multiple ? "m " : "", R.Address, R.Size,
299 R.ParamSize, R.Name);
302 llvm::Optional<LineRecord> LineRecord::parse(llvm::StringRef Line) {
303 lldb::addr_t Address;
305 std::tie(Str, Line) = getToken(Line);
306 if (!to_integer(Str, Address, 16))
310 std::tie(Str, Line) = getToken(Line);
311 if (!to_integer(Str, Size, 16))
315 std::tie(Str, Line) = getToken(Line);
316 if (!to_integer(Str, LineNum))
320 std::tie(Str, Line) = getToken(Line);
321 if (!to_integer(Str, FileNum))
324 return LineRecord(Address, Size, LineNum, FileNum);
327 bool breakpad::operator==(const LineRecord &L, const LineRecord &R) {
328 return L.Address == R.Address && L.Size == R.Size && L.LineNum == R.LineNum &&
329 L.FileNum == R.FileNum;
331 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
332 const LineRecord &R) {
333 return OS << llvm::formatv("{0:x-} {1:x-} {2} {3}", R.Address, R.Size,
334 R.LineNum, R.FileNum);
337 llvm::Optional<PublicRecord> PublicRecord::parse(llvm::StringRef Line) {
339 lldb::addr_t Address, ParamSize;
340 llvm::StringRef Name;
342 if (parsePublicOrFunc(Line, Multiple, Address, nullptr, ParamSize, Name))
343 return PublicRecord(Multiple, Address, ParamSize, Name);
348 bool breakpad::operator==(const PublicRecord &L, const PublicRecord &R) {
349 return L.Multiple == R.Multiple && L.Address == R.Address &&
350 L.ParamSize == R.ParamSize && L.Name == R.Name;
352 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
353 const PublicRecord &R) {
354 return OS << llvm::formatv("PUBLIC {0}{1:x-} {2:x-} {3}",
355 R.Multiple ? "m " : "", R.Address, R.ParamSize,
359 llvm::Optional<StackCFIRecord> StackCFIRecord::parse(llvm::StringRef Line) {
360 // STACK CFI INIT address size reg1: expr1 reg2: expr2 ...
362 // STACK CFI address reg1: expr1 reg2: expr2 ...
363 // No token in exprN ends with a colon.
365 if (consume<Token>(Line) != Token::Stack)
367 if (consume<Token>(Line) != Token::CFI)
371 std::tie(Str, Line) = getToken(Line);
373 bool IsInitRecord = stringTo<Token>(Str) == Token::Init;
375 std::tie(Str, Line) = getToken(Line);
377 lldb::addr_t Address;
378 if (!to_integer(Str, Address, 16))
381 llvm::Optional<lldb::addr_t> Size;
384 std::tie(Str, Line) = getToken(Line);
385 if (!to_integer(Str, *Size, 16))
389 return StackCFIRecord(Address, Size, Line.trim());
392 bool breakpad::operator==(const StackCFIRecord &L, const StackCFIRecord &R) {
393 return L.Address == R.Address && L.Size == R.Size &&
394 L.UnwindRules == R.UnwindRules;
397 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
398 const StackCFIRecord &R) {
402 OS << llvm::formatv("{0:x-} ", R.Address);
404 OS << llvm::formatv("{0:x-} ", *R.Size);
405 return OS << " " << R.UnwindRules;
408 llvm::Optional<StackWinRecord> StackWinRecord::parse(llvm::StringRef Line) {
409 // STACK WIN type rva code_size prologue_size epilogue_size parameter_size
410 // saved_register_size local_size max_stack_size has_program_string
411 // program_string_OR_allocates_base_pointer
413 if (consume<Token>(Line) != Token::Stack)
415 if (consume<Token>(Line) != Token::Win)
420 std::tie(Str, Line) = getToken(Line);
421 // Right now we only support the "FrameData" frame type.
422 if (!to_integer(Str, Type) || FrameType(Type) != FrameType::FrameData)
426 std::tie(Str, Line) = getToken(Line);
427 if (!to_integer(Str, RVA, 16))
430 lldb::addr_t CodeSize;
431 std::tie(Str, Line) = getToken(Line);
432 if (!to_integer(Str, CodeSize, 16))
435 // Skip fields which we aren't using right now.
436 std::tie(Str, Line) = getToken(Line); // prologue_size
437 std::tie(Str, Line) = getToken(Line); // epilogue_size
439 lldb::addr_t ParameterSize;
440 std::tie(Str, Line) = getToken(Line);
441 if (!to_integer(Str, ParameterSize, 16))
444 lldb::addr_t SavedRegisterSize;
445 std::tie(Str, Line) = getToken(Line);
446 if (!to_integer(Str, SavedRegisterSize, 16))
449 lldb::addr_t LocalSize;
450 std::tie(Str, Line) = getToken(Line);
451 if (!to_integer(Str, LocalSize, 16))
454 std::tie(Str, Line) = getToken(Line); // max_stack_size
456 uint8_t HasProgramString;
457 std::tie(Str, Line) = getToken(Line);
458 if (!to_integer(Str, HasProgramString))
460 // FrameData records should always have a program string.
461 if (!HasProgramString)
464 return StackWinRecord(RVA, CodeSize, ParameterSize, SavedRegisterSize,
465 LocalSize, Line.trim());
468 bool breakpad::operator==(const StackWinRecord &L, const StackWinRecord &R) {
469 return L.RVA == R.RVA && L.CodeSize == R.CodeSize &&
470 L.ParameterSize == R.ParameterSize &&
471 L.SavedRegisterSize == R.SavedRegisterSize &&
472 L.LocalSize == R.LocalSize && L.ProgramString == R.ProgramString;
475 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
476 const StackWinRecord &R) {
477 return OS << llvm::formatv(
478 "STACK WIN 4 {0:x-} {1:x-} ? ? {2} {3} {4} ? 1 {5}", R.RVA,
479 R.CodeSize, R.ParameterSize, R.SavedRegisterSize, R.LocalSize,
483 llvm::StringRef breakpad::toString(Record::Kind K) {
497 case Record::StackCFI:
499 case Record::StackWin:
502 llvm_unreachable("Unknown record kind!");