DWARFDebugLine.cpp [plain text]
#include "DWARFDebugLine.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm;
using namespace dwarf;
void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
OS << "Line table prologue:\n"
<< format(" total_length: 0x%8.8x\n", TotalLength)
<< format(" version: %u\n", Version)
<< format("prologue_length: 0x%8.8x\n", PrologueLength)
<< format("min_inst_length: %u\n", MinInstLength)
<< format("default_is_stmt: %u\n", DefaultIsStmt)
<< format(" line_base: %i\n", LineBase)
<< format(" line_range: %u\n", LineRange)
<< format(" opcode_base: %u\n", OpcodeBase);
for (uint32_t i = 0; i < StandardOpcodeLengths.size(); ++i)
OS << format("standard_opcode_lengths[%s] = %u\n", LNStandardString(i+1),
StandardOpcodeLengths[i]);
if (!IncludeDirectories.empty())
for (uint32_t i = 0; i < IncludeDirectories.size(); ++i)
OS << format("include_directories[%3u] = '", i+1)
<< IncludeDirectories[i] << "'\n";
if (!FileNames.empty()) {
OS << " Dir Mod Time File Len File Name\n"
<< " ---- ---------- ---------- -----------"
"----------------\n";
for (uint32_t i = 0; i < FileNames.size(); ++i) {
const FileNameEntry& fileEntry = FileNames[i];
OS << format("file_names[%3u] %4" PRIu64 " ", i+1, fileEntry.DirIdx)
<< format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ",
fileEntry.ModTime, fileEntry.Length)
<< fileEntry.Name << '\n';
}
}
}
void DWARFDebugLine::Row::postAppend() {
BasicBlock = false;
PrologueEnd = false;
EpilogueBegin = false;
}
void DWARFDebugLine::Row::reset(bool default_is_stmt) {
Address = 0;
Line = 1;
Column = 0;
File = 1;
Isa = 0;
IsStmt = default_is_stmt;
BasicBlock = false;
EndSequence = false;
PrologueEnd = false;
EpilogueBegin = false;
}
void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
<< format(" %6u %3u ", File, Isa)
<< (IsStmt ? " is_stmt" : "")
<< (BasicBlock ? " basic_block" : "")
<< (PrologueEnd ? " prologue_end" : "")
<< (EpilogueBegin ? " epilogue_begin" : "")
<< (EndSequence ? " end_sequence" : "")
<< '\n';
}
void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
Prologue.dump(OS);
OS << '\n';
if (!Rows.empty()) {
OS << "Address Line Column File ISA Flags\n"
<< "------------------ ------ ------ ------ --- -------------\n";
for (std::vector<Row>::const_iterator pos = Rows.begin(),
end = Rows.end(); pos != end; ++pos)
pos->dump(OS);
}
}
DWARFDebugLine::State::~State() {}
void DWARFDebugLine::State::appendRowToMatrix(uint32_t offset) {
if (Sequence::Empty) {
Sequence::Empty = false;
Sequence::LowPC = Address;
Sequence::FirstRowIndex = row;
}
++row; LineTable::appendRow(*this);
if (EndSequence) {
Sequence::HighPC = Address;
Sequence::LastRowIndex = row;
if (Sequence::isValid())
LineTable::appendSequence(*this);
Sequence::reset();
}
Row::postAppend();
}
void DWARFDebugLine::State::finalize() {
row = DoneParsingLineTable;
if (!Sequence::Empty) {
fprintf(stderr, "warning: last sequence in debug line table is not"
"terminated!\n");
}
if (!Sequences.empty()) {
std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
}
}
DWARFDebugLine::DumpingState::~DumpingState() {}
void DWARFDebugLine::DumpingState::finalize() {
LineTable::dump(OS);
}
const DWARFDebugLine::LineTable *
DWARFDebugLine::getLineTable(uint32_t offset) const {
LineTableConstIter pos = LineTableMap.find(offset);
if (pos != LineTableMap.end())
return &pos->second;
return 0;
}
const DWARFDebugLine::LineTable *
DWARFDebugLine::getOrParseLineTable(DataExtractor debug_line_data,
uint32_t offset) {
std::pair<LineTableIter, bool> pos =
LineTableMap.insert(LineTableMapTy::value_type(offset, LineTable()));
if (pos.second) {
State state;
if (!parseStatementTable(debug_line_data, RelocMap, &offset, state))
return 0;
pos.first->second = state;
}
return &pos.first->second;
}
bool
DWARFDebugLine::parsePrologue(DataExtractor debug_line_data,
uint32_t *offset_ptr, Prologue *prologue) {
const uint32_t prologue_offset = *offset_ptr;
prologue->clear();
prologue->TotalLength = debug_line_data.getU32(offset_ptr);
prologue->Version = debug_line_data.getU16(offset_ptr);
if (prologue->Version != 2)
return false;
prologue->PrologueLength = debug_line_data.getU32(offset_ptr);
const uint32_t end_prologue_offset = prologue->PrologueLength + *offset_ptr;
prologue->MinInstLength = debug_line_data.getU8(offset_ptr);
prologue->DefaultIsStmt = debug_line_data.getU8(offset_ptr);
prologue->LineBase = debug_line_data.getU8(offset_ptr);
prologue->LineRange = debug_line_data.getU8(offset_ptr);
prologue->OpcodeBase = debug_line_data.getU8(offset_ptr);
prologue->StandardOpcodeLengths.reserve(prologue->OpcodeBase-1);
for (uint32_t i = 1; i < prologue->OpcodeBase; ++i) {
uint8_t op_len = debug_line_data.getU8(offset_ptr);
prologue->StandardOpcodeLengths.push_back(op_len);
}
while (*offset_ptr < end_prologue_offset) {
const char *s = debug_line_data.getCStr(offset_ptr);
if (s && s[0])
prologue->IncludeDirectories.push_back(s);
else
break;
}
while (*offset_ptr < end_prologue_offset) {
const char *name = debug_line_data.getCStr(offset_ptr);
if (name && name[0]) {
FileNameEntry fileEntry;
fileEntry.Name = name;
fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
prologue->FileNames.push_back(fileEntry);
} else {
break;
}
}
if (*offset_ptr != end_prologue_offset) {
fprintf(stderr, "warning: parsing line table prologue at 0x%8.8x should"
" have ended at 0x%8.8x but it ended ad 0x%8.8x\n",
prologue_offset, end_prologue_offset, *offset_ptr);
return false;
}
return true;
}
bool
DWARFDebugLine::parseStatementTable(DataExtractor debug_line_data,
const RelocAddrMap *RMap,
uint32_t *offset_ptr, State &state) {
const uint32_t debug_line_offset = *offset_ptr;
Prologue *prologue = &state.Prologue;
if (!parsePrologue(debug_line_data, offset_ptr, prologue)) {
*offset_ptr = debug_line_offset;
return false;
}
const uint32_t end_offset = debug_line_offset + prologue->TotalLength +
sizeof(prologue->TotalLength);
state.reset();
while (*offset_ptr < end_offset) {
uint8_t opcode = debug_line_data.getU8(offset_ptr);
if (opcode == 0) {
uint32_t ext_offset = *offset_ptr;
uint64_t len = debug_line_data.getULEB128(offset_ptr);
uint32_t arg_size = len - (*offset_ptr - ext_offset);
uint8_t sub_opcode = debug_line_data.getU8(offset_ptr);
switch (sub_opcode) {
case DW_LNE_end_sequence:
state.EndSequence = true;
state.appendRowToMatrix(*offset_ptr);
state.reset();
break;
case DW_LNE_set_address:
{
RelocAddrMap::const_iterator AI = RMap->find(*offset_ptr);
if (AI != RMap->end()) {
const std::pair<uint8_t, int64_t> &R = AI->second;
state.Address = debug_line_data.getAddress(offset_ptr) + R.second;
} else
state.Address = debug_line_data.getAddress(offset_ptr);
}
break;
case DW_LNE_define_file:
{
FileNameEntry fileEntry;
fileEntry.Name = debug_line_data.getCStr(offset_ptr);
fileEntry.DirIdx = debug_line_data.getULEB128(offset_ptr);
fileEntry.ModTime = debug_line_data.getULEB128(offset_ptr);
fileEntry.Length = debug_line_data.getULEB128(offset_ptr);
prologue->FileNames.push_back(fileEntry);
}
break;
default:
(*offset_ptr) += arg_size;
break;
}
} else if (opcode < prologue->OpcodeBase) {
switch (opcode) {
case DW_LNS_copy:
state.appendRowToMatrix(*offset_ptr);
break;
case DW_LNS_advance_pc:
state.Address += debug_line_data.getULEB128(offset_ptr) *
prologue->MinInstLength;
break;
case DW_LNS_advance_line:
state.Line += debug_line_data.getSLEB128(offset_ptr);
break;
case DW_LNS_set_file:
state.File = debug_line_data.getULEB128(offset_ptr);
break;
case DW_LNS_set_column:
state.Column = debug_line_data.getULEB128(offset_ptr);
break;
case DW_LNS_negate_stmt:
state.IsStmt = !state.IsStmt;
break;
case DW_LNS_set_basic_block:
state.BasicBlock = true;
break;
case DW_LNS_const_add_pc:
{
uint8_t adjust_opcode = 255 - prologue->OpcodeBase;
uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
prologue->MinInstLength;
state.Address += addr_offset;
}
break;
case DW_LNS_fixed_advance_pc:
state.Address += debug_line_data.getU16(offset_ptr);
break;
case DW_LNS_set_prologue_end:
state.PrologueEnd = true;
break;
case DW_LNS_set_epilogue_begin:
state.EpilogueBegin = true;
break;
case DW_LNS_set_isa:
state.Isa = debug_line_data.getULEB128(offset_ptr);
break;
default:
{
assert(opcode - 1U < prologue->StandardOpcodeLengths.size());
uint8_t opcode_length = prologue->StandardOpcodeLengths[opcode - 1];
for (uint8_t i=0; i<opcode_length; ++i)
debug_line_data.getULEB128(offset_ptr);
}
break;
}
} else {
uint8_t adjust_opcode = opcode - prologue->OpcodeBase;
uint64_t addr_offset = (adjust_opcode / prologue->LineRange) *
prologue->MinInstLength;
int32_t line_offset = prologue->LineBase +
(adjust_opcode % prologue->LineRange);
state.Line += line_offset;
state.Address += addr_offset;
state.appendRowToMatrix(*offset_ptr);
}
}
state.finalize();
return end_offset;
}
uint32_t
DWARFDebugLine::LineTable::lookupAddress(uint64_t address) const {
uint32_t unknown_index = UINT32_MAX;
if (Sequences.empty())
return unknown_index;
DWARFDebugLine::Sequence sequence;
sequence.LowPC = address;
SequenceIter first_seq = Sequences.begin();
SequenceIter last_seq = Sequences.end();
SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
DWARFDebugLine::Sequence::orderByLowPC);
DWARFDebugLine::Sequence found_seq;
if (seq_pos == last_seq) {
found_seq = Sequences.back();
} else if (seq_pos->LowPC == address) {
found_seq = *seq_pos;
} else {
if (seq_pos == first_seq)
return unknown_index;
found_seq = *(seq_pos - 1);
}
if (!found_seq.containsPC(address))
return unknown_index;
DWARFDebugLine::Row row;
row.Address = address;
RowIter first_row = Rows.begin() + found_seq.FirstRowIndex;
RowIter last_row = Rows.begin() + found_seq.LastRowIndex;
RowIter row_pos = std::lower_bound(first_row, last_row, row,
DWARFDebugLine::Row::orderByAddress);
if (row_pos == last_row) {
return found_seq.LastRowIndex - 1;
}
uint32_t index = found_seq.FirstRowIndex + (row_pos - first_row);
if (row_pos->Address > address) {
if (row_pos == first_row)
return unknown_index;
else
index--;
}
return index;
}
bool
DWARFDebugLine::LineTable::lookupAddressRange(uint64_t address,
uint64_t size,
std::vector<uint32_t>& result) const {
if (Sequences.empty())
return false;
uint64_t end_addr = address + size;
DWARFDebugLine::Sequence sequence;
sequence.LowPC = address;
SequenceIter first_seq = Sequences.begin();
SequenceIter last_seq = Sequences.end();
SequenceIter seq_pos = std::lower_bound(first_seq, last_seq, sequence,
DWARFDebugLine::Sequence::orderByLowPC);
if (seq_pos == last_seq || seq_pos->LowPC != address) {
if (seq_pos == first_seq)
return false;
seq_pos--;
}
if (!seq_pos->containsPC(address))
return false;
SequenceIter start_pos = seq_pos;
while (seq_pos != last_seq && seq_pos->LowPC < end_addr) {
DWARFDebugLine::Sequence cur_seq = *seq_pos;
uint32_t first_row_index;
uint32_t last_row_index;
if (seq_pos == start_pos) {
DWARFDebugLine::Row row;
row.Address = address;
RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
RowIter row_pos = std::upper_bound(first_row, last_row, row,
DWARFDebugLine::Row::orderByAddress);
first_row_index = cur_seq.FirstRowIndex + (row_pos - first_row);
if (row_pos != first_row)
--first_row_index;
} else
first_row_index = cur_seq.FirstRowIndex;
if (cur_seq.HighPC > end_addr) {
DWARFDebugLine::Row row;
row.Address = end_addr;
RowIter first_row = Rows.begin() + cur_seq.FirstRowIndex;
RowIter last_row = Rows.begin() + cur_seq.LastRowIndex;
RowIter row_pos = std::upper_bound(first_row, last_row, row,
DWARFDebugLine::Row::orderByAddress);
last_row_index = cur_seq.FirstRowIndex + (row_pos - first_row) - 1;
} else
last_row_index = cur_seq.LastRowIndex - 1;
for (uint32_t i = first_row_index; i <= last_row_index; ++i) {
result.push_back(i);
}
++seq_pos;
}
return true;
}
bool
DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
bool NeedsAbsoluteFilePath,
std::string &Result) const {
if (FileIndex == 0 || FileIndex > Prologue.FileNames.size())
return false;
const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
const char *FileName = Entry.Name;
if (!NeedsAbsoluteFilePath ||
sys::path::is_absolute(FileName)) {
Result = FileName;
return true;
}
SmallString<16> FilePath;
uint64_t IncludeDirIndex = Entry.DirIdx;
if (IncludeDirIndex > 0 &&
IncludeDirIndex <= Prologue.IncludeDirectories.size()) {
const char *IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
sys::path::append(FilePath, IncludeDir);
}
sys::path::append(FilePath, FileName);
Result = FilePath.str();
return true;
}