Home | History | Annotate | Download | only in DWARF
      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/Mangled.h"
     13 #include "lldb/Core/Module.h"
     14 #include "lldb/Core/Stream.h"
     15 #include "lldb/Core/Timer.h"
     16 #include "lldb/Symbol/CompileUnit.h"
     17 #include "lldb/Symbol/LineTable.h"
     18 #include "lldb/Symbol/ObjectFile.h"
     19 #include "lldb/Target/ObjCLanguageRuntime.h"
     20 
     21 #include "DWARFDebugAbbrev.h"
     22 #include "DWARFDebugAranges.h"
     23 #include "DWARFDebugInfo.h"
     24 #include "DWARFDIECollection.h"
     25 #include "DWARFFormValue.h"
     26 #include "LogChannelDWARF.h"
     27 #include "NameToDIE.h"
     28 #include "SymbolFileDWARF.h"
     29 #include "SymbolFileDWARFDebugMap.h"
     30 
     31 using namespace lldb;
     32 using namespace lldb_private;
     33 using namespace std;
     34 
     35 
     36 extern int g_verbose;
     37 
     38 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) :
     39     m_dwarf2Data    (dwarf2Data),
     40     m_abbrevs       (NULL),
     41     m_user_data     (NULL),
     42     m_die_array     (),
     43     m_func_aranges_ap (),
     44     m_base_addr     (0),
     45     m_offset        (DW_INVALID_OFFSET),
     46     m_length        (0),
     47     m_version       (0),
     48     m_addr_size     (DWARFCompileUnit::GetDefaultAddressSize()),
     49     m_producer      (eProducerInvalid),
     50     m_producer_version_major (0),
     51     m_producer_version_minor (0),
     52     m_producer_version_update (0)
     53 {
     54 }
     55 
     56 void
     57 DWARFCompileUnit::Clear()
     58 {
     59     m_offset        = DW_INVALID_OFFSET;
     60     m_length        = 0;
     61     m_version       = 0;
     62     m_abbrevs       = NULL;
     63     m_addr_size     = DWARFCompileUnit::GetDefaultAddressSize();
     64     m_base_addr     = 0;
     65     m_die_array.clear();
     66     m_func_aranges_ap.reset();
     67     m_user_data     = NULL;
     68     m_producer      = eProducerInvalid;
     69 }
     70 
     71 bool
     72 DWARFCompileUnit::Extract(const DataExtractor &debug_info, lldb::offset_t *offset_ptr)
     73 {
     74     Clear();
     75 
     76     m_offset = *offset_ptr;
     77 
     78     if (debug_info.ValidOffset(*offset_ptr))
     79     {
     80         dw_offset_t abbr_offset;
     81         const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
     82         m_length        = debug_info.GetU32(offset_ptr);
     83         m_version       = debug_info.GetU16(offset_ptr);
     84         abbr_offset     = debug_info.GetU32(offset_ptr);
     85         m_addr_size     = debug_info.GetU8 (offset_ptr);
     86 
     87         bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
     88         bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
     89         bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
     90         bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
     91 
     92         if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
     93         {
     94             m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
     95             return true;
     96         }
     97 
     98         // reset the offset to where we tried to parse from if anything went wrong
     99         *offset_ptr = m_offset;
    100     }
    101 
    102     return false;
    103 }
    104 
    105 
    106 dw_offset_t
    107 DWARFCompileUnit::Extract(lldb::offset_t offset, const DataExtractor& debug_info_data, const DWARFAbbreviationDeclarationSet* abbrevs)
    108 {
    109     Clear();
    110 
    111     m_offset = offset;
    112 
    113     if (debug_info_data.ValidOffset(offset))
    114     {
    115         m_length        = debug_info_data.GetU32(&offset);
    116         m_version       = debug_info_data.GetU16(&offset);
    117         bool abbrevs_OK = debug_info_data.GetU32(&offset) == abbrevs->GetOffset();
    118         m_abbrevs       = abbrevs;
    119         m_addr_size     = debug_info_data.GetU8 (&offset);
    120 
    121         bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
    122         bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
    123 
    124         if (version_OK && addr_size_OK && abbrevs_OK && debug_info_data.ValidOffset(offset))
    125             return offset;
    126     }
    127     return DW_INVALID_OFFSET;
    128 }
    129 
    130 void
    131 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
    132 {
    133     if (m_die_array.size() > 1)
    134     {
    135         // std::vectors never get any smaller when resized to a smaller size,
    136         // or when clear() or erase() are called, the size will report that it
    137         // is smaller, but the memory allocated remains intact (call capacity()
    138         // to see this). So we need to create a temporary vector and swap the
    139         // contents which will cause just the internal pointers to be swapped
    140         // so that when "tmp_array" goes out of scope, it will destroy the
    141         // contents.
    142 
    143         // Save at least the compile unit DIE
    144         DWARFDebugInfoEntry::collection tmp_array;
    145         m_die_array.swap(tmp_array);
    146         if (keep_compile_unit_die)
    147             m_die_array.push_back(tmp_array.front());
    148     }
    149 }
    150 
    151 //----------------------------------------------------------------------
    152 // ParseCompileUnitDIEsIfNeeded
    153 //
    154 // Parses a compile unit and indexes its DIEs if it hasn't already been
    155 // done.
    156 //----------------------------------------------------------------------
    157 size_t
    158 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
    159 {
    160     const size_t initial_die_array_size = m_die_array.size();
    161     if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
    162         return 0; // Already parsed
    163 
    164     Timer scoped_timer (__PRETTY_FUNCTION__,
    165                         "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
    166                         m_offset,
    167                         cu_die_only);
    168 
    169     // Set the offset to that of the first DIE and calculate the start of the
    170     // next compilation unit header.
    171     lldb::offset_t offset = GetFirstDIEOffset();
    172     lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
    173 
    174     DWARFDebugInfoEntry die;
    175         // Keep a flat array of the DIE for binary lookup by DIE offset
    176     if (!cu_die_only)
    177     {
    178         Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
    179         if (log)
    180         {
    181             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
    182                                                                                     "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]",
    183                                                                                     GetOffset());
    184         }
    185     }
    186 
    187     uint32_t depth = 0;
    188     // We are in our compile unit, parse starting at the offset
    189     // we were told to parse
    190     const DataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data();
    191     std::vector<uint32_t> die_index_stack;
    192     die_index_stack.reserve(32);
    193     die_index_stack.push_back(0);
    194     bool prev_die_had_children = false;
    195     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
    196     while (offset < next_cu_offset &&
    197            die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset))
    198     {
    199 //        if (log)
    200 //            log->Printf("0x%8.8x: %*.*s%s%s",
    201 //                        die.GetOffset(),
    202 //                        depth * 2, depth * 2, "",
    203 //                        DW_TAG_value_to_name (die.Tag()),
    204 //                        die.HasChildren() ? " *" : "");
    205 
    206         const bool null_die = die.IsNULL();
    207         if (depth == 0)
    208         {
    209             uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
    210             if (base_addr == LLDB_INVALID_ADDRESS)
    211                 base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0);
    212             SetBaseAddress (base_addr);
    213             if (initial_die_array_size == 0)
    214                 AddDIE (die);
    215             if (cu_die_only)
    216                 return 1;
    217         }
    218         else
    219         {
    220             if (null_die)
    221             {
    222                 if (prev_die_had_children)
    223                 {
    224                     // This will only happen if a DIE says is has children
    225                     // but all it contains is a NULL tag. Since we are removing
    226                     // the NULL DIEs from the list (saves up to 25% in C++ code),
    227                     // we need a way to let the DIE know that it actually doesn't
    228                     // have children.
    229                     if (!m_die_array.empty())
    230                         m_die_array.back().SetEmptyChildren(true);
    231                 }
    232             }
    233             else
    234             {
    235                 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]);
    236 
    237                 if (die_index_stack.back())
    238                     m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back());
    239 
    240                 // Only push the DIE if it isn't a NULL DIE
    241                     m_die_array.push_back(die);
    242             }
    243         }
    244 
    245         if (null_die)
    246         {
    247             // NULL DIE.
    248             if (!die_index_stack.empty())
    249                 die_index_stack.pop_back();
    250 
    251             if (depth > 0)
    252                 --depth;
    253             if (depth == 0)
    254                 break;  // We are done with this compile unit!
    255 
    256             prev_die_had_children = false;
    257         }
    258         else
    259         {
    260             die_index_stack.back() = m_die_array.size() - 1;
    261             // Normal DIE
    262             const bool die_has_children = die.HasChildren();
    263             if (die_has_children)
    264             {
    265                 die_index_stack.push_back(0);
    266                 ++depth;
    267             }
    268             prev_die_had_children = die_has_children;
    269         }
    270     }
    271 
    272     // Give a little bit of info if we encounter corrupt DWARF (our offset
    273     // should always terminate at or before the start of the next compilation
    274     // unit header).
    275     if (offset > next_cu_offset)
    276     {
    277         m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8" PRIx64 "\n",
    278                                                                    GetOffset(),
    279                                                                    offset);
    280     }
    281 
    282     // Since std::vector objects will double their size, we really need to
    283     // make a new array with the perfect size so we don't end up wasting
    284     // space. So here we copy and swap to make sure we don't have any extra
    285     // memory taken up.
    286 
    287     if (m_die_array.size () < m_die_array.capacity())
    288     {
    289         DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end());
    290         exact_size_die_array.swap (m_die_array);
    291     }
    292     Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE));
    293     if (log)
    294     {
    295         StreamString strm;
    296         DWARFDebugInfoEntry::DumpDIECollection (strm, m_die_array);
    297         log->PutCString (strm.GetString().c_str());
    298     }
    299 
    300     return m_die_array.size();
    301 }
    302 
    303 
    304 dw_offset_t
    305 DWARFCompileUnit::GetAbbrevOffset() const
    306 {
    307     return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
    308 }
    309 
    310 
    311 
    312 bool
    313 DWARFCompileUnit::Verify(Stream *s) const
    314 {
    315     const DataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
    316     bool valid_offset = debug_info.ValidOffset(m_offset);
    317     bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
    318     bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
    319     bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
    320     bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
    321     bool verbose = s->GetVerbose();
    322     if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
    323     {
    324         if (verbose)
    325             s->Printf("    0x%8.8x: OK\n", m_offset);
    326         return true;
    327     }
    328     else
    329     {
    330         s->Printf("    0x%8.8x: ", m_offset);
    331 
    332         m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
    333         s->EOL();
    334         if (valid_offset)
    335         {
    336             if (!length_OK)
    337                 s->Printf("        The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
    338             if (!version_OK)
    339                 s->Printf("        The 16 bit compile unit header version is not supported.\n");
    340             if (!abbr_offset_OK)
    341                 s->Printf("        The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
    342             if (!addr_size_OK)
    343                 s->Printf("        The address size is unsupported: 0x%2.2x\n", m_addr_size);
    344         }
    345         else
    346             s->Printf("        The start offset of the compile unit header in the .debug_info is invalid.\n");
    347     }
    348     return false;
    349 }
    350 
    351 
    352 void
    353 DWARFCompileUnit::Dump(Stream *s) const
    354 {
    355     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",
    356                 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
    357 }
    358 
    359 
    360 static uint8_t g_default_addr_size = 4;
    361 
    362 uint8_t
    363 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
    364 {
    365     if (cu)
    366         return cu->GetAddressByteSize();
    367     return DWARFCompileUnit::GetDefaultAddressSize();
    368 }
    369 
    370 uint8_t
    371 DWARFCompileUnit::GetDefaultAddressSize()
    372 {
    373     return g_default_addr_size;
    374 }
    375 
    376 void
    377 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
    378 {
    379     g_default_addr_size = addr_size;
    380 }
    381 
    382 void
    383 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data,
    384                                           DWARFDebugAranges* debug_aranges,
    385                                           bool clear_dies_if_already_not_parsed)
    386 {
    387     // This function is usually called if there in no .debug_aranges section
    388     // in order to produce a compile unit level set of address ranges that
    389     // is accurate. If the DIEs weren't parsed, then we don't want all dies for
    390     // all compile units to stay loaded when they weren't needed. So we can end
    391     // up parsing the DWARF and then throwing them all away to keep memory usage
    392     // down.
    393     const bool clear_dies = ExtractDIEsIfNeeded (false) > 1;
    394 
    395     const DWARFDebugInfoEntry* die = DIE();
    396     if (die)
    397         die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
    398 
    399     if (debug_aranges->IsEmpty())
    400     {
    401         // We got nothing from the functions, maybe we have a line tables only
    402         // situation. Check the line tables and build the arange table from this.
    403         SymbolContext sc;
    404         sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
    405         if (sc.comp_unit)
    406         {
    407             SymbolFileDWARFDebugMap *debug_map_sym_file = m_dwarf2Data->GetDebugMapSymfile();
    408             if (debug_map_sym_file == NULL)
    409             {
    410                 LineTable *line_table = sc.comp_unit->GetLineTable();
    411 
    412                 if (line_table)
    413                 {
    414                     LineTable::FileAddressRanges file_ranges;
    415                     const bool append = true;
    416                     const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append);
    417                     for (uint32_t idx=0; idx<num_ranges; ++idx)
    418                     {
    419                         const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx);
    420                         debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(), range.GetRangeEnd());
    421                         printf ("0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", GetOffset(), range.GetRangeBase(), range.GetRangeEnd());
    422                     }
    423                 }
    424             }
    425             else
    426                 debug_map_sym_file->AddOSOARanges(dwarf2Data,debug_aranges);
    427         }
    428     }
    429 
    430     // Keep memory down by clearing DIEs if this generate function
    431     // caused them to be parsed
    432     if (clear_dies)
    433         ClearDIEs (true);
    434 
    435 }
    436 
    437 
    438 const DWARFDebugAranges &
    439 DWARFCompileUnit::GetFunctionAranges ()
    440 {
    441     if (m_func_aranges_ap.get() == NULL)
    442     {
    443         m_func_aranges_ap.reset (new DWARFDebugAranges());
    444         Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
    445 
    446         if (log)
    447         {
    448             m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log,
    449                                                                     "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]",
    450                                                                     GetOffset());
    451         }
    452         const DWARFDebugInfoEntry* die = DIE();
    453         if (die)
    454             die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get());
    455         const bool minimize = false;
    456         m_func_aranges_ap->Sort(minimize);
    457     }
    458     return *m_func_aranges_ap.get();
    459 }
    460 
    461 bool
    462 DWARFCompileUnit::LookupAddress
    463 (
    464     const dw_addr_t address,
    465     DWARFDebugInfoEntry** function_die_handle,
    466     DWARFDebugInfoEntry** block_die_handle
    467 )
    468 {
    469     bool success = false;
    470 
    471     if (function_die_handle != NULL && DIE())
    472     {
    473 
    474         const DWARFDebugAranges &func_aranges = GetFunctionAranges ();
    475 
    476         // Re-check the aranges auto pointer contents in case it was created above
    477         if (!func_aranges.IsEmpty())
    478         {
    479             *function_die_handle = GetDIEPtr(func_aranges.FindAddress(address));
    480             if (*function_die_handle != NULL)
    481             {
    482                 success = true;
    483                 if (block_die_handle != NULL)
    484                 {
    485                     DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild();
    486                     while (child)
    487                     {
    488                         if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle))
    489                             break;
    490                         child = child->GetSibling();
    491                     }
    492                 }
    493             }
    494         }
    495     }
    496     return success;
    497 }
    498 
    499 //----------------------------------------------------------------------
    500 // Compare function DWARFDebugAranges::Range structures
    501 //----------------------------------------------------------------------
    502 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2)
    503 {
    504     return die1.GetOffset() < die2.GetOffset();
    505 }
    506 
    507 //----------------------------------------------------------------------
    508 // GetDIEPtr()
    509 //
    510 // Get the DIE (Debug Information Entry) with the specified offset.
    511 //----------------------------------------------------------------------
    512 DWARFDebugInfoEntry*
    513 DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset)
    514 {
    515     if (die_offset != DW_INVALID_OFFSET)
    516     {
    517         ExtractDIEsIfNeeded (false);
    518         DWARFDebugInfoEntry compare_die;
    519         compare_die.SetOffset(die_offset);
    520         DWARFDebugInfoEntry::iterator end = m_die_array.end();
    521         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
    522         if (pos != end)
    523         {
    524             if (die_offset == (*pos).GetOffset())
    525                 return &(*pos);
    526         }
    527     }
    528     return NULL;    // Not found in any compile units
    529 }
    530 
    531 //----------------------------------------------------------------------
    532 // GetDIEPtrContainingOffset()
    533 //
    534 // Get the DIE (Debug Information Entry) that contains the specified
    535 // .debug_info offset.
    536 //----------------------------------------------------------------------
    537 const DWARFDebugInfoEntry*
    538 DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset)
    539 {
    540     if (die_offset != DW_INVALID_OFFSET)
    541     {
    542         ExtractDIEsIfNeeded (false);
    543         DWARFDebugInfoEntry compare_die;
    544         compare_die.SetOffset(die_offset);
    545         DWARFDebugInfoEntry::iterator end = m_die_array.end();
    546         DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset);
    547         if (pos != end)
    548         {
    549             if (die_offset >= (*pos).GetOffset())
    550             {
    551                 DWARFDebugInfoEntry::iterator next = pos + 1;
    552                 if (next != end)
    553                 {
    554                     if (die_offset < (*next).GetOffset())
    555                         return &(*pos);
    556                 }
    557             }
    558         }
    559     }
    560     return NULL;    // Not found in any compile units
    561 }
    562 
    563 
    564 
    565 size_t
    566 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
    567 {
    568     size_t old_size = dies.Size();
    569     DWARFDebugInfoEntry::const_iterator pos;
    570     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
    571     for (pos = m_die_array.begin(); pos != end; ++pos)
    572     {
    573         if (pos->Tag() == tag)
    574             dies.Append (&(*pos));
    575     }
    576 
    577     // Return the number of DIEs added to the collection
    578     return dies.Size() - old_size;
    579 }
    580 
    581 //void
    582 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
    583 //{
    584 //    m_global_die_indexes.push_back (die_idx);
    585 //}
    586 //
    587 //
    588 //void
    589 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
    590 //{
    591 //    // Indexes to all file level global and static variables
    592 //    m_global_die_indexes;
    593 //
    594 //    if (m_die_array.empty())
    595 //        return;
    596 //
    597 //    const DWARFDebugInfoEntry* first_die = &m_die_array[0];
    598 //    const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
    599 //    if (first_die <= die && die < end)
    600 //        m_global_die_indexes.push_back (die - first_die);
    601 //}
    602 
    603 
    604 void
    605 DWARFCompileUnit::Index (const uint32_t cu_idx,
    606                          NameToDIE& func_basenames,
    607                          NameToDIE& func_fullnames,
    608                          NameToDIE& func_methods,
    609                          NameToDIE& func_selectors,
    610                          NameToDIE& objc_class_selectors,
    611                          NameToDIE& globals,
    612                          NameToDIE& types,
    613                          NameToDIE& namespaces)
    614 {
    615     const DataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data();
    616 
    617     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize());
    618 
    619     Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS));
    620 
    621     if (log)
    622     {
    623         m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log,
    624                                                                 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
    625                                                                 GetOffset());
    626     }
    627 
    628     DWARFDebugInfoEntry::const_iterator pos;
    629     DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin();
    630     DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
    631     for (pos = begin; pos != end; ++pos)
    632     {
    633         const DWARFDebugInfoEntry &die = *pos;
    634 
    635         const dw_tag_t tag = die.Tag();
    636 
    637         switch (tag)
    638         {
    639         case DW_TAG_subprogram:
    640         case DW_TAG_inlined_subroutine:
    641         case DW_TAG_base_type:
    642         case DW_TAG_class_type:
    643         case DW_TAG_constant:
    644         case DW_TAG_enumeration_type:
    645         case DW_TAG_string_type:
    646         case DW_TAG_subroutine_type:
    647         case DW_TAG_structure_type:
    648         case DW_TAG_union_type:
    649         case DW_TAG_typedef:
    650         case DW_TAG_namespace:
    651         case DW_TAG_variable:
    652         case DW_TAG_unspecified_type:
    653             break;
    654 
    655         default:
    656             continue;
    657         }
    658 
    659         DWARFDebugInfoEntry::Attributes attributes;
    660         const char *name = NULL;
    661         const char *mangled_cstr = NULL;
    662         bool is_declaration = false;
    663         //bool is_artificial = false;
    664         bool has_address = false;
    665         bool has_location = false;
    666         bool is_global_or_static_variable = false;
    667 
    668         dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
    669         const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes);
    670         if (num_attributes > 0)
    671         {
    672             for (uint32_t i=0; i<num_attributes; ++i)
    673             {
    674                 dw_attr_t attr = attributes.AttributeAtIndex(i);
    675                 DWARFFormValue form_value;
    676                 switch (attr)
    677                 {
    678                 case DW_AT_name:
    679                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
    680                         name = form_value.AsCString(debug_str);
    681                     break;
    682 
    683                 case DW_AT_declaration:
    684                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
    685                         is_declaration = form_value.Unsigned() != 0;
    686                     break;
    687 
    688 //                case DW_AT_artificial:
    689 //                    if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
    690 //                        is_artificial = form_value.Unsigned() != 0;
    691 //                    break;
    692 
    693                 case DW_AT_MIPS_linkage_name:
    694                 case DW_AT_linkage_name:
    695                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
    696                         mangled_cstr = form_value.AsCString(debug_str);
    697                     break;
    698 
    699                 case DW_AT_low_pc:
    700                 case DW_AT_high_pc:
    701                 case DW_AT_ranges:
    702                     has_address = true;
    703                     break;
    704 
    705                 case DW_AT_entry_pc:
    706                     has_address = true;
    707                     break;
    708 
    709                 case DW_AT_location:
    710                     has_location = true;
    711                     if (tag == DW_TAG_variable)
    712                     {
    713                         const DWARFDebugInfoEntry* parent_die = die.GetParent();
    714                         while ( parent_die != NULL )
    715                         {
    716                             switch (parent_die->Tag())
    717                             {
    718                             case DW_TAG_subprogram:
    719                             case DW_TAG_lexical_block:
    720                             case DW_TAG_inlined_subroutine:
    721                                 // Even if this is a function level static, we don't add it. We could theoretically
    722                                 // add these if we wanted to by introspecting into the DW_AT_location and seeing
    723                                 // if the location describes a hard coded address, but we dont want the performance
    724                                 // penalty of that right now.
    725                                 is_global_or_static_variable = false;
    726 //                              if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
    727 //                              {
    728 //                                  // If we have valid block data, then we have location expression bytes
    729 //                                  // that are fixed (not a location list).
    730 //                                  const uint8_t *block_data = form_value.BlockData();
    731 //                                  if (block_data)
    732 //                                  {
    733 //                                      uint32_t block_length = form_value.Unsigned();
    734 //                                      if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
    735 //                                      {
    736 //                                          if (block_data[0] == DW_OP_addr)
    737 //                                              add_die = true;
    738 //                                      }
    739 //                                  }
    740 //                              }
    741                                 parent_die = NULL;  // Terminate the while loop.
    742                                 break;
    743 
    744                             case DW_TAG_compile_unit:
    745                                 is_global_or_static_variable = true;
    746                                 parent_die = NULL;  // Terminate the while loop.
    747                                 break;
    748 
    749                             default:
    750                                 parent_die = parent_die->GetParent();   // Keep going in the while loop.
    751                                 break;
    752                             }
    753                         }
    754                     }
    755                     break;
    756 
    757                 case DW_AT_specification:
    758                     if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value))
    759                         specification_die_offset = form_value.Reference(this);
    760                     break;
    761                 }
    762             }
    763         }
    764 
    765         switch (tag)
    766         {
    767         case DW_TAG_subprogram:
    768             if (has_address)
    769             {
    770                 if (name)
    771                 {
    772                     // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the
    773                     // simple inlined check outside the call.
    774                     ObjCLanguageRuntime::MethodName objc_method(name, true);
    775                     if (objc_method.IsValid(true))
    776                     {
    777                         ConstString objc_class_name_with_category (objc_method.GetClassNameWithCategory());
    778                         ConstString objc_selector_name (objc_method.GetSelector());
    779                         ConstString objc_fullname_no_category_name (objc_method.GetFullNameWithoutCategory(true));
    780                         ConstString objc_class_name_no_category (objc_method.GetClassName());
    781                         func_fullnames.Insert (ConstString(name), die.GetOffset());
    782                         if (objc_class_name_with_category)
    783                             objc_class_selectors.Insert(objc_class_name_with_category, die.GetOffset());
    784                         if (objc_class_name_no_category && objc_class_name_no_category != objc_class_name_with_category)
    785                             objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset());
    786                         if (objc_selector_name)
    787                             func_selectors.Insert (objc_selector_name, die.GetOffset());
    788                         if (objc_fullname_no_category_name)
    789                             func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset());
    790                     }
    791                     // If we have a mangled name, then the DW_AT_name attribute
    792                     // is usually the method name without the class or any parameters
    793                     const DWARFDebugInfoEntry *parent = die.GetParent();
    794                     bool is_method = false;
    795                     if (parent)
    796                     {
    797                         dw_tag_t parent_tag = parent->Tag();
    798                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
    799                         {
    800                             is_method = true;
    801                         }
    802                         else
    803                         {
    804                             if (specification_die_offset != DW_INVALID_OFFSET)
    805                             {
    806                                 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL);
    807                                 if (specification_die)
    808                                 {
    809                                     parent = specification_die->GetParent();
    810                                     if (parent)
    811                                     {
    812                                         parent_tag = parent->Tag();
    813 
    814                                         if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
    815                                             is_method = true;
    816                                     }
    817                                 }
    818                             }
    819                         }
    820                     }
    821 
    822 
    823                     if (is_method)
    824                         func_methods.Insert (ConstString(name), die.GetOffset());
    825                     else
    826                         func_basenames.Insert (ConstString(name), die.GetOffset());
    827 
    828                     if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
    829                         func_fullnames.Insert (ConstString(name), die.GetOffset());
    830                 }
    831                 if (mangled_cstr)
    832                 {
    833                     // Make sure our mangled name isn't the same string table entry
    834                     // as our name. If it starts with '_', then it is ok, else compare
    835                     // the string to make sure it isn't the same and we don't end up
    836                     // with duplicate entries
    837                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0)))
    838                     {
    839                         Mangled mangled (ConstString(mangled_cstr), true);
    840                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
    841                         if (mangled.GetDemangledName())
    842                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
    843                     }
    844                 }
    845             }
    846             break;
    847 
    848         case DW_TAG_inlined_subroutine:
    849             if (has_address)
    850             {
    851                 if (name)
    852                     func_basenames.Insert (ConstString(name), die.GetOffset());
    853                 if (mangled_cstr)
    854                 {
    855                     // Make sure our mangled name isn't the same string table entry
    856                     // as our name. If it starts with '_', then it is ok, else compare
    857                     // the string to make sure it isn't the same and we don't end up
    858                     // with duplicate entries
    859                     if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
    860                     {
    861                         Mangled mangled (ConstString(mangled_cstr), true);
    862                         func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset());
    863                         if (mangled.GetDemangledName())
    864                             func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset());
    865                     }
    866                 }
    867                 else
    868                     func_fullnames.Insert (ConstString(name), die.GetOffset());
    869             }
    870             break;
    871 
    872         case DW_TAG_base_type:
    873         case DW_TAG_class_type:
    874         case DW_TAG_constant:
    875         case DW_TAG_enumeration_type:
    876         case DW_TAG_string_type:
    877         case DW_TAG_subroutine_type:
    878         case DW_TAG_structure_type:
    879         case DW_TAG_union_type:
    880         case DW_TAG_typedef:
    881         case DW_TAG_unspecified_type:
    882             if (name && is_declaration == false)
    883             {
    884                 types.Insert (ConstString(name), die.GetOffset());
    885             }
    886             break;
    887 
    888         case DW_TAG_namespace:
    889             if (name)
    890                 namespaces.Insert (ConstString(name), die.GetOffset());
    891             break;
    892 
    893         case DW_TAG_variable:
    894             if (name && has_location && is_global_or_static_variable)
    895             {
    896                 globals.Insert (ConstString(name), die.GetOffset());
    897                 // Be sure to include variables by their mangled and demangled
    898                 // names if they have any since a variable can have a basename
    899                 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled
    900                 // mangled name "(anonymous namespace)::i"...
    901 
    902                 // Make sure our mangled name isn't the same string table entry
    903                 // as our name. If it starts with '_', then it is ok, else compare
    904                 // the string to make sure it isn't the same and we don't end up
    905                 // with duplicate entries
    906                 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
    907                 {
    908                     Mangled mangled (ConstString(mangled_cstr), true);
    909                     globals.Insert (mangled.GetMangledName(), die.GetOffset());
    910                     if (mangled.GetDemangledName())
    911                         globals.Insert (mangled.GetDemangledName(), die.GetOffset());
    912                 }
    913             }
    914             break;
    915 
    916         default:
    917             continue;
    918         }
    919     }
    920 }
    921 
    922 bool
    923 DWARFCompileUnit::Supports_unnamed_objc_bitfields ()
    924 {
    925     if (GetProducer() == eProducerClang)
    926     {
    927         const uint32_t major_version = GetProducerVersionMajor();
    928         if (major_version > 425 || (major_version == 425 && GetProducerVersionUpdate() >= 13))
    929             return true;
    930         else
    931             return false;
    932     }
    933     return true; // Assume all other compilers didn't have incorrect ObjC bitfield info
    934 }
    935 
    936 bool
    937 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type ()
    938 {
    939     if (GetProducer() == eProducerLLVMGCC)
    940         return false;
    941     return true;
    942 }
    943 
    944 bool
    945 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid()
    946 {
    947     // llvm-gcc makes completely invalid decl file attributes and won't ever
    948     // be fixed, so we need to know to ignore these.
    949     return GetProducer() == eProducerLLVMGCC;
    950 }
    951 
    952 void
    953 DWARFCompileUnit::ParseProducerInfo ()
    954 {
    955     m_producer_version_major = UINT32_MAX;
    956     m_producer_version_minor = UINT32_MAX;
    957     m_producer_version_update = UINT32_MAX;
    958 
    959     const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly();
    960     if (die)
    961     {
    962 
    963         const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL);
    964         if (producer_cstr)
    965         {
    966             RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$");
    967             if (llvm_gcc_regex.Execute (producer_cstr))
    968             {
    969                 m_producer = eProducerLLVMGCC;
    970             }
    971             else if (strstr(producer_cstr, "clang"))
    972             {
    973                 static RegularExpression g_clang_version_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)");
    974                 RegularExpression::Match regex_match(3);
    975                 if (g_clang_version_regex.Execute (producer_cstr, &regex_match))
    976                 {
    977                     std::string str;
    978                     if (regex_match.GetMatchAtIndex (producer_cstr, 1, str))
    979                         m_producer_version_major = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
    980                     if (regex_match.GetMatchAtIndex (producer_cstr, 2, str))
    981                         m_producer_version_minor = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
    982                     if (regex_match.GetMatchAtIndex (producer_cstr, 3, str))
    983                         m_producer_version_update = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10);
    984                 }
    985                 m_producer = eProducerClang;
    986             }
    987             else if (strstr(producer_cstr, "GNU"))
    988                 m_producer = eProducerGCC;
    989         }
    990     }
    991     if (m_producer == eProducerInvalid)
    992         m_producer = eProcucerOther;
    993 }
    994 
    995 DWARFCompileUnit::Producer
    996 DWARFCompileUnit::GetProducer ()
    997 {
    998     if (m_producer == eProducerInvalid)
    999         ParseProducerInfo ();
   1000     return m_producer;
   1001 }
   1002 
   1003 
   1004 uint32_t
   1005 DWARFCompileUnit::GetProducerVersionMajor()
   1006 {
   1007     if (m_producer_version_major == 0)
   1008         ParseProducerInfo ();
   1009     return m_producer_version_major;
   1010 }
   1011 
   1012 uint32_t
   1013 DWARFCompileUnit::GetProducerVersionMinor()
   1014 {
   1015     if (m_producer_version_minor == 0)
   1016         ParseProducerInfo ();
   1017     return m_producer_version_minor;
   1018 }
   1019 
   1020 uint32_t
   1021 DWARFCompileUnit::GetProducerVersionUpdate()
   1022 {
   1023     if (m_producer_version_update == 0)
   1024         ParseProducerInfo ();
   1025     return m_producer_version_update;
   1026 }
   1027 
   1028