Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame^] | 1 | //===-- DWARFCompileUnit.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "DWARFCompileUnit.h" |
| 11 | |
| 12 | #include "lldb/Core/Stream.h" |
| 13 | #include "lldb/Core/Timer.h" |
| 14 | |
| 15 | #include "DWARFDebugAbbrev.h" |
| 16 | #include "DWARFDebugAranges.h" |
| 17 | #include "DWARFDIECollection.h" |
| 18 | #include "DWARFFormValue.h" |
| 19 | #include "LogChannelDWARF.h" |
| 20 | #include "SymbolFileDWARF.h" |
| 21 | |
| 22 | using namespace lldb_private; |
| 23 | using namespace std; |
| 24 | |
| 25 | extern int g_verbose; |
| 26 | |
| 27 | DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* m_dwarf2Data) : |
| 28 | m_dwarf2Data ( m_dwarf2Data ), |
| 29 | m_offset ( DW_INVALID_OFFSET ), |
| 30 | m_length ( 0 ), |
| 31 | m_version ( 0 ), |
| 32 | m_abbrevs ( NULL ), |
| 33 | m_addr_size ( DWARFCompileUnit::GetDefaultAddressSize() ), |
| 34 | m_base_addr ( 0 ), |
| 35 | m_die_array (), |
| 36 | m_aranges_ap (), |
| 37 | m_user_data ( NULL ) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | DWARFCompileUnit::Clear() |
| 43 | { |
| 44 | m_offset = DW_INVALID_OFFSET; |
| 45 | m_length = 0; |
| 46 | m_version = 0; |
| 47 | m_abbrevs = NULL; |
| 48 | m_addr_size = DWARFCompileUnit::GetDefaultAddressSize(); |
| 49 | m_base_addr = 0; |
| 50 | m_die_array.clear(); |
| 51 | m_aranges_ap.reset(); |
| 52 | m_user_data = NULL; |
| 53 | } |
| 54 | |
| 55 | bool |
| 56 | DWARFCompileUnit::Extract(const DataExtractor &debug_info, uint32_t* offset_ptr) |
| 57 | { |
| 58 | Clear(); |
| 59 | |
| 60 | m_offset = *offset_ptr; |
| 61 | |
| 62 | if (debug_info.ValidOffset(*offset_ptr)) |
| 63 | { |
| 64 | dw_offset_t abbr_offset; |
| 65 | const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev(); |
| 66 | m_length = debug_info.GetU32(offset_ptr); |
| 67 | m_version = debug_info.GetU16(offset_ptr); |
| 68 | abbr_offset = debug_info.GetU32(offset_ptr); |
| 69 | m_addr_size = debug_info.GetU8 (offset_ptr); |
| 70 | |
| 71 | bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); |
| 72 | bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); |
| 73 | bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset); |
| 74 | bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); |
| 75 | |
| 76 | if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL) |
| 77 | { |
| 78 | m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | // reset the offset to where we tried to parse from if anything went wrong |
| 83 | *offset_ptr = m_offset; |
| 84 | } |
| 85 | |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | dw_offset_t |
| 91 | DWARFCompileUnit::Extract(dw_offset_t offset, const DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs) |
| 92 | { |
| 93 | Clear(); |
| 94 | |
| 95 | m_offset = offset; |
| 96 | |
| 97 | if (debug_info_data.ValidOffset(offset)) |
| 98 | { |
| 99 | m_length = debug_info_data.GetU32(&offset); |
| 100 | m_version = debug_info_data.GetU16(&offset); |
| 101 | bool abbrevs_OK = debug_info_data.GetU32(&offset) == abbrevs->GetOffset(); |
| 102 | m_abbrevs = abbrevs; |
| 103 | m_addr_size = debug_info_data.GetU8 (&offset); |
| 104 | |
| 105 | bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); |
| 106 | bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); |
| 107 | |
| 108 | if (version_OK && addr_size_OK && abbrevs_OK && debug_info_data.ValidOffset(offset)) |
| 109 | return offset; |
| 110 | } |
| 111 | return DW_INVALID_OFFSET; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die) |
| 116 | { |
| 117 | if (m_die_array.size() > 1) |
| 118 | { |
| 119 | // std::vectors never get any smaller when resized to a smaller size, |
| 120 | // or when clear() or erase() are called, the size will report that it |
| 121 | // is smaller, but the memory allocated remains intact (call capacity() |
| 122 | // to see this). So we need to create a temporary vector and swap the |
| 123 | // contents which will cause just the internal pointers to be swapped |
| 124 | // so that when "tmp_array" goes out of scope, it will destroy the |
| 125 | // contents. |
| 126 | |
| 127 | // Save at least the compile unit DIE |
| 128 | DWARFDebugInfoEntry::collection tmp_array; |
| 129 | m_die_array.swap(tmp_array); |
| 130 | if (keep_compile_unit_die) |
| 131 | m_die_array.push_back(tmp_array.front()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | //---------------------------------------------------------------------- |
| 136 | // ParseCompileUnitDIEsIfNeeded |
| 137 | // |
| 138 | // Parses a compile unit and indexes its DIEs if it already hasn't been |
| 139 | // done. |
| 140 | //---------------------------------------------------------------------- |
| 141 | size_t |
| 142 | DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only) |
| 143 | { |
| 144 | const size_t initial_die_array_size = m_die_array.size(); |
| 145 | if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1) |
| 146 | return 0; // Already parsed |
| 147 | |
| 148 | Timer scoped_timer (__PRETTY_FUNCTION__, |
| 149 | "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )", |
| 150 | m_offset, |
| 151 | cu_die_only); |
| 152 | |
| 153 | // Set the offset to that of the first DIE |
| 154 | uint32_t offset = GetFirstDIEOffset(); |
| 155 | const dw_offset_t next_cu_offset = GetNextCompileUnitOffset(); |
| 156 | DWARFDebugInfoEntry die; |
| 157 | // Keep a flat array of the DIE for binary lookup by DIE offset |
| 158 | Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); |
| 159 | // if (log) |
| 160 | // log->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x", |
| 161 | // cu->GetOffset(), |
| 162 | // cu->GetLength(), |
| 163 | // cu->GetVersion(), |
| 164 | // cu->GetAbbrevOffset(), |
| 165 | // cu->GetAddressByteSize()); |
| 166 | |
| 167 | uint32_t depth = 0; |
| 168 | // We are in our compile unit, parse starting at the offset |
| 169 | // we were told to parse |
| 170 | while (die.Extract(m_dwarf2Data, this, &offset)) |
| 171 | { |
| 172 | if (log) |
| 173 | log->Printf("0x%8.8x: %*.*s%s%s", |
| 174 | die.GetOffset(), |
| 175 | depth * 2, depth * 2, "", |
| 176 | DW_TAG_value_to_name (die.Tag()), |
| 177 | die.HasChildren() ? " *" : ""); |
| 178 | if (cu_die_only) |
| 179 | { |
| 180 | AddDIE(die); |
| 181 | return 1; |
| 182 | } |
| 183 | else if (depth == 0 && initial_die_array_size == 1) |
| 184 | { |
| 185 | // Don't append the CU die as we already did that |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | AddDIE(die); |
| 190 | } |
| 191 | |
| 192 | const DWARFAbbreviationDeclaration* abbrDecl = die.GetAbbreviationDeclarationPtr(); |
| 193 | if (abbrDecl) |
| 194 | { |
| 195 | // Normal DIE |
| 196 | if (abbrDecl->HasChildren()) |
| 197 | ++depth; |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | // NULL DIE. |
| 202 | if (depth > 0) |
| 203 | --depth; |
| 204 | else |
| 205 | break; // We are done with this compile unit! |
| 206 | } |
| 207 | |
| 208 | assert(offset <= next_cu_offset); |
| 209 | } |
| 210 | SetDIERelations(); |
| 211 | return m_die_array.size(); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | dw_offset_t |
| 216 | DWARFCompileUnit::GetAbbrevOffset() const |
| 217 | { |
| 218 | return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | bool |
| 224 | DWARFCompileUnit::Verify(Stream *s) const |
| 225 | { |
| 226 | const DataExtractor& debug_info = m_dwarf2Data->get_debug_info_data(); |
| 227 | bool valid_offset = debug_info.ValidOffset(m_offset); |
| 228 | bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); |
| 229 | bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); |
| 230 | bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset()); |
| 231 | bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); |
| 232 | bool verbose = s->GetVerbose(); |
| 233 | if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK) |
| 234 | { |
| 235 | if (verbose) |
| 236 | s->Printf(" 0x%8.8x: OK\n", m_offset); |
| 237 | return true; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | s->Printf(" 0x%8.8x: ", m_offset); |
| 242 | |
| 243 | m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0); |
| 244 | s->EOL(); |
| 245 | if (valid_offset) |
| 246 | { |
| 247 | if (!length_OK) |
| 248 | s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length); |
| 249 | if (!version_OK) |
| 250 | s->Printf(" The 16 bit compile unit header version is not supported.\n"); |
| 251 | if (!abbr_offset_OK) |
| 252 | s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset()); |
| 253 | if (!addr_size_OK) |
| 254 | s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size); |
| 255 | } |
| 256 | else |
| 257 | s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n"); |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void |
| 264 | DWARFCompileUnit::Dump(Stream *s) const |
| 265 | { |
| 266 | s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n", |
| 267 | m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset()); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static uint8_t g_default_addr_size = 4; |
| 272 | |
| 273 | uint8_t |
| 274 | DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu) |
| 275 | { |
| 276 | if (cu) |
| 277 | return cu->GetAddressByteSize(); |
| 278 | return DWARFCompileUnit::GetDefaultAddressSize(); |
| 279 | } |
| 280 | |
| 281 | uint8_t |
| 282 | DWARFCompileUnit::GetDefaultAddressSize() |
| 283 | { |
| 284 | return g_default_addr_size; |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size) |
| 289 | { |
| 290 | g_default_addr_size = addr_size; |
| 291 | } |
| 292 | |
| 293 | bool |
| 294 | DWARFCompileUnit::LookupAddress |
| 295 | ( |
| 296 | const dw_addr_t address, |
| 297 | DWARFDebugInfoEntry** function_die_handle, |
| 298 | DWARFDebugInfoEntry** block_die_handle |
| 299 | ) |
| 300 | { |
| 301 | bool success = false; |
| 302 | |
| 303 | if (function_die_handle != NULL && DIE()) |
| 304 | { |
| 305 | if (m_aranges_ap.get() == NULL) |
| 306 | { |
| 307 | m_aranges_ap.reset(new DWARFDebugAranges()); |
| 308 | m_die_array.front().BuildFunctionAddressRangeTable(m_dwarf2Data, this, m_aranges_ap.get()); |
| 309 | } |
| 310 | |
| 311 | // Re-check the aranges auto pointer contents in case it was created above |
| 312 | if (m_aranges_ap.get() != NULL) |
| 313 | { |
| 314 | *function_die_handle = GetDIEPtr(m_aranges_ap->FindAddress(address)); |
| 315 | if (*function_die_handle != NULL) |
| 316 | { |
| 317 | success = true; |
| 318 | if (block_die_handle != NULL) |
| 319 | { |
| 320 | DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild(); |
| 321 | while (child) |
| 322 | { |
| 323 | if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle)) |
| 324 | break; |
| 325 | child = child->GetSibling(); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | return success; |
| 332 | } |
| 333 | |
| 334 | //---------------------------------------------------------------------- |
| 335 | // SetDIERelations() |
| 336 | // |
| 337 | // We read in all of the DIE entries into our flat list of DIE entries |
| 338 | // and now we need to go back through all of them and set the parent, |
| 339 | // sibling and child pointers for quick DIE navigation. |
| 340 | //---------------------------------------------------------------------- |
| 341 | void |
| 342 | DWARFCompileUnit::SetDIERelations() |
| 343 | { |
| 344 | #if 0 |
| 345 | // Compute average bytes per DIE |
| 346 | // |
| 347 | // We can figure out what the average number of bytes per DIE is |
| 348 | // to help us pre-allocate the correct number of m_die_array |
| 349 | // entries so we don't end up doing a lot of memory copies as we |
| 350 | // are creating our DIE array when parsing |
| 351 | // |
| 352 | // Enable this code by changing "#if 0" above to "#if 1" and running |
| 353 | // the dsymutil or dwarfdump with a bunch of dwarf files and see what |
| 354 | // the running average ends up being in the stdout log. |
| 355 | static size_t g_total_cu_debug_info_size = 0; |
| 356 | static size_t g_total_num_dies = 0; |
| 357 | static size_t g_min_bytes_per_die = UINT_MAX; |
| 358 | static size_t g_max_bytes_per_die = 0; |
| 359 | const size_t num_dies = m_die_array.size(); |
| 360 | const size_t cu_debug_info_size = GetDebugInfoSize(); |
| 361 | const size_t bytes_per_die = cu_debug_info_size / num_dies; |
| 362 | if (g_min_bytes_per_die > bytes_per_die) |
| 363 | g_min_bytes_per_die = bytes_per_die; |
| 364 | if (g_max_bytes_per_die < bytes_per_die) |
| 365 | g_max_bytes_per_die = bytes_per_die; |
| 366 | if (g_total_cu_debug_info_size == 0) |
| 367 | { |
| 368 | cout << " min max avg" << endl |
| 369 | << "n dies cu size bpd bpd bpd bpd" << endl |
| 370 | << "------ -------- --- === === ===" << endl; |
| 371 | } |
| 372 | g_total_cu_debug_info_size += cu_debug_info_size; |
| 373 | g_total_num_dies += num_dies; |
| 374 | const size_t avg_bytes_per_die = g_total_cu_debug_info_size / g_total_num_dies; |
| 375 | cout |
| 376 | << DECIMAL_WIDTH(6) << num_dies << ' ' |
| 377 | << DECIMAL_WIDTH(8) << cu_debug_info_size << ' ' |
| 378 | << DECIMAL_WIDTH(3) << bytes_per_die << ' ' |
| 379 | << DECIMAL_WIDTH(3) << g_min_bytes_per_die << ' ' |
| 380 | << DECIMAL_WIDTH(3) << g_max_bytes_per_die << ' ' |
| 381 | << DECIMAL_WIDTH(3) << avg_bytes_per_die |
| 382 | << endl; |
| 383 | #endif |
| 384 | if (m_die_array.empty()) |
| 385 | return; |
| 386 | DWARFDebugInfoEntry* die_array_begin = &m_die_array.front(); |
| 387 | DWARFDebugInfoEntry* die_array_end = &m_die_array.back(); |
| 388 | DWARFDebugInfoEntry* curr_die; |
| 389 | // We purposely are skipping the last element in the array in the loop below |
| 390 | // so that we can always have a valid next item |
| 391 | for (curr_die = die_array_begin; curr_die < die_array_end; ++curr_die) |
| 392 | { |
| 393 | // Since our loop doesn't include the last element, we can always |
| 394 | // safely access the next die in the array. |
| 395 | DWARFDebugInfoEntry* next_die = curr_die + 1; |
| 396 | |
| 397 | const DWARFAbbreviationDeclaration* curr_die_abbrev = curr_die->GetAbbreviationDeclarationPtr(); |
| 398 | |
| 399 | if (curr_die_abbrev) |
| 400 | { |
| 401 | // Normal DIE |
| 402 | if (curr_die_abbrev->HasChildren()) |
| 403 | next_die->SetParent(curr_die); |
| 404 | else |
| 405 | curr_die->SetSibling(next_die); |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | // NULL DIE that terminates a sibling chain |
| 410 | DWARFDebugInfoEntry* parent = curr_die->GetParent(); |
| 411 | if (parent) |
| 412 | parent->SetSibling(next_die); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // Since we skipped the last element, we need to fix it up! |
| 417 | if (die_array_begin < die_array_end) |
| 418 | curr_die->SetParent(die_array_begin); |
| 419 | |
| 420 | #if 0 |
| 421 | // The code below will dump the DIE relations in case any modification |
| 422 | // is done to the above code. This dump can be used in a diff to make |
| 423 | // sure that no functionality is lost. |
| 424 | { |
| 425 | DWARFDebugInfoEntry::const_iterator pos; |
| 426 | DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); |
| 427 | puts("offset parent sibling child"); |
| 428 | puts("-------- -------- -------- --------"); |
| 429 | for (pos = m_die_array.begin(); pos != end; ++pos) |
| 430 | { |
| 431 | const DWARFDebugInfoEntry& die_ref = *pos; |
| 432 | const DWARFDebugInfoEntry* p = die_ref.GetParent(); |
| 433 | const DWARFDebugInfoEntry* s = die_ref.GetSibling(); |
| 434 | const DWARFDebugInfoEntry* c = die_ref.GetFirstChild(); |
| 435 | printf("%.8x: %.8x %.8x %.8x\n", die_ref.GetOffset(), |
| 436 | p ? p->GetOffset() : 0, |
| 437 | s ? s->GetOffset() : 0, |
| 438 | c ? c->GetOffset() : 0); |
| 439 | } |
| 440 | } |
| 441 | #endif |
| 442 | |
| 443 | } |
| 444 | //---------------------------------------------------------------------- |
| 445 | // Compare function DWARFDebugAranges::Range structures |
| 446 | //---------------------------------------------------------------------- |
| 447 | static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2) |
| 448 | { |
| 449 | return die1.GetOffset() < die2.GetOffset(); |
| 450 | } |
| 451 | |
| 452 | //---------------------------------------------------------------------- |
| 453 | // GetDIEPtr() |
| 454 | // |
| 455 | // Get the DIE (Debug Information Entry) with the specified offset. |
| 456 | //---------------------------------------------------------------------- |
| 457 | DWARFDebugInfoEntry* |
| 458 | DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset) |
| 459 | { |
| 460 | if (die_offset != DW_INVALID_OFFSET) |
| 461 | { |
| 462 | ExtractDIEsIfNeeded (false); |
| 463 | DWARFDebugInfoEntry compare_die; |
| 464 | compare_die.SetOffset(die_offset); |
| 465 | DWARFDebugInfoEntry::iterator end = m_die_array.end(); |
| 466 | DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); |
| 467 | if (pos != end) |
| 468 | { |
| 469 | if (die_offset == (*pos).GetOffset()) |
| 470 | return &(*pos); |
| 471 | } |
| 472 | } |
| 473 | return NULL; // Not found in any compile units |
| 474 | } |
| 475 | |
| 476 | //---------------------------------------------------------------------- |
| 477 | // GetDIEPtrContainingOffset() |
| 478 | // |
| 479 | // Get the DIE (Debug Information Entry) that contains the specified |
| 480 | // .debug_info offset. |
| 481 | //---------------------------------------------------------------------- |
| 482 | const DWARFDebugInfoEntry* |
| 483 | DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset) |
| 484 | { |
| 485 | if (die_offset != DW_INVALID_OFFSET) |
| 486 | { |
| 487 | ExtractDIEsIfNeeded (false); |
| 488 | DWARFDebugInfoEntry compare_die; |
| 489 | compare_die.SetOffset(die_offset); |
| 490 | DWARFDebugInfoEntry::iterator end = m_die_array.end(); |
| 491 | DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); |
| 492 | if (pos != end) |
| 493 | { |
| 494 | if (die_offset >= (*pos).GetOffset()) |
| 495 | { |
| 496 | DWARFDebugInfoEntry::iterator next = pos + 1; |
| 497 | if (next != end) |
| 498 | { |
| 499 | if (die_offset < (*next).GetOffset()) |
| 500 | return &(*pos); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | return NULL; // Not found in any compile units |
| 506 | } |
| 507 | |
| 508 | |
| 509 | |
| 510 | size_t |
| 511 | DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const |
| 512 | { |
| 513 | size_t old_size = dies.Size(); |
| 514 | DWARFDebugInfoEntry::const_iterator pos; |
| 515 | DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); |
| 516 | for (pos = m_die_array.begin(); pos != end; ++pos) |
| 517 | { |
| 518 | if (pos->Tag() == tag) |
| 519 | dies.Insert(&(*pos)); |
| 520 | } |
| 521 | |
| 522 | // Return the number of DIEs added to the collection |
| 523 | return dies.Size() - old_size; |
| 524 | } |
| 525 | |
| 526 | void |
| 527 | DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx) |
| 528 | { |
| 529 | m_global_die_indexes.push_back (die_idx); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | void |
| 534 | DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die) |
| 535 | { |
| 536 | // Indexes to all file level global and static variables |
| 537 | m_global_die_indexes; |
| 538 | |
| 539 | if (m_die_array.empty()) |
| 540 | return; |
| 541 | |
| 542 | const DWARFDebugInfoEntry* first_die = &m_die_array[0]; |
| 543 | const DWARFDebugInfoEntry* end = first_die + m_die_array.size(); |
| 544 | if (first_die <= die && die < end) |
| 545 | m_global_die_indexes.push_back (die - first_die); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void |
| 550 | DWARFCompileUnit::Index |
| 551 | ( |
| 552 | lldb_private::UniqueCStringMap<dw_offset_t>& name_to_function_die, |
| 553 | lldb_private::UniqueCStringMap<dw_offset_t>& name_to_inlined_die, |
| 554 | lldb_private::UniqueCStringMap<dw_offset_t>& name_to_global_die, |
| 555 | lldb_private::UniqueCStringMap<dw_offset_t>& name_to_type_die |
| 556 | ) |
| 557 | { |
| 558 | |
| 559 | const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data(); |
| 560 | |
| 561 | DWARFDebugInfoEntry::const_iterator pos; |
| 562 | DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin(); |
| 563 | DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); |
| 564 | for (pos = begin; pos != end; ++pos) |
| 565 | { |
| 566 | const DWARFDebugInfoEntry &die = *pos; |
| 567 | |
| 568 | const dw_tag_t tag = die.Tag(); |
| 569 | |
| 570 | switch (tag) |
| 571 | { |
| 572 | case DW_TAG_subprogram: |
| 573 | case DW_TAG_inlined_subroutine: |
| 574 | case DW_TAG_base_type: |
| 575 | case DW_TAG_class_type: |
| 576 | case DW_TAG_constant: |
| 577 | case DW_TAG_enumeration_type: |
| 578 | case DW_TAG_string_type: |
| 579 | case DW_TAG_subroutine_type: |
| 580 | case DW_TAG_structure_type: |
| 581 | case DW_TAG_union_type: |
| 582 | case DW_TAG_typedef: |
| 583 | case DW_TAG_namespace: |
| 584 | case DW_TAG_variable: |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | DWARFDebugInfoEntry::Attributes attributes; |
| 592 | const char *name = NULL; |
| 593 | const char *mangled = NULL; |
| 594 | bool is_variable = false; |
| 595 | bool is_declaration = false; |
| 596 | bool is_artificial = false; |
| 597 | bool has_address = false; |
| 598 | bool has_location = false; |
| 599 | bool is_global_or_static_variable = false; |
| 600 | const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, attributes); |
| 601 | if (num_attributes > 0) |
| 602 | { |
| 603 | uint32_t i; |
| 604 | |
| 605 | dw_tag_t tag = die.Tag(); |
| 606 | |
| 607 | is_variable = tag == DW_TAG_variable; |
| 608 | |
| 609 | for (i=0; i<num_attributes; ++i) |
| 610 | { |
| 611 | dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 612 | DWARFFormValue form_value; |
| 613 | switch (attr) |
| 614 | { |
| 615 | case DW_AT_name: |
| 616 | if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) |
| 617 | name = form_value.AsCString(debug_str); |
| 618 | break; |
| 619 | |
| 620 | case DW_AT_declaration: |
| 621 | if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) |
| 622 | is_declaration = form_value.Unsigned() != 0; |
| 623 | break; |
| 624 | |
| 625 | case DW_AT_artificial: |
| 626 | if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) |
| 627 | is_artificial = form_value.Unsigned() != 0; |
| 628 | break; |
| 629 | |
| 630 | case DW_AT_MIPS_linkage_name: |
| 631 | if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) |
| 632 | mangled = form_value.AsCString(debug_str); |
| 633 | break; |
| 634 | |
| 635 | case DW_AT_low_pc: |
| 636 | case DW_AT_ranges: |
| 637 | case DW_AT_entry_pc: |
| 638 | has_address = true; |
| 639 | break; |
| 640 | |
| 641 | case DW_AT_location: |
| 642 | has_location = true; |
| 643 | if (tag == DW_TAG_variable) |
| 644 | { |
| 645 | const DWARFDebugInfoEntry* parent_die = die.GetParent(); |
| 646 | while ( parent_die != NULL ) |
| 647 | { |
| 648 | switch (parent_die->Tag()) |
| 649 | { |
| 650 | case DW_TAG_subprogram: |
| 651 | case DW_TAG_lexical_block: |
| 652 | case DW_TAG_inlined_subroutine: |
| 653 | // Even if this is a function level static, we don't add it. We could theoretically |
| 654 | // add these if we wanted to by introspecting into the DW_AT_location and seeing |
| 655 | // if the location describes a hard coded address, but we dont want the performance |
| 656 | // penalty of that right now. |
| 657 | is_global_or_static_variable = false; |
| 658 | // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) |
| 659 | // { |
| 660 | // // If we have valid block data, then we have location expression bytes |
| 661 | // // that are fixed (not a location list). |
| 662 | // const uint8_t *block_data = form_value.BlockData(); |
| 663 | // if (block_data) |
| 664 | // { |
| 665 | // uint32_t block_length = form_value.Unsigned(); |
| 666 | // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) |
| 667 | // { |
| 668 | // if (block_data[0] == DW_OP_addr) |
| 669 | // add_die = true; |
| 670 | // } |
| 671 | // } |
| 672 | // } |
| 673 | parent_die = NULL; // Terminate the while loop. |
| 674 | break; |
| 675 | |
| 676 | case DW_TAG_compile_unit: |
| 677 | is_global_or_static_variable = true; |
| 678 | parent_die = NULL; // Terminate the while loop. |
| 679 | break; |
| 680 | |
| 681 | default: |
| 682 | parent_die = parent_die->GetParent(); // Keep going in the while loop. |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | switch (tag) |
| 693 | { |
| 694 | case DW_TAG_subprogram: |
| 695 | if (has_address) |
| 696 | { |
| 697 | if (name && name[0]) |
| 698 | { |
| 699 | if ((name[0] == '-' || name[0] == '+') && name[1] == '[') |
| 700 | { |
| 701 | int name_len = strlen (name); |
| 702 | // Objective C methods must have at least: |
| 703 | // "-[" or "+[" prefix |
| 704 | // One character for a class name |
| 705 | // One character for the space between the class name |
| 706 | // One character for the method name |
| 707 | // "]" suffix |
| 708 | if (name_len >= 6 && name[name_len - 1] == ']') |
| 709 | { |
| 710 | const char *method_name = strchr (name, ' '); |
| 711 | if (method_name) |
| 712 | { |
| 713 | // Skip the space |
| 714 | ++method_name; |
| 715 | // Extract the objective C basename and add it to the |
| 716 | // accelerator tables |
| 717 | size_t method_name_len = name_len - (method_name - name) - 1; |
| 718 | ConstString method_const_str (method_name, method_name_len); |
| 719 | name_to_function_die.Append(method_const_str.AsCString(), die.GetOffset()); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | name_to_function_die.Append(ConstString(name).AsCString(), die.GetOffset()); |
| 724 | } |
| 725 | if (mangled && mangled[0]) |
| 726 | name_to_function_die.Append(ConstString(mangled).AsCString(), die.GetOffset()); |
| 727 | } |
| 728 | break; |
| 729 | |
| 730 | case DW_TAG_inlined_subroutine: |
| 731 | if (has_address) |
| 732 | { |
| 733 | if (name && name[0]) |
| 734 | name_to_inlined_die.Append(ConstString(name).AsCString(), die.GetOffset()); |
| 735 | if (mangled && mangled[0]) |
| 736 | name_to_inlined_die.Append(ConstString(mangled).AsCString(), die.GetOffset()); |
| 737 | } |
| 738 | break; |
| 739 | |
| 740 | case DW_TAG_base_type: |
| 741 | case DW_TAG_class_type: |
| 742 | case DW_TAG_constant: |
| 743 | case DW_TAG_enumeration_type: |
| 744 | case DW_TAG_string_type: |
| 745 | case DW_TAG_subroutine_type: |
| 746 | case DW_TAG_structure_type: |
| 747 | case DW_TAG_union_type: |
| 748 | case DW_TAG_typedef: |
| 749 | case DW_TAG_namespace: |
| 750 | if (name && is_declaration == false) |
| 751 | { |
| 752 | name_to_type_die.Append(ConstString(name).AsCString(), die.GetOffset()); |
| 753 | } |
| 754 | break; |
| 755 | |
| 756 | case DW_TAG_variable: |
| 757 | if (name && has_location && is_global_or_static_variable) |
| 758 | { |
| 759 | AddGlobalDIEByIndex (std::distance (begin, pos)); |
| 760 | name_to_global_die.Append(ConstString(name).AsCString(), die.GetOffset()); |
| 761 | } |
| 762 | break; |
| 763 | |
| 764 | default: |
| 765 | continue; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | |