void DWARFDebugAranges::Sort (bool minimize) { Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); size_t orig_arange_size = 0; if (log) { orig_arange_size = m_aranges.GetSize(); log->Printf ("DWARFDebugAranges::Sort(minimize = %u) with %zu entries", minimize, orig_arange_size); } m_aranges.Sort(); m_aranges.CombineConsecutiveEntriesWithEqualData(); if (log) { if (minimize) { const size_t new_arange_size = m_aranges.GetSize(); const size_t delta = orig_arange_size - new_arange_size; log->Printf ("DWARFDebugAranges::Sort() %zu entries after minimizing (%zu entries combined for %zu bytes saved)", new_arange_size, delta, delta * sizeof(Range)); } Dump (log.get()); } }
bool DWARFDebugPubnames::Extract(const DataExtractor& data) { Timer scoped_timer (__PRETTY_FUNCTION__, "DWARFDebugPubnames::Extract (byte_size = %zu)", data.GetByteSize()); LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES)); if (log) log->Printf("DWARFDebugPubnames::Extract (byte_size = %zu)", data.GetByteSize()); if (data.ValidOffset(0)) { uint32_t offset = 0; DWARFDebugPubnamesSet set; while (data.ValidOffset(offset)) { if (set.Extract(data, &offset)) { m_sets.push_back(set); offset = set.GetOffsetOfNextEntry(); } else break; } if (log) Dump (log.get()); return true; } return false; }
kern_return_t RegisterContextMach_x86_64::ReadGPR (bool force) { int set = GPRRegSet; if (force || !RegisterSetIsCached(set)) { mach_msg_type_number_t count = GPRWordCount; SetError(GPRRegSet, Read, ::thread_get_state(GetThreadID(), set, (thread_state_t)&gpr, &count)); LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_THREAD)); if (log) LogGPR (log.get(), "RegisterContextMach_x86_64::ReadGPR(thread = 0x%4.4x)", GetThreadID()); } return GetError(GPRRegSet, Read); }
kern_return_t RegisterContextMach_x86_64::WriteGPR () { int set = GPRRegSet; if (!RegisterSetIsCached(set)) { SetError (set, Write, -1); return KERN_INVALID_ARGUMENT; } LogSP log (ProcessMacOSXLog::GetLogIfAllCategoriesSet (PD_LOG_THREAD)); if (log) LogGPR (log.get(), "RegisterContextMach_x86_64::WriteGPR (thread = 0x%4.4x)", GetThreadID()); SetError (set, Write, ::thread_set_state(GetThreadID(), set, (thread_state_t)&gpr, GPRWordCount)); SetError (set, Read, -1); return GetError (set, Write); }
const DWARFDebugAranges & DWARFCompileUnit::GetFunctionAranges () { if (m_func_aranges_ap.get() == NULL) { m_func_aranges_ap.reset (new DWARFDebugAranges()); LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); if (log) { m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log.get(), "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]", GetOffset()); } const DWARFDebugInfoEntry* die = DIE(); if (die) die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get()); const bool minimize = false; m_func_aranges_ap->Sort(minimize); } return *m_func_aranges_ap.get(); }
bool DWARFDebugPubnames::GeneratePubnames(SymbolFileDWARF* dwarf2Data) { Timer scoped_timer (__PRETTY_FUNCTION__, "DWARFDebugPubnames::GeneratePubnames (data = %p)", dwarf2Data); LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES)); if (log) log->Printf("DWARFDebugPubnames::GeneratePubnames (data = %p)", dwarf2Data); m_sets.clear(); DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo(); if (debug_info) { const DataExtractor* debug_str = &dwarf2Data->get_debug_str_data(); uint32_t cu_idx = 0; const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits(); for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) { DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx); const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (cu->GetAddressByteSize()); bool clear_dies = cu->ExtractDIEsIfNeeded (false) > 1; DWARFDIECollection dies; const size_t die_count = cu->AppendDIEsWithTag (DW_TAG_subprogram, dies) + cu->AppendDIEsWithTag (DW_TAG_variable, dies); dw_offset_t cu_offset = cu->GetOffset(); DWARFDebugPubnamesSet pubnames_set(DW_INVALID_OFFSET, cu_offset, cu->GetNextCompileUnitOffset() - cu_offset); size_t die_idx; for (die_idx = 0; die_idx < die_count; ++die_idx) { const DWARFDebugInfoEntry *die = dies.GetDIEPtrAtIndex(die_idx); DWARFDebugInfoEntry::Attributes attributes; const char *name = NULL; const char *mangled = NULL; bool add_die = false; bool is_variable = false; const size_t num_attributes = die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes); if (num_attributes > 0) { uint32_t i; dw_tag_t tag = die->Tag(); is_variable = tag == DW_TAG_variable; for (i=0; i<num_attributes; ++i) { dw_attr_t attr = attributes.AttributeAtIndex(i); DWARFFormValue form_value; switch (attr) { case DW_AT_name: if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) name = form_value.AsCString(debug_str); break; case DW_AT_MIPS_linkage_name: if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) mangled = form_value.AsCString(debug_str); break; case DW_AT_low_pc: case DW_AT_ranges: case DW_AT_entry_pc: if (tag == DW_TAG_subprogram) add_die = true; break; case DW_AT_location: if (tag == DW_TAG_variable) { const DWARFDebugInfoEntry* parent_die = die->GetParent(); while ( parent_die != NULL ) { switch (parent_die->Tag()) { case DW_TAG_subprogram: case DW_TAG_lexical_block: case DW_TAG_inlined_subroutine: // Even if this is a function level static, we don't add it. We could theoretically // add these if we wanted to by introspecting into the DW_AT_location and seeing // if the location describes a hard coded address, but we don't want the performance // penalty of that right now. add_die = false; // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) // { // // If we have valid block data, then we have location expression bytes // // that are fixed (not a location list). // const uint8_t *block_data = form_value.BlockData(); // if (block_data) // { // uint32_t block_length = form_value.Unsigned(); // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) // { // if (block_data[0] == DW_OP_addr) // add_die = true; // } // } // } parent_die = NULL; // Terminate the while loop. break; case DW_TAG_compile_unit: add_die = true; parent_die = NULL; // Terminate the while loop. break; default: parent_die = parent_die->GetParent(); // Keep going in the while loop. break; } } } break; } } } if (add_die && (name || mangled)) { pubnames_set.AddDescriptor(die->GetOffset() - cu_offset, mangled ? mangled : name); } } if (pubnames_set.NumDescriptors() > 0) { m_sets.push_back(pubnames_set); } // Keep memory down by clearing DIEs if this generate function // caused them to be parsed if (clear_dies) cu->ClearDIEs (true); } } if (m_sets.empty()) return false; if (log) Dump (log.get()); return true; }
void DWARFCompileUnit::Index (const uint32_t cu_idx, NameToDIE& func_basenames, NameToDIE& func_fullnames, NameToDIE& func_methods, NameToDIE& func_selectors, NameToDIE& objc_class_selectors, NameToDIE& globals, NameToDIE& types, NameToDIE& namespaces) { const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data(); const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize()); LogSP log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS)); if (log) { m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log.get(), "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]", GetOffset()); } DWARFDebugInfoEntry::const_iterator pos; DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin(); DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); for (pos = begin; pos != end; ++pos) { const DWARFDebugInfoEntry &die = *pos; const dw_tag_t tag = die.Tag(); switch (tag) { case DW_TAG_subprogram: case DW_TAG_inlined_subroutine: case DW_TAG_base_type: case DW_TAG_class_type: case DW_TAG_constant: case DW_TAG_enumeration_type: case DW_TAG_string_type: case DW_TAG_subroutine_type: case DW_TAG_structure_type: case DW_TAG_union_type: case DW_TAG_typedef: case DW_TAG_namespace: case DW_TAG_variable: case DW_TAG_unspecified_type: break; default: continue; } DWARFDebugInfoEntry::Attributes attributes; const char *name = NULL; const char *mangled_cstr = NULL; bool is_declaration = false; //bool is_artificial = false; bool has_address = false; bool has_location = false; bool is_global_or_static_variable = false; dw_offset_t specification_die_offset = DW_INVALID_OFFSET; const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes); if (num_attributes > 0) { for (uint32_t i=0; i<num_attributes; ++i) { dw_attr_t attr = attributes.AttributeAtIndex(i); DWARFFormValue form_value; switch (attr) { case DW_AT_name: if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) name = form_value.AsCString(debug_str); break; case DW_AT_declaration: if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) is_declaration = form_value.Unsigned() != 0; break; // case DW_AT_artificial: // if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) // is_artificial = form_value.Unsigned() != 0; // break; case DW_AT_MIPS_linkage_name: case DW_AT_linkage_name: if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) mangled_cstr = form_value.AsCString(debug_str); break; case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges: has_address = true; break; case DW_AT_entry_pc: has_address = true; break; case DW_AT_location: has_location = true; if (tag == DW_TAG_variable) { const DWARFDebugInfoEntry* parent_die = die.GetParent(); while ( parent_die != NULL ) { switch (parent_die->Tag()) { case DW_TAG_subprogram: case DW_TAG_lexical_block: case DW_TAG_inlined_subroutine: // Even if this is a function level static, we don't add it. We could theoretically // add these if we wanted to by introspecting into the DW_AT_location and seeing // if the location describes a hard coded address, but we dont want the performance // penalty of that right now. is_global_or_static_variable = false; // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) // { // // If we have valid block data, then we have location expression bytes // // that are fixed (not a location list). // const uint8_t *block_data = form_value.BlockData(); // if (block_data) // { // uint32_t block_length = form_value.Unsigned(); // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) // { // if (block_data[0] == DW_OP_addr) // add_die = true; // } // } // } parent_die = NULL; // Terminate the while loop. break; case DW_TAG_compile_unit: is_global_or_static_variable = true; parent_die = NULL; // Terminate the while loop. break; default: parent_die = parent_die->GetParent(); // Keep going in the while loop. break; } } } break; case DW_AT_specification: if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) specification_die_offset = form_value.Reference(this); break; } } } switch (tag) { case DW_TAG_subprogram: if (has_address) { if (name) { // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the // simple inlined check outside the call. if (ObjCLanguageRuntime::IsPossibleObjCMethodName(name)) { ConstString objc_class_name; ConstString objc_selector_name; ConstString objc_fullname_no_category_name; ConstString objc_class_name_no_category; if (ObjCLanguageRuntime::ParseMethodName (name, &objc_class_name, &objc_selector_name, &objc_fullname_no_category_name, &objc_class_name_no_category)) { func_fullnames.Insert (ConstString(name), die.GetOffset()); if (objc_class_name) objc_class_selectors.Insert(objc_class_name, die.GetOffset()); if (objc_class_name_no_category) objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset()); if (objc_selector_name) func_selectors.Insert (objc_selector_name, die.GetOffset()); if (objc_fullname_no_category_name) func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset()); } } // If we have a mangled name, then the DW_AT_name attribute // is usually the method name without the class or any parameters const DWARFDebugInfoEntry *parent = die.GetParent(); bool is_method = false; if (parent) { dw_tag_t parent_tag = parent->Tag(); if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) { is_method = true; } else { if (specification_die_offset != DW_INVALID_OFFSET) { const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL); if (specification_die) { parent = specification_die->GetParent(); if (parent) { parent_tag = parent->Tag(); if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) is_method = true; } } } } } if (is_method) func_methods.Insert (ConstString(name), die.GetOffset()); else func_basenames.Insert (ConstString(name), die.GetOffset()); } if (mangled_cstr) { // Make sure our mangled name isn't the same string table entry // as our name. If it starts with '_', then it is ok, else compare // the string to make sure it isn't the same and we don't end up // with duplicate entries if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0))) { Mangled mangled (ConstString(mangled_cstr), true); func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); if (mangled.GetDemangledName()) func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset()); } } } break; case DW_TAG_inlined_subroutine: if (has_address) { if (name) func_basenames.Insert (ConstString(name), die.GetOffset()); if (mangled_cstr) { // Make sure our mangled name isn't the same string table entry // as our name. If it starts with '_', then it is ok, else compare // the string to make sure it isn't the same and we don't end up // with duplicate entries if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) { Mangled mangled (ConstString(mangled_cstr), true); func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); if (mangled.GetDemangledName()) func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset()); } } } break; case DW_TAG_base_type: case DW_TAG_class_type: case DW_TAG_constant: case DW_TAG_enumeration_type: case DW_TAG_string_type: case DW_TAG_subroutine_type: case DW_TAG_structure_type: case DW_TAG_union_type: case DW_TAG_typedef: case DW_TAG_unspecified_type: if (name && is_declaration == false) { types.Insert (ConstString(name), die.GetOffset()); } break; case DW_TAG_namespace: if (name) namespaces.Insert (ConstString(name), die.GetOffset()); break; case DW_TAG_variable: if (name && has_location && is_global_or_static_variable) { globals.Insert (ConstString(name), die.GetOffset()); // Be sure to include variables by their mangled and demangled // names if they have any since a variable can have a basename // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled // mangled name "(anonymous namespace)::i"... // Make sure our mangled name isn't the same string table entry // as our name. If it starts with '_', then it is ok, else compare // the string to make sure it isn't the same and we don't end up // with duplicate entries if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) { Mangled mangled (ConstString(mangled_cstr), true); globals.Insert (mangled.GetMangledName(), die.GetOffset()); if (mangled.GetDemangledName()) globals.Insert (mangled.GetDemangledName(), die.GetOffset()); } } break; default: continue; } } }
//---------------------------------------------------------------------- // ParseCompileUnitDIEsIfNeeded // // Parses a compile unit and indexes its DIEs if it hasn't already been // done. //---------------------------------------------------------------------- size_t DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only) { const size_t initial_die_array_size = m_die_array.size(); if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1) return 0; // Already parsed Timer scoped_timer (__PRETTY_FUNCTION__, "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )", m_offset, cu_die_only); // Set the offset to that of the first DIE and calculate the start of the // next compilation unit header. uint32_t offset = GetFirstDIEOffset(); uint32_t next_cu_offset = GetNextCompileUnitOffset(); DWARFDebugInfoEntry die; // Keep a flat array of the DIE for binary lookup by DIE offset if (!cu_die_only) { LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS)); if (log) { m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(), "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]", GetOffset()); } } uint32_t depth = 0; // We are in our compile unit, parse starting at the offset // we were told to parse const DataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data(); std::vector<uint32_t> die_index_stack; die_index_stack.reserve(32); die_index_stack.push_back(0); bool prev_die_had_children = false; const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize()); while (offset < next_cu_offset && die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset)) { // if (log) // log->Printf("0x%8.8x: %*.*s%s%s", // die.GetOffset(), // depth * 2, depth * 2, "", // DW_TAG_value_to_name (die.Tag()), // die.HasChildren() ? " *" : ""); const bool null_die = die.IsNULL(); if (depth == 0) { uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS); if (base_addr == LLDB_INVALID_ADDRESS) base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0); SetBaseAddress (base_addr); if (initial_die_array_size == 0) AddDIE (die); if (cu_die_only) return 1; } else { if (null_die) { if (prev_die_had_children) { // This will only happen if a DIE says is has children // but all it contains is a NULL tag. Since we are removing // the NULL DIEs from the list (saves up to 25% in C++ code), // we need a way to let the DIE know that it actually doesn't // have children. if (!m_die_array.empty()) m_die_array.back().SetEmptyChildren(true); } } else { die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]); if (die_index_stack.back()) m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back()); // Only push the DIE if it isn't a NULL DIE m_die_array.push_back(die); } } if (null_die) { // NULL DIE. if (!die_index_stack.empty()) die_index_stack.pop_back(); if (depth > 0) --depth; if (depth == 0) break; // We are done with this compile unit! prev_die_had_children = false; } else { die_index_stack.back() = m_die_array.size() - 1; // Normal DIE const bool die_has_children = die.HasChildren(); if (die_has_children) { die_index_stack.push_back(0); ++depth; } prev_die_had_children = die_has_children; } } // Give a little bit of info if we encounter corrupt DWARF (our offset // should always terminate at or before the start of the next compilation // unit header). if (offset > next_cu_offset) { m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8x\n", GetOffset(), offset); } // Since std::vector objects will double their size, we really need to // make a new array with the perfect size so we don't end up wasting // space. So here we copy and swap to make sure we don't have any extra // memory taken up. if (m_die_array.size () < m_die_array.capacity()) { DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end()); exact_size_die_array.swap (m_die_array); } LogSP log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE)); if (log) { StreamString strm; DWARFDebugInfoEntry::DumpDIECollection (strm, m_die_array); log->PutCString (strm.GetString().c_str()); } return m_die_array.size(); }