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