1 //===-- SymbolFileBreakpad.cpp --------------------------------------------===// 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/SymbolFile/Breakpad/SymbolFileBreakpad.h" 10 #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h" 11 #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Core/Section.h" 15 #include "lldb/Host/FileSystem.h" 16 #include "lldb/Symbol/CompileUnit.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Symbol/SymbolVendor.h" 19 #include "lldb/Symbol/TypeMap.h" 20 #include "lldb/Utility/Log.h" 21 #include "lldb/Utility/StreamString.h" 22 #include "llvm/ADT/StringExtras.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 using namespace lldb_private::breakpad; 27 28 LLDB_PLUGIN_DEFINE(SymbolFileBreakpad) 29 30 char SymbolFileBreakpad::ID; 31 32 class SymbolFileBreakpad::LineIterator { 33 public: 34 // begin iterator for sections of given type 35 LineIterator(ObjectFile &obj, Record::Kind section_type) 36 : m_obj(&obj), m_section_type(toString(section_type)), 37 m_next_section_idx(0), m_next_line(llvm::StringRef::npos) { 38 ++*this; 39 } 40 41 // An iterator starting at the position given by the bookmark. 42 LineIterator(ObjectFile &obj, Record::Kind section_type, Bookmark bookmark); 43 44 // end iterator 45 explicit LineIterator(ObjectFile &obj) 46 : m_obj(&obj), 47 m_next_section_idx(m_obj->GetSectionList()->GetNumSections(0)), 48 m_current_line(llvm::StringRef::npos), 49 m_next_line(llvm::StringRef::npos) {} 50 51 friend bool operator!=(const LineIterator &lhs, const LineIterator &rhs) { 52 assert(lhs.m_obj == rhs.m_obj); 53 if (lhs.m_next_section_idx != rhs.m_next_section_idx) 54 return true; 55 if (lhs.m_current_line != rhs.m_current_line) 56 return true; 57 assert(lhs.m_next_line == rhs.m_next_line); 58 return false; 59 } 60 61 const LineIterator &operator++(); 62 llvm::StringRef operator*() const { 63 return m_section_text.slice(m_current_line, m_next_line); 64 } 65 66 Bookmark GetBookmark() const { 67 return Bookmark{m_next_section_idx, m_current_line}; 68 } 69 70 private: 71 ObjectFile *m_obj; 72 ConstString m_section_type; 73 uint32_t m_next_section_idx; 74 llvm::StringRef m_section_text; 75 size_t m_current_line; 76 size_t m_next_line; 77 78 void FindNextLine() { 79 m_next_line = m_section_text.find('\n', m_current_line); 80 if (m_next_line != llvm::StringRef::npos) { 81 ++m_next_line; 82 if (m_next_line >= m_section_text.size()) 83 m_next_line = llvm::StringRef::npos; 84 } 85 } 86 }; 87 88 SymbolFileBreakpad::LineIterator::LineIterator(ObjectFile &obj, 89 Record::Kind section_type, 90 Bookmark bookmark) 91 : m_obj(&obj), m_section_type(toString(section_type)), 92 m_next_section_idx(bookmark.section), m_current_line(bookmark.offset) { 93 Section § = 94 *obj.GetSectionList()->GetSectionAtIndex(m_next_section_idx - 1); 95 assert(sect.GetName() == m_section_type); 96 97 DataExtractor data; 98 obj.ReadSectionData(§, data); 99 m_section_text = toStringRef(data.GetData()); 100 101 assert(m_current_line < m_section_text.size()); 102 FindNextLine(); 103 } 104 105 const SymbolFileBreakpad::LineIterator & 106 SymbolFileBreakpad::LineIterator::operator++() { 107 const SectionList &list = *m_obj->GetSectionList(); 108 size_t num_sections = list.GetNumSections(0); 109 while (m_next_line != llvm::StringRef::npos || 110 m_next_section_idx < num_sections) { 111 if (m_next_line != llvm::StringRef::npos) { 112 m_current_line = m_next_line; 113 FindNextLine(); 114 return *this; 115 } 116 117 Section § = *list.GetSectionAtIndex(m_next_section_idx++); 118 if (sect.GetName() != m_section_type) 119 continue; 120 DataExtractor data; 121 m_obj->ReadSectionData(§, data); 122 m_section_text = toStringRef(data.GetData()); 123 m_next_line = 0; 124 } 125 // We've reached the end. 126 m_current_line = m_next_line; 127 return *this; 128 } 129 130 llvm::iterator_range<SymbolFileBreakpad::LineIterator> 131 SymbolFileBreakpad::lines(Record::Kind section_type) { 132 return llvm::make_range(LineIterator(*m_objfile_sp, section_type), 133 LineIterator(*m_objfile_sp)); 134 } 135 136 namespace { 137 // A helper class for constructing the list of support files for a given compile 138 // unit. 139 class SupportFileMap { 140 public: 141 // Given a breakpad file ID, return a file ID to be used in the support files 142 // for this compile unit. 143 size_t operator[](size_t file) { 144 return m_map.try_emplace(file, m_map.size() + 1).first->second; 145 } 146 147 // Construct a FileSpecList containing only the support files relevant for 148 // this compile unit (in the correct order). 149 FileSpecList translate(const FileSpec &cu_spec, 150 llvm::ArrayRef<FileSpec> all_files); 151 152 private: 153 llvm::DenseMap<size_t, size_t> m_map; 154 }; 155 } // namespace 156 157 FileSpecList SupportFileMap::translate(const FileSpec &cu_spec, 158 llvm::ArrayRef<FileSpec> all_files) { 159 std::vector<FileSpec> result; 160 result.resize(m_map.size() + 1); 161 result[0] = cu_spec; 162 for (const auto &KV : m_map) { 163 if (KV.first < all_files.size()) 164 result[KV.second] = all_files[KV.first]; 165 } 166 return FileSpecList(std::move(result)); 167 } 168 169 void SymbolFileBreakpad::Initialize() { 170 PluginManager::RegisterPlugin(GetPluginNameStatic(), 171 GetPluginDescriptionStatic(), CreateInstance, 172 DebuggerInitialize); 173 } 174 175 void SymbolFileBreakpad::Terminate() { 176 PluginManager::UnregisterPlugin(CreateInstance); 177 } 178 179 ConstString SymbolFileBreakpad::GetPluginNameStatic() { 180 static ConstString g_name("breakpad"); 181 return g_name; 182 } 183 184 uint32_t SymbolFileBreakpad::CalculateAbilities() { 185 if (!m_objfile_sp || !llvm::isa<ObjectFileBreakpad>(*m_objfile_sp)) 186 return 0; 187 188 return CompileUnits | Functions | LineTables; 189 } 190 191 uint32_t SymbolFileBreakpad::CalculateNumCompileUnits() { 192 ParseCUData(); 193 return m_cu_data->GetSize(); 194 } 195 196 CompUnitSP SymbolFileBreakpad::ParseCompileUnitAtIndex(uint32_t index) { 197 if (index >= m_cu_data->GetSize()) 198 return nullptr; 199 200 CompUnitData &data = m_cu_data->GetEntryRef(index).data; 201 202 ParseFileRecords(); 203 204 FileSpec spec; 205 206 // The FileSpec of the compile unit will be the file corresponding to the 207 // first LINE record. 208 LineIterator It(*m_objfile_sp, Record::Func, data.bookmark), 209 End(*m_objfile_sp); 210 assert(Record::classify(*It) == Record::Func); 211 ++It; // Skip FUNC record. 212 if (It != End) { 213 auto record = LineRecord::parse(*It); 214 if (record && record->FileNum < m_files->size()) 215 spec = (*m_files)[record->FileNum]; 216 } 217 218 auto cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), 219 /*user_data*/ nullptr, spec, index, 220 eLanguageTypeUnknown, 221 /*is_optimized*/ eLazyBoolNo); 222 223 SetCompileUnitAtIndex(index, cu_sp); 224 return cu_sp; 225 } 226 227 size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) { 228 // TODO 229 return 0; 230 } 231 232 bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) { 233 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 234 CompUnitData &data = m_cu_data->GetEntryRef(comp_unit.GetID()).data; 235 236 if (!data.line_table_up) 237 ParseLineTableAndSupportFiles(comp_unit, data); 238 239 comp_unit.SetLineTable(data.line_table_up.release()); 240 return true; 241 } 242 243 bool SymbolFileBreakpad::ParseSupportFiles(CompileUnit &comp_unit, 244 FileSpecList &support_files) { 245 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 246 CompUnitData &data = m_cu_data->GetEntryRef(comp_unit.GetID()).data; 247 if (!data.support_files) 248 ParseLineTableAndSupportFiles(comp_unit, data); 249 250 support_files = std::move(*data.support_files); 251 return true; 252 } 253 254 uint32_t 255 SymbolFileBreakpad::ResolveSymbolContext(const Address &so_addr, 256 SymbolContextItem resolve_scope, 257 SymbolContext &sc) { 258 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 259 if (!(resolve_scope & (eSymbolContextCompUnit | eSymbolContextLineEntry))) 260 return 0; 261 262 ParseCUData(); 263 uint32_t idx = 264 m_cu_data->FindEntryIndexThatContains(so_addr.GetFileAddress()); 265 if (idx == UINT32_MAX) 266 return 0; 267 268 sc.comp_unit = GetCompileUnitAtIndex(idx).get(); 269 SymbolContextItem result = eSymbolContextCompUnit; 270 if (resolve_scope & eSymbolContextLineEntry) { 271 if (sc.comp_unit->GetLineTable()->FindLineEntryByAddress(so_addr, 272 sc.line_entry)) { 273 result |= eSymbolContextLineEntry; 274 } 275 } 276 277 return result; 278 } 279 280 uint32_t SymbolFileBreakpad::ResolveSymbolContext( 281 const FileSpec &file_spec, uint32_t line, bool check_inlines, 282 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { 283 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 284 if (!(resolve_scope & eSymbolContextCompUnit)) 285 return 0; 286 287 uint32_t old_size = sc_list.GetSize(); 288 for (size_t i = 0, size = GetNumCompileUnits(); i < size; ++i) { 289 CompileUnit &cu = *GetCompileUnitAtIndex(i); 290 cu.ResolveSymbolContext(file_spec, line, check_inlines, 291 /*exact*/ false, resolve_scope, sc_list); 292 } 293 return sc_list.GetSize() - old_size; 294 } 295 296 void SymbolFileBreakpad::FindFunctions( 297 ConstString name, const CompilerDeclContext *parent_decl_ctx, 298 FunctionNameType name_type_mask, bool include_inlines, 299 SymbolContextList &sc_list) { 300 // TODO 301 } 302 303 void SymbolFileBreakpad::FindFunctions(const RegularExpression ®ex, 304 bool include_inlines, 305 SymbolContextList &sc_list) { 306 // TODO 307 } 308 309 void SymbolFileBreakpad::FindTypes( 310 ConstString name, const CompilerDeclContext *parent_decl_ctx, 311 uint32_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files, 312 TypeMap &types) {} 313 314 void SymbolFileBreakpad::FindTypes( 315 llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 316 llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {} 317 318 void SymbolFileBreakpad::AddSymbols(Symtab &symtab) { 319 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 320 Module &module = *m_objfile_sp->GetModule(); 321 addr_t base = GetBaseFileAddress(); 322 if (base == LLDB_INVALID_ADDRESS) { 323 LLDB_LOG(log, "Unable to fetch the base address of object file. Skipping " 324 "symtab population."); 325 return; 326 } 327 328 const SectionList &list = *module.GetSectionList(); 329 llvm::DenseMap<addr_t, Symbol> symbols; 330 auto add_symbol = [&](addr_t address, llvm::Optional<addr_t> size, 331 llvm::StringRef name) { 332 address += base; 333 SectionSP section_sp = list.FindSectionContainingFileAddress(address); 334 if (!section_sp) { 335 LLDB_LOG(log, 336 "Ignoring symbol {0}, whose address ({1}) is outside of the " 337 "object file. Mismatched symbol file?", 338 name, address); 339 return; 340 } 341 symbols.try_emplace( 342 address, /*symID*/ 0, Mangled(name), eSymbolTypeCode, 343 /*is_global*/ true, /*is_debug*/ false, 344 /*is_trampoline*/ false, /*is_artificial*/ false, 345 AddressRange(section_sp, address - section_sp->GetFileAddress(), 346 size.getValueOr(0)), 347 size.hasValue(), /*contains_linker_annotations*/ false, /*flags*/ 0); 348 }; 349 350 for (llvm::StringRef line : lines(Record::Func)) { 351 if (auto record = FuncRecord::parse(line)) 352 add_symbol(record->Address, record->Size, record->Name); 353 } 354 355 for (llvm::StringRef line : lines(Record::Public)) { 356 if (auto record = PublicRecord::parse(line)) 357 add_symbol(record->Address, llvm::None, record->Name); 358 else 359 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", line); 360 } 361 362 for (auto &KV : symbols) 363 symtab.AddSymbol(std::move(KV.second)); 364 symtab.CalculateSymbolSizes(); 365 } 366 367 llvm::Expected<lldb::addr_t> 368 SymbolFileBreakpad::GetParameterStackSize(Symbol &symbol) { 369 ParseUnwindData(); 370 if (auto *entry = m_unwind_data->win.FindEntryThatContains( 371 symbol.GetAddress().GetFileAddress())) { 372 auto record = StackWinRecord::parse( 373 *LineIterator(*m_objfile_sp, Record::StackWin, entry->data)); 374 assert(record.hasValue()); 375 return record->ParameterSize; 376 } 377 return llvm::createStringError(llvm::inconvertibleErrorCode(), 378 "Parameter size unknown."); 379 } 380 381 static llvm::Optional<std::pair<llvm::StringRef, llvm::StringRef>> 382 GetRule(llvm::StringRef &unwind_rules) { 383 // Unwind rules are of the form 384 // register1: expression1 register2: expression2 ... 385 // We assume none of the tokens in expression<n> end with a colon. 386 387 llvm::StringRef lhs, rest; 388 std::tie(lhs, rest) = getToken(unwind_rules); 389 if (!lhs.consume_back(":")) 390 return llvm::None; 391 392 // Seek forward to the next register: expression pair 393 llvm::StringRef::size_type pos = rest.find(": "); 394 if (pos == llvm::StringRef::npos) { 395 // No pair found, this means the rest of the string is a single expression. 396 unwind_rules = llvm::StringRef(); 397 return std::make_pair(lhs, rest); 398 } 399 400 // Go back one token to find the end of the current rule. 401 pos = rest.rfind(' ', pos); 402 if (pos == llvm::StringRef::npos) 403 return llvm::None; 404 405 llvm::StringRef rhs = rest.take_front(pos); 406 unwind_rules = rest.drop_front(pos); 407 return std::make_pair(lhs, rhs); 408 } 409 410 static const RegisterInfo * 411 ResolveRegister(const SymbolFile::RegisterInfoResolver &resolver, 412 llvm::StringRef name) { 413 if (name.consume_front("$")) 414 return resolver.ResolveName(name); 415 416 return nullptr; 417 } 418 419 static const RegisterInfo * 420 ResolveRegisterOrRA(const SymbolFile::RegisterInfoResolver &resolver, 421 llvm::StringRef name) { 422 if (name == ".ra") 423 return resolver.ResolveNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 424 return ResolveRegister(resolver, name); 425 } 426 427 llvm::ArrayRef<uint8_t> SymbolFileBreakpad::SaveAsDWARF(postfix::Node &node) { 428 ArchSpec arch = m_objfile_sp->GetArchitecture(); 429 StreamString dwarf(Stream::eBinary, arch.GetAddressByteSize(), 430 arch.GetByteOrder()); 431 ToDWARF(node, dwarf); 432 uint8_t *saved = m_allocator.Allocate<uint8_t>(dwarf.GetSize()); 433 std::memcpy(saved, dwarf.GetData(), dwarf.GetSize()); 434 return {saved, dwarf.GetSize()}; 435 } 436 437 bool SymbolFileBreakpad::ParseCFIUnwindRow(llvm::StringRef unwind_rules, 438 const RegisterInfoResolver &resolver, 439 UnwindPlan::Row &row) { 440 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 441 442 llvm::BumpPtrAllocator node_alloc; 443 while (auto rule = GetRule(unwind_rules)) { 444 node_alloc.Reset(); 445 llvm::StringRef lhs = rule->first; 446 postfix::Node *rhs = postfix::ParseOneExpression(rule->second, node_alloc); 447 if (!rhs) { 448 LLDB_LOG(log, "Could not parse `{0}` as unwind rhs.", rule->second); 449 return false; 450 } 451 452 bool success = postfix::ResolveSymbols( 453 rhs, [&](postfix::SymbolNode &symbol) -> postfix::Node * { 454 llvm::StringRef name = symbol.GetName(); 455 if (name == ".cfa" && lhs != ".cfa") 456 return postfix::MakeNode<postfix::InitialValueNode>(node_alloc); 457 458 if (const RegisterInfo *info = ResolveRegister(resolver, name)) { 459 return postfix::MakeNode<postfix::RegisterNode>( 460 node_alloc, info->kinds[eRegisterKindLLDB]); 461 } 462 return nullptr; 463 }); 464 465 if (!success) { 466 LLDB_LOG(log, "Resolving symbols in `{0}` failed.", rule->second); 467 return false; 468 } 469 470 llvm::ArrayRef<uint8_t> saved = SaveAsDWARF(*rhs); 471 if (lhs == ".cfa") { 472 row.GetCFAValue().SetIsDWARFExpression(saved.data(), saved.size()); 473 } else if (const RegisterInfo *info = ResolveRegisterOrRA(resolver, lhs)) { 474 UnwindPlan::Row::RegisterLocation loc; 475 loc.SetIsDWARFExpression(saved.data(), saved.size()); 476 row.SetRegisterInfo(info->kinds[eRegisterKindLLDB], loc); 477 } else 478 LLDB_LOG(log, "Invalid register `{0}` in unwind rule.", lhs); 479 } 480 if (unwind_rules.empty()) 481 return true; 482 483 LLDB_LOG(log, "Could not parse `{0}` as an unwind rule.", unwind_rules); 484 return false; 485 } 486 487 UnwindPlanSP 488 SymbolFileBreakpad::GetUnwindPlan(const Address &address, 489 const RegisterInfoResolver &resolver) { 490 ParseUnwindData(); 491 if (auto *entry = 492 m_unwind_data->cfi.FindEntryThatContains(address.GetFileAddress())) 493 return ParseCFIUnwindPlan(entry->data, resolver); 494 if (auto *entry = 495 m_unwind_data->win.FindEntryThatContains(address.GetFileAddress())) 496 return ParseWinUnwindPlan(entry->data, resolver); 497 return nullptr; 498 } 499 500 UnwindPlanSP 501 SymbolFileBreakpad::ParseCFIUnwindPlan(const Bookmark &bookmark, 502 const RegisterInfoResolver &resolver) { 503 addr_t base = GetBaseFileAddress(); 504 if (base == LLDB_INVALID_ADDRESS) 505 return nullptr; 506 507 LineIterator It(*m_objfile_sp, Record::StackCFI, bookmark), 508 End(*m_objfile_sp); 509 llvm::Optional<StackCFIRecord> init_record = StackCFIRecord::parse(*It); 510 assert(init_record.hasValue() && init_record->Size.hasValue() && 511 "Record already parsed successfully in ParseUnwindData!"); 512 513 auto plan_sp = std::make_shared<UnwindPlan>(lldb::eRegisterKindLLDB); 514 plan_sp->SetSourceName("breakpad STACK CFI"); 515 plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 516 plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolNo); 517 plan_sp->SetSourcedFromCompiler(eLazyBoolYes); 518 plan_sp->SetPlanValidAddressRange( 519 AddressRange(base + init_record->Address, *init_record->Size, 520 m_objfile_sp->GetModule()->GetSectionList())); 521 522 auto row_sp = std::make_shared<UnwindPlan::Row>(); 523 row_sp->SetOffset(0); 524 if (!ParseCFIUnwindRow(init_record->UnwindRules, resolver, *row_sp)) 525 return nullptr; 526 plan_sp->AppendRow(row_sp); 527 for (++It; It != End; ++It) { 528 llvm::Optional<StackCFIRecord> record = StackCFIRecord::parse(*It); 529 if (!record.hasValue()) 530 return nullptr; 531 if (record->Size.hasValue()) 532 break; 533 534 row_sp = std::make_shared<UnwindPlan::Row>(*row_sp); 535 row_sp->SetOffset(record->Address - init_record->Address); 536 if (!ParseCFIUnwindRow(record->UnwindRules, resolver, *row_sp)) 537 return nullptr; 538 plan_sp->AppendRow(row_sp); 539 } 540 return plan_sp; 541 } 542 543 UnwindPlanSP 544 SymbolFileBreakpad::ParseWinUnwindPlan(const Bookmark &bookmark, 545 const RegisterInfoResolver &resolver) { 546 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 547 addr_t base = GetBaseFileAddress(); 548 if (base == LLDB_INVALID_ADDRESS) 549 return nullptr; 550 551 LineIterator It(*m_objfile_sp, Record::StackWin, bookmark); 552 llvm::Optional<StackWinRecord> record = StackWinRecord::parse(*It); 553 assert(record.hasValue() && 554 "Record already parsed successfully in ParseUnwindData!"); 555 556 auto plan_sp = std::make_shared<UnwindPlan>(lldb::eRegisterKindLLDB); 557 plan_sp->SetSourceName("breakpad STACK WIN"); 558 plan_sp->SetUnwindPlanValidAtAllInstructions(eLazyBoolNo); 559 plan_sp->SetUnwindPlanForSignalTrap(eLazyBoolNo); 560 plan_sp->SetSourcedFromCompiler(eLazyBoolYes); 561 plan_sp->SetPlanValidAddressRange( 562 AddressRange(base + record->RVA, record->CodeSize, 563 m_objfile_sp->GetModule()->GetSectionList())); 564 565 auto row_sp = std::make_shared<UnwindPlan::Row>(); 566 row_sp->SetOffset(0); 567 568 llvm::BumpPtrAllocator node_alloc; 569 std::vector<std::pair<llvm::StringRef, postfix::Node *>> program = 570 postfix::ParseFPOProgram(record->ProgramString, node_alloc); 571 572 if (program.empty()) { 573 LLDB_LOG(log, "Invalid unwind rule: {0}.", record->ProgramString); 574 return nullptr; 575 } 576 auto it = program.begin(); 577 const auto &symbol_resolver = 578 [&](postfix::SymbolNode &symbol) -> postfix::Node * { 579 llvm::StringRef name = symbol.GetName(); 580 for (const auto &rule : llvm::make_range(program.begin(), it)) { 581 if (rule.first == name) 582 return rule.second; 583 } 584 if (const RegisterInfo *info = ResolveRegister(resolver, name)) 585 return postfix::MakeNode<postfix::RegisterNode>( 586 node_alloc, info->kinds[eRegisterKindLLDB]); 587 return nullptr; 588 }; 589 590 // We assume the first value will be the CFA. It is usually called T0, but 591 // clang will use T1, if it needs to realign the stack. 592 auto *symbol = llvm::dyn_cast<postfix::SymbolNode>(it->second); 593 if (symbol && symbol->GetName() == ".raSearch") { 594 row_sp->GetCFAValue().SetRaSearch(record->LocalSize + 595 record->SavedRegisterSize); 596 } else { 597 if (!postfix::ResolveSymbols(it->second, symbol_resolver)) { 598 LLDB_LOG(log, "Resolving symbols in `{0}` failed.", 599 record->ProgramString); 600 return nullptr; 601 } 602 llvm::ArrayRef<uint8_t> saved = SaveAsDWARF(*it->second); 603 row_sp->GetCFAValue().SetIsDWARFExpression(saved.data(), saved.size()); 604 } 605 606 // Replace the node value with InitialValueNode, so that subsequent 607 // expressions refer to the CFA value instead of recomputing the whole 608 // expression. 609 it->second = postfix::MakeNode<postfix::InitialValueNode>(node_alloc); 610 611 612 // Now process the rest of the assignments. 613 for (++it; it != program.end(); ++it) { 614 const RegisterInfo *info = ResolveRegister(resolver, it->first); 615 // It is not an error if the resolution fails because the program may 616 // contain temporary variables. 617 if (!info) 618 continue; 619 if (!postfix::ResolveSymbols(it->second, symbol_resolver)) { 620 LLDB_LOG(log, "Resolving symbols in `{0}` failed.", 621 record->ProgramString); 622 return nullptr; 623 } 624 625 llvm::ArrayRef<uint8_t> saved = SaveAsDWARF(*it->second); 626 UnwindPlan::Row::RegisterLocation loc; 627 loc.SetIsDWARFExpression(saved.data(), saved.size()); 628 row_sp->SetRegisterInfo(info->kinds[eRegisterKindLLDB], loc); 629 } 630 631 plan_sp->AppendRow(row_sp); 632 return plan_sp; 633 } 634 635 addr_t SymbolFileBreakpad::GetBaseFileAddress() { 636 return m_objfile_sp->GetModule() 637 ->GetObjectFile() 638 ->GetBaseAddress() 639 .GetFileAddress(); 640 } 641 642 // Parse out all the FILE records from the breakpad file. These will be needed 643 // when constructing the support file lists for individual compile units. 644 void SymbolFileBreakpad::ParseFileRecords() { 645 if (m_files) 646 return; 647 m_files.emplace(); 648 649 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 650 for (llvm::StringRef line : lines(Record::File)) { 651 auto record = FileRecord::parse(line); 652 if (!record) { 653 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", line); 654 continue; 655 } 656 657 if (record->Number >= m_files->size()) 658 m_files->resize(record->Number + 1); 659 FileSpec::Style style = FileSpec::GuessPathStyle(record->Name) 660 .getValueOr(FileSpec::Style::native); 661 (*m_files)[record->Number] = FileSpec(record->Name, style); 662 } 663 } 664 665 void SymbolFileBreakpad::ParseCUData() { 666 if (m_cu_data) 667 return; 668 669 m_cu_data.emplace(); 670 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 671 addr_t base = GetBaseFileAddress(); 672 if (base == LLDB_INVALID_ADDRESS) { 673 LLDB_LOG(log, "SymbolFile parsing failed: Unable to fetch the base address " 674 "of object file."); 675 } 676 677 // We shall create one compile unit for each FUNC record. So, count the number 678 // of FUNC records, and store them in m_cu_data, together with their ranges. 679 for (LineIterator It(*m_objfile_sp, Record::Func), End(*m_objfile_sp); 680 It != End; ++It) { 681 if (auto record = FuncRecord::parse(*It)) { 682 m_cu_data->Append(CompUnitMap::Entry(base + record->Address, record->Size, 683 CompUnitData(It.GetBookmark()))); 684 } else 685 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", *It); 686 } 687 m_cu_data->Sort(); 688 } 689 690 // Construct the list of support files and line table entries for the given 691 // compile unit. 692 void SymbolFileBreakpad::ParseLineTableAndSupportFiles(CompileUnit &cu, 693 CompUnitData &data) { 694 addr_t base = GetBaseFileAddress(); 695 assert(base != LLDB_INVALID_ADDRESS && 696 "How did we create compile units without a base address?"); 697 698 SupportFileMap map; 699 std::vector<std::unique_ptr<LineSequence>> sequences; 700 std::unique_ptr<LineSequence> line_seq_up = 701 LineTable::CreateLineSequenceContainer(); 702 llvm::Optional<addr_t> next_addr; 703 auto finish_sequence = [&]() { 704 LineTable::AppendLineEntryToSequence( 705 line_seq_up.get(), *next_addr, /*line*/ 0, /*column*/ 0, 706 /*file_idx*/ 0, /*is_start_of_statement*/ false, 707 /*is_start_of_basic_block*/ false, /*is_prologue_end*/ false, 708 /*is_epilogue_begin*/ false, /*is_terminal_entry*/ true); 709 sequences.push_back(std::move(line_seq_up)); 710 line_seq_up = LineTable::CreateLineSequenceContainer(); 711 }; 712 713 LineIterator It(*m_objfile_sp, Record::Func, data.bookmark), 714 End(*m_objfile_sp); 715 assert(Record::classify(*It) == Record::Func); 716 for (++It; It != End; ++It) { 717 auto record = LineRecord::parse(*It); 718 if (!record) 719 break; 720 721 record->Address += base; 722 723 if (next_addr && *next_addr != record->Address) { 724 // Discontiguous entries. Finish off the previous sequence and reset. 725 finish_sequence(); 726 } 727 LineTable::AppendLineEntryToSequence( 728 line_seq_up.get(), record->Address, record->LineNum, /*column*/ 0, 729 map[record->FileNum], /*is_start_of_statement*/ true, 730 /*is_start_of_basic_block*/ false, /*is_prologue_end*/ false, 731 /*is_epilogue_begin*/ false, /*is_terminal_entry*/ false); 732 next_addr = record->Address + record->Size; 733 } 734 if (next_addr) 735 finish_sequence(); 736 data.line_table_up = std::make_unique<LineTable>(&cu, std::move(sequences)); 737 data.support_files = map.translate(cu.GetPrimaryFile(), *m_files); 738 } 739 740 void SymbolFileBreakpad::ParseUnwindData() { 741 if (m_unwind_data) 742 return; 743 m_unwind_data.emplace(); 744 745 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS); 746 addr_t base = GetBaseFileAddress(); 747 if (base == LLDB_INVALID_ADDRESS) { 748 LLDB_LOG(log, "SymbolFile parsing failed: Unable to fetch the base address " 749 "of object file."); 750 } 751 752 for (LineIterator It(*m_objfile_sp, Record::StackCFI), End(*m_objfile_sp); 753 It != End; ++It) { 754 if (auto record = StackCFIRecord::parse(*It)) { 755 if (record->Size) 756 m_unwind_data->cfi.Append(UnwindMap::Entry( 757 base + record->Address, *record->Size, It.GetBookmark())); 758 } else 759 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", *It); 760 } 761 m_unwind_data->cfi.Sort(); 762 763 for (LineIterator It(*m_objfile_sp, Record::StackWin), End(*m_objfile_sp); 764 It != End; ++It) { 765 if (auto record = StackWinRecord::parse(*It)) { 766 m_unwind_data->win.Append(UnwindMap::Entry( 767 base + record->RVA, record->CodeSize, It.GetBookmark())); 768 } else 769 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", *It); 770 } 771 m_unwind_data->win.Sort(); 772 } 773