Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DWARFDebugInfo.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 "SymbolFileDWARF.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <set> |
| 14 | |
| 15 | #include "lldb/Core/RegularExpression.h" |
| 16 | #include "lldb/Core/Stream.h" |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/ObjectFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 19 | #include "DWARFDebugAranges.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "DWARFDebugInfo.h" |
| 21 | #include "DWARFCompileUnit.h" |
| 22 | #include "DWARFDebugAranges.h" |
| 23 | #include "DWARFDebugInfoEntry.h" |
| 24 | #include "DWARFFormValue.h" |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 25 | #include "LogChannelDWARF.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Greg Clayton | 21f2a49 | 2011-10-06 00:09:08 +0000 | [diff] [blame] | 27 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | using namespace lldb_private; |
| 29 | using namespace std; |
| 30 | |
| 31 | //---------------------------------------------------------------------- |
| 32 | // Constructor |
| 33 | //---------------------------------------------------------------------- |
| 34 | DWARFDebugInfo::DWARFDebugInfo() : |
| 35 | m_dwarf2Data(NULL), |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 36 | m_compile_units(), |
| 37 | m_cu_aranges_ap () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | //---------------------------------------------------------------------- |
| 42 | // SetDwarfData |
| 43 | //---------------------------------------------------------------------- |
| 44 | void |
| 45 | DWARFDebugInfo::SetDwarfData(SymbolFileDWARF* dwarf2Data) |
| 46 | { |
| 47 | m_dwarf2Data = dwarf2Data; |
| 48 | m_compile_units.clear(); |
| 49 | } |
| 50 | |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 51 | |
| 52 | DWARFDebugAranges & |
| 53 | DWARFDebugInfo::GetCompileUnitAranges () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | { |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 55 | if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 57 | Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 58 | |
| 59 | m_cu_aranges_ap.reset (new DWARFDebugAranges()); |
Ed Maste | eeae721 | 2013-10-24 20:43:47 +0000 | [diff] [blame] | 60 | const DWARFDataExtractor &debug_aranges_data = m_dwarf2Data->get_debug_aranges_data(); |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 61 | if (debug_aranges_data.GetByteSize() > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | { |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 63 | if (log) |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 64 | log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" from .debug_aranges", |
| 65 | m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str()); |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 66 | m_cu_aranges_ap->Extract (debug_aranges_data); |
| 67 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | } |
Richard Mitton | b6d18be | 2013-09-09 23:17:40 +0000 | [diff] [blame] | 69 | |
| 70 | // Make a list of all CUs represented by the arange data in the file. |
| 71 | std::set<dw_offset_t> cus_with_data; |
| 72 | for (size_t n=0;n<m_cu_aranges_ap.get()->GetNumRanges();n++) |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 73 | { |
Richard Mitton | b6d18be | 2013-09-09 23:17:40 +0000 | [diff] [blame] | 74 | dw_offset_t offset = m_cu_aranges_ap.get()->OffsetAtIndex(n); |
| 75 | if (offset != DW_INVALID_OFFSET) |
| 76 | cus_with_data.insert (offset); |
| 77 | } |
| 78 | |
| 79 | // Manually build arange data for everything that wasn't in the .debug_aranges table. |
| 80 | bool printed = false; |
| 81 | const size_t num_compile_units = GetNumCompileUnits(); |
Richard Mitton | b6d18be | 2013-09-09 23:17:40 +0000 | [diff] [blame] | 82 | for (size_t idx = 0; idx < num_compile_units; ++idx) |
| 83 | { |
| 84 | DWARFCompileUnit* cu = GetCompileUnitAtIndex(idx); |
| 85 | |
| 86 | dw_offset_t offset = cu->GetOffset(); |
| 87 | if (cus_with_data.find(offset) == cus_with_data.end()) |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 88 | { |
Richard Mitton | b6d18be | 2013-09-09 23:17:40 +0000 | [diff] [blame] | 89 | if (log) |
| 90 | { |
| 91 | if (!printed) |
| 92 | log->Printf ("DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing", |
| 93 | m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str()); |
| 94 | printed = true; |
| 95 | } |
Greg Clayton | 26c1fe3 | 2014-03-14 23:36:38 +0000 | [diff] [blame] | 96 | cu->BuildAddressRangeTable (m_dwarf2Data, m_cu_aranges_ap.get()); |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 100 | const bool minimize = true; |
Greg Clayton | c26e445 | 2011-10-07 03:58:56 +0000 | [diff] [blame] | 101 | m_cu_aranges_ap->Sort (minimize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | } |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 103 | return *m_cu_aranges_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | //---------------------------------------------------------------------- |
| 108 | // LookupAddress |
| 109 | //---------------------------------------------------------------------- |
| 110 | bool |
| 111 | DWARFDebugInfo::LookupAddress |
| 112 | ( |
| 113 | const dw_addr_t address, |
| 114 | const dw_offset_t hint_die_offset, |
| 115 | DWARFCompileUnitSP& cu_sp, |
| 116 | DWARFDebugInfoEntry** function_die, |
| 117 | DWARFDebugInfoEntry** block_die |
| 118 | ) |
| 119 | { |
| 120 | |
| 121 | if (hint_die_offset != DW_INVALID_OFFSET) |
| 122 | cu_sp = GetCompileUnit(hint_die_offset); |
| 123 | else |
| 124 | { |
Greg Clayton | d4a2b37 | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 125 | DWARFDebugAranges &cu_aranges = GetCompileUnitAranges (); |
| 126 | const dw_offset_t cu_offset = cu_aranges.FindAddress (address); |
| 127 | cu_sp = GetCompileUnit(cu_offset); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (cu_sp.get()) |
| 131 | { |
| 132 | if (cu_sp->LookupAddress(address, function_die, block_die)) |
| 133 | return true; |
| 134 | cu_sp.reset(); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | // The hint_die_offset may have been a pointer to the actual item that |
| 139 | // we are looking for |
| 140 | DWARFDebugInfoEntry* die_ptr = GetDIEPtr(hint_die_offset, &cu_sp); |
| 141 | if (die_ptr) |
| 142 | { |
| 143 | if (cu_sp.get()) |
| 144 | { |
| 145 | if (function_die || block_die) |
| 146 | return die_ptr->LookupAddress(address, m_dwarf2Data, cu_sp.get(), function_die, block_die); |
| 147 | |
| 148 | // We only wanted the compile unit that contained this address |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void |
| 158 | DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded() |
| 159 | { |
| 160 | if (m_compile_units.empty()) |
| 161 | { |
| 162 | if (m_dwarf2Data != NULL) |
| 163 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 164 | lldb::offset_t offset = 0; |
Ed Maste | eeae721 | 2013-10-24 20:43:47 +0000 | [diff] [blame] | 165 | const DWARFDataExtractor &debug_info_data = m_dwarf2Data->get_debug_info_data(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | while (debug_info_data.ValidOffset(offset)) |
| 167 | { |
| 168 | DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(m_dwarf2Data)); |
| 169 | // Out of memory? |
| 170 | if (cu_sp.get() == NULL) |
| 171 | break; |
| 172 | |
| 173 | if (cu_sp->Extract(debug_info_data, &offset) == false) |
| 174 | break; |
| 175 | |
| 176 | m_compile_units.push_back(cu_sp); |
| 177 | |
| 178 | offset = cu_sp->GetNextCompileUnitOffset(); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 184 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | DWARFDebugInfo::GetNumCompileUnits() |
| 186 | { |
| 187 | ParseCompileUnitHeadersIfNeeded(); |
| 188 | return m_compile_units.size(); |
| 189 | } |
| 190 | |
| 191 | DWARFCompileUnit* |
| 192 | DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx) |
| 193 | { |
| 194 | DWARFCompileUnit* cu = NULL; |
| 195 | if (idx < GetNumCompileUnits()) |
| 196 | cu = m_compile_units[idx].get(); |
| 197 | return cu; |
| 198 | } |
| 199 | |
Greg Clayton | a8022fa | 2012-04-24 21:22:41 +0000 | [diff] [blame] | 200 | bool |
| 201 | DWARFDebugInfo::ContainsCompileUnit (const DWARFCompileUnit *cu) const |
| 202 | { |
| 203 | // Not a verify efficient function, but it is handy for use in assertions |
| 204 | // to make sure that a compile unit comes from a debug information file. |
| 205 | CompileUnitColl::const_iterator end_pos = m_compile_units.end(); |
| 206 | CompileUnitColl::const_iterator pos; |
| 207 | |
| 208 | for (pos = m_compile_units.begin(); pos != end_pos; ++pos) |
| 209 | { |
| 210 | if (pos->get() == cu) |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | static int |
| 217 | CompareDWARFCompileUnitSPOffset (const void *key, const void *arrmem) |
| 218 | { |
| 219 | const dw_offset_t key_cu_offset = *(dw_offset_t*) key; |
| 220 | const dw_offset_t cu_offset = ((DWARFCompileUnitSP *)arrmem)->get()->GetOffset(); |
| 221 | if (key_cu_offset < cu_offset) |
| 222 | return -1; |
| 223 | if (key_cu_offset > cu_offset) |
| 224 | return 1; |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | DWARFCompileUnitSP |
| 229 | DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, uint32_t* idx_ptr) |
| 230 | { |
| 231 | DWARFCompileUnitSP cu_sp; |
| 232 | uint32_t cu_idx = DW_INVALID_INDEX; |
| 233 | if (cu_offset != DW_INVALID_OFFSET) |
| 234 | { |
| 235 | ParseCompileUnitHeadersIfNeeded(); |
| 236 | |
| 237 | DWARFCompileUnitSP* match = (DWARFCompileUnitSP*)bsearch(&cu_offset, &m_compile_units[0], m_compile_units.size(), sizeof(DWARFCompileUnitSP), CompareDWARFCompileUnitSPOffset); |
| 238 | if (match) |
| 239 | { |
| 240 | cu_sp = *match; |
| 241 | cu_idx = match - &m_compile_units[0]; |
| 242 | } |
| 243 | } |
| 244 | if (idx_ptr) |
| 245 | *idx_ptr = cu_idx; |
| 246 | return cu_sp; |
| 247 | } |
| 248 | |
| 249 | DWARFCompileUnitSP |
| 250 | DWARFDebugInfo::GetCompileUnitContainingDIE(dw_offset_t die_offset) |
| 251 | { |
| 252 | DWARFCompileUnitSP cu_sp; |
| 253 | if (die_offset != DW_INVALID_OFFSET) |
| 254 | { |
| 255 | ParseCompileUnitHeadersIfNeeded(); |
| 256 | |
| 257 | CompileUnitColl::const_iterator end_pos = m_compile_units.end(); |
| 258 | CompileUnitColl::const_iterator pos; |
| 259 | |
| 260 | for (pos = m_compile_units.begin(); pos != end_pos; ++pos) |
| 261 | { |
| 262 | dw_offset_t cu_start_offset = (*pos)->GetOffset(); |
| 263 | dw_offset_t cu_end_offset = (*pos)->GetNextCompileUnitOffset(); |
| 264 | if (cu_start_offset <= die_offset && die_offset < cu_end_offset) |
| 265 | { |
| 266 | cu_sp = *pos; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | return cu_sp; |
| 272 | } |
| 273 | |
| 274 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | // GetDIE() |
| 276 | // |
| 277 | // Get the DIE (Debug Information Entry) with the specified offset. |
| 278 | //---------------------------------------------------------------------- |
| 279 | DWARFDebugInfoEntry* |
| 280 | DWARFDebugInfo::GetDIEPtr(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr) |
| 281 | { |
| 282 | DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset)); |
| 283 | if (cu_sp_ptr) |
| 284 | *cu_sp_ptr = cu_sp; |
| 285 | if (cu_sp.get()) |
| 286 | return cu_sp->GetDIEPtr(die_offset); |
| 287 | return NULL; // Not found in any compile units |
| 288 | } |
| 289 | |
Greg Clayton | c662ec8 | 2011-06-17 22:10:16 +0000 | [diff] [blame] | 290 | DWARFDebugInfoEntry* |
| 291 | DWARFDebugInfo::GetDIEPtrWithCompileUnitHint (dw_offset_t die_offset, DWARFCompileUnit**cu_handle) |
| 292 | { |
| 293 | assert (cu_handle); |
| 294 | DWARFDebugInfoEntry* die = NULL; |
| 295 | if (*cu_handle) |
| 296 | die = (*cu_handle)->GetDIEPtr(die_offset); |
| 297 | |
| 298 | if (die == NULL) |
| 299 | { |
| 300 | DWARFCompileUnitSP cu_sp (GetCompileUnitContainingDIE(die_offset)); |
| 301 | if (cu_sp.get()) |
| 302 | { |
| 303 | *cu_handle = cu_sp.get(); |
| 304 | die = cu_sp->GetDIEPtr(die_offset); |
| 305 | } |
| 306 | } |
| 307 | if (die == NULL) |
| 308 | *cu_handle = NULL; |
| 309 | return die; |
| 310 | } |
| 311 | |
| 312 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | const DWARFDebugInfoEntry* |
| 314 | DWARFDebugInfo::GetDIEPtrContainingOffset(dw_offset_t die_offset, DWARFCompileUnitSP* cu_sp_ptr) |
| 315 | { |
| 316 | DWARFCompileUnitSP cu_sp(GetCompileUnitContainingDIE(die_offset)); |
| 317 | if (cu_sp_ptr) |
| 318 | *cu_sp_ptr = cu_sp; |
| 319 | if (cu_sp.get()) |
| 320 | return cu_sp->GetDIEPtrContainingOffset(die_offset); |
| 321 | |
| 322 | return NULL; // Not found in any compile units |
| 323 | |
| 324 | } |
| 325 | |
| 326 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | // AddCompileUnit |
| 328 | //---------------------------------------------------------------------- |
| 329 | void |
| 330 | DWARFDebugInfo::AddCompileUnit(DWARFCompileUnitSP& cu) |
| 331 | { |
| 332 | m_compile_units.push_back(cu); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | void |
| 337 | DWARFDebugInfo::AddDIE(DWARFDebugInfoEntry& die) |
| 338 | { |
| 339 | m_die_array.push_back(die); |
| 340 | } |
| 341 | */ |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | //---------------------------------------------------------------------- |
| 347 | // Parse |
| 348 | // |
| 349 | // Parses the .debug_info section and uses the .debug_abbrev section |
| 350 | // and various other sections in the SymbolFileDWARF class and calls the |
| 351 | // supplied callback function each time a compile unit header, or debug |
| 352 | // information entry is successfully parsed. This function can be used |
| 353 | // for different tasks such as parsing the file contents into a |
| 354 | // structured data, dumping, verifying and much more. |
| 355 | //---------------------------------------------------------------------- |
| 356 | void |
| 357 | DWARFDebugInfo::Parse(SymbolFileDWARF* dwarf2Data, Callback callback, void* userData) |
| 358 | { |
| 359 | if (dwarf2Data) |
| 360 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 361 | lldb::offset_t offset = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | uint32_t depth = 0; |
| 363 | DWARFCompileUnitSP cu(new DWARFCompileUnit(dwarf2Data)); |
| 364 | if (cu.get() == NULL) |
| 365 | return; |
| 366 | DWARFDebugInfoEntry die; |
| 367 | |
| 368 | while (cu->Extract(dwarf2Data->get_debug_info_data(), &offset)) |
| 369 | { |
| 370 | const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset(); |
| 371 | |
| 372 | depth = 0; |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 373 | // Call the callback function with no DIE pointer for the compile unit |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 374 | // and get the offset that we are to continue to parse from |
| 375 | offset = callback(dwarf2Data, cu, NULL, offset, depth, userData); |
| 376 | |
| 377 | // Make sure we are within our compile unit |
| 378 | if (offset < next_cu_offset) |
| 379 | { |
| 380 | // We are in our compile unit, parse starting at the offset |
| 381 | // we were told to parse |
| 382 | bool done = false; |
| 383 | while (!done && die.Extract(dwarf2Data, cu.get(), &offset)) |
| 384 | { |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 385 | // Call the callback function with DIE pointer that falls within the compile unit |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | offset = callback(dwarf2Data, cu, &die, offset, depth, userData); |
| 387 | |
| 388 | if (die.IsNULL()) |
| 389 | { |
| 390 | if (depth) |
| 391 | --depth; |
| 392 | else |
| 393 | done = true; // We are done with this compile unit! |
| 394 | } |
| 395 | else if (die.HasChildren()) |
| 396 | ++depth; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Make sure the offset returned is valid, and if not stop parsing. |
| 401 | // Returning DW_INVALID_OFFSET from this callback is a good way to end |
| 402 | // all parsing |
| 403 | if (!dwarf2Data->get_debug_info_data().ValidOffset(offset)) |
| 404 | break; |
| 405 | |
| 406 | // See if during the callback anyone retained a copy of the compile |
| 407 | // unit other than ourselves and if so, let whomever did own the object |
| 408 | // and create a new one for our own use! |
| 409 | if (!cu.unique()) |
| 410 | cu.reset(new DWARFCompileUnit(dwarf2Data)); |
| 411 | |
| 412 | |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 413 | // Make sure we start on a proper |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 414 | offset = next_cu_offset; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | typedef struct DumpInfo |
| 420 | { |
| 421 | DumpInfo(Stream* init_strm, uint32_t off, uint32_t depth) : |
| 422 | strm(init_strm), |
| 423 | die_offset(off), |
| 424 | recurse_depth(depth), |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 425 | found_depth(UINT32_MAX), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 426 | found_die(false), |
| 427 | ancestors() |
| 428 | { |
| 429 | } |
| 430 | Stream* strm; |
| 431 | const uint32_t die_offset; |
| 432 | const uint32_t recurse_depth; |
| 433 | uint32_t found_depth; |
| 434 | bool found_die; |
| 435 | std::vector<DWARFDebugInfoEntry> ancestors; |
| 436 | |
| 437 | DISALLOW_COPY_AND_ASSIGN(DumpInfo); |
| 438 | } DumpInfo; |
| 439 | |
| 440 | //---------------------------------------------------------------------- |
| 441 | // DumpCallback |
| 442 | // |
| 443 | // A callback function for the static DWARFDebugInfo::Parse() function |
| 444 | // that gets called each time a compile unit header or debug information |
| 445 | // entry is successfully parsed. |
| 446 | // |
| 447 | // This function dump DWARF information and obey recurse depth and |
Greg Clayton | ed8a705 | 2010-09-18 03:37:20 +0000 | [diff] [blame] | 448 | // whether a single DIE is to be dumped (or all of the data). |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 449 | //---------------------------------------------------------------------- |
| 450 | static dw_offset_t DumpCallback |
| 451 | ( |
| 452 | SymbolFileDWARF* dwarf2Data, |
| 453 | DWARFCompileUnitSP& cu_sp, |
| 454 | DWARFDebugInfoEntry* die, |
| 455 | const dw_offset_t next_offset, |
| 456 | const uint32_t curr_depth, |
| 457 | void* userData |
| 458 | ) |
| 459 | { |
| 460 | DumpInfo* dumpInfo = (DumpInfo*)userData; |
| 461 | |
| 462 | const DWARFCompileUnit* cu = cu_sp.get(); |
| 463 | |
| 464 | Stream *s = dumpInfo->strm; |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 465 | bool show_parents = s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | |
| 467 | if (die) |
| 468 | { |
| 469 | // Are we dumping everything? |
| 470 | if (dumpInfo->die_offset == DW_INVALID_OFFSET) |
| 471 | { |
| 472 | // Yes we are dumping everything. Obey our recurse level though |
| 473 | if (curr_depth < dumpInfo->recurse_depth) |
Jim Ingham | d3d25d9 | 2011-08-27 01:24:54 +0000 | [diff] [blame] | 474 | die->Dump(dwarf2Data, cu, *s, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | } |
| 476 | else |
| 477 | { |
| 478 | // We are dumping a specific DIE entry by offset |
| 479 | if (dumpInfo->die_offset == die->GetOffset()) |
| 480 | { |
| 481 | // We found the DIE we were looking for, dump it! |
| 482 | if (show_parents) |
| 483 | { |
| 484 | s->SetIndentLevel(0); |
| 485 | const uint32_t num_ancestors = dumpInfo->ancestors.size(); |
| 486 | if (num_ancestors > 0) |
| 487 | { |
| 488 | for (uint32_t i=0; i<num_ancestors-1; ++i) |
| 489 | { |
Jim Ingham | d3d25d9 | 2011-08-27 01:24:54 +0000 | [diff] [blame] | 490 | dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 491 | s->IndentMore(); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | dumpInfo->found_depth = curr_depth; |
| 497 | |
Jim Ingham | d3d25d9 | 2011-08-27 01:24:54 +0000 | [diff] [blame] | 498 | die->Dump(dwarf2Data, cu, *s, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 499 | |
| 500 | // Note that we found the DIE we were looking for |
| 501 | dumpInfo->found_die = true; |
| 502 | |
| 503 | // Since we are dumping a single DIE, if there are no children we are done! |
| 504 | if (!die->HasChildren() || dumpInfo->recurse_depth == 0) |
| 505 | return DW_INVALID_OFFSET; // Return an invalid address to end parsing |
| 506 | } |
| 507 | else if (dumpInfo->found_die) |
| 508 | { |
| 509 | // Are we done with all the children? |
| 510 | if (curr_depth <= dumpInfo->found_depth) |
| 511 | return DW_INVALID_OFFSET; |
| 512 | |
| 513 | // We have already found our DIE and are printing it's children. Obey |
| 514 | // our recurse depth and return an invalid offset if we get done |
Bruce Mitchener | aaa0ba3 | 2014-07-08 18:05:41 +0000 | [diff] [blame] | 515 | // dumping all of the children |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 516 | if (dumpInfo->recurse_depth == UINT32_MAX || curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth) |
Jim Ingham | d3d25d9 | 2011-08-27 01:24:54 +0000 | [diff] [blame] | 517 | die->Dump(dwarf2Data, cu, *s, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | } |
| 519 | else if (dumpInfo->die_offset > die->GetOffset()) |
| 520 | { |
| 521 | if (show_parents) |
| 522 | dumpInfo->ancestors.back() = *die; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Keep up with our indent level |
| 527 | if (die->IsNULL()) |
| 528 | { |
| 529 | if (show_parents) |
| 530 | dumpInfo->ancestors.pop_back(); |
| 531 | |
| 532 | if (curr_depth <= 1) |
| 533 | return cu->GetNextCompileUnitOffset(); |
| 534 | else |
| 535 | s->IndentLess(); |
| 536 | } |
| 537 | else if (die->HasChildren()) |
| 538 | { |
| 539 | if (show_parents) |
| 540 | { |
| 541 | DWARFDebugInfoEntry null_die; |
| 542 | dumpInfo->ancestors.push_back(null_die); |
| 543 | } |
| 544 | s->IndentMore(); |
| 545 | } |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | if (cu == NULL) |
| 550 | s->PutCString("NULL - cu"); |
| 551 | // We have a compile unit, reset our indent level to zero just in case |
| 552 | s->SetIndentLevel(0); |
| 553 | |
| 554 | // See if we are dumping everything? |
| 555 | if (dumpInfo->die_offset == DW_INVALID_OFFSET) |
| 556 | { |
| 557 | // We are dumping everything |
Jason Molenda | d91a662 | 2014-10-15 03:07:39 +0000 | [diff] [blame] | 558 | if (cu) |
Jason Molenda | 4c9bd87 | 2014-10-16 01:21:25 +0000 | [diff] [blame] | 559 | { |
Jason Molenda | d91a662 | 2014-10-15 03:07:39 +0000 | [diff] [blame] | 560 | cu->Dump(s); |
Jason Molenda | 4c9bd87 | 2014-10-16 01:21:25 +0000 | [diff] [blame] | 561 | return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this Compile Unit |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | return DW_INVALID_OFFSET; |
| 566 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | } |
| 568 | else |
| 569 | { |
| 570 | if (show_parents) |
| 571 | { |
| 572 | dumpInfo->ancestors.clear(); |
| 573 | dumpInfo->ancestors.resize(1); |
| 574 | } |
| 575 | |
| 576 | // We are dumping only a single DIE possibly with it's children and |
| 577 | // we must find it's compile unit before we can dump it properly |
Jason Molenda | d91a662 | 2014-10-15 03:07:39 +0000 | [diff] [blame] | 578 | if (cu && dumpInfo->die_offset < cu->GetFirstDIEOffset()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 579 | { |
| 580 | // Not found, maybe the DIE offset provided wasn't correct? |
| 581 | // *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << " was not found." << endl; |
| 582 | return DW_INVALID_OFFSET; |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | // See if the DIE is in this compile unit? |
Jason Molenda | 4c9bd87 | 2014-10-16 01:21:25 +0000 | [diff] [blame] | 587 | if (cu && dumpInfo->die_offset < cu->GetNextCompileUnitOffset()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | { |
| 589 | // This DIE is in this compile unit! |
| 590 | if (s->GetVerbose()) |
| 591 | cu->Dump(s); // Dump the compile unit for the DIE in verbose mode |
| 592 | |
| 593 | return next_offset; |
| 594 | // // We found our compile unit that contains our DIE, just skip to dumping the requested DIE... |
| 595 | // return dumpInfo->die_offset; |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | // Skip to the next compile unit as the DIE isn't in the current one! |
Jason Molenda | 4c9bd87 | 2014-10-16 01:21:25 +0000 | [diff] [blame] | 600 | if (cu) |
| 601 | { |
| 602 | return cu->GetNextCompileUnitOffset(); |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | return DW_INVALID_OFFSET; |
| 607 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // Just return the current offset to parse the next CU or DIE entry |
| 614 | return next_offset; |
| 615 | } |
| 616 | |
| 617 | //---------------------------------------------------------------------- |
| 618 | // Dump |
| 619 | // |
| 620 | // Dump the information in the .debug_info section to the specified |
| 621 | // ostream. If die_offset is valid, a single DIE will be dumped. If the |
| 622 | // die_offset is invalid, all the DWARF information will be dumped. Both |
| 623 | // cases will obey a "recurse_depth" or how deep to traverse into the |
| 624 | // children of each DIE entry. A recurse_depth of zero will dump all |
| 625 | // compile unit headers. A recurse_depth of 1 will dump all compile unit |
| 626 | // headers and the DW_TAG_compile unit tags. A depth of 2 will also |
| 627 | // dump all types and functions. |
| 628 | //---------------------------------------------------------------------- |
| 629 | void |
| 630 | DWARFDebugInfo::Dump |
| 631 | ( |
| 632 | Stream *s, |
| 633 | SymbolFileDWARF* dwarf2Data, |
| 634 | const uint32_t die_offset, |
| 635 | const uint32_t recurse_depth |
| 636 | ) |
| 637 | { |
| 638 | DumpInfo dumpInfo(s, die_offset, recurse_depth); |
| 639 | s->PutCString(".debug_info contents"); |
| 640 | if (dwarf2Data->get_debug_info_data().GetByteSize() > 0) |
| 641 | { |
| 642 | if (die_offset == DW_INVALID_OFFSET) |
| 643 | s->PutCString(":\n"); |
| 644 | else |
| 645 | { |
| 646 | s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset); |
Greg Clayton | 016a95e | 2010-09-14 02:20:48 +0000 | [diff] [blame] | 647 | if (recurse_depth != UINT32_MAX) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | s->Printf(" recursing %u levels deep.", recurse_depth); |
| 649 | s->EOL(); |
| 650 | } |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | s->PutCString(": < EMPTY >\n"); |
| 655 | return; |
| 656 | } |
| 657 | DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | //---------------------------------------------------------------------- |
| 662 | // Dump |
| 663 | // |
| 664 | // Dump the contents of this DWARFDebugInfo object as has been parsed |
| 665 | // and/or modified after it has been parsed. |
| 666 | //---------------------------------------------------------------------- |
| 667 | void |
| 668 | DWARFDebugInfo::Dump (Stream *s, const uint32_t die_offset, const uint32_t recurse_depth) |
| 669 | { |
| 670 | DumpInfo dumpInfo(s, die_offset, recurse_depth); |
| 671 | |
| 672 | s->PutCString("Dumping .debug_info section from internal representation\n"); |
| 673 | |
| 674 | CompileUnitColl::const_iterator pos; |
| 675 | uint32_t curr_depth = 0; |
| 676 | ParseCompileUnitHeadersIfNeeded(); |
| 677 | for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos) |
| 678 | { |
| 679 | const DWARFCompileUnitSP& cu_sp = *pos; |
| 680 | DumpCallback(m_dwarf2Data, (DWARFCompileUnitSP&)cu_sp, NULL, 0, curr_depth, &dumpInfo); |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 681 | |
| 682 | const DWARFDebugInfoEntry* die = cu_sp->DIE(); |
| 683 | if (die) |
| 684 | die->Dump(m_dwarf2Data, cu_sp.get(), *s, recurse_depth); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
| 688 | |
| 689 | //---------------------------------------------------------------------- |
| 690 | // FindCallbackString |
| 691 | // |
| 692 | // A callback function for the static DWARFDebugInfo::Parse() function |
| 693 | // that gets called each time a compile unit header or debug information |
| 694 | // entry is successfully parsed. |
| 695 | // |
| 696 | // This function will find the die_offset of any items whose DW_AT_name |
| 697 | // matches the given string |
| 698 | //---------------------------------------------------------------------- |
| 699 | typedef struct FindCallbackStringInfoTag |
| 700 | { |
| 701 | const char* name; |
| 702 | bool ignore_case; |
| 703 | RegularExpression* regex; |
| 704 | vector<dw_offset_t>& die_offsets; |
| 705 | } FindCallbackStringInfo; |
| 706 | |
| 707 | static dw_offset_t FindCallbackString |
| 708 | ( |
| 709 | SymbolFileDWARF* dwarf2Data, |
| 710 | DWARFCompileUnitSP& cu_sp, |
| 711 | DWARFDebugInfoEntry* die, |
| 712 | const dw_offset_t next_offset, |
| 713 | const uint32_t curr_depth, |
| 714 | void* userData |
| 715 | ) |
| 716 | { |
| 717 | FindCallbackStringInfo* info = (FindCallbackStringInfo*)userData; |
| 718 | const DWARFCompileUnit* cu = cu_sp.get(); |
| 719 | |
| 720 | if (die) |
| 721 | { |
| 722 | const char* die_name = die->GetName(dwarf2Data, cu); |
| 723 | if (die_name) |
| 724 | { |
| 725 | if (info->regex) |
| 726 | { |
| 727 | if (info->regex->Execute(die_name)) |
| 728 | info->die_offsets.push_back(die->GetOffset()); |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | if ((info->ignore_case ? strcasecmp(die_name, info->name) : strcmp(die_name, info->name)) == 0) |
| 733 | info->die_offsets.push_back(die->GetOffset()); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // Just return the current offset to parse the next CU or DIE entry |
| 739 | return next_offset; |
| 740 | } |
| 741 | |
| 742 | //---------------------------------------------------------------------- |
| 743 | // Find |
| 744 | // |
| 745 | // Finds all DIE that have a specific DW_AT_name attribute by manually |
| 746 | // searching through the debug information (not using the |
| 747 | // .debug_pubnames section). The string must match the entire name |
| 748 | // and case sensitive searches are an option. |
| 749 | //---------------------------------------------------------------------- |
| 750 | bool |
| 751 | DWARFDebugInfo::Find(const char* name, bool ignore_case, vector<dw_offset_t>& die_offsets) const |
| 752 | { |
| 753 | die_offsets.clear(); |
| 754 | if (name && name[0]) |
| 755 | { |
| 756 | FindCallbackStringInfo info = { name, ignore_case, NULL, die_offsets }; |
| 757 | DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info); |
| 758 | } |
| 759 | return !die_offsets.empty(); |
| 760 | } |
| 761 | |
| 762 | //---------------------------------------------------------------------- |
| 763 | // Find |
| 764 | // |
| 765 | // Finds all DIE that have a specific DW_AT_name attribute by manually |
| 766 | // searching through the debug information (not using the |
| 767 | // .debug_pubnames section). The string must match the supplied regular |
| 768 | // expression. |
| 769 | //---------------------------------------------------------------------- |
| 770 | bool |
| 771 | DWARFDebugInfo::Find(RegularExpression& re, vector<dw_offset_t>& die_offsets) const |
| 772 | { |
| 773 | die_offsets.clear(); |
| 774 | FindCallbackStringInfo info = { NULL, false, &re, die_offsets }; |
| 775 | DWARFDebugInfo::Parse(m_dwarf2Data, FindCallbackString, &info); |
| 776 | return !die_offsets.empty(); |
| 777 | } |