Page MenuHomePhabricator

No OneTemporary

This file is larger than 256 KB, so syntax highlighting was skipped.
diff --git a/lldb/include/lldb/Expression/DWARFExpression.h b/lldb/include/lldb/Expression/DWARFExpression.h
index 0a7d919cc688..21830a562800 100644
--- a/lldb/include/lldb/Expression/DWARFExpression.h
+++ b/lldb/include/lldb/Expression/DWARFExpression.h
@@ -1,376 +1,322 @@
//===-- DWARFExpression.h ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_DWARFExpression_h_
#define liblldb_DWARFExpression_h_
#include "lldb/Core/Address.h"
#include "lldb/Core/Disassembler.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-private.h"
#include <functional>
class DWARFUnit;
namespace lldb_private {
/// \class DWARFExpression DWARFExpression.h
/// "lldb/Expression/DWARFExpression.h" Encapsulates a DWARF location
/// expression and interprets it.
///
/// DWARF location expressions are used in two ways by LLDB. The first
/// use is to find entities specified in the debug information, since their
/// locations are specified in precisely this language. The second is to
/// interpret expressions without having to run the target in cases where the
/// overhead from copying JIT-compiled code into the target is too high or
/// where the target cannot be run. This class encapsulates a single DWARF
/// location expression or a location list and interprets it.
class DWARFExpression {
public:
enum LocationListFormat : uint8_t {
NonLocationList, // Not a location list
RegularLocationList, // Location list format used in non-split dwarf files
SplitDwarfLocationList, // Location list format used in pre-DWARF v5 split
// dwarf files (.debug_loc.dwo)
LocLists, // Location list format used in DWARF v5
// (.debug_loclists/.debug_loclists.dwo).
};
- /// Constructor
- explicit DWARFExpression(DWARFUnit *dwarf_cu);
+ DWARFExpression();
/// Constructor
///
/// \param[in] data
/// A data extractor configured to read the DWARF location expression's
/// bytecode.
///
/// \param[in] data_offset
/// The offset of the location expression in the extractor.
///
/// \param[in] data_length
/// The byte length of the location expression.
DWARFExpression(lldb::ModuleSP module, const DataExtractor &data,
- DWARFUnit *dwarf_cu, lldb::offset_t data_offset,
+ const DWARFUnit *dwarf_cu, lldb::offset_t data_offset,
lldb::offset_t data_length);
/// Destructor
virtual ~DWARFExpression();
/// Print the description of the expression to a stream
///
/// \param[in] s
/// The stream to print to.
///
/// \param[in] level
/// The level of verbosity to use.
///
/// \param[in] location_list_base_addr
/// If this is a location list based expression, this is the
/// address of the object that owns it. NOTE: this value is
/// different from the DWARF version of the location list base
/// address which is compile unit relative. This base address
/// is the address of the object that owns the location list.
///
/// \param[in] abi
/// An optional ABI plug-in that can be used to resolve register
/// names.
void GetDescription(Stream *s, lldb::DescriptionLevel level,
lldb::addr_t location_list_base_addr, ABI *abi) const;
/// Return true if the location expression contains data
bool IsValid() const;
/// Return true if a location list was provided
bool IsLocationList() const;
/// Search for a load address in the location list
///
/// \param[in] process
/// The process to use when resolving the load address
///
/// \param[in] addr
/// The address to resolve
///
/// \return
/// True if IsLocationList() is true and the address was found;
/// false otherwise.
// bool
// LocationListContainsLoadAddress (Process* process, const Address &addr)
// const;
//
bool LocationListContainsAddress(lldb::addr_t loclist_base_addr,
lldb::addr_t addr) const;
/// If a location is not a location list, return true if the location
/// contains a DW_OP_addr () opcode in the stream that matches \a file_addr.
/// If file_addr is LLDB_INVALID_ADDRESS, the this function will return true
/// if the variable there is any DW_OP_addr in a location that (yet still is
/// NOT a location list). This helps us detect if a variable is a global or
/// static variable since there is no other indication from DWARF debug
/// info.
///
/// \param[in] op_addr_idx
/// The DW_OP_addr index to retrieve in case there is more than
/// one DW_OP_addr opcode in the location byte stream.
///
/// \param[out] error
/// If the location stream contains unknown DW_OP opcodes or the
/// data is missing, \a error will be set to \b true.
///
/// \return
/// LLDB_INVALID_ADDRESS if the location doesn't contain a
/// DW_OP_addr for \a op_addr_idx, otherwise a valid file address
lldb::addr_t GetLocation_DW_OP_addr(uint32_t op_addr_idx, bool &error) const;
bool Update_DW_OP_addr(lldb::addr_t file_addr);
+ void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size,
+ uint8_t addr_byte_size);
+
void SetModule(const lldb::ModuleSP &module) { m_module_wp = module; }
bool ContainsThreadLocalStorage() const;
bool LinkThreadLocalStorage(
lldb::ModuleSP new_module_sp,
std::function<lldb::addr_t(lldb::addr_t file_addr)> const
&link_address_callback);
- /// Make the expression parser read its location information from a given
- /// data source. Does not change the offset and length
- ///
- /// \param[in] data
- /// A data extractor configured to read the DWARF location expression's
- /// bytecode.
- void SetOpcodeData(const DataExtractor &data);
-
- /// Make the expression parser read its location information from a given
- /// data source
- ///
- /// \param[in] module_sp
- /// The module that defines the DWARF expression.
- ///
- /// \param[in] data
- /// A data extractor configured to read the DWARF location expression's
- /// bytecode.
- ///
- /// \param[in] data_offset
- /// The offset of the location expression in the extractor.
- ///
- /// \param[in] data_length
- /// The byte length of the location expression.
- void SetOpcodeData(lldb::ModuleSP module_sp, const DataExtractor &data,
- lldb::offset_t data_offset, lldb::offset_t data_length);
-
- /// Copy the DWARF location expression into a local buffer.
- ///
- /// It is a good idea to copy the data so we don't keep the entire object
- /// file worth of data around just for a few bytes of location expression.
- /// LLDB typically will mmap the entire contents of debug information files,
- /// and if we use SetOpcodeData, it will get a shared reference to all of
- /// this data for the and cause the object file to have to stay around. Even
- /// worse, a very very large ".a" that contains one or more .o files could
- /// end up being referenced. Location lists are typically small so even
- /// though we are copying the data, it shouldn't amount to that much for the
- /// variables we end up parsing.
- ///
- /// \param[in] module_sp
- /// The module that defines the DWARF expression.
- ///
- /// \param[in] data
- /// A data extractor configured to read and copy the DWARF
- /// location expression's bytecode.
- ///
- /// \param[in] data_offset
- /// The offset of the location expression in the extractor.
- ///
- /// \param[in] data_length
- /// The byte length of the location expression.
- void CopyOpcodeData(lldb::ModuleSP module_sp, const DataExtractor &data,
- lldb::offset_t data_offset, lldb::offset_t data_length);
-
- void CopyOpcodeData(const void *data, lldb::offset_t data_length,
- lldb::ByteOrder byte_order, uint8_t addr_byte_size);
-
- void CopyOpcodeData(uint64_t const_value,
- lldb::offset_t const_value_byte_size,
- uint8_t addr_byte_size);
-
/// Tells the expression that it refers to a location list.
///
/// \param[in] slide
/// This value should be a slide that is applied to any values
/// in the location list data so the values become zero based
/// offsets into the object that owns the location list. We need
/// to make location lists relative to the objects that own them
/// so we can relink addresses on the fly.
void SetLocationListSlide(lldb::addr_t slide);
/// Return the call-frame-info style register kind
int GetRegisterKind();
/// Set the call-frame-info style register kind
///
/// \param[in] reg_kind
/// The register kind.
void SetRegisterKind(lldb::RegisterKind reg_kind);
/// Wrapper for the static evaluate function that accepts an
/// ExecutionContextScope instead of an ExecutionContext and uses member
/// variables to populate many operands
bool Evaluate(ExecutionContextScope *exe_scope,
lldb::addr_t loclist_base_load_addr,
const Value *initial_value_ptr, const Value *object_address_ptr,
Value &result, Status *error_ptr) const;
/// Wrapper for the static evaluate function that uses member variables to
/// populate many operands
bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
lldb::addr_t loclist_base_load_addr,
const Value *initial_value_ptr, const Value *object_address_ptr,
Value &result, Status *error_ptr) const;
/// Evaluate a DWARF location expression in a particular context
///
/// \param[in] exe_ctx
/// The execution context in which to evaluate the location
/// expression. The location expression may access the target's
/// memory, especially if it comes from the expression parser.
///
/// \param[in] opcode_ctx
/// The module which defined the expression.
///
/// \param[in] opcodes
/// This is a static method so the opcodes need to be provided
/// explicitly.
///
/// \param[in] expr_locals
/// If the location expression was produced by the expression parser,
/// the list of local variables referenced by the DWARF expression.
/// This list should already have been populated during parsing;
/// the DWARF expression refers to variables by index. Can be NULL if
/// the location expression uses no locals.
///
/// \param[in] decl_map
/// If the location expression was produced by the expression parser,
/// the list of external variables referenced by the location
/// expression. Can be NULL if the location expression uses no
/// external variables.
///
/// \param[in] reg_ctx
/// An optional parameter which provides a RegisterContext for use
/// when evaluating the expression (i.e. for fetching register values).
/// Normally this will come from the ExecutionContext's StackFrame but
/// in the case where an expression needs to be evaluated while building
/// the stack frame list, this short-cut is available.
///
/// \param[in] offset
/// The offset of the location expression in the data extractor.
///
/// \param[in] length
/// The length in bytes of the location expression.
///
/// \param[in] reg_set
/// The call-frame-info style register kind.
///
/// \param[in] initial_value_ptr
/// A value to put on top of the interpreter stack before evaluating
/// the expression, if the expression is parametrized. Can be NULL.
///
/// \param[in] result
/// A value into which the result of evaluating the expression is
/// to be placed.
///
/// \param[in] error_ptr
/// If non-NULL, used to report errors in expression evaluation.
///
/// \return
/// True on success; false otherwise. If error_ptr is non-NULL,
/// details of the failure are provided through it.
static bool Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
lldb::ModuleSP opcode_ctx, const DataExtractor &opcodes,
- DWARFUnit *dwarf_cu, const lldb::offset_t offset,
+ const DWARFUnit *dwarf_cu, const lldb::offset_t offset,
const lldb::offset_t length,
const lldb::RegisterKind reg_set,
const Value *initial_value_ptr,
const Value *object_address_ptr, Value &result,
Status *error_ptr);
bool GetExpressionData(DataExtractor &data) const {
data = m_data;
return data.GetByteSize() > 0;
}
bool DumpLocationForAddress(Stream *s, lldb::DescriptionLevel level,
lldb::addr_t loclist_base_load_addr,
lldb::addr_t address, ABI *abi);
static size_t LocationListSize(const DWARFUnit *dwarf_cu,
const DataExtractor &debug_loc_data,
lldb::offset_t offset);
static bool PrintDWARFExpression(Stream &s, const DataExtractor &data,
int address_size, int dwarf_ref_size,
bool location_expression);
static void PrintDWARFLocationList(Stream &s, const DWARFUnit *cu,
const DataExtractor &debug_loc_data,
lldb::offset_t offset);
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op);
-protected:
+private:
/// Pretty-prints the location expression to a stream
///
/// \param[in] stream
/// The stream to use for pretty-printing.
///
/// \param[in] offset
/// The offset into the data buffer of the opcodes to be printed.
///
/// \param[in] length
/// The length in bytes of the opcodes to be printed.
///
/// \param[in] level
/// The level of detail to use in pretty-printing.
///
/// \param[in] abi
/// An optional ABI plug-in that can be used to resolve register
/// names.
void DumpLocation(Stream *s, lldb::offset_t offset, lldb::offset_t length,
lldb::DescriptionLevel level, ABI *abi) const;
bool GetLocation(lldb::addr_t base_addr, lldb::addr_t pc,
lldb::offset_t &offset, lldb::offset_t &len);
static bool AddressRangeForLocationListEntry(
const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc);
bool GetOpAndEndOffsets(StackFrame &frame, lldb::offset_t &op_offset,
lldb::offset_t &end_offset);
- /// Classes that inherit from DWARFExpression can see and modify these
-
- lldb::ModuleWP m_module_wp; ///< Module which defined this expression.
- DataExtractor m_data; ///< A data extractor capable of reading opcode bytes
- DWARFUnit *m_dwarf_cu; ///< The DWARF compile unit this expression
- ///belongs to. It is used
- ///< to evaluate values indexing into the .debug_addr section (e.g.
- ///< DW_OP_GNU_addr_index, DW_OP_GNU_const_index)
- lldb::RegisterKind
- m_reg_kind; ///< One of the defines that starts with LLDB_REGKIND_
- lldb::addr_t m_loclist_slide; ///< A value used to slide the location list
- ///offsets so that
- ///< they are relative to the object that owns the location list
- ///< (the function for frame base and variable location lists)
+ /// Module which defined this expression.
+ lldb::ModuleWP m_module_wp;
+
+ /// A data extractor capable of reading opcode bytes
+ DataExtractor m_data;
+
+ /// The DWARF compile unit this expression belongs to. It is used to evaluate
+ /// values indexing into the .debug_addr section (e.g. DW_OP_GNU_addr_index,
+ /// DW_OP_GNU_const_index)
+ const DWARFUnit *m_dwarf_cu;
+
+ /// One of the defines that starts with LLDB_REGKIND_
+ lldb::RegisterKind m_reg_kind;
+
+ /// A value used to slide the location list offsets so that m_c they are
+ /// relative to the object that owns the location list (the function for
+ /// frame base and variable location lists)
+ lldb::addr_t m_loclist_slide;
};
} // namespace lldb_private
#endif // liblldb_DWARFExpression_h_
diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index d6fa41bfb74b..50750c430332 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -1,3175 +1,3140 @@
//===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/DWARFExpression.h"
#include <inttypes.h>
#include <vector>
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Core/dwarf.h"
#include "lldb/Utility/DataEncoder.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/VMRange.h"
#include "lldb/Host/Host.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Target/ABI.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/StackID.h"
#include "lldb/Target/Thread.h"
#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
using namespace lldb;
using namespace lldb_private;
static lldb::addr_t
ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
uint32_t index) {
uint32_t index_size = dwarf_cu->GetAddressByteSize();
dw_offset_t addr_base = dwarf_cu->GetAddrBase();
lldb::offset_t offset = addr_base + index * index_size;
return dwarf_cu->GetSymbolFileDWARF()
->GetDWARFContext()
.getOrLoadAddrData()
.GetMaxU64(&offset, index_size);
}
// DWARFExpression constructor
-DWARFExpression::DWARFExpression(DWARFUnit *dwarf_cu)
- : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
+DWARFExpression::DWARFExpression()
+ : m_module_wp(), m_data(), m_dwarf_cu(nullptr),
m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
const DataExtractor &data,
- DWARFUnit *dwarf_cu,
+ const DWARFUnit *dwarf_cu,
lldb::offset_t data_offset,
lldb::offset_t data_length)
: m_module_wp(), m_data(data, data_offset, data_length),
m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
m_loclist_slide(LLDB_INVALID_ADDRESS) {
if (module_sp)
m_module_wp = module_sp;
}
// Destructor
DWARFExpression::~DWARFExpression() {}
bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
-void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
- m_data = data;
-}
-
-void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
- const DataExtractor &data,
- lldb::offset_t data_offset,
- lldb::offset_t data_length) {
- const uint8_t *bytes = data.PeekData(data_offset, data_length);
- if (bytes) {
- m_module_wp = module_sp;
- m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
- m_data.SetByteOrder(data.GetByteOrder());
- m_data.SetAddressByteSize(data.GetAddressByteSize());
- }
-}
-
-void DWARFExpression::CopyOpcodeData(const void *data,
- lldb::offset_t data_length,
- ByteOrder byte_order,
- uint8_t addr_byte_size) {
- if (data && data_length) {
- m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
- m_data.SetByteOrder(byte_order);
- m_data.SetAddressByteSize(addr_byte_size);
- }
-}
-
-void DWARFExpression::CopyOpcodeData(uint64_t const_value,
- lldb::offset_t const_value_byte_size,
- uint8_t addr_byte_size) {
- if (const_value_byte_size) {
- m_data.SetData(
- DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
- m_data.SetByteOrder(endian::InlHostByteOrder());
- m_data.SetAddressByteSize(addr_byte_size);
- }
-}
+void DWARFExpression::UpdateValue(uint64_t const_value,
+ lldb::offset_t const_value_byte_size,
+ uint8_t addr_byte_size) {
+ if (!const_value_byte_size)
+ return;
-void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
- const DataExtractor &data,
- lldb::offset_t data_offset,
- lldb::offset_t data_length) {
- m_module_wp = module_sp;
- m_data.SetData(data, data_offset, data_length);
+ m_data.SetData(
+ DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
+ m_data.SetByteOrder(endian::InlHostByteOrder());
+ m_data.SetAddressByteSize(addr_byte_size);
}
void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
lldb::offset_t length,
lldb::DescriptionLevel level,
ABI *abi) const {
if (!m_data.ValidOffsetForDataOfSize(offset, length))
return;
const lldb::offset_t start_offset = offset;
const lldb::offset_t end_offset = offset + length;
while (m_data.ValidOffset(offset) && offset < end_offset) {
const lldb::offset_t op_offset = offset;
const uint8_t op = m_data.GetU8(&offset);
switch (level) {
default:
break;
case lldb::eDescriptionLevelBrief:
if (op_offset > start_offset)
s->PutChar(' ');
break;
case lldb::eDescriptionLevelFull:
case lldb::eDescriptionLevelVerbose:
if (op_offset > start_offset)
s->EOL();
s->Indent();
if (level == lldb::eDescriptionLevelFull)
break;
// Fall through for verbose and print offset and DW_OP prefix..
s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
break;
}
switch (op) {
case DW_OP_addr:
*s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
break; // 0x03 1 address
case DW_OP_deref:
*s << "DW_OP_deref";
break; // 0x06
case DW_OP_const1u:
s->Printf("DW_OP_const1u(0x%2.2x)", m_data.GetU8(&offset));
break; // 0x08 1 1-byte constant
case DW_OP_const1s:
s->Printf("DW_OP_const1s(0x%2.2x)", m_data.GetU8(&offset));
break; // 0x09 1 1-byte constant
case DW_OP_const2u:
s->Printf("DW_OP_const2u(0x%4.4x)", m_data.GetU16(&offset));
break; // 0x0a 1 2-byte constant
case DW_OP_const2s:
s->Printf("DW_OP_const2s(0x%4.4x)", m_data.GetU16(&offset));
break; // 0x0b 1 2-byte constant
case DW_OP_const4u:
s->Printf("DW_OP_const4u(0x%8.8x)", m_data.GetU32(&offset));
break; // 0x0c 1 4-byte constant
case DW_OP_const4s:
s->Printf("DW_OP_const4s(0x%8.8x)", m_data.GetU32(&offset));
break; // 0x0d 1 4-byte constant
case DW_OP_const8u:
s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
break; // 0x0e 1 8-byte constant
case DW_OP_const8s:
s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
break; // 0x0f 1 8-byte constant
case DW_OP_constu:
s->Printf("DW_OP_constu(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
break; // 0x10 1 ULEB128 constant
case DW_OP_consts:
s->Printf("DW_OP_consts(0x%" PRId64 ")", m_data.GetSLEB128(&offset));
break; // 0x11 1 SLEB128 constant
case DW_OP_dup:
s->PutCString("DW_OP_dup");
break; // 0x12
case DW_OP_drop:
s->PutCString("DW_OP_drop");
break; // 0x13
case DW_OP_over:
s->PutCString("DW_OP_over");
break; // 0x14
case DW_OP_pick:
s->Printf("DW_OP_pick(0x%2.2x)", m_data.GetU8(&offset));
break; // 0x15 1 1-byte stack index
case DW_OP_swap:
s->PutCString("DW_OP_swap");
break; // 0x16
case DW_OP_rot:
s->PutCString("DW_OP_rot");
break; // 0x17
case DW_OP_xderef:
s->PutCString("DW_OP_xderef");
break; // 0x18
case DW_OP_abs:
s->PutCString("DW_OP_abs");
break; // 0x19
case DW_OP_and:
s->PutCString("DW_OP_and");
break; // 0x1a
case DW_OP_div:
s->PutCString("DW_OP_div");
break; // 0x1b
case DW_OP_minus:
s->PutCString("DW_OP_minus");
break; // 0x1c
case DW_OP_mod:
s->PutCString("DW_OP_mod");
break; // 0x1d
case DW_OP_mul:
s->PutCString("DW_OP_mul");
break; // 0x1e
case DW_OP_neg:
s->PutCString("DW_OP_neg");
break; // 0x1f
case DW_OP_not:
s->PutCString("DW_OP_not");
break; // 0x20
case DW_OP_or:
s->PutCString("DW_OP_or");
break; // 0x21
case DW_OP_plus:
s->PutCString("DW_OP_plus");
break; // 0x22
case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ")",
m_data.GetULEB128(&offset));
break;
case DW_OP_shl:
s->PutCString("DW_OP_shl");
break; // 0x24
case DW_OP_shr:
s->PutCString("DW_OP_shr");
break; // 0x25
case DW_OP_shra:
s->PutCString("DW_OP_shra");
break; // 0x26
case DW_OP_xor:
s->PutCString("DW_OP_xor");
break; // 0x27
case DW_OP_skip:
s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
break; // 0x2f 1 signed 2-byte constant
case DW_OP_bra:
s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
break; // 0x28 1 signed 2-byte constant
case DW_OP_eq:
s->PutCString("DW_OP_eq");
break; // 0x29
case DW_OP_ge:
s->PutCString("DW_OP_ge");
break; // 0x2a
case DW_OP_gt:
s->PutCString("DW_OP_gt");
break; // 0x2b
case DW_OP_le:
s->PutCString("DW_OP_le");
break; // 0x2c
case DW_OP_lt:
s->PutCString("DW_OP_lt");
break; // 0x2d
case DW_OP_ne:
s->PutCString("DW_OP_ne");
break; // 0x2e
case DW_OP_lit0: // 0x30
case DW_OP_lit1: // 0x31
case DW_OP_lit2: // 0x32
case DW_OP_lit3: // 0x33
case DW_OP_lit4: // 0x34
case DW_OP_lit5: // 0x35
case DW_OP_lit6: // 0x36
case DW_OP_lit7: // 0x37
case DW_OP_lit8: // 0x38
case DW_OP_lit9: // 0x39
case DW_OP_lit10: // 0x3A
case DW_OP_lit11: // 0x3B
case DW_OP_lit12: // 0x3C
case DW_OP_lit13: // 0x3D
case DW_OP_lit14: // 0x3E
case DW_OP_lit15: // 0x3F
case DW_OP_lit16: // 0x40
case DW_OP_lit17: // 0x41
case DW_OP_lit18: // 0x42
case DW_OP_lit19: // 0x43
case DW_OP_lit20: // 0x44
case DW_OP_lit21: // 0x45
case DW_OP_lit22: // 0x46
case DW_OP_lit23: // 0x47
case DW_OP_lit24: // 0x48
case DW_OP_lit25: // 0x49
case DW_OP_lit26: // 0x4A
case DW_OP_lit27: // 0x4B
case DW_OP_lit28: // 0x4C
case DW_OP_lit29: // 0x4D
case DW_OP_lit30: // 0x4E
case DW_OP_lit31:
s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
break; // 0x4f
case DW_OP_reg0: // 0x50
case DW_OP_reg1: // 0x51
case DW_OP_reg2: // 0x52
case DW_OP_reg3: // 0x53
case DW_OP_reg4: // 0x54
case DW_OP_reg5: // 0x55
case DW_OP_reg6: // 0x56
case DW_OP_reg7: // 0x57
case DW_OP_reg8: // 0x58
case DW_OP_reg9: // 0x59
case DW_OP_reg10: // 0x5A
case DW_OP_reg11: // 0x5B
case DW_OP_reg12: // 0x5C
case DW_OP_reg13: // 0x5D
case DW_OP_reg14: // 0x5E
case DW_OP_reg15: // 0x5F
case DW_OP_reg16: // 0x60
case DW_OP_reg17: // 0x61
case DW_OP_reg18: // 0x62
case DW_OP_reg19: // 0x63
case DW_OP_reg20: // 0x64
case DW_OP_reg21: // 0x65
case DW_OP_reg22: // 0x66
case DW_OP_reg23: // 0x67
case DW_OP_reg24: // 0x68
case DW_OP_reg25: // 0x69
case DW_OP_reg26: // 0x6A
case DW_OP_reg27: // 0x6B
case DW_OP_reg28: // 0x6C
case DW_OP_reg29: // 0x6D
case DW_OP_reg30: // 0x6E
case DW_OP_reg31: // 0x6F
{
uint32_t reg_num = op - DW_OP_reg0;
if (abi) {
RegisterInfo reg_info;
if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
if (reg_info.name) {
s->PutCString(reg_info.name);
break;
} else if (reg_info.alt_name) {
s->PutCString(reg_info.alt_name);
break;
}
}
}
s->Printf("DW_OP_reg%u", reg_num);
break;
} break;
case DW_OP_breg0:
case DW_OP_breg1:
case DW_OP_breg2:
case DW_OP_breg3:
case DW_OP_breg4:
case DW_OP_breg5:
case DW_OP_breg6:
case DW_OP_breg7:
case DW_OP_breg8:
case DW_OP_breg9:
case DW_OP_breg10:
case DW_OP_breg11:
case DW_OP_breg12:
case DW_OP_breg13:
case DW_OP_breg14:
case DW_OP_breg15:
case DW_OP_breg16:
case DW_OP_breg17:
case DW_OP_breg18:
case DW_OP_breg19:
case DW_OP_breg20:
case DW_OP_breg21:
case DW_OP_breg22:
case DW_OP_breg23:
case DW_OP_breg24:
case DW_OP_breg25:
case DW_OP_breg26:
case DW_OP_breg27:
case DW_OP_breg28:
case DW_OP_breg29:
case DW_OP_breg30:
case DW_OP_breg31: {
uint32_t reg_num = op - DW_OP_breg0;
int64_t reg_offset = m_data.GetSLEB128(&offset);
if (abi) {
RegisterInfo reg_info;
if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
if (reg_info.name) {
s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
break;
} else if (reg_info.alt_name) {
s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
break;
}
}
}
s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
} break;
case DW_OP_regx: // 0x90 1 ULEB128 register
{
uint32_t reg_num = m_data.GetULEB128(&offset);
if (abi) {
RegisterInfo reg_info;
if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
if (reg_info.name) {
s->PutCString(reg_info.name);
break;
} else if (reg_info.alt_name) {
s->PutCString(reg_info.alt_name);
break;
}
}
}
s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
break;
} break;
case DW_OP_fbreg: // 0x91 1 SLEB128 offset
s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
break;
case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
{
uint32_t reg_num = m_data.GetULEB128(&offset);
int64_t reg_offset = m_data.GetSLEB128(&offset);
if (abi) {
RegisterInfo reg_info;
if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
if (reg_info.name) {
s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
break;
} else if (reg_info.alt_name) {
s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
break;
}
}
}
s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
reg_offset);
} break;
case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
break;
case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
break;
case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
break;
case DW_OP_nop:
s->PutCString("DW_OP_nop");
break; // 0x96
case DW_OP_push_object_address:
s->PutCString("DW_OP_push_object_address");
break; // 0x97 DWARF3
case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
break;
case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
break;
case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
break;
case DW_OP_form_tls_address:
s->PutCString("DW_OP_form_tls_address"); // 0x9b
break;
case DW_OP_GNU_addr_index: // 0xfb
s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
m_data.GetULEB128(&offset));
break;
case DW_OP_addrx:
s->Printf("DW_OP_addrx(0x%" PRIx64 ")",
m_data.GetULEB128(&offset));
break;
case DW_OP_GNU_const_index: // 0xfc
s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
m_data.GetULEB128(&offset));
break;
case DW_OP_GNU_push_tls_address:
s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
break;
case DW_OP_APPLE_uninit:
s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
break;
}
}
}
void DWARFExpression::SetLocationListSlide(addr_t slide) {
m_loclist_slide = slide;
}
int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
m_reg_kind = reg_kind;
}
bool DWARFExpression::IsLocationList() const {
return m_loclist_slide != LLDB_INVALID_ADDRESS;
}
void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
addr_t location_list_base_addr,
ABI *abi) const {
if (IsLocationList()) {
// We have a location list
lldb::offset_t offset = 0;
uint32_t count = 0;
addr_t curr_base_addr = location_list_base_addr;
while (m_data.ValidOffset(offset)) {
addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
begin_addr_offset, end_addr_offset))
break;
if (begin_addr_offset == 0 && end_addr_offset == 0)
break;
if (begin_addr_offset < end_addr_offset) {
if (count > 0)
s->PutCString(", ");
VMRange addr_range(curr_base_addr + begin_addr_offset,
curr_base_addr + end_addr_offset);
addr_range.Dump(s, 0, 8);
s->PutChar('{');
lldb::offset_t location_length = m_data.GetU16(&offset);
DumpLocation(s, offset, location_length, level, abi);
s->PutChar('}');
offset += location_length;
} else {
if ((m_data.GetAddressByteSize() == 4 &&
(begin_addr_offset == UINT32_MAX)) ||
(m_data.GetAddressByteSize() == 8 &&
(begin_addr_offset == UINT64_MAX))) {
curr_base_addr = end_addr_offset + location_list_base_addr;
// We have a new base address
if (count > 0)
s->PutCString(", ");
*s << "base_addr = " << end_addr_offset;
}
}
count++;
}
} else {
// We have a normal location that contains DW_OP location opcodes
DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
}
}
static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
uint32_t reg_num, Status *error_ptr,
Value &value) {
if (reg_ctx == nullptr) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
} else {
uint32_t native_reg =
reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
if (native_reg == LLDB_INVALID_REGNUM) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat("Unable to convert register "
"kind=%u reg_num=%u to a native "
"register number.\n",
reg_kind, reg_num);
} else {
const RegisterInfo *reg_info =
reg_ctx->GetRegisterInfoAtIndex(native_reg);
RegisterValue reg_value;
if (reg_ctx->ReadRegister(reg_info, reg_value)) {
if (reg_value.GetScalarValue(value.GetScalar())) {
value.SetValueType(Value::eValueTypeScalar);
value.SetContext(Value::eContextTypeRegisterInfo,
const_cast<RegisterInfo *>(reg_info));
if (error_ptr)
error_ptr->Clear();
return true;
} else {
// If we get this error, then we need to implement a value buffer in
// the dwarf expression evaluation function...
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"register %s can't be converted to a scalar value",
reg_info->name);
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat("register %s is not available",
reg_info->name);
}
}
}
return false;
}
static offset_t GetOpcodeDataSize(const DataExtractor &data,
const lldb::offset_t data_offset,
const uint8_t op) {
lldb::offset_t offset = data_offset;
switch (op) {
case DW_OP_addr:
case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
return data.GetAddressByteSize();
// Opcodes with no arguments
case DW_OP_deref: // 0x06
case DW_OP_dup: // 0x12
case DW_OP_drop: // 0x13
case DW_OP_over: // 0x14
case DW_OP_swap: // 0x16
case DW_OP_rot: // 0x17
case DW_OP_xderef: // 0x18
case DW_OP_abs: // 0x19
case DW_OP_and: // 0x1a
case DW_OP_div: // 0x1b
case DW_OP_minus: // 0x1c
case DW_OP_mod: // 0x1d
case DW_OP_mul: // 0x1e
case DW_OP_neg: // 0x1f
case DW_OP_not: // 0x20
case DW_OP_or: // 0x21
case DW_OP_plus: // 0x22
case DW_OP_shl: // 0x24
case DW_OP_shr: // 0x25
case DW_OP_shra: // 0x26
case DW_OP_xor: // 0x27
case DW_OP_eq: // 0x29
case DW_OP_ge: // 0x2a
case DW_OP_gt: // 0x2b
case DW_OP_le: // 0x2c
case DW_OP_lt: // 0x2d
case DW_OP_ne: // 0x2e
case DW_OP_lit0: // 0x30
case DW_OP_lit1: // 0x31
case DW_OP_lit2: // 0x32
case DW_OP_lit3: // 0x33
case DW_OP_lit4: // 0x34
case DW_OP_lit5: // 0x35
case DW_OP_lit6: // 0x36
case DW_OP_lit7: // 0x37
case DW_OP_lit8: // 0x38
case DW_OP_lit9: // 0x39
case DW_OP_lit10: // 0x3A
case DW_OP_lit11: // 0x3B
case DW_OP_lit12: // 0x3C
case DW_OP_lit13: // 0x3D
case DW_OP_lit14: // 0x3E
case DW_OP_lit15: // 0x3F
case DW_OP_lit16: // 0x40
case DW_OP_lit17: // 0x41
case DW_OP_lit18: // 0x42
case DW_OP_lit19: // 0x43
case DW_OP_lit20: // 0x44
case DW_OP_lit21: // 0x45
case DW_OP_lit22: // 0x46
case DW_OP_lit23: // 0x47
case DW_OP_lit24: // 0x48
case DW_OP_lit25: // 0x49
case DW_OP_lit26: // 0x4A
case DW_OP_lit27: // 0x4B
case DW_OP_lit28: // 0x4C
case DW_OP_lit29: // 0x4D
case DW_OP_lit30: // 0x4E
case DW_OP_lit31: // 0x4f
case DW_OP_reg0: // 0x50
case DW_OP_reg1: // 0x51
case DW_OP_reg2: // 0x52
case DW_OP_reg3: // 0x53
case DW_OP_reg4: // 0x54
case DW_OP_reg5: // 0x55
case DW_OP_reg6: // 0x56
case DW_OP_reg7: // 0x57
case DW_OP_reg8: // 0x58
case DW_OP_reg9: // 0x59
case DW_OP_reg10: // 0x5A
case DW_OP_reg11: // 0x5B
case DW_OP_reg12: // 0x5C
case DW_OP_reg13: // 0x5D
case DW_OP_reg14: // 0x5E
case DW_OP_reg15: // 0x5F
case DW_OP_reg16: // 0x60
case DW_OP_reg17: // 0x61
case DW_OP_reg18: // 0x62
case DW_OP_reg19: // 0x63
case DW_OP_reg20: // 0x64
case DW_OP_reg21: // 0x65
case DW_OP_reg22: // 0x66
case DW_OP_reg23: // 0x67
case DW_OP_reg24: // 0x68
case DW_OP_reg25: // 0x69
case DW_OP_reg26: // 0x6A
case DW_OP_reg27: // 0x6B
case DW_OP_reg28: // 0x6C
case DW_OP_reg29: // 0x6D
case DW_OP_reg30: // 0x6E
case DW_OP_reg31: // 0x6F
case DW_OP_nop: // 0x96
case DW_OP_push_object_address: // 0x97 DWARF3
case DW_OP_form_tls_address: // 0x9b DWARF3
case DW_OP_call_frame_cfa: // 0x9c DWARF3
case DW_OP_stack_value: // 0x9f DWARF4
case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
return 0;
// Opcodes with a single 1 byte arguments
case DW_OP_const1u: // 0x08 1 1-byte constant
case DW_OP_const1s: // 0x09 1 1-byte constant
case DW_OP_pick: // 0x15 1 1-byte stack index
case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
return 1;
// Opcodes with a single 2 byte arguments
case DW_OP_const2u: // 0x0a 1 2-byte constant
case DW_OP_const2s: // 0x0b 1 2-byte constant
case DW_OP_skip: // 0x2f 1 signed 2-byte constant
case DW_OP_bra: // 0x28 1 signed 2-byte constant
case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
return 2;
// Opcodes with a single 4 byte arguments
case DW_OP_const4u: // 0x0c 1 4-byte constant
case DW_OP_const4s: // 0x0d 1 4-byte constant
case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
return 4;
// Opcodes with a single 8 byte arguments
case DW_OP_const8u: // 0x0e 1 8-byte constant
case DW_OP_const8s: // 0x0f 1 8-byte constant
return 8;
// All opcodes that have a single ULEB (signed or unsigned) argument
case DW_OP_addrx: // 0xa1 1 ULEB128 index
case DW_OP_constu: // 0x10 1 ULEB128 constant
case DW_OP_consts: // 0x11 1 SLEB128 constant
case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
case DW_OP_breg0: // 0x70 1 ULEB128 register
case DW_OP_breg1: // 0x71 1 ULEB128 register
case DW_OP_breg2: // 0x72 1 ULEB128 register
case DW_OP_breg3: // 0x73 1 ULEB128 register
case DW_OP_breg4: // 0x74 1 ULEB128 register
case DW_OP_breg5: // 0x75 1 ULEB128 register
case DW_OP_breg6: // 0x76 1 ULEB128 register
case DW_OP_breg7: // 0x77 1 ULEB128 register
case DW_OP_breg8: // 0x78 1 ULEB128 register
case DW_OP_breg9: // 0x79 1 ULEB128 register
case DW_OP_breg10: // 0x7a 1 ULEB128 register
case DW_OP_breg11: // 0x7b 1 ULEB128 register
case DW_OP_breg12: // 0x7c 1 ULEB128 register
case DW_OP_breg13: // 0x7d 1 ULEB128 register
case DW_OP_breg14: // 0x7e 1 ULEB128 register
case DW_OP_breg15: // 0x7f 1 ULEB128 register
case DW_OP_breg16: // 0x80 1 ULEB128 register
case DW_OP_breg17: // 0x81 1 ULEB128 register
case DW_OP_breg18: // 0x82 1 ULEB128 register
case DW_OP_breg19: // 0x83 1 ULEB128 register
case DW_OP_breg20: // 0x84 1 ULEB128 register
case DW_OP_breg21: // 0x85 1 ULEB128 register
case DW_OP_breg22: // 0x86 1 ULEB128 register
case DW_OP_breg23: // 0x87 1 ULEB128 register
case DW_OP_breg24: // 0x88 1 ULEB128 register
case DW_OP_breg25: // 0x89 1 ULEB128 register
case DW_OP_breg26: // 0x8a 1 ULEB128 register
case DW_OP_breg27: // 0x8b 1 ULEB128 register
case DW_OP_breg28: // 0x8c 1 ULEB128 register
case DW_OP_breg29: // 0x8d 1 ULEB128 register
case DW_OP_breg30: // 0x8e 1 ULEB128 register
case DW_OP_breg31: // 0x8f 1 ULEB128 register
case DW_OP_regx: // 0x90 1 ULEB128 register
case DW_OP_fbreg: // 0x91 1 SLEB128 offset
case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
data.Skip_LEB128(&offset);
return offset - data_offset;
// All opcodes that have a 2 ULEB (signed or unsigned) arguments
case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
data.Skip_LEB128(&offset);
data.Skip_LEB128(&offset);
return offset - data_offset;
case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
// (DWARF4)
{
uint64_t block_len = data.Skip_LEB128(&offset);
offset += block_len;
return offset - data_offset;
}
default:
break;
}
return LLDB_INVALID_OFFSET;
}
lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
bool &error) const {
error = false;
if (IsLocationList())
return LLDB_INVALID_ADDRESS;
lldb::offset_t offset = 0;
uint32_t curr_op_addr_idx = 0;
while (m_data.ValidOffset(offset)) {
const uint8_t op = m_data.GetU8(&offset);
if (op == DW_OP_addr) {
const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
if (curr_op_addr_idx == op_addr_idx)
return op_file_addr;
else
++curr_op_addr_idx;
} else if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
uint64_t index = m_data.GetULEB128(&offset);
if (curr_op_addr_idx == op_addr_idx) {
if (!m_dwarf_cu) {
error = true;
break;
}
return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
} else
++curr_op_addr_idx;
} else {
const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
if (op_arg_size == LLDB_INVALID_OFFSET) {
error = true;
break;
}
offset += op_arg_size;
}
}
return LLDB_INVALID_ADDRESS;
}
bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
if (IsLocationList())
return false;
lldb::offset_t offset = 0;
while (m_data.ValidOffset(offset)) {
const uint8_t op = m_data.GetU8(&offset);
if (op == DW_OP_addr) {
const uint32_t addr_byte_size = m_data.GetAddressByteSize();
// We have to make a copy of the data as we don't know if this data is
// from a read only memory mapped buffer, so we duplicate all of the data
// first, then modify it, and if all goes well, we then replace the data
// for this expression
// So first we copy the data into a heap buffer
std::unique_ptr<DataBufferHeap> head_data_up(
new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
// Make en encoder so we can write the address into the buffer using the
// correct byte order (endianness)
DataEncoder encoder(head_data_up->GetBytes(), head_data_up->GetByteSize(),
m_data.GetByteOrder(), addr_byte_size);
// Replace the address in the new buffer
if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
return false;
// All went well, so now we can reset the data using a shared pointer to
// the heap data so "m_data" will now correctly manage the heap data.
m_data.SetData(DataBufferSP(head_data_up.release()));
return true;
} else {
const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
if (op_arg_size == LLDB_INVALID_OFFSET)
break;
offset += op_arg_size;
}
}
return false;
}
bool DWARFExpression::ContainsThreadLocalStorage() const {
// We are assuming for now that any thread local variable will not have a
// location list. This has been true for all thread local variables we have
// seen so far produced by any compiler.
if (IsLocationList())
return false;
lldb::offset_t offset = 0;
while (m_data.ValidOffset(offset)) {
const uint8_t op = m_data.GetU8(&offset);
if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
return true;
const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
if (op_arg_size == LLDB_INVALID_OFFSET)
return false;
else
offset += op_arg_size;
}
return false;
}
bool DWARFExpression::LinkThreadLocalStorage(
lldb::ModuleSP new_module_sp,
std::function<lldb::addr_t(lldb::addr_t file_addr)> const
&link_address_callback) {
// We are assuming for now that any thread local variable will not have a
// location list. This has been true for all thread local variables we have
// seen so far produced by any compiler.
if (IsLocationList())
return false;
const uint32_t addr_byte_size = m_data.GetAddressByteSize();
// We have to make a copy of the data as we don't know if this data is from a
// read only memory mapped buffer, so we duplicate all of the data first,
// then modify it, and if all goes well, we then replace the data for this
// expression
// So first we copy the data into a heap buffer
std::shared_ptr<DataBufferHeap> heap_data_sp(
new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
// Make en encoder so we can write the address into the buffer using the
// correct byte order (endianness)
DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
m_data.GetByteOrder(), addr_byte_size);
lldb::offset_t offset = 0;
lldb::offset_t const_offset = 0;
lldb::addr_t const_value = 0;
size_t const_byte_size = 0;
while (m_data.ValidOffset(offset)) {
const uint8_t op = m_data.GetU8(&offset);
bool decoded_data = false;
switch (op) {
case DW_OP_const4u:
// Remember the const offset in case we later have a
// DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
const_offset = offset;
const_value = m_data.GetU32(&offset);
decoded_data = true;
const_byte_size = 4;
break;
case DW_OP_const8u:
// Remember the const offset in case we later have a
// DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
const_offset = offset;
const_value = m_data.GetU64(&offset);
decoded_data = true;
const_byte_size = 8;
break;
case DW_OP_form_tls_address:
case DW_OP_GNU_push_tls_address:
// DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
// by a file address on the stack. We assume that DW_OP_const4u or
// DW_OP_const8u is used for these values, and we check that the last
// opcode we got before either of these was DW_OP_const4u or
// DW_OP_const8u. If so, then we can link the value accodingly. For
// Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
// address of a structure that contains a function pointer, the pthread
// key and the offset into the data pointed to by the pthread key. So we
// must link this address and also set the module of this expression to
// the new_module_sp so we can resolve the file address correctly
if (const_byte_size > 0) {
lldb::addr_t linked_file_addr = link_address_callback(const_value);
if (linked_file_addr == LLDB_INVALID_ADDRESS)
return false;
// Replace the address in the new buffer
if (encoder.PutMaxU64(const_offset, const_byte_size,
linked_file_addr) == UINT32_MAX)
return false;
}
break;
default:
const_offset = 0;
const_value = 0;
const_byte_size = 0;
break;
}
if (!decoded_data) {
const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
if (op_arg_size == LLDB_INVALID_OFFSET)
return false;
else
offset += op_arg_size;
}
}
// If we linked the TLS address correctly, update the module so that when the
// expression is evaluated it can resolve the file address to a load address
// and read the
// TLS data
m_module_wp = new_module_sp;
m_data.SetData(heap_data_sp);
return true;
}
bool DWARFExpression::LocationListContainsAddress(
lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
if (addr == LLDB_INVALID_ADDRESS)
return false;
if (IsLocationList()) {
lldb::offset_t offset = 0;
if (loclist_base_addr == LLDB_INVALID_ADDRESS)
return false;
while (m_data.ValidOffset(offset)) {
// We need to figure out what the value is for the location.
addr_t lo_pc = LLDB_INVALID_ADDRESS;
addr_t hi_pc = LLDB_INVALID_ADDRESS;
if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
hi_pc))
break;
if (lo_pc == 0 && hi_pc == 0)
break;
lo_pc += loclist_base_addr - m_loclist_slide;
hi_pc += loclist_base_addr - m_loclist_slide;
if (lo_pc <= addr && addr < hi_pc)
return true;
offset += m_data.GetU16(&offset);
}
}
return false;
}
bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
lldb::offset_t &offset,
lldb::offset_t &length) {
offset = 0;
if (!IsLocationList()) {
length = m_data.GetByteSize();
return true;
}
if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
addr_t curr_base_addr = base_addr;
while (m_data.ValidOffset(offset)) {
// We need to figure out what the value is for the location.
addr_t lo_pc = LLDB_INVALID_ADDRESS;
addr_t hi_pc = LLDB_INVALID_ADDRESS;
if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
hi_pc))
break;
if (lo_pc == 0 && hi_pc == 0)
break;
lo_pc += curr_base_addr - m_loclist_slide;
hi_pc += curr_base_addr - m_loclist_slide;
length = m_data.GetU16(&offset);
if (length > 0 && lo_pc <= pc && pc < hi_pc)
return true;
offset += length;
}
}
offset = LLDB_INVALID_OFFSET;
length = 0;
return false;
}
bool DWARFExpression::DumpLocationForAddress(Stream *s,
lldb::DescriptionLevel level,
addr_t base_addr, addr_t address,
ABI *abi) {
lldb::offset_t offset = 0;
lldb::offset_t length = 0;
if (GetLocation(base_addr, address, offset, length)) {
if (length > 0) {
DumpLocation(s, offset, length, level, abi);
return true;
}
}
return false;
}
bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
lldb::addr_t loclist_base_load_addr,
const Value *initial_value_ptr,
const Value *object_address_ptr, Value &result,
Status *error_ptr) const {
ExecutionContext exe_ctx(exe_scope);
return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
object_address_ptr, result, error_ptr);
}
bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
RegisterContext *reg_ctx,
lldb::addr_t loclist_base_load_addr,
const Value *initial_value_ptr,
const Value *object_address_ptr, Value &result,
Status *error_ptr) const {
ModuleSP module_sp = m_module_wp.lock();
if (IsLocationList()) {
lldb::offset_t offset = 0;
addr_t pc;
StackFrame *frame = nullptr;
if (reg_ctx)
pc = reg_ctx->GetPC();
else {
frame = exe_ctx->GetFramePtr();
if (!frame)
return false;
RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
if (!reg_ctx_sp)
return false;
pc = reg_ctx_sp->GetPC();
}
if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
if (pc == LLDB_INVALID_ADDRESS) {
if (error_ptr)
error_ptr->SetErrorString("Invalid PC in frame.");
return false;
}
addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
while (m_data.ValidOffset(offset)) {
// We need to figure out what the value is for the location.
addr_t lo_pc = LLDB_INVALID_ADDRESS;
addr_t hi_pc = LLDB_INVALID_ADDRESS;
if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
lo_pc, hi_pc))
break;
if (lo_pc == 0 && hi_pc == 0)
break;
lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
uint16_t length = m_data.GetU16(&offset);
if (length > 0 && lo_pc <= pc && pc < hi_pc) {
return DWARFExpression::Evaluate(
exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
m_reg_kind, initial_value_ptr, object_address_ptr, result,
error_ptr);
}
offset += length;
}
}
if (error_ptr)
error_ptr->SetErrorString("variable not available");
return false;
}
// Not a location list, just a single expression.
return DWARFExpression::Evaluate(
exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
}
bool DWARFExpression::Evaluate(
ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
lldb::ModuleSP module_sp, const DataExtractor &opcodes,
- DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
+ const DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
const Value *initial_value_ptr, const Value *object_address_ptr,
Value &result, Status *error_ptr) {
if (opcodes_length == 0) {
if (error_ptr)
error_ptr->SetErrorString(
"no location, value may have been optimized out");
return false;
}
std::vector<Value> stack;
Process *process = nullptr;
StackFrame *frame = nullptr;
if (exe_ctx) {
process = exe_ctx->GetProcessPtr();
frame = exe_ctx->GetFramePtr();
}
if (reg_ctx == nullptr && frame)
reg_ctx = frame->GetRegisterContext().get();
if (initial_value_ptr)
stack.push_back(*initial_value_ptr);
lldb::offset_t offset = opcodes_offset;
const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
Value tmp;
uint32_t reg_num;
/// Insertion point for evaluating multi-piece expression.
uint64_t op_piece_offset = 0;
Value pieces; // Used for DW_OP_piece
// Make sure all of the data is available in opcodes.
if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
if (error_ptr)
error_ptr->SetErrorString(
"invalid offset and/or length for opcodes buffer.");
return false;
}
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
while (opcodes.ValidOffset(offset) && offset < end_offset) {
const lldb::offset_t op_offset = offset;
const uint8_t op = opcodes.GetU8(&offset);
if (log && log->GetVerbose()) {
size_t count = stack.size();
log->Printf("Stack before operation has %" PRIu64 " values:",
(uint64_t)count);
for (size_t i = 0; i < count; ++i) {
StreamString new_value;
new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
stack[i].Dump(&new_value);
log->Printf(" %s", new_value.GetData());
}
log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
}
switch (op) {
// The DW_OP_addr operation has a single operand that encodes a machine
// address and whose size is the size of an address on the target machine.
case DW_OP_addr:
stack.push_back(Scalar(opcodes.GetAddress(&offset)));
stack.back().SetValueType(Value::eValueTypeFileAddress);
// Convert the file address to a load address, so subsequent
// DWARF operators can operate on it.
if (frame)
stack.back().ConvertToLoadAddress(module_sp.get(),
frame->CalculateTarget().get());
break;
// The DW_OP_addr_sect_offset4 is used for any location expressions in
// shared libraries that have a location like:
// DW_OP_addr(0x1000)
// If this address resides in a shared library, then this virtual address
// won't make sense when it is evaluated in the context of a running
// process where shared libraries have been slid. To account for this, this
// new address type where we can store the section pointer and a 4 byte
// offset.
// case DW_OP_addr_sect_offset4:
// {
// result_type = eResultTypeFileAddress;
// lldb::Section *sect = (lldb::Section
// *)opcodes.GetMaxU64(&offset, sizeof(void *));
// lldb::addr_t sect_offset = opcodes.GetU32(&offset);
//
// Address so_addr (sect, sect_offset);
// lldb::addr_t load_addr = so_addr.GetLoadAddress();
// if (load_addr != LLDB_INVALID_ADDRESS)
// {
// // We successfully resolve a file address to a load
// // address.
// stack.push_back(load_addr);
// break;
// }
// else
// {
// // We were able
// if (error_ptr)
// error_ptr->SetErrorStringWithFormat ("Section %s in
// %s is not currently loaded.\n",
// sect->GetName().AsCString(),
// sect->GetModule()->GetFileSpec().GetFilename().AsCString());
// return false;
// }
// }
// break;
// OPCODE: DW_OP_deref
// OPERANDS: none
// DESCRIPTION: Pops the top stack entry and treats it as an address.
// The value retrieved from that address is pushed. The size of the data
// retrieved from the dereferenced address is the size of an address on the
// target machine.
case DW_OP_deref: {
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
return false;
}
Value::ValueType value_type = stack.back().GetValueType();
switch (value_type) {
case Value::eValueTypeHostAddress: {
void *src = (void *)stack.back().GetScalar().ULongLong();
intptr_t ptr;
::memcpy(&ptr, src, sizeof(void *));
stack.back().GetScalar() = ptr;
stack.back().ClearContext();
} break;
case Value::eValueTypeFileAddress: {
auto file_addr = stack.back().GetScalar().ULongLong(
LLDB_INVALID_ADDRESS);
if (!module_sp) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"need module to resolve file address for DW_OP_deref");
return false;
}
Address so_addr;
if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"failed to resolve file address in module");
return false;
}
addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
if (load_Addr == LLDB_INVALID_ADDRESS) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"failed to resolve load address");
return false;
}
stack.back().GetScalar() = load_Addr;
stack.back().SetValueType(Value::eValueTypeLoadAddress);
// Fall through to load address code below...
} LLVM_FALLTHROUGH;
case Value::eValueTypeLoadAddress:
if (exe_ctx) {
if (process) {
lldb::addr_t pointer_addr =
stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Status error;
lldb::addr_t pointer_value =
process->ReadPointerFromMemory(pointer_addr, error);
if (pointer_value != LLDB_INVALID_ADDRESS) {
stack.back().GetScalar() = pointer_value;
stack.back().ClearContext();
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"Failed to dereference pointer from 0x%" PRIx64
" for DW_OP_deref: %s\n",
pointer_addr, error.AsCString());
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"NULL process for DW_OP_deref.\n");
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"NULL execution context for DW_OP_deref.\n");
return false;
}
break;
default:
break;
}
} break;
// OPCODE: DW_OP_deref_size
// OPERANDS: 1
// 1 - uint8_t that specifies the size of the data to dereference.
// DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
// stack entry and treats it as an address. The value retrieved from that
// address is pushed. In the DW_OP_deref_size operation, however, the size
// in bytes of the data retrieved from the dereferenced address is
// specified by the single operand. This operand is a 1-byte unsigned
// integral constant whose value may not be larger than the size of an
// address on the target machine. The data retrieved is zero extended to
// the size of an address on the target machine before being pushed on the
// expression stack.
case DW_OP_deref_size: {
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack empty for DW_OP_deref_size.");
return false;
}
uint8_t size = opcodes.GetU8(&offset);
Value::ValueType value_type = stack.back().GetValueType();
switch (value_type) {
case Value::eValueTypeHostAddress: {
void *src = (void *)stack.back().GetScalar().ULongLong();
intptr_t ptr;
::memcpy(&ptr, src, sizeof(void *));
// I can't decide whether the size operand should apply to the bytes in
// their
// lldb-host endianness or the target endianness.. I doubt this'll ever
// come up but I'll opt for assuming big endian regardless.
switch (size) {
case 1:
ptr = ptr & 0xff;
break;
case 2:
ptr = ptr & 0xffff;
break;
case 3:
ptr = ptr & 0xffffff;
break;
case 4:
ptr = ptr & 0xffffffff;
break;
// the casts are added to work around the case where intptr_t is a 32
// bit quantity;
// presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
// program.
case 5:
ptr = (intptr_t)ptr & 0xffffffffffULL;
break;
case 6:
ptr = (intptr_t)ptr & 0xffffffffffffULL;
break;
case 7:
ptr = (intptr_t)ptr & 0xffffffffffffffULL;
break;
default:
break;
}
stack.back().GetScalar() = ptr;
stack.back().ClearContext();
} break;
case Value::eValueTypeLoadAddress:
if (exe_ctx) {
if (process) {
lldb::addr_t pointer_addr =
stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
uint8_t addr_bytes[sizeof(lldb::addr_t)];
Status error;
if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
size) {
DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
process->GetByteOrder(), size);
lldb::offset_t addr_data_offset = 0;
switch (size) {
case 1:
stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
break;
case 2:
stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
break;
case 4:
stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
break;
case 8:
stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
break;
default:
stack.back().GetScalar() =
addr_data.GetPointer(&addr_data_offset);
}
stack.back().ClearContext();
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"Failed to dereference pointer from 0x%" PRIx64
" for DW_OP_deref: %s\n",
pointer_addr, error.AsCString());
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"NULL process for DW_OP_deref.\n");
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"NULL execution context for DW_OP_deref.\n");
return false;
}
break;
default:
break;
}
} break;
// OPCODE: DW_OP_xderef_size
// OPERANDS: 1
// 1 - uint8_t that specifies the size of the data to dereference.
// DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
// the top of the stack is treated as an address. The second stack entry is
// treated as an "address space identifier" for those architectures that
// support multiple address spaces. The top two stack elements are popped,
// a data item is retrieved through an implementation-defined address
// calculation and pushed as the new stack top. In the DW_OP_xderef_size
// operation, however, the size in bytes of the data retrieved from the
// dereferenced address is specified by the single operand. This operand is
// a 1-byte unsigned integral constant whose value may not be larger than
// the size of an address on the target machine. The data retrieved is zero
// extended to the size of an address on the target machine before being
// pushed on the expression stack.
case DW_OP_xderef_size:
if (error_ptr)
error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
return false;
// OPCODE: DW_OP_xderef
// OPERANDS: none
// DESCRIPTION: Provides an extended dereference mechanism. The entry at
// the top of the stack is treated as an address. The second stack entry is
// treated as an "address space identifier" for those architectures that
// support multiple address spaces. The top two stack elements are popped,
// a data item is retrieved through an implementation-defined address
// calculation and pushed as the new stack top. The size of the data
// retrieved from the dereferenced address is the size of an address on the
// target machine.
case DW_OP_xderef:
if (error_ptr)
error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
return false;
// All DW_OP_constXXX opcodes have a single operand as noted below:
//
// Opcode Operand 1
// DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
// 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
// constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
// 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
// constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
// 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
// constant DW_OP_consts signed LEB128 integer constant
case DW_OP_const1u:
stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
break;
case DW_OP_const1s:
stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
break;
case DW_OP_const2u:
stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
break;
case DW_OP_const2s:
stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
break;
case DW_OP_const4u:
stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
break;
case DW_OP_const4s:
stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
break;
case DW_OP_const8u:
stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
break;
case DW_OP_const8s:
stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
break;
case DW_OP_constu:
stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
break;
case DW_OP_consts:
stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
break;
// OPCODE: DW_OP_dup
// OPERANDS: none
// DESCRIPTION: duplicates the value at the top of the stack
case DW_OP_dup:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
return false;
} else
stack.push_back(stack.back());
break;
// OPCODE: DW_OP_drop
// OPERANDS: none
// DESCRIPTION: pops the value at the top of the stack
case DW_OP_drop:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
return false;
} else
stack.pop_back();
break;
// OPCODE: DW_OP_over
// OPERANDS: none
// DESCRIPTION: Duplicates the entry currently second in the stack at
// the top of the stack.
case DW_OP_over:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_over.");
return false;
} else
stack.push_back(stack[stack.size() - 2]);
break;
// OPCODE: DW_OP_pick
// OPERANDS: uint8_t index into the current stack
// DESCRIPTION: The stack entry with the specified index (0 through 255,
// inclusive) is pushed on the stack
case DW_OP_pick: {
uint8_t pick_idx = opcodes.GetU8(&offset);
if (pick_idx < stack.size())
stack.push_back(stack[stack.size() - 1 - pick_idx]);
else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"Index %u out of range for DW_OP_pick.\n", pick_idx);
return false;
}
} break;
// OPCODE: DW_OP_swap
// OPERANDS: none
// DESCRIPTION: swaps the top two stack entries. The entry at the top
// of the stack becomes the second stack entry, and the second entry
// becomes the top of the stack
case DW_OP_swap:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_swap.");
return false;
} else {
tmp = stack.back();
stack.back() = stack[stack.size() - 2];
stack[stack.size() - 2] = tmp;
}
break;
// OPCODE: DW_OP_rot
// OPERANDS: none
// DESCRIPTION: Rotates the first three stack entries. The entry at
// the top of the stack becomes the third stack entry, the second entry
// becomes the top of the stack, and the third entry becomes the second
// entry.
case DW_OP_rot:
if (stack.size() < 3) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 3 items for DW_OP_rot.");
return false;
} else {
size_t last_idx = stack.size() - 1;
Value old_top = stack[last_idx];
stack[last_idx] = stack[last_idx - 1];
stack[last_idx - 1] = stack[last_idx - 2];
stack[last_idx - 2] = old_top;
}
break;
// OPCODE: DW_OP_abs
// OPERANDS: none
// DESCRIPTION: pops the top stack entry, interprets it as a signed
// value and pushes its absolute value. If the absolute value can not be
// represented, the result is undefined.
case DW_OP_abs:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_abs.");
return false;
} else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
if (error_ptr)
error_ptr->SetErrorString(
"Failed to take the absolute value of the first stack item.");
return false;
}
break;
// OPCODE: DW_OP_and
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, performs a bitwise and
// operation on the two, and pushes the result.
case DW_OP_and:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_and.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_div
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, divides the former second
// entry by the former top of the stack using signed division, and pushes
// the result.
case DW_OP_div:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_div.");
return false;
} else {
tmp = stack.back();
if (tmp.ResolveValue(exe_ctx).IsZero()) {
if (error_ptr)
error_ptr->SetErrorString("Divide by zero.");
return false;
} else {
stack.pop_back();
stack.back() =
stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
if (error_ptr)
error_ptr->SetErrorString("Divide failed.");
return false;
}
}
}
break;
// OPCODE: DW_OP_minus
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, subtracts the former top
// of the stack from the former second entry, and pushes the result.
case DW_OP_minus:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_minus.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_mod
// OPERANDS: none
// DESCRIPTION: pops the top two stack values and pushes the result of
// the calculation: former second stack entry modulo the former top of the
// stack.
case DW_OP_mod:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_mod.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_mul
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, multiplies them
// together, and pushes the result.
case DW_OP_mul:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_mul.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_neg
// OPERANDS: none
// DESCRIPTION: pops the top stack entry, and pushes its negation.
case DW_OP_neg:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_neg.");
return false;
} else {
if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
if (error_ptr)
error_ptr->SetErrorString("Unary negate failed.");
return false;
}
}
break;
// OPCODE: DW_OP_not
// OPERANDS: none
// DESCRIPTION: pops the top stack entry, and pushes its bitwise
// complement
case DW_OP_not:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_not.");
return false;
} else {
if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
if (error_ptr)
error_ptr->SetErrorString("Logical NOT failed.");
return false;
}
}
break;
// OPCODE: DW_OP_or
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, performs a bitwise or
// operation on the two, and pushes the result.
case DW_OP_or:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_or.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_plus
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, adds them together, and
// pushes the result.
case DW_OP_plus:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_plus.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().GetScalar() += tmp.GetScalar();
}
break;
// OPCODE: DW_OP_plus_uconst
// OPERANDS: none
// DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
// constant operand and pushes the result.
case DW_OP_plus_uconst:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_plus_uconst.");
return false;
} else {
const uint64_t uconst_value = opcodes.GetULEB128(&offset);
// Implicit conversion from a UINT to a Scalar...
stack.back().GetScalar() += uconst_value;
if (!stack.back().GetScalar().IsValid()) {
if (error_ptr)
error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
return false;
}
}
break;
// OPCODE: DW_OP_shl
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, shifts the former
// second entry left by the number of bits specified by the former top of
// the stack, and pushes the result.
case DW_OP_shl:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_shl.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_shr
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, shifts the former second
// entry right logically (filling with zero bits) by the number of bits
// specified by the former top of the stack, and pushes the result.
case DW_OP_shr:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_shr.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
tmp.ResolveValue(exe_ctx))) {
if (error_ptr)
error_ptr->SetErrorString("DW_OP_shr failed.");
return false;
}
}
break;
// OPCODE: DW_OP_shra
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, shifts the former second
// entry right arithmetically (divide the magnitude by 2, keep the same
// sign for the result) by the number of bits specified by the former top
// of the stack, and pushes the result.
case DW_OP_shra:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_shra.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_xor
// OPERANDS: none
// DESCRIPTION: pops the top two stack entries, performs the bitwise
// exclusive-or operation on the two, and pushes the result.
case DW_OP_xor:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_xor.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_skip
// OPERANDS: int16_t
// DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
// signed integer constant. The 2-byte constant is the number of bytes of
// the DWARF expression to skip forward or backward from the current
// operation, beginning after the 2-byte constant.
case DW_OP_skip: {
int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
lldb::offset_t new_offset = offset + skip_offset;
if (new_offset >= opcodes_offset && new_offset < end_offset)
offset = new_offset;
else {
if (error_ptr)
error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
return false;
}
} break;
// OPCODE: DW_OP_bra
// OPERANDS: int16_t
// DESCRIPTION: A conditional branch. Its single operand is a 2-byte
// signed integer constant. This operation pops the top of stack. If the
// value popped is not the constant 0, the 2-byte constant operand is the
// number of bytes of the DWARF expression to skip forward or backward from
// the current operation, beginning after the 2-byte constant.
case DW_OP_bra:
if (stack.empty()) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_bra.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
Scalar zero(0);
if (tmp.ResolveValue(exe_ctx) != zero) {
lldb::offset_t new_offset = offset + bra_offset;
if (new_offset >= opcodes_offset && new_offset < end_offset)
offset = new_offset;
else {
if (error_ptr)
error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
return false;
}
}
}
break;
// OPCODE: DW_OP_eq
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// equals (==) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_eq:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_eq.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_ge
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// greater than or equal to (>=) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_ge:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_ge.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_gt
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// greater than (>) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_gt:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_gt.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_le
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// less than or equal to (<=) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_le:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_le.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_lt
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// less than (<) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_lt:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_lt.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_ne
// OPERANDS: none
// DESCRIPTION: pops the top two stack values, compares using the
// not equal (!=) operator.
// STACK RESULT: push the constant value 1 onto the stack if the result
// of the operation is true or the constant value 0 if the result of the
// operation is false.
case DW_OP_ne:
if (stack.size() < 2) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 2 items for DW_OP_ne.");
return false;
} else {
tmp = stack.back();
stack.pop_back();
stack.back().ResolveValue(exe_ctx) =
stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
}
break;
// OPCODE: DW_OP_litn
// OPERANDS: none
// DESCRIPTION: encode the unsigned literal values from 0 through 31.
// STACK RESULT: push the unsigned literal constant value onto the top
// of the stack.
case DW_OP_lit0:
case DW_OP_lit1:
case DW_OP_lit2:
case DW_OP_lit3:
case DW_OP_lit4:
case DW_OP_lit5:
case DW_OP_lit6:
case DW_OP_lit7:
case DW_OP_lit8:
case DW_OP_lit9:
case DW_OP_lit10:
case DW_OP_lit11:
case DW_OP_lit12:
case DW_OP_lit13:
case DW_OP_lit14:
case DW_OP_lit15:
case DW_OP_lit16:
case DW_OP_lit17:
case DW_OP_lit18:
case DW_OP_lit19:
case DW_OP_lit20:
case DW_OP_lit21:
case DW_OP_lit22:
case DW_OP_lit23:
case DW_OP_lit24:
case DW_OP_lit25:
case DW_OP_lit26:
case DW_OP_lit27:
case DW_OP_lit28:
case DW_OP_lit29:
case DW_OP_lit30:
case DW_OP_lit31:
stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
break;
// OPCODE: DW_OP_regN
// OPERANDS: none
// DESCRIPTION: Push the value in register n on the top of the stack.
case DW_OP_reg0:
case DW_OP_reg1:
case DW_OP_reg2:
case DW_OP_reg3:
case DW_OP_reg4:
case DW_OP_reg5:
case DW_OP_reg6:
case DW_OP_reg7:
case DW_OP_reg8:
case DW_OP_reg9:
case DW_OP_reg10:
case DW_OP_reg11:
case DW_OP_reg12:
case DW_OP_reg13:
case DW_OP_reg14:
case DW_OP_reg15:
case DW_OP_reg16:
case DW_OP_reg17:
case DW_OP_reg18:
case DW_OP_reg19:
case DW_OP_reg20:
case DW_OP_reg21:
case DW_OP_reg22:
case DW_OP_reg23:
case DW_OP_reg24:
case DW_OP_reg25:
case DW_OP_reg26:
case DW_OP_reg27:
case DW_OP_reg28:
case DW_OP_reg29:
case DW_OP_reg30:
case DW_OP_reg31: {
reg_num = op - DW_OP_reg0;
if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
stack.push_back(tmp);
else
return false;
} break;
// OPCODE: DW_OP_regx
// OPERANDS:
// ULEB128 literal operand that encodes the register.
// DESCRIPTION: Push the value in register on the top of the stack.
case DW_OP_regx: {
reg_num = opcodes.GetULEB128(&offset);
if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
stack.push_back(tmp);
else
return false;
} break;
// OPCODE: DW_OP_bregN
// OPERANDS:
// SLEB128 offset from register N
// DESCRIPTION: Value is in memory at the address specified by register
// N plus an offset.
case DW_OP_breg0:
case DW_OP_breg1:
case DW_OP_breg2:
case DW_OP_breg3:
case DW_OP_breg4:
case DW_OP_breg5:
case DW_OP_breg6:
case DW_OP_breg7:
case DW_OP_breg8:
case DW_OP_breg9:
case DW_OP_breg10:
case DW_OP_breg11:
case DW_OP_breg12:
case DW_OP_breg13:
case DW_OP_breg14:
case DW_OP_breg15:
case DW_OP_breg16:
case DW_OP_breg17:
case DW_OP_breg18:
case DW_OP_breg19:
case DW_OP_breg20:
case DW_OP_breg21:
case DW_OP_breg22:
case DW_OP_breg23:
case DW_OP_breg24:
case DW_OP_breg25:
case DW_OP_breg26:
case DW_OP_breg27:
case DW_OP_breg28:
case DW_OP_breg29:
case DW_OP_breg30:
case DW_OP_breg31: {
reg_num = op - DW_OP_breg0;
if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
tmp)) {
int64_t breg_offset = opcodes.GetSLEB128(&offset);
tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
tmp.ClearContext();
stack.push_back(tmp);
stack.back().SetValueType(Value::eValueTypeLoadAddress);
} else
return false;
} break;
// OPCODE: DW_OP_bregx
// OPERANDS: 2
// ULEB128 literal operand that encodes the register.
// SLEB128 offset from register N
// DESCRIPTION: Value is in memory at the address specified by register
// N plus an offset.
case DW_OP_bregx: {
reg_num = opcodes.GetULEB128(&offset);
if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
tmp)) {
int64_t breg_offset = opcodes.GetSLEB128(&offset);
tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
tmp.ClearContext();
stack.push_back(tmp);
stack.back().SetValueType(Value::eValueTypeLoadAddress);
} else
return false;
} break;
case DW_OP_fbreg:
if (exe_ctx) {
if (frame) {
Scalar value;
if (frame->GetFrameBaseValue(value, error_ptr)) {
int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
value += fbreg_offset;
stack.push_back(value);
stack.back().SetValueType(Value::eValueTypeLoadAddress);
} else
return false;
} else {
if (error_ptr)
error_ptr->SetErrorString(
"Invalid stack frame in context for DW_OP_fbreg opcode.");
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"NULL execution context for DW_OP_fbreg.\n");
return false;
}
break;
// OPCODE: DW_OP_nop
// OPERANDS: none
// DESCRIPTION: A place holder. It has no effect on the location stack
// or any of its values.
case DW_OP_nop:
break;
// OPCODE: DW_OP_piece
// OPERANDS: 1
// ULEB128: byte size of the piece
// DESCRIPTION: The operand describes the size in bytes of the piece of
// the object referenced by the DWARF expression whose result is at the top
// of the stack. If the piece is located in a register, but does not occupy
// the entire register, the placement of the piece within that register is
// defined by the ABI.
//
// Many compilers store a single variable in sets of registers, or store a
// variable partially in memory and partially in registers. DW_OP_piece
// provides a way of describing how large a part of a variable a particular
// DWARF expression refers to.
case DW_OP_piece: {
const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
if (piece_byte_size > 0) {
Value curr_piece;
if (stack.empty()) {
// In a multi-piece expression, this means that the current piece is
// not available. Fill with zeros for now by resizing the data and
// appending it
curr_piece.ResizeData(piece_byte_size);
::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
pieces.AppendDataToHostBuffer(curr_piece);
} else {
Status error;
// Extract the current piece into "curr_piece"
Value curr_piece_source_value(stack.back());
stack.pop_back();
const Value::ValueType curr_piece_source_value_type =
curr_piece_source_value.GetValueType();
switch (curr_piece_source_value_type) {
case Value::eValueTypeLoadAddress:
if (process) {
if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
lldb::addr_t load_addr =
curr_piece_source_value.GetScalar().ULongLong(
LLDB_INVALID_ADDRESS);
if (process->ReadMemory(
load_addr, curr_piece.GetBuffer().GetBytes(),
piece_byte_size, error) != piece_byte_size) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"failed to read memory DW_OP_piece(%" PRIu64
") from 0x%" PRIx64,
piece_byte_size, load_addr);
return false;
}
} else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"failed to resize the piece memory buffer for "
"DW_OP_piece(%" PRIu64 ")",
piece_byte_size);
return false;
}
}
break;
case Value::eValueTypeFileAddress:
case Value::eValueTypeHostAddress:
if (error_ptr) {
lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
LLDB_INVALID_ADDRESS);
error_ptr->SetErrorStringWithFormat(
"failed to read memory DW_OP_piece(%" PRIu64
") from %s address 0x%" PRIx64,
piece_byte_size, curr_piece_source_value.GetValueType() ==
Value::eValueTypeFileAddress
? "file"
: "host",
addr);
}
return false;
case Value::eValueTypeScalar: {
uint32_t bit_size = piece_byte_size * 8;
uint32_t bit_offset = 0;
if (!curr_piece_source_value.GetScalar().ExtractBitfield(
bit_size, bit_offset)) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"unable to extract %" PRIu64 " bytes from a %" PRIu64
" byte scalar value.",
piece_byte_size,
(uint64_t)curr_piece_source_value.GetScalar()
.GetByteSize());
return false;
}
curr_piece = curr_piece_source_value;
} break;
case Value::eValueTypeVector: {
if (curr_piece_source_value.GetVector().length >= piece_byte_size)
curr_piece_source_value.GetVector().length = piece_byte_size;
else {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"unable to extract %" PRIu64 " bytes from a %" PRIu64
" byte vector value.",
piece_byte_size,
(uint64_t)curr_piece_source_value.GetVector().length);
return false;
}
} break;
}
// Check if this is the first piece?
if (op_piece_offset == 0) {
// This is the first piece, we should push it back onto the stack
// so subsequent pieces will be able to access this piece and add
// to it
if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
if (error_ptr)
error_ptr->SetErrorString("failed to append piece data");
return false;
}
} else {
// If this is the second or later piece there should be a value on
// the stack
if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"DW_OP_piece for offset %" PRIu64
" but top of stack is of size %" PRIu64,
op_piece_offset, pieces.GetBuffer().GetByteSize());
return false;
}
if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
if (error_ptr)
error_ptr->SetErrorString("failed to append piece data");
return false;
}
}
op_piece_offset += piece_byte_size;
}
}
} break;
case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
if (stack.size() < 1) {
if (error_ptr)
error_ptr->SetErrorString(
"Expression stack needs at least 1 item for DW_OP_bit_piece.");
return false;
} else {
const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
switch (stack.back().GetValueType()) {
case Value::eValueTypeScalar: {
if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
piece_bit_offset)) {
if (error_ptr)
error_ptr->SetErrorStringWithFormat(
"unable to extract %" PRIu64 " bit value with %" PRIu64
" bit offset from a %" PRIu64 " bit scalar value.",
piece_bit_size, piece_bit_offset,
(uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
return false;
}
} break;
case Value::eValueTypeFileAddress:
case Value::eValueTypeLoadAddress:
case Value::eValueTypeHostAddress:
if (error_ptr) {
error_ptr->SetErrorStringWithFormat(
"unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
", bit_offset = %" PRIu64 ") from an address value.",
piece_bit_size, piece_bit_offset);
}
return false;
case Value::eValueTypeVector:
if (error_ptr) {
error_ptr->SetErrorStringWithFormat(
"unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
", bit_offset = %" PRIu64 ") from a vector value.",
piece_bit_size, piece_bit_offset);
}
return false;
}
}
break;
// OPCODE: DW_OP_push_object_address
// OPERANDS: none
// DESCRIPTION: Pushes the address of the object currently being
// evaluated as part of evaluation of a user presented expression. This
// object may correspond to an independent variable described by its own
// DIE or it may be a component of an array, structure, or class whose
// address has been dynamically determined by an earlier step during user
// expression evaluation.
case DW_OP_push_object_address:
if (object_address_ptr)
stack.push_back(*object_address_ptr);
else {
if (error_ptr)
error_ptr->SetErrorString("DW_OP_push_object_address used without "
"specifying an object address");
return false;
}
break;
// OPCODE: DW_OP_call2
// OPERANDS:
// uint16_t compile unit relative offset of a DIE
// DESCRIPTION: Performs subroutine calls during evaluation
// of a DWARF expression. The operand is the 2-byte unsigned offset of a
// debugging information entry in the current compilation unit.
//
// Operand interpretation is exactly like that for DW_FORM_ref2.
//
// This operation transfers control of DWARF expression evaluation to the
// DW_AT_location attribute of the referenced DIE. If there is no such
// attribute, then there is no effect. Execution of the DWARF expression of
// a DW_AT_location attribute may add to and/or remove from values on the
// stack. Execution returns to the point following the call when the end of
// the attribute is reached. Values on the stack at the time of the call
// may be used as parameters by the called expression and values left on
// the stack by the called expression may be used as return values by prior
// agreement between the calling and called expressions.
case DW_OP_call2:
if (error_ptr)
error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
return false;
// OPCODE: DW_OP_call4
// OPERANDS: 1
// uint32_t compile unit relative offset of a DIE
// DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
// expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
// a debugging information entry in the current compilation unit.
//
// Operand interpretation DW_OP_call4 is exactly like that for
// DW_FORM_ref4.
//
// This operation transfers control of DWARF expression evaluation to the
// DW_AT_location attribute of the referenced DIE. If there is no such
// attribute, then there is no effect. Execution of the DWARF expression of
// a DW_AT_location attribute may add to and/or remove from values on the
// stack. Execution returns to the point following the call when the end of
// the attribute is reached. Values on the stack at the time of the call
// may be used as parameters by the called expression and values left on
// the stack by the called expression may be used as return values by prior
// agreement between the calling and called expressions.
case DW_OP_call4:
if (error_ptr)
error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
return false;
// OPCODE: DW_OP_stack_value
// OPERANDS: None
// DESCRIPTION: Specifies that the object does not exist in memory but
// rather is a constant value. The value from the top of the stack is the
// value to be used. This is the actual object value and not the location.
case DW_OP_stack_value:
stack.back().SetValueType(Value::eValueTypeScalar);
break;
// OPCODE: DW_OP_call_frame_cfa
// OPERANDS: None
// DESCRIPTION: Specifies a DWARF expression that pushes the value of
// the canonical frame address consistent with the call frame information
// located in .debug_frame (or in the FDEs of the eh_frame section).
case DW_OP_call_frame_cfa:
if (frame) {
// Note that we don't have to parse FDEs because this DWARF expression
// is commonly evaluated with a valid stack frame.
StackID id = frame->GetStackID();
addr_t cfa = id.GetCallFrameAddress();
if (cfa != LLDB_INVALID_ADDRESS) {
stack.push_back(Scalar(cfa));
stack.back().SetValueType(Value::eValueTypeLoadAddress);
} else if (error_ptr)
error_ptr->SetErrorString("Stack frame does not include a canonical "
"frame address for DW_OP_call_frame_cfa "
"opcode.");
} else {
if (error_ptr)
error_ptr->SetErrorString("Invalid stack frame in context for "
"DW_OP_call_frame_cfa opcode.");
return false;
}
break;
// OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
// opcode, DW_OP_GNU_push_tls_address)
// OPERANDS: none
// DESCRIPTION: Pops a TLS offset from the stack, converts it to
// an address in the current thread's thread-local storage block, and
// pushes it on the stack.
case DW_OP_form_tls_address:
case DW_OP_GNU_push_tls_address: {
if (stack.size() < 1) {
if (error_ptr) {
if (op == DW_OP_form_tls_address)
error_ptr->SetErrorString(
"DW_OP_form_tls_address needs an argument.");
else
error_ptr->SetErrorString(
"DW_OP_GNU_push_tls_address needs an argument.");
}
return false;
}
if (!exe_ctx || !module_sp) {
if (error_ptr)
error_ptr->SetErrorString("No context to evaluate TLS within.");
return false;
}
Thread *thread = exe_ctx->GetThreadPtr();
if (!thread) {
if (error_ptr)
error_ptr->SetErrorString("No thread to evaluate TLS within.");
return false;
}
// Lookup the TLS block address for this thread and module.
const addr_t tls_file_addr =
stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
const addr_t tls_load_addr =
thread->GetThreadLocalData(module_sp, tls_file_addr);
if (tls_load_addr == LLDB_INVALID_ADDRESS) {
if (error_ptr)
error_ptr->SetErrorString(
"No TLS data currently exists for this thread.");
return false;
}
stack.back().GetScalar() = tls_load_addr;
stack.back().SetValueType(Value::eValueTypeLoadAddress);
} break;
// OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
// OPERANDS: 1
// ULEB128: index to the .debug_addr section
// DESCRIPTION: Pushes an address to the stack from the .debug_addr
// section with the base address specified by the DW_AT_addr_base attribute
// and the 0 based index is the ULEB128 encoded index.
case DW_OP_addrx:
case DW_OP_GNU_addr_index: {
if (!dwarf_cu) {
if (error_ptr)
error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
"compile unit being specified");
return false;
}
uint64_t index = opcodes.GetULEB128(&offset);
lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
stack.push_back(Scalar(value));
stack.back().SetValueType(Value::eValueTypeFileAddress);
} break;
// OPCODE: DW_OP_GNU_const_index
// OPERANDS: 1
// ULEB128: index to the .debug_addr section
// DESCRIPTION: Pushes an constant with the size of a machine address to
// the stack from the .debug_addr section with the base address specified
// by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
// encoded index.
case DW_OP_GNU_const_index: {
if (!dwarf_cu) {
if (error_ptr)
error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
"compile unit being specified");
return false;
}
uint64_t index = opcodes.GetULEB128(&offset);
lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index);
stack.push_back(Scalar(value));
} break;
default:
if (log)
log->Printf("Unhandled opcode %s in DWARFExpression.",
DW_OP_value_to_name(op));
break;
}
}
if (stack.empty()) {
// Nothing on the stack, check if we created a piece value from DW_OP_piece
// or DW_OP_bit_piece opcodes
if (pieces.GetBuffer().GetByteSize()) {
result = pieces;
} else {
if (error_ptr)
error_ptr->SetErrorString("Stack empty after evaluation.");
return false;
}
} else {
if (log && log->GetVerbose()) {
size_t count = stack.size();
log->Printf("Stack after operation has %" PRIu64 " values:",
(uint64_t)count);
for (size_t i = 0; i < count; ++i) {
StreamString new_value;
new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
stack[i].Dump(&new_value);
log->Printf(" %s", new_value.GetData());
}
}
result = stack.back();
}
return true; // Return true on success
}
size_t DWARFExpression::LocationListSize(const DWARFUnit *dwarf_cu,
const DataExtractor &debug_loc_data,
lldb::offset_t offset) {
const lldb::offset_t debug_loc_offset = offset;
while (debug_loc_data.ValidOffset(offset)) {
lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
start_addr, end_addr))
break;
if (start_addr == 0 && end_addr == 0)
break;
uint16_t loc_length = debug_loc_data.GetU16(&offset);
offset += loc_length;
}
if (offset > debug_loc_offset)
return offset - debug_loc_offset;
return 0;
}
bool DWARFExpression::AddressRangeForLocationListEntry(
const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
if (!debug_loc_data.ValidOffset(*offset_ptr))
return false;
DWARFExpression::LocationListFormat format =
dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat();
switch (format) {
case NonLocationList:
return false;
case RegularLocationList:
low_pc = debug_loc_data.GetAddress(offset_ptr);
high_pc = debug_loc_data.GetAddress(offset_ptr);
return true;
case SplitDwarfLocationList:
case LocLists:
switch (debug_loc_data.GetU8(offset_ptr)) {
case DW_LLE_end_of_list:
return false;
case DW_LLE_startx_endx: {
uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
index = debug_loc_data.GetULEB128(offset_ptr);
high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
return true;
}
case DW_LLE_startx_length: {
uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
uint64_t length = (format == LocLists)
? debug_loc_data.GetULEB128(offset_ptr)
: debug_loc_data.GetU32(offset_ptr);
high_pc = low_pc + length;
return true;
}
case DW_LLE_start_length: {
low_pc = debug_loc_data.GetAddress(offset_ptr);
high_pc = low_pc + debug_loc_data.GetULEB128(offset_ptr);
return true;
}
case DW_LLE_start_end: {
low_pc = debug_loc_data.GetAddress(offset_ptr);
high_pc = debug_loc_data.GetAddress(offset_ptr);
return true;
}
default:
// Not supported entry type
lldbassert(false && "Not supported location list type");
return false;
}
}
assert(false && "Not supported location list type");
return false;
}
static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
lldb::offset_t *offset_ptr, int address_size,
int dwarf_ref_size) {
uint8_t opcode = data.GetU8(offset_ptr);
DRC_class opcode_class;
uint64_t uint;
int64_t sint;
int size;
opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
s.Printf("%s ", DW_OP_value_to_name(opcode));
/* Does this take zero parameters? If so we can shortcut this function. */
if (opcode_class == DRC_ZEROOPERANDS)
return true;
if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
uint = data.GetULEB128(offset_ptr);
sint = data.GetSLEB128(offset_ptr);
s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
return true;
}
if (opcode_class != DRC_ONEOPERAND) {
s.Printf("UNKNOWN OP %u", opcode);
return false;
}
switch (opcode) {
case DW_OP_addr:
size = address_size;
break;
case DW_OP_const1u:
size = 1;
break;
case DW_OP_const1s:
size = -1;
break;
case DW_OP_const2u:
size = 2;
break;
case DW_OP_const2s:
size = -2;
break;
case DW_OP_const4u:
size = 4;
break;
case DW_OP_const4s:
size = -4;
break;
case DW_OP_const8u:
size = 8;
break;
case DW_OP_const8s:
size = -8;
break;
case DW_OP_constu:
size = 128;
break;
case DW_OP_consts:
size = -128;
break;
case DW_OP_fbreg:
size = -128;
break;
case DW_OP_breg0:
case DW_OP_breg1:
case DW_OP_breg2:
case DW_OP_breg3:
case DW_OP_breg4:
case DW_OP_breg5:
case DW_OP_breg6:
case DW_OP_breg7:
case DW_OP_breg8:
case DW_OP_breg9:
case DW_OP_breg10:
case DW_OP_breg11:
case DW_OP_breg12:
case DW_OP_breg13:
case DW_OP_breg14:
case DW_OP_breg15:
case DW_OP_breg16:
case DW_OP_breg17:
case DW_OP_breg18:
case DW_OP_breg19:
case DW_OP_breg20:
case DW_OP_breg21:
case DW_OP_breg22:
case DW_OP_breg23:
case DW_OP_breg24:
case DW_OP_breg25:
case DW_OP_breg26:
case DW_OP_breg27:
case DW_OP_breg28:
case DW_OP_breg29:
case DW_OP_breg30:
case DW_OP_breg31:
size = -128;
break;
case DW_OP_pick:
case DW_OP_deref_size:
case DW_OP_xderef_size:
size = 1;
break;
case DW_OP_skip:
case DW_OP_bra:
size = -2;
break;
case DW_OP_call2:
size = 2;
break;
case DW_OP_call4:
size = 4;
break;
case DW_OP_call_ref:
size = dwarf_ref_size;
break;
case DW_OP_addrx:
case DW_OP_piece:
case DW_OP_plus_uconst:
case DW_OP_regx:
case DW_OP_GNU_addr_index:
case DW_OP_GNU_const_index:
size = 128;
break;
default:
s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
return false;
}
switch (size) {
case -1:
sint = (int8_t)data.GetU8(offset_ptr);
s.Printf("%+" PRIi64, sint);
break;
case -2:
sint = (int16_t)data.GetU16(offset_ptr);
s.Printf("%+" PRIi64, sint);
break;
case -4:
sint = (int32_t)data.GetU32(offset_ptr);
s.Printf("%+" PRIi64, sint);
break;
case -8:
sint = (int64_t)data.GetU64(offset_ptr);
s.Printf("%+" PRIi64, sint);
break;
case -128:
sint = data.GetSLEB128(offset_ptr);
s.Printf("%+" PRIi64, sint);
break;
case 1:
uint = data.GetU8(offset_ptr);
s.Printf("0x%2.2" PRIx64, uint);
break;
case 2:
uint = data.GetU16(offset_ptr);
s.Printf("0x%4.4" PRIx64, uint);
break;
case 4:
uint = data.GetU32(offset_ptr);
s.Printf("0x%8.8" PRIx64, uint);
break;
case 8:
uint = data.GetU64(offset_ptr);
s.Printf("0x%16.16" PRIx64, uint);
break;
case 128:
uint = data.GetULEB128(offset_ptr);
s.Printf("0x%" PRIx64, uint);
break;
}
return true;
}
bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
int address_size, int dwarf_ref_size,
bool location_expression) {
int op_count = 0;
lldb::offset_t offset = 0;
while (data.ValidOffset(offset)) {
if (location_expression && op_count > 0)
return false;
if (op_count > 0)
s.PutCString(", ");
if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
return false;
op_count++;
}
return true;
}
void DWARFExpression::PrintDWARFLocationList(
Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
lldb::offset_t offset) {
uint64_t start_addr, end_addr;
uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
while (debug_loc_data.ValidOffset(offset)) {
start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
if (start_addr == 0 && end_addr == 0)
break;
s.PutCString("\n ");
s.Indent();
if (cu)
s.AddressRange(start_addr + base_addr, end_addr + base_addr,
cu->GetAddressByteSize(), nullptr, ": ");
uint32_t loc_length = debug_loc_data.GetU16(&offset);
DataExtractor locationData(debug_loc_data, offset, loc_length);
PrintDWARFExpression(s, locationData, addr_size, 4, false);
offset += loc_length;
}
}
bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
lldb::offset_t &op_offset,
lldb::offset_t &end_offset) {
SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
if (!sc.function) {
return false;
}
addr_t loclist_base_file_addr =
sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
return false;
}
addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
lldb::offset_t opcodes_offset, opcodes_length;
if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
opcodes_length)) {
return false;
}
if (opcodes_length == 0) {
return false;
}
op_offset = opcodes_offset;
end_offset = opcodes_offset + opcodes_length;
return true;
}
bool DWARFExpression::MatchesOperand(StackFrame &frame,
const Instruction::Operand &operand) {
using namespace OperandMatchers;
lldb::offset_t op_offset;
lldb::offset_t end_offset;
if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
return false;
}
if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
return false;
}
RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
if (!reg_ctx_sp) {
return false;
}
DataExtractor opcodes = m_data;
uint8_t opcode = opcodes.GetU8(&op_offset);
if (opcode == DW_OP_fbreg) {
int64_t offset = opcodes.GetSLEB128(&op_offset);
DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
if (!fb_expr) {
return false;
}
auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
return fb_expr->MatchesOperand(frame, child);
};
if (!offset &&
MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
recurse)(operand)) {
return true;
}
return MatchUnaryOp(
MatchOpType(Instruction::Operand::Type::Dereference),
MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
MatchImmOp(offset), recurse))(operand);
}
bool dereference = false;
const RegisterInfo *reg = nullptr;
int64_t offset = 0;
if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
} else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
offset = opcodes.GetSLEB128(&op_offset);
reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
} else if (opcode == DW_OP_regx) {
uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
} else if (opcode == DW_OP_bregx) {
uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
offset = opcodes.GetSLEB128(&op_offset);
reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
} else {
return false;
}
if (!reg) {
return false;
}
if (dereference) {
if (!offset &&
MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
MatchRegOp(*reg))(operand)) {
return true;
}
return MatchUnaryOp(
MatchOpType(Instruction::Operand::Type::Dereference),
MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
MatchRegOp(*reg),
MatchImmOp(offset)))(operand);
} else {
return MatchRegOp(*reg)(operand);
}
}
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 1e7cc0468f8b..b1e826582a83 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -1,4136 +1,4134 @@
//===-- DWARFASTParserClang.cpp ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <stdlib.h>
#include "DWARFASTParserClang.h"
#include "DWARFDIE.h"
#include "DWARFDebugInfo.h"
#include "DWARFDeclContext.h"
#include "DWARFDefines.h"
#include "SymbolFileDWARF.h"
#include "SymbolFileDWARFDwo.h"
#include "SymbolFileDWARFDebugMap.h"
#include "UniqueDWARFASTType.h"
#include "Plugins/Language/ObjC/ObjCLanguage.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Host/Host.h"
#include "lldb/Symbol/ClangASTImporter.h"
#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
#include "lldb/Symbol/ClangUtil.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Symbol/TypeMap.h"
#include "lldb/Target/Language.h"
#include "lldb/Utility/LLDBAssert.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include <map>
#include <memory>
#include <vector>
//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
#ifdef ENABLE_DEBUG_PRINTF
#include <stdio.h>
#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINTF(fmt, ...)
#endif
using namespace lldb;
using namespace lldb_private;
DWARFASTParserClang::DWARFASTParserClang(ClangASTContext &ast)
: m_ast(ast), m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
DWARFASTParserClang::~DWARFASTParserClang() {}
static AccessType DW_ACCESS_to_AccessType(uint32_t dwarf_accessibility) {
switch (dwarf_accessibility) {
case DW_ACCESS_public:
return eAccessPublic;
case DW_ACCESS_private:
return eAccessPrivate;
case DW_ACCESS_protected:
return eAccessProtected;
default:
break;
}
return eAccessNone;
}
static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
switch (decl_kind) {
case clang::Decl::CXXRecord:
case clang::Decl::ClassTemplateSpecialization:
return true;
default:
break;
}
return false;
}
struct BitfieldInfo {
uint64_t bit_size;
uint64_t bit_offset;
BitfieldInfo()
: bit_size(LLDB_INVALID_ADDRESS), bit_offset(LLDB_INVALID_ADDRESS) {}
void Clear() {
bit_size = LLDB_INVALID_ADDRESS;
bit_offset = LLDB_INVALID_ADDRESS;
}
bool IsValid() const {
return (bit_size != LLDB_INVALID_ADDRESS) &&
(bit_offset != LLDB_INVALID_ADDRESS);
}
bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const {
if (IsValid()) {
// This bitfield info is valid, so any subsequent bitfields must not
// overlap and must be at a higher bit offset than any previous bitfield
// + size.
return (bit_size + bit_offset) <= next_bit_offset;
} else {
// If the this BitfieldInfo is not valid, then any offset isOK
return true;
}
}
};
ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() {
if (!m_clang_ast_importer_up) {
m_clang_ast_importer_up.reset(new ClangASTImporter);
}
return *m_clang_ast_importer_up;
}
/// Detect a forward declaration that is nested in a DW_TAG_module.
static bool IsClangModuleFwdDecl(const DWARFDIE &Die) {
if (!Die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0))
return false;
auto Parent = Die.GetParent();
while (Parent.IsValid()) {
if (Parent.Tag() == DW_TAG_module)
return true;
Parent = Parent.GetParent();
}
return false;
}
TypeSP DWARFASTParserClang::ParseTypeFromDWO(const DWARFDIE &die, Log *log) {
ModuleSP dwo_module_sp = die.GetContainingDWOModule();
if (!dwo_module_sp)
return TypeSP();
// If this type comes from a Clang module, look in the DWARF section
// of the pcm file in the module cache. Clang generates DWO skeleton
// units as breadcrumbs to find them.
std::vector<CompilerContext> decl_context;
die.GetDeclContext(decl_context);
TypeMap dwo_types;
if (!dwo_module_sp->GetSymbolVendor()->FindTypes(decl_context, true,
dwo_types)) {
if (!IsClangModuleFwdDecl(die))
return TypeSP();
// Since this this type is defined in one of the Clang modules imported by
// this symbol file, search all of them.
auto *sym_file = die.GetCU()->GetSymbolFileDWARF();
for (const auto &name_module : sym_file->getExternalTypeModules()) {
if (!name_module.second)
continue;
SymbolVendor *sym_vendor = name_module.second->GetSymbolVendor();
if (sym_vendor->FindTypes(decl_context, true, dwo_types))
break;
}
}
if (dwo_types.GetSize() != 1)
return TypeSP();
// We found a real definition for this type in the Clang module, so lets use
// it and cache the fact that we found a complete type for this die.
TypeSP dwo_type_sp = dwo_types.GetTypeAtIndex(0);
if (!dwo_type_sp)
return TypeSP();
lldb_private::CompilerType dwo_type = dwo_type_sp->GetForwardCompilerType();
lldb_private::CompilerType type =
GetClangASTImporter().CopyType(m_ast, dwo_type);
if (!type)
return TypeSP();
SymbolFileDWARF *dwarf = die.GetDWARF();
TypeSP type_sp(new Type(
die.GetID(), dwarf, dwo_type_sp->GetName(), dwo_type_sp->GetByteSize(),
nullptr, LLDB_INVALID_UID, Type::eEncodingInvalid,
&dwo_type_sp->GetDeclaration(), type, Type::eResolveStateForward));
dwarf->GetTypeList()->Insert(type_sp);
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
clang::TagDecl *tag_decl = ClangASTContext::GetAsTagDecl(type);
if (tag_decl)
LinkDeclContextToDIE(tag_decl, die);
else {
clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
if (defn_decl_ctx)
LinkDeclContextToDIE(defn_decl_ctx, die);
}
return type_sp;
}
static void CompleteExternalTagDeclType(ClangASTImporter &ast_importer,
clang::DeclContext *decl_ctx,
DWARFDIE die,
const char *type_name_cstr) {
auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
if (!tag_decl_ctx)
return;
// If this type was not imported from an external AST, there's nothing to do.
CompilerType type = ClangASTContext::GetTypeForDecl(tag_decl_ctx);
if (!type || !ast_importer.CanImport(type))
return;
auto qual_type = ClangUtil::GetQualType(type);
if (!ast_importer.RequireCompleteType(qual_type)) {
die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
"Unable to complete the Decl context for DIE '%s' at offset "
"0x%8.8x.\nPlease file a bug report.",
type_name_cstr ? type_name_cstr : "", die.GetOffset());
// We need to make the type look complete otherwise, we might crash in
// Clang when adding children.
if (ClangASTContext::StartTagDeclarationDefinition(type))
ClangASTContext::CompleteTagDeclarationDefinition(type);
}
}
TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
const DWARFDIE &die, Log *log,
bool *type_is_new_ptr) {
if (type_is_new_ptr)
*type_is_new_ptr = false;
AccessType accessibility = eAccessNone;
if (!die)
return nullptr;
SymbolFileDWARF *dwarf = die.GetDWARF();
if (log) {
DWARFDIE context_die;
clang::DeclContext *context =
GetClangDeclContextContainingDIE(die, &context_die);
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die "
"0x%8.8x)) %s name = '%s')",
die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
die.GetTagAsCString(), die.GetName());
}
Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
if (type_ptr == DIE_IS_BEING_PARSED)
return nullptr;
if (type_ptr)
return type_ptr->shared_from_this();
// Set a bit that lets us know that we are currently parsing this
dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
if (DWARFDIE signature_die =
die.GetAttributeValueAsReferenceDIE(DW_AT_signature)) {
if (TypeSP type_sp =
ParseTypeFromDWARF(sc, signature_die, log, type_is_new_ptr)) {
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
if (clang::DeclContext *decl_ctx =
GetCachedClangDeclContextForDIE(signature_die))
LinkDeclContextToDIE(decl_ctx, die);
return type_sp;
}
return nullptr;
}
TypeList *type_list = dwarf->GetTypeList();
if (type_is_new_ptr)
*type_is_new_ptr = true;
const dw_tag_t tag = die.Tag();
bool is_forward_declaration = false;
DWARFAttributes attributes;
const char *type_name_cstr = NULL;
const char *mangled_name_cstr = NULL;
ConstString type_name_const_str;
Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
llvm::Optional<uint64_t> byte_size;
Declaration decl;
Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
CompilerType clang_type;
DWARFFormValue form_value;
dw_attr_t attr;
TypeSP type_sp;
switch (tag) {
case DW_TAG_typedef:
case DW_TAG_base_type:
case DW_TAG_pointer_type:
case DW_TAG_reference_type:
case DW_TAG_rvalue_reference_type:
case DW_TAG_const_type:
case DW_TAG_restrict_type:
case DW_TAG_volatile_type:
case DW_TAG_unspecified_type: {
const size_t num_attributes = die.GetAttributes(attributes);
uint32_t encoding = 0;
DWARFFormValue encoding_uid;
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
type_name_cstr = form_value.AsCString();
if (type_name_cstr)
type_name_const_str.SetCString(type_name_cstr);
break;
case DW_AT_byte_size:
byte_size = form_value.Unsigned();
break;
case DW_AT_encoding:
encoding = form_value.Unsigned();
break;
case DW_AT_type:
encoding_uid = form_value;
break;
default:
case DW_AT_sibling:
break;
}
}
}
}
if (tag == DW_TAG_typedef && encoding_uid.IsValid()) {
// Try to parse a typedef from the DWO file first as modules can
// contain typedef'ed structures that have no names like:
//
// typedef struct { int a; } Foo;
//
// In this case we will have a structure with no name and a typedef
// named "Foo" that points to this unnamed structure. The name in the
// typedef is the only identifier for the struct, so always try to
// get typedefs from DWO files if possible.
//
// The type_sp returned will be empty if the typedef doesn't exist in
// a DWO file, so it is cheap to call this function just to check.
//
// If we don't do this we end up creating a TypeSP that says this is
// a typedef to type 0x123 (the DW_AT_type value would be 0x123 in
// the DW_TAG_typedef), and this is the unnamed structure type. We
// will have a hard time tracking down an unnammed structure type in
// the module DWO file, so we make sure we don't get into this
// situation by always resolving typedefs from the DWO file.
const DWARFDIE encoding_die = encoding_uid.Reference();
// First make sure that the die that this is typedef'ed to _is_ just
// a declaration (DW_AT_declaration == 1), not a full definition
// since template types can't be represented in modules since only
// concrete instances of templates are ever emitted and modules won't
// contain those
if (encoding_die &&
encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
type_sp = ParseTypeFromDWO(die, log);
if (type_sp)
return type_sp;
}
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n",
die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr,
encoding_uid.Reference());
switch (tag) {
default:
break;
case DW_TAG_unspecified_type:
if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
strcmp(type_name_cstr, "decltype(nullptr)") == 0) {
resolve_state = Type::eResolveStateFull;
clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
break;
}
// Fall through to base type below in case we can handle the type
// there...
LLVM_FALLTHROUGH;
case DW_TAG_base_type:
resolve_state = Type::eResolveStateFull;
clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
type_name_cstr, encoding, byte_size.getValueOr(0) * 8);
break;
case DW_TAG_pointer_type:
encoding_data_type = Type::eEncodingIsPointerUID;
break;
case DW_TAG_reference_type:
encoding_data_type = Type::eEncodingIsLValueReferenceUID;
break;
case DW_TAG_rvalue_reference_type:
encoding_data_type = Type::eEncodingIsRValueReferenceUID;
break;
case DW_TAG_typedef:
encoding_data_type = Type::eEncodingIsTypedefUID;
break;
case DW_TAG_const_type:
encoding_data_type = Type::eEncodingIsConstUID;
break;
case DW_TAG_restrict_type:
encoding_data_type = Type::eEncodingIsRestrictUID;
break;
case DW_TAG_volatile_type:
encoding_data_type = Type::eEncodingIsVolatileUID;
break;
}
if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
encoding_data_type == Type::eEncodingIsTypedefUID)) {
if (tag == DW_TAG_pointer_type) {
DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
// Blocks have a __FuncPtr inside them which is a pointer to a
// function of the proper type.
for (DWARFDIE child_die = target_die.GetFirstChild();
child_die.IsValid(); child_die = child_die.GetSibling()) {
if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
"__FuncPtr")) {
DWARFDIE function_pointer_type =
child_die.GetReferencedDIE(DW_AT_type);
if (function_pointer_type) {
DWARFDIE function_type =
function_pointer_type.GetReferencedDIE(DW_AT_type);
bool function_type_is_new_pointer;
TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
sc, function_type, log, &function_type_is_new_pointer);
if (lldb_function_type_sp) {
clang_type = m_ast.CreateBlockPointerType(
lldb_function_type_sp->GetForwardCompilerType());
encoding_data_type = Type::eEncodingIsUID;
encoding_uid.Clear();
resolve_state = Type::eResolveStateFull;
}
}
break;
}
}
}
}
bool translation_unit_is_objc =
(sc.comp_unit->GetLanguage() == eLanguageTypeObjC ||
sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
if (translation_unit_is_objc) {
if (type_name_cstr != nullptr) {
static ConstString g_objc_type_name_id("id");
static ConstString g_objc_type_name_Class("Class");
static ConstString g_objc_type_name_selector("SEL");
if (type_name_const_str == g_objc_type_name_id) {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
"is Objective-C 'id' built-in type.",
die.GetOffset(), die.GetTagAsCString(), die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
encoding_data_type = Type::eEncodingIsUID;
encoding_uid.Clear();
resolve_state = Type::eResolveStateFull;
} else if (type_name_const_str == g_objc_type_name_Class) {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
"is Objective-C 'Class' built-in type.",
die.GetOffset(), die.GetTagAsCString(), die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
encoding_data_type = Type::eEncodingIsUID;
encoding_uid.Clear();
resolve_state = Type::eResolveStateFull;
} else if (type_name_const_str == g_objc_type_name_selector) {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
"is Objective-C 'selector' built-in type.",
die.GetOffset(), die.GetTagAsCString(), die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
encoding_data_type = Type::eEncodingIsUID;
encoding_uid.Clear();
resolve_state = Type::eResolveStateFull;
}
} else if (encoding_data_type == Type::eEncodingIsPointerUID &&
encoding_uid.IsValid()) {
// Clang sometimes erroneously emits id as objc_object*. In that
// case we fix up the type to "id".
const DWARFDIE encoding_die = encoding_uid.Reference();
if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
if (const char *struct_name = encoding_die.GetName()) {
if (!strcmp(struct_name, "objc_object")) {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = 0x%8.8x) %s "
"'%s' is 'objc_object*', which we overrode to "
"'id'.",
die.GetOffset(), die.GetTagAsCString(), die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
encoding_data_type = Type::eEncodingIsUID;
encoding_uid.Clear();
resolve_state = Type::eResolveStateFull;
}
}
}
}
}
}
type_sp = std::make_shared<Type>(
die.GetID(), dwarf, type_name_const_str, byte_size, nullptr,
dwarf->GetUID(DIERef(encoding_uid)), encoding_data_type, &decl,
clang_type, resolve_state);
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
} break;
case DW_TAG_structure_type:
case DW_TAG_union_type:
case DW_TAG_class_type: {
LanguageType class_language = eLanguageTypeUnknown;
bool is_complete_objc_class = false;
size_t calling_convention = llvm::dwarf::CallingConvention::DW_CC_normal;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
type_name_cstr = form_value.AsCString();
type_name_const_str.SetCString(type_name_cstr);
break;
case DW_AT_byte_size:
byte_size = form_value.Unsigned();
break;
case DW_AT_accessibility:
accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
break;
case DW_AT_declaration:
is_forward_declaration = form_value.Boolean();
break;
case DW_AT_APPLE_runtime_class:
class_language = (LanguageType)form_value.Signed();
break;
case DW_AT_APPLE_objc_complete_type:
is_complete_objc_class = form_value.Signed();
break;
case DW_AT_calling_convention:
calling_convention = form_value.Unsigned();
break;
case DW_AT_allocated:
case DW_AT_associated:
case DW_AT_data_location:
case DW_AT_description:
case DW_AT_start_scope:
case DW_AT_visibility:
default:
case DW_AT_sibling:
break;
}
}
}
}
// UniqueDWARFASTType is large, so don't create a local variables on
// the stack, put it on the heap. This function is often called
// recursively and clang isn't good and sharing the stack space for
// variables in different blocks.
std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_up(
new UniqueDWARFASTType());
ConstString unique_typename(type_name_const_str);
Declaration unique_decl(decl);
if (type_name_const_str) {
LanguageType die_language = die.GetLanguage();
if (Language::LanguageIsCPlusPlus(die_language)) {
// For C++, we rely solely upon the one definition rule that says
// only one thing can exist at a given decl context. We ignore the
// file and line that things are declared on.
std::string qualified_name;
if (die.GetQualifiedName(qualified_name))
unique_typename = ConstString(qualified_name);
unique_decl.Clear();
}
if (dwarf->GetUniqueDWARFASTTypeMap().Find(
unique_typename, die, unique_decl, byte_size ? *byte_size : -1,
*unique_ast_entry_up)) {
type_sp = unique_ast_entry_up->m_type_sp;
if (type_sp) {
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
return type_sp;
}
}
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
DW_TAG_value_to_name(tag), type_name_cstr);
int tag_decl_kind = -1;
AccessType default_accessibility = eAccessNone;
if (tag == DW_TAG_structure_type) {
tag_decl_kind = clang::TTK_Struct;
default_accessibility = eAccessPublic;
} else if (tag == DW_TAG_union_type) {
tag_decl_kind = clang::TTK_Union;
default_accessibility = eAccessPublic;
} else if (tag == DW_TAG_class_type) {
tag_decl_kind = clang::TTK_Class;
default_accessibility = eAccessPrivate;
}
if (byte_size && *byte_size == 0 && type_name_cstr && !die.HasChildren() &&
sc.comp_unit->GetLanguage() == eLanguageTypeObjC) {
// Work around an issue with clang at the moment where forward
// declarations for objective C classes are emitted as:
// DW_TAG_structure_type [2]
// DW_AT_name( "ForwardObjcClass" )
// DW_AT_byte_size( 0x00 )
// DW_AT_decl_file( "..." )
// DW_AT_decl_line( 1 )
//
// Note that there is no DW_AT_declaration and there are no children,
// and the byte size is zero.
is_forward_declaration = true;
}
if (class_language == eLanguageTypeObjC ||
class_language == eLanguageTypeObjC_plus_plus) {
if (!is_complete_objc_class &&
die.Supports_DW_AT_APPLE_objc_complete_type()) {
// We have a valid eSymbolTypeObjCClass class symbol whose name
// matches the current objective C class that we are trying to find
// and this DIE isn't the complete definition (we checked
// is_complete_objc_class above and know it is false), so the real
// definition is in here somewhere
type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE(
die, type_name_const_str, true);
if (!type_sp) {
SymbolFileDWARFDebugMap *debug_map_symfile =
dwarf->GetDebugMapSymfile();
if (debug_map_symfile) {
// We weren't able to find a full declaration in this DWARF,
// see if we have a declaration anywhere else...
type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
die, type_name_const_str, true);
}
}
if (type_sp) {
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an "
"incomplete objc type, complete type is 0x%8.8" PRIx64,
static_cast<void *>(this), die.GetOffset(),
DW_TAG_value_to_name(tag), type_name_cstr, type_sp->GetID());
}
// We found a real definition for this type elsewhere so lets use
// it and cache the fact that we found a complete type for this
// die
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
return type_sp;
}
}
}
if (is_forward_declaration) {
// We have a forward declaration to a type and we need to try and
// find a full declaration. We look in the current type index just in
// case we have a forward declaration followed by an actual
// declarations in the DWARF. If this fails, we need to look
// elsewhere...
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
"forward declaration, trying to find complete type",
static_cast<void *>(this), die.GetOffset(),
DW_TAG_value_to_name(tag), type_name_cstr);
}
// See if the type comes from a DWO module and if so, track down that
// type.
type_sp = ParseTypeFromDWO(die, log);
if (type_sp)
return type_sp;
DWARFDeclContext die_decl_ctx;
die.GetDWARFDeclContext(die_decl_ctx);
// type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
// type_name_const_str);
type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
if (!type_sp) {
SymbolFileDWARFDebugMap *debug_map_symfile =
dwarf->GetDebugMapSymfile();
if (debug_map_symfile) {
// We weren't able to find a full declaration in this DWARF, see
// if we have a declaration anywhere else...
type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
die_decl_ctx);
}
}
if (type_sp) {
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
"forward declaration, complete type is 0x%8.8" PRIx64,
static_cast<void *>(this), die.GetOffset(),
DW_TAG_value_to_name(tag), type_name_cstr, type_sp->GetID());
}
// We found a real definition for this type elsewhere so lets use
// it and cache the fact that we found a complete type for this die
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
clang::DeclContext *defn_decl_ctx =
GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
if (defn_decl_ctx)
LinkDeclContextToDIE(defn_decl_ctx, die);
return type_sp;
}
}
assert(tag_decl_kind != -1);
bool clang_type_was_created = false;
clang_type.SetCompilerType(
&m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
if (!clang_type) {
clang::DeclContext *decl_ctx =
GetClangDeclContextContainingDIE(die, nullptr);
// If your decl context is a record that was imported from another
// AST context (in the gmodules case), we need to make sure the type
// backing the Decl is complete before adding children to it. This is
// not an issue in the non-gmodules case because the debug info will
// always contain a full definition of parent types in that case.
CompleteExternalTagDeclType(GetClangASTImporter(), decl_ctx, die,
type_name_cstr);
if (accessibility == eAccessNone && decl_ctx) {
// Check the decl context that contains this class/struct/union. If
// it is a class we must give it an accessibility.
const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
if (DeclKindIsCXXClass(containing_decl_kind))
accessibility = default_accessibility;
}
ClangASTMetadata metadata;
metadata.SetUserID(die.GetID());
metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
if (type_name_cstr && strchr(type_name_cstr, '<')) {
ClangASTContext::TemplateParameterInfos template_param_infos;
if (ParseTemplateParameterInfos(die, template_param_infos)) {
clang::ClassTemplateDecl *class_template_decl =
m_ast.ParseClassTemplateDecl(decl_ctx, accessibility,
type_name_cstr, tag_decl_kind,
template_param_infos);
if (!class_template_decl) {
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" "
"clang::ClassTemplateDecl failed to return a decl.",
static_cast<void *>(this), die.GetOffset(),
DW_TAG_value_to_name(tag), type_name_cstr);
}
return TypeSP();
}
clang::ClassTemplateSpecializationDecl *class_specialization_decl =
m_ast.CreateClassTemplateSpecializationDecl(
decl_ctx, class_template_decl, tag_decl_kind,
template_param_infos);
clang_type = m_ast.CreateClassTemplateSpecializationType(
class_specialization_decl);
clang_type_was_created = true;
m_ast.SetMetadata(class_template_decl, metadata);
m_ast.SetMetadata(class_specialization_decl, metadata);
}
}
if (!clang_type_was_created) {
clang_type_was_created = true;
clang_type =
m_ast.CreateRecordType(decl_ctx, accessibility, type_name_cstr,
tag_decl_kind, class_language, &metadata);
}
}
// Store a forward declaration to this class type in case any
// parameters in any class methods need it for the clang types for
// function prototypes.
LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
type_sp = std::make_shared<Type>(die.GetID(), dwarf, type_name_const_str,
byte_size, nullptr, LLDB_INVALID_UID,
Type::eEncodingIsUID, &decl, clang_type,
Type::eResolveStateForward);
type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
// Add our type to the unique type map so we don't end up creating many
// copies of the same type over and over in the ASTContext for our
// module
unique_ast_entry_up->m_type_sp = type_sp;
unique_ast_entry_up->m_die = die;
unique_ast_entry_up->m_declaration = unique_decl;
unique_ast_entry_up->m_byte_size = byte_size.getValueOr(0);
dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
*unique_ast_entry_up);
if (is_forward_declaration && die.HasChildren()) {
// Check to see if the DIE actually has a definition, some version of
// GCC will
// emit DIEs with DW_AT_declaration set to true, but yet still have
// subprogram, members, or inheritance, so we can't trust it
DWARFDIE child_die = die.GetFirstChild();
while (child_die) {
switch (child_die.Tag()) {
case DW_TAG_inheritance:
case DW_TAG_subprogram:
case DW_TAG_member:
case DW_TAG_APPLE_property:
case DW_TAG_class_type:
case DW_TAG_structure_type:
case DW_TAG_enumeration_type:
case DW_TAG_typedef:
case DW_TAG_union_type:
child_die.Clear();
is_forward_declaration = false;
break;
default:
child_die = child_die.GetSibling();
break;
}
}
}
if (!is_forward_declaration) {
// Always start the definition for a class type so that if the class
// has child classes or types that require the class to be created
// for use as their decl contexts the class will be ready to accept
// these child definitions.
if (!die.HasChildren()) {
// No children for this struct/union/class, lets finish it
if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
} else {
dwarf->GetObjectFile()->GetModule()->ReportError(
"DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
"definition.\nPlease file a bug and attach the file at the "
"start of this error message",
die.GetOffset(), type_name_cstr);
}
if (tag == DW_TAG_structure_type) // this only applies in C
{
clang::RecordDecl *record_decl =
ClangASTContext::GetAsRecordDecl(clang_type);
if (record_decl) {
GetClangASTImporter().InsertRecordDecl(
record_decl, ClangASTImporter::LayoutInfo());
}
}
} else if (clang_type_was_created) {
// Start the definition if the class is not objective C since the
// underlying decls respond to isCompleteDefinition(). Objective
// C decls don't respond to isCompleteDefinition() so we can't
// start the declaration definition right away. For C++
// class/union/structs we want to start the definition in case the
// class is needed as the declaration context for a contained class
// or type without the need to complete that type..
if (class_language != eLanguageTypeObjC &&
class_language != eLanguageTypeObjC_plus_plus)
ClangASTContext::StartTagDeclarationDefinition(clang_type);
// Leave this as a forward declaration until we need to know the
// details of the type. lldb_private::Type will automatically call
// the SymbolFile virtual function
// "SymbolFileDWARF::CompleteType(Type *)" When the definition
// needs to be defined.
assert(!dwarf->GetForwardDeclClangTypeToDie().count(
ClangUtil::RemoveFastQualifiers(clang_type)
.GetOpaqueQualType()) &&
"Type already in the forward declaration map!");
// Can't assume m_ast.GetSymbolFile() is actually a
// SymbolFileDWARF, it can be a SymbolFileDWARFDebugMap for Apple
// binaries.
dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
clang_type.GetOpaqueQualType();
dwarf->GetForwardDeclClangTypeToDie()
[ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] =
die.GetDIERef();
m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
}
}
// If we made a clang type, set the trivial abi if applicable: We only
// do this for pass by value - which implies the Trivial ABI. There
// isn't a way to assert that something that would normally be pass by
// value is pass by reference, so we ignore that attribute if set.
if (calling_convention == llvm::dwarf::DW_CC_pass_by_value) {
clang::CXXRecordDecl *record_decl =
m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
if (record_decl) {
record_decl->setHasTrivialSpecialMemberForCall();
}
}
if (calling_convention == llvm::dwarf::DW_CC_pass_by_reference) {
clang::CXXRecordDecl *record_decl =
m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
if (record_decl)
record_decl->setArgPassingRestrictions(
clang::RecordDecl::APK_CannotPassInRegs);
}
} break;
case DW_TAG_enumeration_type: {
bool is_scoped = false;
DWARFFormValue encoding_form;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
type_name_cstr = form_value.AsCString();
type_name_const_str.SetCString(type_name_cstr);
break;
case DW_AT_type:
encoding_form = form_value;
break;
case DW_AT_byte_size:
byte_size = form_value.Unsigned();
break;
case DW_AT_accessibility:
break; // accessibility =
// DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
case DW_AT_declaration:
is_forward_declaration = form_value.Boolean();
break;
case DW_AT_enum_class:
is_scoped = form_value.Boolean();
break;
case DW_AT_allocated:
case DW_AT_associated:
case DW_AT_bit_stride:
case DW_AT_byte_stride:
case DW_AT_data_location:
case DW_AT_description:
case DW_AT_start_scope:
case DW_AT_visibility:
case DW_AT_specification:
case DW_AT_abstract_origin:
case DW_AT_sibling:
break;
}
}
}
if (is_forward_declaration) {
type_sp = ParseTypeFromDWO(die, log);
if (type_sp)
return type_sp;
DWARFDeclContext die_decl_ctx;
die.GetDWARFDeclContext(die_decl_ctx);
type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
if (!type_sp) {
SymbolFileDWARFDebugMap *debug_map_symfile =
dwarf->GetDebugMapSymfile();
if (debug_map_symfile) {
// We weren't able to find a full declaration in this DWARF,
// see if we have a declaration anywhere else...
type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
die_decl_ctx);
}
}
if (type_sp) {
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
"forward declaration, complete type is 0x%8.8" PRIx64,
static_cast<void *>(this), die.GetOffset(),
DW_TAG_value_to_name(tag), type_name_cstr, type_sp->GetID());
}
// We found a real definition for this type elsewhere so lets use
// it and cache the fact that we found a complete type for this
// die
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
clang::DeclContext *defn_decl_ctx =
GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
if (defn_decl_ctx)
LinkDeclContextToDIE(defn_decl_ctx, die);
return type_sp;
}
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
DW_TAG_value_to_name(tag), type_name_cstr);
CompilerType enumerator_clang_type;
clang_type.SetCompilerType(
&m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
if (!clang_type) {
if (encoding_form.IsValid()) {
Type *enumerator_type = dwarf->ResolveTypeUID(DIERef(encoding_form));
if (enumerator_type)
enumerator_clang_type = enumerator_type->GetFullCompilerType();
}
if (!enumerator_clang_type) {
if (byte_size) {
enumerator_clang_type =
m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
NULL, DW_ATE_signed, *byte_size * 8);
} else {
enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
}
}
clang_type = m_ast.CreateEnumerationType(
type_name_cstr, GetClangDeclContextContainingDIE(die, nullptr),
decl, enumerator_clang_type, is_scoped);
} else {
enumerator_clang_type =
m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType());
}
LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type),
die);
type_sp = std::make_shared<Type>(
die.GetID(), dwarf, type_name_const_str, byte_size, nullptr,
dwarf->GetUID(DIERef(encoding_form)), Type::eEncodingIsUID, &decl,
clang_type, Type::eResolveStateForward);
if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
if (die.HasChildren()) {
SymbolContext cu_sc(die.GetLLDBCompileUnit());
bool is_signed = false;
enumerator_clang_type.IsIntegerType(is_signed);
ParseChildEnumerators(cu_sc, clang_type, is_signed,
type_sp->GetByteSize().getValueOr(0), die);
}
ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
} else {
dwarf->GetObjectFile()->GetModule()->ReportError(
"DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
"definition.\nPlease file a bug and attach the file at the "
"start of this error message",
die.GetOffset(), type_name_cstr);
}
}
} break;
case DW_TAG_inlined_subroutine:
case DW_TAG_subprogram:
case DW_TAG_subroutine_type: {
DWARFFormValue type_die_form;
bool is_variadic = false;
bool is_inline = false;
bool is_static = false;
bool is_virtual = false;
bool is_explicit = false;
bool is_artificial = false;
bool has_template_params = false;
DWARFFormValue specification_die_form;
DWARFFormValue abstract_origin_die_form;
DWARFDIE object_pointer_die;
unsigned type_quals = 0;
clang::StorageClass storage =
clang::SC_None; //, Extern, Static, PrivateExtern
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
type_name_cstr = form_value.AsCString();
type_name_const_str.SetCString(type_name_cstr);
break;
case DW_AT_linkage_name:
case DW_AT_MIPS_linkage_name:
mangled_name_cstr = form_value.AsCString();
break;
case DW_AT_type:
type_die_form = form_value;
break;
case DW_AT_accessibility:
accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
break;
case DW_AT_declaration:
break; // is_forward_declaration = form_value.Boolean(); break;
case DW_AT_inline:
is_inline = form_value.Boolean();
break;
case DW_AT_virtuality:
is_virtual = form_value.Boolean();
break;
case DW_AT_explicit:
is_explicit = form_value.Boolean();
break;
case DW_AT_artificial:
is_artificial = form_value.Boolean();
break;
case DW_AT_external:
if (form_value.Unsigned()) {
if (storage == clang::SC_None)
storage = clang::SC_Extern;
else
storage = clang::SC_PrivateExtern;
}
break;
case DW_AT_specification:
specification_die_form = form_value;
break;
case DW_AT_abstract_origin:
abstract_origin_die_form = form_value;
break;
case DW_AT_object_pointer:
object_pointer_die = form_value.Reference();
break;
case DW_AT_allocated:
case DW_AT_associated:
case DW_AT_address_class:
case DW_AT_calling_convention:
case DW_AT_data_location:
case DW_AT_elemental:
case DW_AT_entry_pc:
case DW_AT_frame_base:
case DW_AT_high_pc:
case DW_AT_low_pc:
case DW_AT_prototyped:
case DW_AT_pure:
case DW_AT_ranges:
case DW_AT_recursive:
case DW_AT_return_addr:
case DW_AT_segment:
case DW_AT_start_scope:
case DW_AT_static_link:
case DW_AT_trampoline:
case DW_AT_visibility:
case DW_AT_vtable_elem_location:
case DW_AT_description:
case DW_AT_sibling:
break;
}
}
}
}
std::string object_pointer_name;
if (object_pointer_die) {
const char *object_pointer_name_cstr = object_pointer_die.GetName();
if (object_pointer_name_cstr)
object_pointer_name = object_pointer_name_cstr;
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
DW_TAG_value_to_name(tag), type_name_cstr);
CompilerType return_clang_type;
Type *func_type = NULL;
if (type_die_form.IsValid())
func_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
if (func_type)
return_clang_type = func_type->GetForwardCompilerType();
else
return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
std::vector<CompilerType> function_param_types;
std::vector<clang::ParmVarDecl *> function_param_decls;
// Parse the function children for the parameters
DWARFDIE decl_ctx_die;
clang::DeclContext *containing_decl_ctx =
GetClangDeclContextContainingDIE(die, &decl_ctx_die);
const clang::Decl::Kind containing_decl_kind =
containing_decl_ctx->getDeclKind();
bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
// Start off static. This will be set to false in
// ParseChildParameters(...) if we find a "this" parameters as the
// first parameter
if (is_cxx_method) {
is_static = true;
}
if (die.HasChildren()) {
bool skip_artificial = true;
ParseChildParameters(*sc.comp_unit, containing_decl_ctx, die,
skip_artificial, is_static, is_variadic,
has_template_params, function_param_types,
function_param_decls, type_quals);
}
bool ignore_containing_context = false;
// Check for templatized class member functions. If we had any
// DW_TAG_template_type_parameter or DW_TAG_template_value_parameter
// the DW_TAG_subprogram DIE, then we can't let this become a method in
// a class. Why? Because templatized functions are only emitted if one
// of the templatized methods is used in the current compile unit and
// we will end up with classes that may or may not include these member
// functions and this means one class won't match another class
// definition and it affects our ability to use a class in the clang
// expression parser. So for the greater good, we currently must not
// allow any template member functions in a class definition.
if (is_cxx_method && has_template_params) {
ignore_containing_context = true;
is_cxx_method = false;
}
// clang_type will get the function prototype clang type after this
// call
clang_type = m_ast.CreateFunctionType(
return_clang_type, function_param_types.data(),
function_param_types.size(), is_variadic, type_quals);
if (type_name_cstr) {
bool type_handled = false;
if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) {
ObjCLanguage::MethodName objc_method(type_name_cstr, true);
if (objc_method.IsValid(true)) {
CompilerType class_opaque_type;
ConstString class_name(objc_method.GetClassName());
if (class_name) {
TypeSP complete_objc_class_type_sp(
dwarf->FindCompleteObjCDefinitionTypeForDIE(DWARFDIE(),
class_name, false));
if (complete_objc_class_type_sp) {
CompilerType type_clang_forward_type =
complete_objc_class_type_sp->GetForwardCompilerType();
if (ClangASTContext::IsObjCObjectOrInterfaceType(
type_clang_forward_type))
class_opaque_type = type_clang_forward_type;
}
}
if (class_opaque_type) {
// If accessibility isn't set to anything valid, assume public
// for now...
if (accessibility == eAccessNone)
accessibility = eAccessPublic;
clang::ObjCMethodDecl *objc_method_decl =
m_ast.AddMethodToObjCObjectType(
class_opaque_type, type_name_cstr, clang_type,
accessibility, is_artificial, is_variadic);
type_handled = objc_method_decl != NULL;
if (type_handled) {
LinkDeclContextToDIE(
ClangASTContext::GetAsDeclContext(objc_method_decl), die);
m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
} else {
dwarf->GetObjectFile()->GetModule()->ReportError(
"{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), "
"please file a bug and attach the file at the start of "
"this error message",
die.GetOffset(), tag, DW_TAG_value_to_name(tag));
}
}
} else if (is_cxx_method) {
// Look at the parent of this DIE and see if is is a class or
// struct and see if this is actually a C++ method
Type *class_type = dwarf->ResolveType(decl_ctx_die);
if (class_type) {
bool alternate_defn = false;
if (class_type->GetID() != decl_ctx_die.GetID() ||
decl_ctx_die.GetContainingDWOModuleDIE()) {
alternate_defn = true;
// We uniqued the parent class of this function to another
// class so we now need to associate all dies under
// "decl_ctx_die" to DIEs in the DIE for "class_type"...
DWARFDIE class_type_die = dwarf->GetDIE(class_type->GetID());
if (class_type_die) {
std::vector<DWARFDIE> failures;
CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
class_type, failures);
// FIXME do something with these failures that's smarter
// than
// just dropping them on the ground. Unfortunately classes
// don't like having stuff added to them after their
// definitions are complete...
type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
type_sp = type_ptr->shared_from_this();
break;
}
}
}
if (specification_die_form.IsValid()) {
// We have a specification which we are going to base our
// function prototype off of, so we need this type to be
// completed so that the m_die_to_decl_ctx for the method in
// the specification has a valid clang decl context.
class_type->GetForwardCompilerType();
// If we have a specification, then the function type should
// have been made with the specification and not with this
// die.
DWARFDIE spec_die =
dwarf->DebugInfo()->GetDIE(DIERef(specification_die_form));
clang::DeclContext *spec_clang_decl_ctx =
GetClangDeclContextForDIE(spec_die);
if (spec_clang_decl_ctx) {
LinkDeclContextToDIE(spec_clang_decl_ctx, die);
} else {
dwarf->GetObjectFile()->GetModule()->ReportWarning(
"0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x"
") has no decl\n",
die.GetID(),
specification_die_form.Reference().GetOffset());
}
type_handled = true;
} else if (abstract_origin_die_form.IsValid()) {
// We have a specification which we are going to base our
// function prototype off of, so we need this type to be
// completed so that the m_die_to_decl_ctx for the method in
// the abstract origin has a valid clang decl context.
class_type->GetForwardCompilerType();
DWARFDIE abs_die =
dwarf->DebugInfo()->GetDIE(DIERef(abstract_origin_die_form));
clang::DeclContext *abs_clang_decl_ctx =
GetClangDeclContextForDIE(abs_die);
if (abs_clang_decl_ctx) {
LinkDeclContextToDIE(abs_clang_decl_ctx, die);
} else {
dwarf->GetObjectFile()->GetModule()->ReportWarning(
"0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x"
") has no decl\n",
die.GetID(),
abstract_origin_die_form.Reference().GetOffset());
}
type_handled = true;
} else {
CompilerType class_opaque_type =
class_type->GetForwardCompilerType();
if (ClangASTContext::IsCXXClassType(class_opaque_type)) {
if (class_opaque_type.IsBeingDefined() || alternate_defn) {
if (!is_static && !die.HasChildren()) {
// We have a C++ member function with no children (this
// pointer!) and clang will get mad if we try and make
// a function that isn't well formed in the DWARF, so
// we will just skip it...
type_handled = true;
} else {
bool add_method = true;
if (alternate_defn) {
// If an alternate definition for the class exists,
// then add the method only if an equivalent is not
// already present.
clang::CXXRecordDecl *record_decl =
m_ast.GetAsCXXRecordDecl(
class_opaque_type.GetOpaqueQualType());
if (record_decl) {
for (auto method_iter = record_decl->method_begin();
method_iter != record_decl->method_end();
method_iter++) {
clang::CXXMethodDecl *method_decl = *method_iter;
if (method_decl->getNameInfo().getAsString() ==
std::string(type_name_cstr)) {
if (method_decl->getType() ==
ClangUtil::GetQualType(clang_type)) {
add_method = false;
LinkDeclContextToDIE(
ClangASTContext::GetAsDeclContext(
method_decl),
die);
type_handled = true;
break;
}
}
}
}
}
if (add_method) {
llvm::PrettyStackTraceFormat stack_trace(
"SymbolFileDWARF::ParseType() is adding a method "
"%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
type_name_cstr, class_type->GetName().GetCString(),
die.GetID(),
dwarf->GetObjectFile()
->GetFileSpec()
.GetPath()
.c_str());
const bool is_attr_used = false;
// Neither GCC 4.2 nor clang++ currently set a valid
// accessibility in the DWARF for C++ methods...
// Default to public for now...
if (accessibility == eAccessNone)
accessibility = eAccessPublic;
clang::CXXMethodDecl *cxx_method_decl =
m_ast.AddMethodToCXXRecordType(
class_opaque_type.GetOpaqueQualType(),
type_name_cstr, mangled_name_cstr, clang_type,
accessibility, is_virtual, is_static, is_inline,
is_explicit, is_attr_used, is_artificial);
type_handled = cxx_method_decl != NULL;
if (type_handled) {
LinkDeclContextToDIE(
ClangASTContext::GetAsDeclContext(cxx_method_decl),
die);
ClangASTMetadata metadata;
metadata.SetUserID(die.GetID());
if (!object_pointer_name.empty()) {
metadata.SetObjectPtrName(
object_pointer_name.c_str());
if (log)
log->Printf(
"Setting object pointer name: %s on method "
"object %p.\n",
object_pointer_name.c_str(),
static_cast<void *>(cxx_method_decl));
}
m_ast.SetMetadata(cxx_method_decl, metadata);
} else {
ignore_containing_context = true;
}
}
}
} else {
// We were asked to parse the type for a method in a
// class, yet the class hasn't been asked to complete
// itself through the clang::ExternalASTSource protocol,
// so we need to just have the class complete itself and
// do things the right way, then our
// DIE should then have an entry in the
// dwarf->GetDIEToType() map. First
// we need to modify the dwarf->GetDIEToType() so it
// doesn't think we are trying to parse this DIE
// anymore...
dwarf->GetDIEToType()[die.GetDIE()] = NULL;
// Now we get the full type to force our class type to
// complete itself using the clang::ExternalASTSource
// protocol which will parse all base classes and all
// methods (including the method for this DIE).
class_type->GetFullCompilerType();
// The type for this DIE should have been filled in the
// function call above
type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
type_sp = type_ptr->shared_from_this();
break;
}
// FIXME This is fixing some even uglier behavior but we
// really need to
// uniq the methods of each class as well as the class
// itself. <rdar://problem/11240464>
type_handled = true;
}
}
}
}
}
}
if (!type_handled) {
clang::FunctionDecl *function_decl = nullptr;
clang::FunctionDecl *template_function_decl = nullptr;
if (abstract_origin_die_form.IsValid()) {
DWARFDIE abs_die = abstract_origin_die_form.Reference();
SymbolContext sc;
if (dwarf->ResolveType(abs_die)) {
function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
GetCachedClangDeclContextForDIE(abs_die));
if (function_decl) {
LinkDeclContextToDIE(function_decl, die);
}
}
}
if (!function_decl) {
// We just have a function that isn't part of a class
function_decl = m_ast.CreateFunctionDeclaration(
ignore_containing_context ? m_ast.GetTranslationUnitDecl()
: containing_decl_ctx,
type_name_cstr, clang_type, storage, is_inline);
if (has_template_params) {
ClangASTContext::TemplateParameterInfos template_param_infos;
ParseTemplateParameterInfos(die, template_param_infos);
template_function_decl = m_ast.CreateFunctionDeclaration(
ignore_containing_context ? m_ast.GetTranslationUnitDecl()
: containing_decl_ctx,
type_name_cstr, clang_type, storage, is_inline);
clang::FunctionTemplateDecl *func_template_decl =
m_ast.CreateFunctionTemplateDecl(
containing_decl_ctx, template_function_decl, type_name_cstr,
template_param_infos);
m_ast.CreateFunctionTemplateSpecializationInfo(
function_decl, func_template_decl, template_param_infos);
}
lldbassert(function_decl);
if (function_decl) {
LinkDeclContextToDIE(function_decl, die);
if (!function_param_decls.empty()) {
m_ast.SetFunctionParameters(function_decl,
&function_param_decls.front(),
function_param_decls.size());
if (template_function_decl)
m_ast.SetFunctionParameters(template_function_decl,
&function_param_decls.front(),
function_param_decls.size());
}
ClangASTMetadata metadata;
metadata.SetUserID(die.GetID());
if (!object_pointer_name.empty()) {
metadata.SetObjectPtrName(object_pointer_name.c_str());
if (log)
log->Printf("Setting object pointer name: %s on function "
"object %p.",
object_pointer_name.c_str(),
static_cast<void *>(function_decl));
}
m_ast.SetMetadata(function_decl, metadata);
}
}
}
}
type_sp = std::make_shared<Type>(die.GetID(), dwarf, type_name_const_str,
llvm::None, nullptr, LLDB_INVALID_UID,
Type::eEncodingIsUID, &decl, clang_type,
Type::eResolveStateFull);
assert(type_sp.get());
} break;
case DW_TAG_array_type: {
DWARFFormValue type_die_form;
uint32_t byte_stride = 0;
uint32_t bit_stride = 0;
bool is_vector = false;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
type_name_cstr = form_value.AsCString();
type_name_const_str.SetCString(type_name_cstr);
break;
case DW_AT_type:
type_die_form = form_value;
break;
case DW_AT_byte_size:
break; // byte_size = form_value.Unsigned(); break;
case DW_AT_byte_stride:
byte_stride = form_value.Unsigned();
break;
case DW_AT_bit_stride:
bit_stride = form_value.Unsigned();
break;
case DW_AT_GNU_vector:
is_vector = form_value.Boolean();
break;
case DW_AT_accessibility:
break; // accessibility =
// DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
case DW_AT_declaration:
break; // is_forward_declaration = form_value.Boolean(); break;
case DW_AT_allocated:
case DW_AT_associated:
case DW_AT_data_location:
case DW_AT_description:
case DW_AT_ordering:
case DW_AT_start_scope:
case DW_AT_visibility:
case DW_AT_specification:
case DW_AT_abstract_origin:
case DW_AT_sibling:
break;
}
}
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
DW_TAG_value_to_name(tag), type_name_cstr);
DIERef type_die_ref(type_die_form);
Type *element_type = dwarf->ResolveTypeUID(type_die_ref);
if (element_type) {
auto array_info = ParseChildArrayInfo(die);
if (array_info) {
byte_stride = array_info->byte_stride;
bit_stride = array_info->bit_stride;
}
if (byte_stride == 0 && bit_stride == 0)
byte_stride = element_type->GetByteSize().getValueOr(0);
CompilerType array_element_type =
element_type->GetForwardCompilerType();
if (ClangASTContext::IsCXXClassType(array_element_type) &&
!array_element_type.GetCompleteType()) {
ModuleSP module_sp = die.GetModule();
if (module_sp) {
if (die.GetCU()->GetProducer() == eProducerClang)
module_sp->ReportError(
"DWARF DW_TAG_array_type DIE at 0x%8.8x has a "
"class/union/struct element type DIE 0x%8.8x that is a "
"forward declaration, not a complete definition.\nTry "
"compiling the source file with -fstandalone-debug or "
"disable -gmodules",
die.GetOffset(), type_die_ref.die_offset);
else
module_sp->ReportError(
"DWARF DW_TAG_array_type DIE at 0x%8.8x has a "
"class/union/struct element type DIE 0x%8.8x that is a "
"forward declaration, not a complete definition.\nPlease "
"file a bug against the compiler and include the "
"preprocessed output for %s",
die.GetOffset(), type_die_ref.die_offset,
die.GetLLDBCompileUnit()
? die.GetLLDBCompileUnit()->GetPath().c_str()
: "the source file");
}
// We have no choice other than to pretend that the element class
// type is complete. If we don't do this, clang will crash when
// trying to layout the class. Since we provide layout
// assistance, all ivars in this class and other classes will be
// fine, this is the best we can do short of crashing.
if (ClangASTContext::StartTagDeclarationDefinition(
array_element_type)) {
ClangASTContext::CompleteTagDeclarationDefinition(
array_element_type);
} else {
module_sp->ReportError("DWARF DIE at 0x%8.8x was not able to "
"start its definition.\nPlease file a "
"bug and attach the file at the start "
"of this error message",
type_die_ref.die_offset);
}
}
uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
if (array_info && array_info->element_orders.size() > 0) {
uint64_t num_elements = 0;
auto end = array_info->element_orders.rend();
for (auto pos = array_info->element_orders.rbegin(); pos != end;
++pos) {
num_elements = *pos;
clang_type = m_ast.CreateArrayType(array_element_type, num_elements,
is_vector);
array_element_type = clang_type;
array_element_bit_stride =
num_elements ? array_element_bit_stride * num_elements
: array_element_bit_stride;
}
} else {
clang_type = m_ast.CreateArrayType(array_element_type, 0, is_vector);
}
ConstString empty_name;
type_sp = std::make_shared<Type>(
die.GetID(), dwarf, empty_name, array_element_bit_stride / 8,
nullptr, dwarf->GetUID(DIERef(type_die_form)), Type::eEncodingIsUID,
&decl, clang_type, Type::eResolveStateFull);
type_sp->SetEncodingType(element_type);
m_ast.SetMetadataAsUserID(clang_type.GetOpaqueQualType(), die.GetID());
}
}
} break;
case DW_TAG_ptr_to_member_type: {
DWARFFormValue type_die_form;
DWARFFormValue containing_type_die_form;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
attr = attributes.AttributeAtIndex(i);
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_type:
type_die_form = form_value;
break;
case DW_AT_containing_type:
containing_type_die_form = form_value;
break;
}
}
}
Type *pointee_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
Type *class_type =
dwarf->ResolveTypeUID(DIERef(containing_type_die_form));
CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType();
CompilerType class_clang_type = class_type->GetLayoutCompilerType();
clang_type = ClangASTContext::CreateMemberPointerType(class_clang_type,
pointee_clang_type);
if (llvm::Optional<uint64_t> clang_type_size =
clang_type.GetByteSize(nullptr)) {
byte_size = *clang_type_size;
type_sp = std::make_shared<Type>(
die.GetID(), dwarf, type_name_const_str, byte_size, nullptr,
LLDB_INVALID_UID, Type::eEncodingIsUID, nullptr, clang_type,
Type::eResolveStateForward);
}
}
break;
}
default:
dwarf->GetObjectFile()->GetModule()->ReportError(
"{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and "
"attach the file at the start of this error message",
die.GetOffset(), tag, DW_TAG_value_to_name(tag));
break;
}
if (type_sp.get()) {
DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
dw_tag_t sc_parent_tag = sc_parent_die.Tag();
SymbolContextScope *symbol_context_scope = NULL;
if (sc_parent_tag == DW_TAG_compile_unit ||
sc_parent_tag == DW_TAG_partial_unit) {
symbol_context_scope = sc.comp_unit;
} else if (sc.function != NULL && sc_parent_die) {
symbol_context_scope =
sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
if (symbol_context_scope == NULL)
symbol_context_scope = sc.function;
}
if (symbol_context_scope != NULL) {
type_sp->SetSymbolContextScope(symbol_context_scope);
}
// We are ready to put this type into the uniqued list up at the module
// level
type_list->Insert(type_sp);
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
}
return type_sp;
}
// DWARF parsing functions
class DWARFASTParserClang::DelayedAddObjCClassProperty {
public:
DelayedAddObjCClassProperty(
const CompilerType &class_opaque_type, const char *property_name,
const CompilerType &property_opaque_type, // The property type is only
// required if you don't have an
// ivar decl
clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name,
const char *property_getter_name, uint32_t property_attributes,
const ClangASTMetadata *metadata)
: m_class_opaque_type(class_opaque_type), m_property_name(property_name),
m_property_opaque_type(property_opaque_type), m_ivar_decl(ivar_decl),
m_property_setter_name(property_setter_name),
m_property_getter_name(property_getter_name),
m_property_attributes(property_attributes) {
if (metadata != nullptr) {
m_metadata_up.reset(new ClangASTMetadata());
*m_metadata_up = *metadata;
}
}
DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs) {
*this = rhs;
}
DelayedAddObjCClassProperty &
operator=(const DelayedAddObjCClassProperty &rhs) {
m_class_opaque_type = rhs.m_class_opaque_type;
m_property_name = rhs.m_property_name;
m_property_opaque_type = rhs.m_property_opaque_type;
m_ivar_decl = rhs.m_ivar_decl;
m_property_setter_name = rhs.m_property_setter_name;
m_property_getter_name = rhs.m_property_getter_name;
m_property_attributes = rhs.m_property_attributes;
if (rhs.m_metadata_up) {
m_metadata_up.reset(new ClangASTMetadata());
*m_metadata_up = *rhs.m_metadata_up;
}
return *this;
}
bool Finalize() {
return ClangASTContext::AddObjCClassProperty(
m_class_opaque_type, m_property_name, m_property_opaque_type,
m_ivar_decl, m_property_setter_name, m_property_getter_name,
m_property_attributes, m_metadata_up.get());
}
private:
CompilerType m_class_opaque_type;
const char *m_property_name;
CompilerType m_property_opaque_type;
clang::ObjCIvarDecl *m_ivar_decl;
const char *m_property_setter_name;
const char *m_property_getter_name;
uint32_t m_property_attributes;
std::unique_ptr<ClangASTMetadata> m_metadata_up;
};
bool DWARFASTParserClang::ParseTemplateDIE(
const DWARFDIE &die,
ClangASTContext::TemplateParameterInfos &template_param_infos) {
const dw_tag_t tag = die.Tag();
bool is_template_template_argument = false;
switch (tag) {
case DW_TAG_GNU_template_parameter_pack: {
template_param_infos.packed_args.reset(
new ClangASTContext::TemplateParameterInfos);
for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
child_die = child_die.GetSibling()) {
if (!ParseTemplateDIE(child_die, *template_param_infos.packed_args))
return false;
}
if (const char *name = die.GetName()) {
template_param_infos.pack_name = name;
}
return true;
}
case DW_TAG_GNU_template_template_param:
is_template_template_argument = true;
LLVM_FALLTHROUGH;
case DW_TAG_template_type_parameter:
case DW_TAG_template_value_parameter: {
DWARFAttributes attributes;
const size_t num_attributes = die.GetAttributes(attributes);
const char *name = nullptr;
const char *template_name = nullptr;
CompilerType clang_type;
uint64_t uval64 = 0;
bool uval64_valid = false;
if (num_attributes > 0) {
DWARFFormValue form_value;
for (size_t i = 0; i < num_attributes; ++i) {
const dw_attr_t attr = attributes.AttributeAtIndex(i);
switch (attr) {
case DW_AT_name:
if (attributes.ExtractFormValueAtIndex(i, form_value))
name = form_value.AsCString();
break;
case DW_AT_GNU_template_name:
if (attributes.ExtractFormValueAtIndex(i, form_value))
template_name = form_value.AsCString();
break;
case DW_AT_type:
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
Type *lldb_type = die.ResolveTypeUID(DIERef(form_value));
if (lldb_type)
clang_type = lldb_type->GetForwardCompilerType();
}
break;
case DW_AT_const_value:
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
uval64_valid = true;
uval64 = form_value.Unsigned();
}
break;
default:
break;
}
}
clang::ASTContext *ast = m_ast.getASTContext();
if (!clang_type)
clang_type = m_ast.GetBasicType(eBasicTypeVoid);
if (!is_template_template_argument) {
bool is_signed = false;
if (name && name[0])
template_param_infos.names.push_back(name);
else
template_param_infos.names.push_back(NULL);
// Get the signed value for any integer or enumeration if available
clang_type.IsIntegerOrEnumerationType(is_signed);
if (tag == DW_TAG_template_value_parameter && uval64_valid) {
llvm::Optional<uint64_t> size = clang_type.GetBitSize(nullptr);
if (!size)
return false;
llvm::APInt apint(*size, uval64, is_signed);
template_param_infos.args.push_back(
clang::TemplateArgument(*ast, llvm::APSInt(apint, !is_signed),
ClangUtil::GetQualType(clang_type)));
} else {
template_param_infos.args.push_back(
clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
}
} else {
auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name);
template_param_infos.names.push_back(name);
template_param_infos.args.push_back(
clang::TemplateArgument(clang::TemplateName(tplt_type)));
}
}
}
return true;
default:
break;
}
return false;
}
bool DWARFASTParserClang::ParseTemplateParameterInfos(
const DWARFDIE &parent_die,
ClangASTContext::TemplateParameterInfos &template_param_infos) {
if (!parent_die)
return false;
for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
die = die.GetSibling()) {
const dw_tag_t tag = die.Tag();
switch (tag) {
case DW_TAG_template_type_parameter:
case DW_TAG_template_value_parameter:
case DW_TAG_GNU_template_parameter_pack:
case DW_TAG_GNU_template_template_param:
ParseTemplateDIE(die, template_param_infos);
break;
default:
break;
}
}
if (template_param_infos.args.empty())
return false;
return template_param_infos.args.size() == template_param_infos.names.size();
}
bool DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die,
lldb_private::Type *type,
CompilerType &clang_type) {
SymbolFileDWARF *dwarf = die.GetDWARF();
std::lock_guard<std::recursive_mutex> guard(
dwarf->GetObjectFile()->GetModule()->GetMutex());
// Disable external storage for this type so we don't get anymore
// clang::ExternalASTSource queries for this type.
m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
if (!die)
return false;
#if defined LLDB_CONFIGURATION_DEBUG
// For debugging purposes, the LLDB_DWARF_DONT_COMPLETE_TYPENAMES environment
// variable can be set with one or more typenames separated by ';'
// characters. This will cause this function to not complete any types whose
// names match.
//
// Examples of setting this environment variable:
//
// LLDB_DWARF_DONT_COMPLETE_TYPENAMES=Foo
// LLDB_DWARF_DONT_COMPLETE_TYPENAMES=Foo;Bar;Baz
const char *dont_complete_typenames_cstr =
getenv("LLDB_DWARF_DONT_COMPLETE_TYPENAMES");
if (dont_complete_typenames_cstr && dont_complete_typenames_cstr[0]) {
const char *die_name = die.GetName();
if (die_name && die_name[0]) {
const char *match = strstr(dont_complete_typenames_cstr, die_name);
if (match) {
size_t die_name_length = strlen(die_name);
while (match) {
const char separator_char = ';';
const char next_char = match[die_name_length];
if (next_char == '\0' || next_char == separator_char) {
if (match == dont_complete_typenames_cstr ||
match[-1] == separator_char)
return false;
}
match = strstr(match + 1, die_name);
}
}
}
}
#endif
const dw_tag_t tag = die.Tag();
Log *log =
nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
die.GetID(), die.GetTagAsCString(), type->GetName().AsCString());
assert(clang_type);
DWARFAttributes attributes;
switch (tag) {
case DW_TAG_structure_type:
case DW_TAG_union_type:
case DW_TAG_class_type: {
ClangASTImporter::LayoutInfo layout_info;
{
if (die.HasChildren()) {
LanguageType class_language = eLanguageTypeUnknown;
if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type)) {
class_language = eLanguageTypeObjC;
// For objective C we don't start the definition when the class is
// created.
ClangASTContext::StartTagDeclarationDefinition(clang_type);
}
int tag_decl_kind = -1;
AccessType default_accessibility = eAccessNone;
if (tag == DW_TAG_structure_type) {
tag_decl_kind = clang::TTK_Struct;
default_accessibility = eAccessPublic;
} else if (tag == DW_TAG_union_type) {
tag_decl_kind = clang::TTK_Union;
default_accessibility = eAccessPublic;
} else if (tag == DW_TAG_class_type) {
tag_decl_kind = clang::TTK_Class;
default_accessibility = eAccessPrivate;
}
SymbolContext sc(die.GetLLDBCompileUnit());
std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
std::vector<int> member_accessibilities;
bool is_a_class = false;
// Parse members and base classes first
std::vector<DWARFDIE> member_function_dies;
DelayedPropertyList delayed_properties;
ParseChildMembers(sc, die, clang_type, class_language, bases,
member_accessibilities, member_function_dies,
delayed_properties, default_accessibility, is_a_class,
layout_info);
// Now parse any methods if there were any...
for (const DWARFDIE &die : member_function_dies)
dwarf->ResolveType(die);
if (class_language == eLanguageTypeObjC) {
ConstString class_name(clang_type.GetTypeName());
if (class_name) {
DIEArray method_die_offsets;
dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
if (!method_die_offsets.empty()) {
DWARFDebugInfo *debug_info = dwarf->DebugInfo();
const size_t num_matches = method_die_offsets.size();
for (size_t i = 0; i < num_matches; ++i) {
const DIERef &die_ref = method_die_offsets[i];
DWARFDIE method_die = debug_info->GetDIE(die_ref);
if (method_die)
method_die.ResolveType();
}
}
for (DelayedPropertyList::iterator pi = delayed_properties.begin(),
pe = delayed_properties.end();
pi != pe; ++pi)
pi->Finalize();
}
}
// If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
// need to tell the clang type it is actually a class.
if (class_language != eLanguageTypeObjC) {
if (is_a_class && tag_decl_kind != clang::TTK_Class)
m_ast.SetTagTypeKind(ClangUtil::GetQualType(clang_type),
clang::TTK_Class);
}
// Since DW_TAG_structure_type gets used for both classes and
// structures, we may need to set any DW_TAG_member fields to have a
// "private" access if none was specified. When we parsed the child
// members we tracked that actual accessibility value for each
// DW_TAG_member in the "member_accessibilities" array. If the value
// for the member is zero, then it was set to the
// "default_accessibility" which for structs was "public". Below we
// correct this by setting any fields to "private" that weren't
// correctly set.
if (is_a_class && !member_accessibilities.empty()) {
// This is a class and all members that didn't have their access
// specified are private.
m_ast.SetDefaultAccessForRecordFields(
m_ast.GetAsRecordDecl(clang_type), eAccessPrivate,
&member_accessibilities.front(), member_accessibilities.size());
}
if (!bases.empty()) {
// Make sure all base classes refer to complete types and not forward
// declarations. If we don't do this, clang will crash with an
// assertion in the call to clang_type.TransferBaseClasses()
for (const auto &base_class : bases) {
clang::TypeSourceInfo *type_source_info =
base_class->getTypeSourceInfo();
if (type_source_info) {
CompilerType base_class_type(
&m_ast, type_source_info->getType().getAsOpaquePtr());
if (!base_class_type.GetCompleteType()) {
auto module = dwarf->GetObjectFile()->GetModule();
module->ReportError(":: Class '%s' has a base class '%s' which "
"does not have a complete definition.",
die.GetName(),
base_class_type.GetTypeName().GetCString());
if (die.GetCU()->GetProducer() == eProducerClang)
module->ReportError(":: Try compiling the source file with "
"-fstandalone-debug.");
// We have no choice other than to pretend that the base class
// is complete. If we don't do this, clang will crash when we
// call setBases() inside of
// "clang_type.TransferBaseClasses()" below. Since we
// provide layout assistance, all ivars in this class and other
// classes will be fine, this is the best we can do short of
// crashing.
if (ClangASTContext::StartTagDeclarationDefinition(
base_class_type)) {
ClangASTContext::CompleteTagDeclarationDefinition(
base_class_type);
}
}
}
}
m_ast.TransferBaseClasses(clang_type.GetOpaqueQualType(),
std::move(bases));
}
}
}
m_ast.AddMethodOverridesForCXXRecordType(clang_type.GetOpaqueQualType());
ClangASTContext::BuildIndirectFields(clang_type);
ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
if (!layout_info.field_offsets.empty() ||
!layout_info.base_offsets.empty() ||
!layout_info.vbase_offsets.empty()) {
if (type)
layout_info.bit_size = type->GetByteSize().getValueOr(0) * 8;
if (layout_info.bit_size == 0)
layout_info.bit_size =
die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
clang::CXXRecordDecl *record_decl =
m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
if (record_decl) {
if (log) {
ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
if (module_sp) {
module_sp->LogMessage(
log,
"ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) "
"caching layout info for record_decl = %p, bit_size = %" PRIu64
", alignment = %" PRIu64
", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
static_cast<void *>(clang_type.GetOpaqueQualType()),
static_cast<void *>(record_decl), layout_info.bit_size,
layout_info.alignment,
static_cast<uint32_t>(layout_info.field_offsets.size()),
static_cast<uint32_t>(layout_info.base_offsets.size()),
static_cast<uint32_t>(layout_info.vbase_offsets.size()));
uint32_t idx;
{
llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator
pos,
end = layout_info.field_offsets.end();
for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end;
++pos, ++idx) {
module_sp->LogMessage(
log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
"%p) field[%u] = { bit_offset=%u, name='%s' }",
static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
static_cast<uint32_t>(pos->second),
pos->first->getNameAsString().c_str());
}
}
{
llvm::DenseMap<const clang::CXXRecordDecl *,
clang::CharUnits>::const_iterator base_pos,
base_end = layout_info.base_offsets.end();
for (idx = 0, base_pos = layout_info.base_offsets.begin();
base_pos != base_end; ++base_pos, ++idx) {
module_sp->LogMessage(
log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
"%p) base[%u] = { byte_offset=%u, name='%s' }",
clang_type.GetOpaqueQualType(), idx,
(uint32_t)base_pos->second.getQuantity(),
base_pos->first->getNameAsString().c_str());
}
}
{
llvm::DenseMap<const clang::CXXRecordDecl *,
clang::CharUnits>::const_iterator vbase_pos,
vbase_end = layout_info.vbase_offsets.end();
for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin();
vbase_pos != vbase_end; ++vbase_pos, ++idx) {
module_sp->LogMessage(
log, "ClangASTContext::CompleteTypeFromDWARF (clang_type = "
"%p) vbase[%u] = { byte_offset=%u, name='%s' }",
static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
static_cast<uint32_t>(vbase_pos->second.getQuantity()),
vbase_pos->first->getNameAsString().c_str());
}
}
}
}
GetClangASTImporter().InsertRecordDecl(record_decl, layout_info);
}
}
}
return (bool)clang_type;
case DW_TAG_enumeration_type:
if (ClangASTContext::StartTagDeclarationDefinition(clang_type)) {
if (die.HasChildren()) {
SymbolContext sc(die.GetLLDBCompileUnit());
bool is_signed = false;
clang_type.IsIntegerType(is_signed);
ParseChildEnumerators(sc, clang_type, is_signed,
type->GetByteSize().getValueOr(0), die);
}
ClangASTContext::CompleteTagDeclarationDefinition(clang_type);
}
return (bool)clang_type;
default:
assert(false && "not a forward clang type decl!");
break;
}
return false;
}
std::vector<DWARFDIE> DWARFASTParserClang::GetDIEForDeclContext(
lldb_private::CompilerDeclContext decl_context) {
std::vector<DWARFDIE> result;
for (auto it = m_decl_ctx_to_die.find(
(clang::DeclContext *)decl_context.GetOpaqueDeclContext());
it != m_decl_ctx_to_die.end(); it++)
result.push_back(it->second);
return result;
}
CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
clang::Decl *clang_decl = GetClangDeclForDIE(die);
if (clang_decl != nullptr)
return CompilerDecl(&m_ast, clang_decl);
return CompilerDecl();
}
CompilerDeclContext
DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
if (clang_decl_ctx)
return CompilerDeclContext(&m_ast, clang_decl_ctx);
return CompilerDeclContext();
}
CompilerDeclContext
DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
clang::DeclContext *clang_decl_ctx =
GetClangDeclContextContainingDIE(die, nullptr);
if (clang_decl_ctx)
return CompilerDeclContext(&m_ast, clang_decl_ctx);
return CompilerDeclContext();
}
size_t DWARFASTParserClang::ParseChildEnumerators(
const SymbolContext &sc, lldb_private::CompilerType &clang_type,
bool is_signed, uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
if (!parent_die)
return 0;
size_t enumerators_added = 0;
for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
die = die.GetSibling()) {
const dw_tag_t tag = die.Tag();
if (tag == DW_TAG_enumerator) {
DWARFAttributes attributes;
const size_t num_child_attributes = die.GetAttributes(attributes);
if (num_child_attributes > 0) {
const char *name = nullptr;
bool got_value = false;
int64_t enum_value = 0;
Declaration decl;
uint32_t i;
for (i = 0; i < num_child_attributes; ++i) {
const dw_attr_t attr = attributes.AttributeAtIndex(i);
DWARFFormValue form_value;
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_const_value:
got_value = true;
if (is_signed)
enum_value = form_value.Signed();
else
enum_value = form_value.Unsigned();
break;
case DW_AT_name:
name = form_value.AsCString();
break;
case DW_AT_description:
default:
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_sibling:
break;
}
}
}
if (name && name[0] && got_value) {
m_ast.AddEnumerationValueToEnumerationType(
clang_type, decl, name, enum_value, enumerator_byte_size * 8);
++enumerators_added;
}
}
}
}
return enumerators_added;
}
#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
class DIEStack {
public:
void Push(const DWARFDIE &die) { m_dies.push_back(die); }
void LogDIEs(Log *log) {
StreamString log_strm;
const size_t n = m_dies.size();
log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
for (size_t i = 0; i < n; i++) {
std::string qualified_name;
const DWARFDIE &die = m_dies[i];
die.GetQualifiedName(qualified_name);
log_strm.Printf("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n", (uint64_t)i,
die.GetOffset(), die.GetTagAsCString(),
qualified_name.c_str());
}
log->PutCString(log_strm.GetData());
}
void Pop() { m_dies.pop_back(); }
class ScopedPopper {
public:
ScopedPopper(DIEStack &die_stack)
: m_die_stack(die_stack), m_valid(false) {}
void Push(const DWARFDIE &die) {
m_valid = true;
m_die_stack.Push(die);
}
~ScopedPopper() {
if (m_valid)
m_die_stack.Pop();
}
protected:
DIEStack &m_die_stack;
bool m_valid;
};
protected:
typedef std::vector<DWARFDIE> Stack;
Stack m_dies;
};
#endif
Function *DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
const DWARFDIE &die) {
DWARFRangeList func_ranges;
const char *name = nullptr;
const char *mangled = nullptr;
int decl_file = 0;
int decl_line = 0;
int decl_column = 0;
int call_file = 0;
int call_line = 0;
int call_column = 0;
- DWARFExpression frame_base(die.GetCU());
+ DWARFExpression frame_base;
const dw_tag_t tag = die.Tag();
if (tag != DW_TAG_subprogram)
return nullptr;
if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
decl_column, call_file, call_line, call_column,
&frame_base)) {
// Union of all ranges in the function DIE (if the function is
// discontiguous)
AddressRange func_range;
lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
if (lowest_func_addr != LLDB_INVALID_ADDRESS &&
lowest_func_addr <= highest_func_addr) {
ModuleSP module_sp(die.GetModule());
func_range.GetBaseAddress().ResolveAddressUsingFileSections(
lowest_func_addr, module_sp->GetSectionList());
if (func_range.GetBaseAddress().IsValid())
func_range.SetByteSize(highest_func_addr - lowest_func_addr);
}
if (func_range.GetBaseAddress().IsValid()) {
Mangled func_name;
if (mangled)
func_name.SetValue(ConstString(mangled), true);
else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
die.GetParent().Tag() == DW_TAG_partial_unit) &&
Language::LanguageIsCPlusPlus(die.GetLanguage()) && name &&
strcmp(name, "main") != 0) {
// If the mangled name is not present in the DWARF, generate the
// demangled name using the decl context. We skip if the function is
// "main" as its name is never mangled.
bool is_static = false;
bool is_variadic = false;
bool has_template_params = false;
unsigned type_quals = 0;
std::vector<CompilerType> param_types;
std::vector<clang::ParmVarDecl *> param_decls;
DWARFDeclContext decl_ctx;
StreamString sstr;
die.GetDWARFDeclContext(decl_ctx);
sstr << decl_ctx.GetQualifiedName();
clang::DeclContext *containing_decl_ctx =
GetClangDeclContextContainingDIE(die, nullptr);
ParseChildParameters(comp_unit, containing_decl_ctx, die, true,
is_static, is_variadic, has_template_params,
param_types, param_decls, type_quals);
sstr << "(";
for (size_t i = 0; i < param_types.size(); i++) {
if (i > 0)
sstr << ", ";
sstr << param_types[i].GetTypeName();
}
if (is_variadic)
sstr << ", ...";
sstr << ")";
if (type_quals & clang::Qualifiers::Const)
sstr << " const";
func_name.SetValue(ConstString(sstr.GetString()), false);
} else
func_name.SetValue(ConstString(name), false);
FunctionSP func_sp;
std::unique_ptr<Declaration> decl_up;
if (decl_file != 0 || decl_line != 0 || decl_column != 0)
decl_up.reset(new Declaration(
comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file),
decl_line, decl_column));
SymbolFileDWARF *dwarf = die.GetDWARF();
// Supply the type _only_ if it has already been parsed
Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
if (dwarf->FixupAddress(func_range.GetBaseAddress())) {
const user_id_t func_user_id = die.GetID();
func_sp =
std::make_shared<Function>(&comp_unit,
func_user_id, // UserID is the DIE offset
func_user_id, func_name, func_type,
func_range); // first address range
if (func_sp.get() != nullptr) {
if (frame_base.IsValid())
func_sp->GetFrameBaseExpression() = frame_base;
comp_unit.AddFunction(func_sp);
return func_sp.get();
}
}
}
}
return nullptr;
}
bool DWARFASTParserClang::ParseChildMembers(
const SymbolContext &sc, const DWARFDIE &parent_die,
CompilerType &class_clang_type, const LanguageType class_language,
std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
std::vector<int> &member_accessibilities,
std::vector<DWARFDIE> &member_function_dies,
DelayedPropertyList &delayed_properties, AccessType &default_accessibility,
bool &is_a_class, ClangASTImporter::LayoutInfo &layout_info) {
if (!parent_die)
return false;
// Get the parent byte size so we can verify any members will fit
const uint64_t parent_byte_size =
parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
const uint64_t parent_bit_size =
parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
uint32_t member_idx = 0;
BitfieldInfo last_field_info;
ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
ClangASTContext *ast =
llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem());
if (ast == nullptr)
return false;
for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid();
die = die.GetSibling()) {
dw_tag_t tag = die.Tag();
switch (tag) {
case DW_TAG_member:
case DW_TAG_APPLE_property: {
DWARFAttributes attributes;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
Declaration decl;
- // DWARFExpression location;
const char *name = nullptr;
const char *prop_name = nullptr;
const char *prop_getter_name = nullptr;
const char *prop_setter_name = nullptr;
uint32_t prop_attributes = 0;
bool is_artificial = false;
DWARFFormValue encoding_form;
AccessType accessibility = eAccessNone;
uint32_t member_byte_offset =
(parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
llvm::Optional<uint64_t> byte_size;
int64_t bit_offset = 0;
uint64_t data_bit_offset = UINT64_MAX;
size_t bit_size = 0;
bool is_external =
false; // On DW_TAG_members, this means the member is static
uint32_t i;
for (i = 0; i < num_attributes && !is_artificial; ++i) {
const dw_attr_t attr = attributes.AttributeAtIndex(i);
DWARFFormValue form_value;
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_name:
name = form_value.AsCString();
break;
case DW_AT_type:
encoding_form = form_value;
break;
case DW_AT_bit_offset:
bit_offset = form_value.Signed();
break;
case DW_AT_bit_size:
bit_size = form_value.Unsigned();
break;
case DW_AT_byte_size:
byte_size = form_value.Unsigned();
break;
case DW_AT_data_bit_offset:
data_bit_offset = form_value.Unsigned();
break;
case DW_AT_data_member_location:
if (form_value.BlockData()) {
Value initialValue(0);
Value memberOffset(0);
const DWARFDataExtractor &debug_info_data = die.GetData();
uint32_t block_length = form_value.Unsigned();
uint32_t block_offset =
form_value.BlockData() - debug_info_data.GetDataStart();
if (DWARFExpression::Evaluate(
nullptr, // ExecutionContext *
nullptr, // RegisterContext *
module_sp, debug_info_data, die.GetCU(), block_offset,
block_length, eRegisterKindDWARF, &initialValue,
nullptr, memberOffset, nullptr)) {
member_byte_offset =
memberOffset.ResolveValue(nullptr).UInt();
}
} else {
// With DWARF 3 and later, if the value is an integer constant,
// this form value is the offset in bytes from the beginning of
// the containing entity.
member_byte_offset = form_value.Unsigned();
}
break;
case DW_AT_accessibility:
accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
break;
case DW_AT_artificial:
is_artificial = form_value.Boolean();
break;
case DW_AT_APPLE_property_name:
prop_name = form_value.AsCString();
break;
case DW_AT_APPLE_property_getter:
prop_getter_name = form_value.AsCString();
break;
case DW_AT_APPLE_property_setter:
prop_setter_name = form_value.AsCString();
break;
case DW_AT_APPLE_property_attribute:
prop_attributes = form_value.Unsigned();
break;
case DW_AT_external:
is_external = form_value.Boolean();
break;
default:
case DW_AT_declaration:
case DW_AT_description:
case DW_AT_mutable:
case DW_AT_visibility:
case DW_AT_sibling:
break;
}
}
}
if (prop_name) {
ConstString fixed_getter;
ConstString fixed_setter;
// Check if the property getter/setter were provided as full names.
// We want basenames, so we extract them.
if (prop_getter_name && prop_getter_name[0] == '-') {
ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
prop_getter_name = prop_getter_method.GetSelector().GetCString();
}
if (prop_setter_name && prop_setter_name[0] == '-') {
ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
prop_setter_name = prop_setter_method.GetSelector().GetCString();
}
// If the names haven't been provided, they need to be filled in.
if (!prop_getter_name) {
prop_getter_name = prop_name;
}
if (!prop_setter_name && prop_name[0] &&
!(prop_attributes & DW_APPLE_PROPERTY_readonly)) {
StreamString ss;
ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
fixed_setter.SetString(ss.GetString());
prop_setter_name = fixed_setter.GetCString();
}
}
// Clang has a DWARF generation bug where sometimes it represents
// fields that are references with bad byte size and bit size/offset
// information such as:
//
// DW_AT_byte_size( 0x00 )
// DW_AT_bit_size( 0x40 )
// DW_AT_bit_offset( 0xffffffffffffffc0 )
//
// So check the bit offset to make sure it is sane, and if the values
// are not sane, remove them. If we don't do this then we will end up
// with a crash if we try to use this type in an expression when clang
// becomes unhappy with its recycled debug info.
if (byte_size.getValueOr(0) == 0 && bit_offset < 0) {
bit_size = 0;
bit_offset = 0;
}
// FIXME: Make Clang ignore Objective-C accessibility for expressions
if (class_language == eLanguageTypeObjC ||
class_language == eLanguageTypeObjC_plus_plus)
accessibility = eAccessNone;
// Handle static members
if (is_external && member_byte_offset == UINT32_MAX) {
Type *var_type = die.ResolveTypeUID(DIERef(encoding_form));
if (var_type) {
if (accessibility == eAccessNone)
accessibility = eAccessPublic;
ClangASTContext::AddVariableToRecordType(
class_clang_type, name, var_type->GetLayoutCompilerType(),
accessibility);
}
break;
}
if (!is_artificial) {
Type *member_type = die.ResolveTypeUID(DIERef(encoding_form));
clang::FieldDecl *field_decl = nullptr;
if (tag == DW_TAG_member) {
if (member_type) {
if (accessibility == eAccessNone)
accessibility = default_accessibility;
member_accessibilities.push_back(accessibility);
uint64_t field_bit_offset =
(member_byte_offset == UINT32_MAX ? 0
: (member_byte_offset * 8));
if (bit_size > 0) {
BitfieldInfo this_field_info;
this_field_info.bit_offset = field_bit_offset;
this_field_info.bit_size = bit_size;
/////////////////////////////////////////////////////////////
// How to locate a field given the DWARF debug information
//
// AT_byte_size indicates the size of the word in which the bit
// offset must be interpreted.
//
// AT_data_member_location indicates the byte offset of the
// word from the base address of the structure.
//
// AT_bit_offset indicates how many bits into the word
// (according to the host endianness) the low-order bit of the
// field starts. AT_bit_offset can be negative.
//
// AT_bit_size indicates the size of the field in bits.
/////////////////////////////////////////////////////////////
if (data_bit_offset != UINT64_MAX) {
this_field_info.bit_offset = data_bit_offset;
} else {
if (!byte_size)
byte_size = member_type->GetByteSize();
ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
if (objfile->GetByteOrder() == eByteOrderLittle) {
this_field_info.bit_offset += byte_size.getValueOr(0) * 8;
this_field_info.bit_offset -= (bit_offset + bit_size);
} else {
this_field_info.bit_offset += bit_offset;
}
}
if ((this_field_info.bit_offset >= parent_bit_size) ||
!last_field_info.NextBitfieldOffsetIsValid(
this_field_info.bit_offset)) {
ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
objfile->GetModule()->ReportWarning(
"0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid "
"bit offset (0x%8.8" PRIx64
") member will be ignored. Please file a bug against the "
"compiler and include the preprocessed output for %s\n",
die.GetID(), DW_TAG_value_to_name(tag), name,
this_field_info.bit_offset,
sc.comp_unit ? sc.comp_unit->GetPath().c_str()
: "the source file");
this_field_info.Clear();
continue;
}
// Update the field bit offset we will report for layout
field_bit_offset = this_field_info.bit_offset;
// If the member to be emitted did not start on a character
// boundary and there is empty space between the last field and
// this one, then we need to emit an anonymous member filling
// up the space up to its start. There are three cases here:
//
// 1 If the previous member ended on a character boundary, then
// we can emit an
// anonymous member starting at the most recent character
// boundary.
//
// 2 If the previous member did not end on a character boundary
// and the distance
// from the end of the previous member to the current member
// is less than a
// word width, then we can emit an anonymous member starting
// right after the
// previous member and right before this member.
//
// 3 If the previous member did not end on a character boundary
// and the distance
// from the end of the previous member to the current member
// is greater than
// or equal a word width, then we act as in Case 1.
const uint64_t character_width = 8;
const uint64_t word_width = 32;
// Objective-C has invalid DW_AT_bit_offset values in older
// versions of clang, so we have to be careful and only insert
// unnamed bitfields if we have a new enough clang.
bool detect_unnamed_bitfields = true;
if (class_language == eLanguageTypeObjC ||
class_language == eLanguageTypeObjC_plus_plus)
detect_unnamed_bitfields =
die.GetCU()->Supports_unnamed_objc_bitfields();
if (detect_unnamed_bitfields) {
BitfieldInfo anon_field_info;
if ((this_field_info.bit_offset % character_width) !=
0) // not char aligned
{
uint64_t last_field_end = 0;
if (last_field_info.IsValid())
last_field_end =
last_field_info.bit_offset + last_field_info.bit_size;
if (this_field_info.bit_offset != last_field_end) {
if (((last_field_end % character_width) == 0) || // case 1
(this_field_info.bit_offset - last_field_end >=
word_width)) // case 3
{
anon_field_info.bit_size =
this_field_info.bit_offset % character_width;
anon_field_info.bit_offset =
this_field_info.bit_offset -
anon_field_info.bit_size;
} else // case 2
{
anon_field_info.bit_size =
this_field_info.bit_offset - last_field_end;
anon_field_info.bit_offset = last_field_end;
}
}
}
if (anon_field_info.IsValid()) {
clang::FieldDecl *unnamed_bitfield_decl =
ClangASTContext::AddFieldToRecordType(
class_clang_type, llvm::StringRef(),
m_ast.GetBuiltinTypeForEncodingAndBitSize(
eEncodingSint, word_width),
accessibility, anon_field_info.bit_size);
layout_info.field_offsets.insert(std::make_pair(
unnamed_bitfield_decl, anon_field_info.bit_offset));
}
}
last_field_info = this_field_info;
} else {
last_field_info.Clear();
}
CompilerType member_clang_type =
member_type->GetLayoutCompilerType();
if (!member_clang_type.IsCompleteType())
member_clang_type.GetCompleteType();
{
// Older versions of clang emit array[0] and array[1] in the
// same way (<rdar://problem/12566646>). If the current field
// is at the end of the structure, then there is definitely no
// room for extra elements and we override the type to
// array[0].
CompilerType member_array_element_type;
uint64_t member_array_size;
bool member_array_is_incomplete;
if (member_clang_type.IsArrayType(
&member_array_element_type, &member_array_size,
&member_array_is_incomplete) &&
!member_array_is_incomplete) {
uint64_t parent_byte_size =
parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size,
UINT64_MAX);
if (member_byte_offset >= parent_byte_size) {
if (member_array_size != 1 &&
(member_array_size != 0 ||
member_byte_offset > parent_byte_size)) {
module_sp->ReportError(
"0x%8.8" PRIx64
": DW_TAG_member '%s' refers to type 0x%8.8x"
" which extends beyond the bounds of 0x%8.8" PRIx64,
die.GetID(), name,
encoding_form.Reference().GetOffset(),
parent_die.GetID());
}
member_clang_type = m_ast.CreateArrayType(
member_array_element_type, 0, false);
}
}
}
if (ClangASTContext::IsCXXClassType(member_clang_type) &&
!member_clang_type.GetCompleteType()) {
if (die.GetCU()->GetProducer() == eProducerClang)
module_sp->ReportError(
"DWARF DIE at 0x%8.8x (class %s) has a member variable "
"0x%8.8x (%s) whose type is a forward declaration, not a "
"complete definition.\nTry compiling the source file "
"with -fstandalone-debug",
parent_die.GetOffset(), parent_die.GetName(),
die.GetOffset(), name);
else
module_sp->ReportError(
"DWARF DIE at 0x%8.8x (class %s) has a member variable "
"0x%8.8x (%s) whose type is a forward declaration, not a "
"complete definition.\nPlease file a bug against the "
"compiler and include the preprocessed output for %s",
parent_die.GetOffset(), parent_die.GetName(),
die.GetOffset(), name,
sc.comp_unit ? sc.comp_unit->GetPath().c_str()
: "the source file");
// We have no choice other than to pretend that the member
// class is complete. If we don't do this, clang will crash
// when trying to layout the class. Since we provide layout
// assistance, all ivars in this class and other classes will
// be fine, this is the best we can do short of crashing.
if (ClangASTContext::StartTagDeclarationDefinition(
member_clang_type)) {
ClangASTContext::CompleteTagDeclarationDefinition(
member_clang_type);
} else {
module_sp->ReportError(
"DWARF DIE at 0x%8.8x (class %s) has a member variable "
"0x%8.8x (%s) whose type claims to be a C++ class but we "
"were not able to start its definition.\nPlease file a "
"bug and attach the file at the start of this error "
"message",
parent_die.GetOffset(), parent_die.GetName(),
die.GetOffset(), name);
}
}
field_decl = ClangASTContext::AddFieldToRecordType(
class_clang_type, name, member_clang_type, accessibility,
bit_size);
m_ast.SetMetadataAsUserID(field_decl, die.GetID());
layout_info.field_offsets.insert(
std::make_pair(field_decl, field_bit_offset));
} else {
if (name)
module_sp->ReportError(
"0x%8.8" PRIx64
": DW_TAG_member '%s' refers to type 0x%8.8x"
" which was unable to be parsed",
die.GetID(), name, encoding_form.Reference().GetOffset());
else
module_sp->ReportError(
"0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8x"
" which was unable to be parsed",
die.GetID(), encoding_form.Reference().GetOffset());
}
}
if (prop_name != nullptr && member_type) {
clang::ObjCIvarDecl *ivar_decl = nullptr;
if (field_decl) {
ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
assert(ivar_decl != nullptr);
}
ClangASTMetadata metadata;
metadata.SetUserID(die.GetID());
delayed_properties.push_back(DelayedAddObjCClassProperty(
class_clang_type, prop_name,
member_type->GetLayoutCompilerType(), ivar_decl,
prop_setter_name, prop_getter_name, prop_attributes,
&metadata));
if (ivar_decl)
m_ast.SetMetadataAsUserID(ivar_decl, die.GetID());
}
}
}
++member_idx;
} break;
case DW_TAG_subprogram:
// Let the type parsing code handle this one for us.
member_function_dies.push_back(die);
break;
case DW_TAG_inheritance: {
is_a_class = true;
if (default_accessibility == eAccessNone)
default_accessibility = eAccessPrivate;
// TODO: implement DW_TAG_inheritance type parsing
DWARFAttributes attributes;
const size_t num_attributes = die.GetAttributes(attributes);
if (num_attributes > 0) {
Declaration decl;
- DWARFExpression location(die.GetCU());
DWARFFormValue encoding_form;
AccessType accessibility = default_accessibility;
bool is_virtual = false;
bool is_base_of_class = true;
off_t member_byte_offset = 0;
uint32_t i;
for (i = 0; i < num_attributes; ++i) {
const dw_attr_t attr = attributes.AttributeAtIndex(i);
DWARFFormValue form_value;
if (attributes.ExtractFormValueAtIndex(i, form_value)) {
switch (attr) {
case DW_AT_decl_file:
decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(
form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_type:
encoding_form = form_value;
break;
case DW_AT_data_member_location:
if (form_value.BlockData()) {
Value initialValue(0);
Value memberOffset(0);
const DWARFDataExtractor &debug_info_data = die.GetData();
uint32_t block_length = form_value.Unsigned();
uint32_t block_offset =
form_value.BlockData() - debug_info_data.GetDataStart();
if (DWARFExpression::Evaluate(nullptr, nullptr, module_sp,
debug_info_data, die.GetCU(),
block_offset, block_length,
eRegisterKindDWARF, &initialValue,
nullptr, memberOffset, nullptr)) {
member_byte_offset =
memberOffset.ResolveValue(nullptr).UInt();
}
} else {
// With DWARF 3 and later, if the value is an integer constant,
// this form value is the offset in bytes from the beginning of
// the containing entity.
member_byte_offset = form_value.Unsigned();
}
break;
case DW_AT_accessibility:
accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
break;
case DW_AT_virtuality