1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
//===-- DWARFDebugInfo.cpp --------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "SymbolFileDWARF.h"
#include <algorithm>
#include <set>
#include "lldb/Host/PosixApi.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include "DWARFCompileUnit.h"
#include "DWARFDebugAranges.h"
#include "DWARFDebugAranges.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugInfoEntry.h"
#include "DWARFFormValue.h"
#include "LogChannelDWARF.h"
using namespace lldb;
using namespace lldb_private;
using namespace std;
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
DWARFDebugInfo::DWARFDebugInfo()
: m_dwarf2Data(NULL), m_compile_units(), m_cu_aranges_ap() {}
//----------------------------------------------------------------------
// SetDwarfData
//----------------------------------------------------------------------
void DWARFDebugInfo::SetDwarfData(SymbolFileDWARF *dwarf2Data) {
m_dwarf2Data = dwarf2Data;
m_compile_units.clear();
}
DWARFDebugAranges &DWARFDebugInfo::GetCompileUnitAranges() {
if (m_cu_aranges_ap.get() == NULL && m_dwarf2Data) {
Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
m_cu_aranges_ap.reset(new DWARFDebugAranges());
const DWARFDataExtractor &debug_aranges_data =
m_dwarf2Data->get_debug_aranges_data();
if (debug_aranges_data.GetByteSize() > 0) {
if (log)
log->Printf(
"DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" from "
".debug_aranges",
m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
m_cu_aranges_ap->Extract(debug_aranges_data);
}
// Make a list of all CUs represented by the arange data in the file.
std::set<dw_offset_t> cus_with_data;
for (size_t n = 0; n < m_cu_aranges_ap.get()->GetNumRanges(); n++) {
dw_offset_t offset = m_cu_aranges_ap.get()->OffsetAtIndex(n);
if (offset != DW_INVALID_OFFSET)
cus_with_data.insert(offset);
}
// Manually build arange data for everything that wasn't in the
// .debug_aranges table.
bool printed = false;
const size_t num_compile_units = GetNumCompileUnits();
for (size_t idx = 0; idx < num_compile_units; ++idx) {
DWARFCompileUnit *cu = GetCompileUnitAtIndex(idx);
dw_offset_t offset = cu->GetOffset();
if (cus_with_data.find(offset) == cus_with_data.end()) {
if (log) {
if (!printed)
log->Printf(
"DWARFDebugInfo::GetCompileUnitAranges() for \"%s\" by parsing",
m_dwarf2Data->GetObjectFile()->GetFileSpec().GetPath().c_str());
printed = true;
}
cu->BuildAddressRangeTable(m_dwarf2Data, m_cu_aranges_ap.get());
}
}
const bool minimize = true;
m_cu_aranges_ap->Sort(minimize);
}
return *m_cu_aranges_ap.get();
}
void DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded() {
if (m_compile_units.empty()) {
if (m_dwarf2Data != NULL) {
lldb::offset_t offset = 0;
DWARFCompileUnitSP cu_sp;
while ((cu_sp = DWARFCompileUnit::Extract(m_dwarf2Data, &offset))) {
m_compile_units.push_back(cu_sp);
offset = cu_sp->GetNextCompileUnitOffset();
}
}
}
}
size_t DWARFDebugInfo::GetNumCompileUnits() {
ParseCompileUnitHeadersIfNeeded();
return m_compile_units.size();
}
DWARFCompileUnit *DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx) {
DWARFCompileUnit *cu = NULL;
if (idx < GetNumCompileUnits())
cu = m_compile_units[idx].get();
return cu;
}
bool DWARFDebugInfo::ContainsCompileUnit(const DWARFCompileUnit *cu) const {
// Not a verify efficient function, but it is handy for use in assertions
// to make sure that a compile unit comes from a debug information file.
CompileUnitColl::const_iterator end_pos = m_compile_units.end();
CompileUnitColl::const_iterator pos;
for (pos = m_compile_units.begin(); pos != end_pos; ++pos) {
if (pos->get() == cu)
return true;
}
return false;
}
bool DWARFDebugInfo::OffsetLessThanCompileUnitOffset(
dw_offset_t offset, const DWARFCompileUnitSP &cu_sp) {
return offset < cu_sp->GetOffset();
}
DWARFCompileUnit *DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset,
uint32_t *idx_ptr) {
DWARFCompileUnitSP cu_sp;
uint32_t cu_idx = DW_INVALID_INDEX;
if (cu_offset != DW_INVALID_OFFSET) {
ParseCompileUnitHeadersIfNeeded();
// Watch out for single compile unit executable as they are pretty common
const size_t num_cus = m_compile_units.size();
if (num_cus == 1) {
if (m_compile_units[0]->GetOffset() == cu_offset) {
cu_sp = m_compile_units[0];
cu_idx = 0;
}
} else if (num_cus) {
CompileUnitColl::const_iterator end_pos = m_compile_units.end();
CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
CompileUnitColl::const_iterator pos = std::upper_bound(
begin_pos, end_pos, cu_offset, OffsetLessThanCompileUnitOffset);
if (pos != begin_pos) {
--pos;
if ((*pos)->GetOffset() == cu_offset) {
cu_sp = *pos;
cu_idx = std::distance(begin_pos, pos);
}
}
}
}
if (idx_ptr)
*idx_ptr = cu_idx;
return cu_sp.get();
}
DWARFCompileUnit *DWARFDebugInfo::GetCompileUnit(const DIERef &die_ref) {
if (die_ref.cu_offset == DW_INVALID_OFFSET)
return GetCompileUnitContainingDIEOffset(die_ref.die_offset);
else
return GetCompileUnit(die_ref.cu_offset);
}
DWARFCompileUnit *
DWARFDebugInfo::GetCompileUnitContainingDIEOffset(dw_offset_t die_offset) {
ParseCompileUnitHeadersIfNeeded();
DWARFCompileUnitSP cu_sp;
// Watch out for single compile unit executable as they are pretty common
const size_t num_cus = m_compile_units.size();
if (num_cus == 1) {
if (m_compile_units[0]->ContainsDIEOffset(die_offset))
return m_compile_units[0].get();
} else if (num_cus) {
CompileUnitColl::const_iterator end_pos = m_compile_units.end();
CompileUnitColl::const_iterator begin_pos = m_compile_units.begin();
CompileUnitColl::const_iterator pos = std::upper_bound(
begin_pos, end_pos, die_offset, OffsetLessThanCompileUnitOffset);
if (pos != begin_pos) {
--pos;
if ((*pos)->ContainsDIEOffset(die_offset))
return (*pos).get();
}
}
return nullptr;
}
DWARFDIE
DWARFDebugInfo::GetDIEForDIEOffset(dw_offset_t die_offset) {
DWARFCompileUnit *cu = GetCompileUnitContainingDIEOffset(die_offset);
if (cu)
return cu->GetDIE(die_offset);
return DWARFDIE();
}
//----------------------------------------------------------------------
// GetDIE()
//
// Get the DIE (Debug Information Entry) with the specified offset.
//----------------------------------------------------------------------
DWARFDIE
DWARFDebugInfo::GetDIE(const DIERef &die_ref) {
DWARFCompileUnit *cu = GetCompileUnit(die_ref);
if (cu)
return cu->GetDIE(die_ref.die_offset);
return DWARFDIE(); // Not found
}
//----------------------------------------------------------------------
// Parse
//
// Parses the .debug_info section and uses the .debug_abbrev section
// and various other sections in the SymbolFileDWARF class and calls the
// supplied callback function each time a compile unit header, or debug
// information entry is successfully parsed. This function can be used
// for different tasks such as parsing the file contents into a
// structured data, dumping, verifying and much more.
//----------------------------------------------------------------------
void DWARFDebugInfo::Parse(SymbolFileDWARF *dwarf2Data, Callback callback,
void *userData) {
if (dwarf2Data) {
lldb::offset_t offset = 0;
uint32_t depth = 0;
DWARFDebugInfoEntry die;
DWARFCompileUnitSP cu;
while ((cu = DWARFCompileUnit::Extract(dwarf2Data, &offset))) {
const dw_offset_t next_cu_offset = cu->GetNextCompileUnitOffset();
depth = 0;
// Call the callback function with no DIE pointer for the compile unit
// and get the offset that we are to continue to parse from
offset = callback(dwarf2Data, cu.get(), NULL, offset, depth, userData);
// Make sure we are within our compile unit
if (offset < next_cu_offset) {
// We are in our compile unit, parse starting at the offset
// we were told to parse
bool done = false;
while (!done && die.Extract(dwarf2Data, cu.get(), &offset)) {
// Call the callback function with DIE pointer that falls within the
// compile unit
offset =
callback(dwarf2Data, cu.get(), &die, offset, depth, userData);
if (die.IsNULL()) {
if (depth)
--depth;
else
done = true; // We are done with this compile unit!
} else if (die.HasChildren())
++depth;
}
}
// Make sure the offset returned is valid, and if not stop parsing.
// Returning DW_INVALID_OFFSET from this callback is a good way to end
// all parsing
if (!dwarf2Data->get_debug_info_data().ValidOffset(offset))
break;
// Make sure we start on a proper
offset = next_cu_offset;
}
}
}
typedef struct DumpInfo {
DumpInfo(Stream *init_strm, uint32_t off, uint32_t depth)
: strm(init_strm), die_offset(off), recurse_depth(depth),
found_depth(UINT32_MAX), found_die(false), ancestors() {}
Stream *strm;
const uint32_t die_offset;
const uint32_t recurse_depth;
uint32_t found_depth;
bool found_die;
std::vector<DWARFDebugInfoEntry> ancestors;
DISALLOW_COPY_AND_ASSIGN(DumpInfo);
} DumpInfo;
//----------------------------------------------------------------------
// DumpCallback
//
// A callback function for the static DWARFDebugInfo::Parse() function
// that gets called each time a compile unit header or debug information
// entry is successfully parsed.
//
// This function dump DWARF information and obey recurse depth and
// whether a single DIE is to be dumped (or all of the data).
//----------------------------------------------------------------------
static dw_offset_t DumpCallback(SymbolFileDWARF *dwarf2Data,
DWARFCompileUnit *cu, DWARFDebugInfoEntry *die,
const dw_offset_t next_offset,
const uint32_t curr_depth, void *userData) {
DumpInfo *dumpInfo = (DumpInfo *)userData;
Stream *s = dumpInfo->strm;
bool show_parents =
s->GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowAncestors);
if (die) {
// Are we dumping everything?
if (dumpInfo->die_offset == DW_INVALID_OFFSET) {
// Yes we are dumping everything. Obey our recurse level though
if (curr_depth < dumpInfo->recurse_depth)
die->Dump(dwarf2Data, cu, *s, 0);
} else {
// We are dumping a specific DIE entry by offset
if (dumpInfo->die_offset == die->GetOffset()) {
// We found the DIE we were looking for, dump it!
if (show_parents) {
s->SetIndentLevel(0);
const uint32_t num_ancestors = dumpInfo->ancestors.size();
if (num_ancestors > 0) {
for (uint32_t i = 0; i < num_ancestors - 1; ++i) {
dumpInfo->ancestors[i].Dump(dwarf2Data, cu, *s, 0);
s->IndentMore();
}
}
}
dumpInfo->found_depth = curr_depth;
die->Dump(dwarf2Data, cu, *s, 0);
// Note that we found the DIE we were looking for
dumpInfo->found_die = true;
// Since we are dumping a single DIE, if there are no children we are
// done!
if (!die->HasChildren() || dumpInfo->recurse_depth == 0)
return DW_INVALID_OFFSET; // Return an invalid address to end parsing
} else if (dumpInfo->found_die) {
// Are we done with all the children?
if (curr_depth <= dumpInfo->found_depth)
return DW_INVALID_OFFSET;
// We have already found our DIE and are printing it's children. Obey
// our recurse depth and return an invalid offset if we get done
// dumping all of the children
if (dumpInfo->recurse_depth == UINT32_MAX ||
curr_depth <= dumpInfo->found_depth + dumpInfo->recurse_depth)
die->Dump(dwarf2Data, cu, *s, 0);
} else if (dumpInfo->die_offset > die->GetOffset()) {
if (show_parents)
dumpInfo->ancestors.back() = *die;
}
}
// Keep up with our indent level
if (die->IsNULL()) {
if (show_parents)
dumpInfo->ancestors.pop_back();
if (curr_depth <= 1)
return cu->GetNextCompileUnitOffset();
else
s->IndentLess();
} else if (die->HasChildren()) {
if (show_parents) {
DWARFDebugInfoEntry null_die;
dumpInfo->ancestors.push_back(null_die);
}
s->IndentMore();
}
} else {
if (cu == NULL)
s->PutCString("NULL - cu");
// We have a compile unit, reset our indent level to zero just in case
s->SetIndentLevel(0);
// See if we are dumping everything?
if (dumpInfo->die_offset == DW_INVALID_OFFSET) {
// We are dumping everything
if (cu) {
cu->Dump(s);
return cu->GetFirstDIEOffset(); // Return true to parse all DIEs in this
// Compile Unit
} else {
return DW_INVALID_OFFSET;
}
} else {
if (show_parents) {
dumpInfo->ancestors.clear();
dumpInfo->ancestors.resize(1);
}
// We are dumping only a single DIE possibly with it's children and
// we must find it's compile unit before we can dump it properly
if (cu && dumpInfo->die_offset < cu->GetFirstDIEOffset()) {
// Not found, maybe the DIE offset provided wasn't correct?
// *ostrm_ptr << "DIE at offset " << HEX32 << dumpInfo->die_offset << "
// was not found." << endl;
return DW_INVALID_OFFSET;
} else {
// See if the DIE is in this compile unit?
if (cu && dumpInfo->die_offset < cu->GetNextCompileUnitOffset()) {
return next_offset;
// // We found our compile unit that contains our DIE, just skip to
// dumping the requested DIE...
// return dumpInfo->die_offset;
} else {
// Skip to the next compile unit as the DIE isn't in the current one!
if (cu) {
return cu->GetNextCompileUnitOffset();
} else {
return DW_INVALID_OFFSET;
}
}
}
}
}
// Just return the current offset to parse the next CU or DIE entry
return next_offset;
}
//----------------------------------------------------------------------
// Dump
//
// Dump the information in the .debug_info section to the specified
// ostream. If die_offset is valid, a single DIE will be dumped. If the
// die_offset is invalid, all the DWARF information will be dumped. Both
// cases will obey a "recurse_depth" or how deep to traverse into the
// children of each DIE entry. A recurse_depth of zero will dump all
// compile unit headers. A recurse_depth of 1 will dump all compile unit
// headers and the DW_TAG_compile unit tags. A depth of 2 will also
// dump all types and functions.
//----------------------------------------------------------------------
void DWARFDebugInfo::Dump(Stream *s, SymbolFileDWARF *dwarf2Data,
const uint32_t die_offset,
const uint32_t recurse_depth) {
DumpInfo dumpInfo(s, die_offset, recurse_depth);
s->PutCString(".debug_info contents");
if (dwarf2Data->get_debug_info_data().GetByteSize() > 0) {
if (die_offset == DW_INVALID_OFFSET)
s->PutCString(":\n");
else {
s->Printf(" for DIE entry at .debug_info[0x%8.8x]", die_offset);
if (recurse_depth != UINT32_MAX)
s->Printf(" recursing %u levels deep.", recurse_depth);
s->EOL();
}
} else {
s->PutCString(": < EMPTY >\n");
return;
}
DWARFDebugInfo::Parse(dwarf2Data, DumpCallback, &dumpInfo);
}
//----------------------------------------------------------------------
// Dump
//
// Dump the contents of this DWARFDebugInfo object as has been parsed
// and/or modified after it has been parsed.
//----------------------------------------------------------------------
void DWARFDebugInfo::Dump(Stream *s, const uint32_t die_offset,
const uint32_t recurse_depth) {
DumpInfo dumpInfo(s, die_offset, recurse_depth);
s->PutCString("Dumping .debug_info section from internal representation\n");
CompileUnitColl::const_iterator pos;
uint32_t curr_depth = 0;
ParseCompileUnitHeadersIfNeeded();
for (pos = m_compile_units.begin(); pos != m_compile_units.end(); ++pos) {
DWARFCompileUnit *cu = pos->get();
DumpCallback(m_dwarf2Data, cu, NULL, 0, curr_depth, &dumpInfo);
const DWARFDIE die = cu->DIE();
if (die)
die.Dump(s, recurse_depth);
}
}
|