1 //===-- BreakpadRecords.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 
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"
14 
15 using namespace lldb_private;
16 using namespace lldb_private::breakpad;
17 
18 namespace {
19 enum class Token { Unknown, Module, Info, CodeID, File, Func, Public, Stack };
20 }
21 
22 static Token toToken(llvm::StringRef str) {
23   return llvm::StringSwitch<Token>(str)
24       .Case("MODULE", Token::Module)
25       .Case("INFO", Token::Info)
26       .Case("CODE_ID", Token::CodeID)
27       .Case("FILE", Token::File)
28       .Case("FUNC", Token::Func)
29       .Case("PUBLIC", Token::Public)
30       .Case("STACK", Token::Stack)
31       .Default(Token::Unknown);
32 }
33 
34 static llvm::Triple::OSType toOS(llvm::StringRef str) {
35   using llvm::Triple;
36   return llvm::StringSwitch<Triple::OSType>(str)
37       .Case("Linux", Triple::Linux)
38       .Case("mac", Triple::MacOSX)
39       .Case("windows", Triple::Win32)
40       .Default(Triple::UnknownOS);
41 }
42 
43 static llvm::Triple::ArchType toArch(llvm::StringRef str) {
44   using llvm::Triple;
45   return llvm::StringSwitch<Triple::ArchType>(str)
46       .Case("arm", Triple::arm)
47       .Case("arm64", Triple::aarch64)
48       .Case("mips", Triple::mips)
49       .Case("ppc", Triple::ppc)
50       .Case("ppc64", Triple::ppc64)
51       .Case("s390", Triple::systemz)
52       .Case("sparc", Triple::sparc)
53       .Case("sparcv9", Triple::sparcv9)
54       .Case("x86", Triple::x86)
55       .Case("x86_64", Triple::x86_64)
56       .Default(Triple::UnknownArch);
57 }
58 
59 /// Return the number of hex digits needed to encode an (POD) object of a given
60 /// type.
61 template <typename T> static constexpr size_t hex_digits() {
62   return 2 * sizeof(T);
63 }
64 
65 /// Consume the right number of digits from the input StringRef and convert it
66 /// to the endian-specific integer N. Return true on success.
67 template <typename T> static bool consume_hex_integer(llvm::StringRef &str, T &N) {
68   llvm::StringRef chunk = str.take_front(hex_digits<T>());
69   uintmax_t t;
70   if (!to_integer(chunk, t, 16))
71     return false;
72   N = t;
73   str = str.drop_front(hex_digits<T>());
74   return true;
75 }
76 
77 static UUID parseModuleId(llvm::Triple::OSType os, llvm::StringRef str) {
78   struct data_t {
79     struct uuid_t {
80       llvm::support::ulittle32_t part1;
81       llvm::support::ulittle16_t part2[2];
82       uint8_t part3[8];
83     } uuid;
84     llvm::support::ulittle32_t age;
85   } data;
86   static_assert(sizeof(data) == 20, "");
87   // The textual module id encoding should be between 33 and 40 bytes long,
88   // depending on the size of the age field, which is of variable length.
89   // The first three chunks of the id are encoded in big endian, so we need to
90   // byte-swap those.
91   if (str.size() <= hex_digits<data_t::uuid_t>() ||
92       str.size() > hex_digits<data_t>())
93     return UUID();
94   if (!consume_hex_integer(str, data.uuid.part1))
95     return UUID();
96   for (auto &t : data.uuid.part2) {
97     if (!consume_hex_integer(str, t))
98       return UUID();
99   }
100   for (auto &t : data.uuid.part3) {
101     if (!consume_hex_integer(str, t))
102       return UUID();
103   }
104   uint32_t age;
105   if (!to_integer(str, age, 16))
106     return UUID();
107   data.age = age;
108 
109   // On non-windows, the age field should always be zero, so we don't include to
110   // match the native uuid format of these platforms.
111   return UUID::fromData(&data, os == llvm::Triple::Win32 ? sizeof(data)
112                                                          : sizeof(data.uuid));
113 }
114 
115 Record::Kind Record::classify(llvm::StringRef Line) {
116   Token Tok = toToken(getToken(Line).first);
117   switch (Tok) {
118   case Token::Module:
119     return Record::Module;
120   case Token::Info:
121     return Record::Info;
122   case Token::File:
123     return Record::File;
124   case Token::Func:
125     return Record::Func;
126   case Token::Public:
127     return Record::Public;
128   case Token::Stack:
129     return Record::Stack;
130 
131   case Token::CodeID:
132   case Token::Unknown:
133     // Optimistically assume that any unrecognised token means this is a line
134     // record, those don't have a special keyword and start directly with a
135     // hex number. CODE_ID should never be at the start of a line, but if it
136     // is, it can be treated the same way as a garbled line record.
137     return Record::Line;
138   }
139   llvm_unreachable("Fully covered switch above!");
140 }
141 
142 llvm::Optional<ModuleRecord> ModuleRecord::parse(llvm::StringRef Line) {
143   // MODULE Linux x86_64 E5894855C35DCCCCCCCCCCCCCCCCCCCC0 a.out
144   llvm::StringRef Str;
145   std::tie(Str, Line) = getToken(Line);
146   if (toToken(Str) != Token::Module)
147     return llvm::None;
148 
149   std::tie(Str, Line) = getToken(Line);
150   llvm::Triple::OSType OS = toOS(Str);
151   if (OS == llvm::Triple::UnknownOS)
152     return llvm::None;
153 
154   std::tie(Str, Line) = getToken(Line);
155   llvm::Triple::ArchType Arch = toArch(Str);
156   if (Arch == llvm::Triple::UnknownArch)
157     return llvm::None;
158 
159   std::tie(Str, Line) = getToken(Line);
160   UUID ID = parseModuleId(OS, Str);
161   if (!ID)
162     return llvm::None;
163 
164   return ModuleRecord(OS, Arch, std::move(ID));
165 }
166 
167 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
168                                         const ModuleRecord &R) {
169   return OS << "MODULE " << llvm::Triple::getOSTypeName(R.OS) << " "
170             << llvm::Triple::getArchTypeName(R.Arch) << " "
171             << R.ID.GetAsString();
172 }
173 
174 llvm::Optional<InfoRecord> InfoRecord::parse(llvm::StringRef Line) {
175   // INFO CODE_ID 554889E55DC3CCCCCCCCCCCCCCCCCCCC [a.exe]
176   llvm::StringRef Str;
177   std::tie(Str, Line) = getToken(Line);
178   if (toToken(Str) != Token::Info)
179     return llvm::None;
180 
181   std::tie(Str, Line) = getToken(Line);
182   if (toToken(Str) != Token::CodeID)
183     return llvm::None;
184 
185   std::tie(Str, Line) = getToken(Line);
186   // If we don't have any text following the code ID (e.g. on linux), we should
187   // use this as the UUID. Otherwise, we should revert back to the module ID.
188   UUID ID;
189   if (Line.trim().empty()) {
190     if (Str.empty() || ID.SetFromStringRef(Str, Str.size() / 2) != Str.size())
191       return llvm::None;
192   }
193   return InfoRecord(std::move(ID));
194 }
195 
196 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
197                                         const InfoRecord &R) {
198   return OS << "INFO CODE_ID " << R.ID.GetAsString();
199 }
200 
201 static bool parsePublicOrFunc(llvm::StringRef Line, bool &Multiple,
202                               lldb::addr_t &Address, lldb::addr_t *Size,
203                               lldb::addr_t &ParamSize, llvm::StringRef &Name) {
204   // PUBLIC [m] address param_size name
205   // or
206   // FUNC [m] address size param_size name
207 
208   Token Tok = Size ? Token::Func : Token::Public;
209 
210   llvm::StringRef Str;
211   std::tie(Str, Line) = getToken(Line);
212   if (toToken(Str) != Tok)
213     return false;
214 
215   std::tie(Str, Line) = getToken(Line);
216   Multiple = Str == "m";
217 
218   if (Multiple)
219     std::tie(Str, Line) = getToken(Line);
220   if (!to_integer(Str, Address, 16))
221     return false;
222 
223   if (Tok == Token::Func) {
224     std::tie(Str, Line) = getToken(Line);
225     if (!to_integer(Str, *Size, 16))
226       return false;
227   }
228 
229   std::tie(Str, Line) = getToken(Line);
230   if (!to_integer(Str, ParamSize, 16))
231     return false;
232 
233   Name = Line.trim();
234   if (Name.empty())
235     return false;
236 
237   return true;
238 }
239 
240 llvm::Optional<FuncRecord> FuncRecord::parse(llvm::StringRef Line) {
241   bool Multiple;
242   lldb::addr_t Address, Size, ParamSize;
243   llvm::StringRef Name;
244 
245   if (parsePublicOrFunc(Line, Multiple, Address, &Size, ParamSize, Name))
246     return FuncRecord(Multiple, Address, Size, ParamSize, Name);
247 
248   return llvm::None;
249 }
250 
251 bool breakpad::operator==(const FuncRecord &L, const FuncRecord &R) {
252   return L.Multiple == R.Multiple && L.Address == R.Address &&
253          L.Size == R.Size && L.ParamSize == R.ParamSize && L.Name == R.Name;
254 }
255 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
256                                         const FuncRecord &R) {
257   return OS << llvm::formatv("FUNC {0}{1:x-} {2:x-} {3:x-} {4}",
258                              R.Multiple ? "m " : "", R.Address, R.Size,
259                              R.ParamSize, R.Name);
260 }
261 
262 llvm::Optional<PublicRecord> PublicRecord::parse(llvm::StringRef Line) {
263   bool Multiple;
264   lldb::addr_t Address, ParamSize;
265   llvm::StringRef Name;
266 
267   if (parsePublicOrFunc(Line, Multiple, Address, nullptr, ParamSize, Name))
268     return PublicRecord(Multiple, Address, ParamSize, Name);
269 
270   return llvm::None;
271 }
272 
273 bool breakpad::operator==(const PublicRecord &L, const PublicRecord &R) {
274   return L.Multiple == R.Multiple && L.Address == R.Address &&
275          L.ParamSize == R.ParamSize && L.Name == R.Name;
276 }
277 llvm::raw_ostream &breakpad::operator<<(llvm::raw_ostream &OS,
278                                         const PublicRecord &R) {
279   return OS << llvm::formatv("PUBLIC {0}{1:x-} {2:x-} {3}",
280                              R.Multiple ? "m " : "", R.Address, R.ParamSize,
281                              R.Name);
282 }
283 
284 llvm::StringRef breakpad::toString(Record::Kind K) {
285   switch (K) {
286   case Record::Module:
287     return "MODULE";
288   case Record::Info:
289     return "INFO";
290   case Record::File:
291     return "FILE";
292   case Record::Func:
293     return "FUNC";
294   case Record::Line:
295     return "LINE";
296   case Record::Public:
297     return "PUBLIC";
298   case Record::Stack:
299     return "STACK";
300   }
301   llvm_unreachable("Unknown record kind!");
302 }
303