1515
1616#include " lldb/Core/Module.h"
1717#include " lldb/Core/Value.h"
18- #include " lldb/Core/dwarf.h"
1918#include " lldb/Utility/DataEncoder.h"
2019#include " lldb/Utility/LLDBLog.h"
2120#include " lldb/Utility/Log.h"
3736#include " lldb/Target/StackID.h"
3837#include " lldb/Target/Target.h"
3938#include " lldb/Target/Thread.h"
40- #include " llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
4139#include " llvm/DebugInfo/DWARF/DWARFExpression.h"
4240
4341#ifdef LLDB_ENABLE_SWIFT
@@ -134,10 +132,10 @@ static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
134132
135133// / Return the length in bytes of the set of operands for \p op. No guarantees
136134// / are made on the state of \p data after this call.
137- static lldb::offset_t GetOpcodeDataSize ( const DataExtractor &data,
138- const lldb::offset_t data_offset,
139- const LocationAtom op,
140- const DWARFUnit *dwarf_cu) {
135+ static lldb::offset_t
136+ GetOpcodeDataSize ( const DataExtractor &data, const lldb::offset_t data_offset,
137+ const LocationAtom op,
138+ const DWARFExpression::Delegate *dwarf_cu) {
141139 lldb::offset_t offset = data_offset;
142140 switch (op) {
143141 // Only used in LLVM metadata.
@@ -366,7 +364,8 @@ static lldb::offset_t GetOpcodeDataSize(const DataExtractor &data,
366364 // + LEB128
367365 {
368366 data.Skip_LEB128 (&offset);
369- return DWARFUnit::GetAddressByteSize (dwarf_cu) + offset - data_offset;
367+ return (dwarf_cu ? dwarf_cu->GetAddressByteSize () : 4 ) + offset -
368+ data_offset;
370369 }
371370
372371 case DW_OP_GNU_entry_value:
@@ -391,14 +390,23 @@ static lldb::offset_t GetOpcodeDataSize(const DataExtractor &data,
391390 }
392391
393392 if (dwarf_cu)
394- return dwarf_cu->GetSymbolFileDWARF ().GetVendorDWARFOpcodeSize (
395- data, data_offset, op);
393+ return dwarf_cu->GetVendorDWARFOpcodeSize (data, data_offset, op);
396394
397395 return LLDB_INVALID_OFFSET;
398396}
399397
400- llvm::Expected<lldb::addr_t >
401- DWARFExpression::GetLocation_DW_OP_addr (const DWARFUnit *dwarf_cu) const {
398+ static const char *DW_OP_value_to_name (uint32_t val) {
399+ static char invalid[100 ];
400+ llvm::StringRef llvmstr = llvm::dwarf::OperationEncodingString (val);
401+ if (llvmstr.empty ()) {
402+ snprintf (invalid, sizeof (invalid), " Unknown DW_OP constant: 0x%x" , val);
403+ return invalid;
404+ }
405+ return llvmstr.data ();
406+ }
407+
408+ llvm::Expected<lldb::addr_t > DWARFExpression::GetLocation_DW_OP_addr (
409+ const DWARFExpression::Delegate *dwarf_cu) const {
402410 lldb::offset_t offset = 0 ;
403411 while (m_data.ValidOffset (offset)) {
404412 const LocationAtom op = static_cast <LocationAtom>(m_data.GetU8 (&offset));
@@ -426,8 +434,8 @@ DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu) const {
426434 return LLDB_INVALID_ADDRESS;
427435}
428436
429- bool DWARFExpression::Update_DW_OP_addr (const DWARFUnit *dwarf_cu,
430- lldb::addr_t file_addr) {
437+ bool DWARFExpression::Update_DW_OP_addr (
438+ const DWARFExpression::Delegate *dwarf_cu, lldb::addr_t file_addr) {
431439 lldb::offset_t offset = 0 ;
432440 while (m_data.ValidOffset (offset)) {
433441 const LocationAtom op = static_cast <LocationAtom>(m_data.GetU8 (&offset));
@@ -485,7 +493,7 @@ bool DWARFExpression::Update_DW_OP_addr(const DWARFUnit *dwarf_cu,
485493}
486494
487495bool DWARFExpression::ContainsThreadLocalStorage (
488- const DWARFUnit *dwarf_cu) const {
496+ const DWARFExpression::Delegate *dwarf_cu) const {
489497 lldb::offset_t offset = 0 ;
490498 while (m_data.ValidOffset (offset)) {
491499 const LocationAtom op = static_cast <LocationAtom>(m_data.GetU8 (&offset));
@@ -501,7 +509,7 @@ bool DWARFExpression::ContainsThreadLocalStorage(
501509 return false ;
502510}
503511bool DWARFExpression::LinkThreadLocalStorage (
504- const DWARFUnit *dwarf_cu,
512+ const DWARFExpression::Delegate *dwarf_cu,
505513 std::function<lldb::addr_t (lldb::addr_t file_addr)> const
506514 &link_address_callback) {
507515 const uint32_t addr_byte_size = m_data.GetAddressByteSize ();
@@ -868,9 +876,9 @@ enum LocationDescriptionKind {
868876 /* Composite*/
869877};
870878// / Adjust value's ValueType according to the kind of location description.
871- void UpdateValueTypeFromLocationDescription (Log *log, const DWARFUnit *dwarf_cu,
872- LocationDescriptionKind kind ,
873- Value *value = nullptr ) {
879+ void UpdateValueTypeFromLocationDescription (
880+ Log *log, const DWARFExpression::Delegate *dwarf_cu ,
881+ LocationDescriptionKind kind, Value *value = nullptr ) {
874882 // Note that this function is conflating DWARF expressions with
875883 // DWARF location descriptions. Perhaps it would be better to define
876884 // a wrapper for DWARFExpression::Eval() that deals with DWARF
@@ -960,8 +968,9 @@ static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
960968llvm::Expected<Value> DWARFExpression::Evaluate (
961969 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
962970 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
963- const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind,
964- const Value *initial_value_ptr, const Value *object_address_ptr) {
971+ const DWARFExpression::Delegate *dwarf_cu,
972+ const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
973+ const Value *object_address_ptr) {
965974
966975 if (opcodes.GetByteSize () == 0 )
967976 return llvm::createStringError (
@@ -2235,10 +2244,10 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
22352244 // DESCRIPTION: Pop the top stack element, convert it to a
22362245 // different type, and push the result.
22372246 case DW_OP_convert: {
2238- const uint64_t die_offset = opcodes.GetULEB128 (&offset);
2247+ const uint64_t relative_die_offset = opcodes.GetULEB128 (&offset);
22392248 uint64_t bit_size;
22402249 bool sign;
2241- if (die_offset == 0 ) {
2250+ if (relative_die_offset == 0 ) {
22422251 // The generic type has the size of an address on the target
22432252 // machine and an unspecified signedness. Scalar has no
22442253 // "unspecified signedness", so we use unsigned types.
@@ -2249,35 +2258,12 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
22492258 if (!bit_size)
22502259 return llvm::createStringError (" unspecified architecture" );
22512260 } else {
2252- // Retrieve the type DIE that the value is being converted to. This
2253- // offset is compile unit relative so we need to fix it up.
2254- const uint64_t abs_die_offset = die_offset + dwarf_cu->GetOffset ();
2255- // FIXME: the constness has annoying ripple effects.
2256- DWARFDIE die = const_cast <DWARFUnit *>(dwarf_cu)->GetDIE (abs_die_offset);
2257- if (!die)
2258- return llvm::createStringError (
2259- " cannot resolve DW_OP_convert type DIE" );
2260- uint64_t encoding =
2261- die.GetAttributeValueAsUnsigned (DW_AT_encoding, DW_ATE_hi_user);
2262- bit_size = die.GetAttributeValueAsUnsigned (DW_AT_byte_size, 0 ) * 8 ;
2263- if (!bit_size)
2264- bit_size = die.GetAttributeValueAsUnsigned (DW_AT_bit_size, 0 );
2265- if (!bit_size)
2266- return llvm::createStringError (
2267- " unsupported type size in DW_OP_convert" );
2268- switch (encoding) {
2269- case DW_ATE_signed:
2270- case DW_ATE_signed_char:
2271- sign = true ;
2272- break ;
2273- case DW_ATE_unsigned:
2274- case DW_ATE_unsigned_char:
2275- sign = false ;
2276- break ;
2277- default :
2278- return llvm::createStringError (
2279- " unsupported encoding in DW_OP_convert" );
2280- }
2261+ auto bit_size_sign_or_err =
2262+ dwarf_cu->GetDIEBitSizeAndSign (relative_die_offset);
2263+ if (!bit_size_sign_or_err)
2264+ return bit_size_sign_or_err.takeError ();
2265+ bit_size = bit_size_sign_or_err->first ;
2266+ sign = bit_size_sign_or_err->second ;
22812267 }
22822268 Scalar &top = stack.back ().ResolveValue (exe_ctx);
22832269 top.TruncOrExtendTo (bit_size, sign);
@@ -2401,8 +2387,7 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
24012387
24022388 default :
24032389 if (dwarf_cu) {
2404- if (dwarf_cu->GetSymbolFileDWARF ().ParseVendorDWARFOpcode (
2405- op, opcodes, offset, stack)) {
2390+ if (dwarf_cu->ParseVendorDWARFOpcode (op, opcodes, offset, stack)) {
24062391 break ;
24072392 }
24082393 }
@@ -2437,43 +2422,6 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
24372422 return stack.back ();
24382423}
24392424
2440- bool DWARFExpression::ParseDWARFLocationList (
2441- const DWARFUnit *dwarf_cu, const DataExtractor &data,
2442- DWARFExpressionList *location_list) {
2443- location_list->Clear ();
2444- std::unique_ptr<llvm::DWARFLocationTable> loctable_up =
2445- dwarf_cu->GetLocationTable (data);
2446- Log *log = GetLog (LLDBLog::Expressions);
2447- auto lookup_addr =
2448- [&](uint32_t index) -> std::optional<llvm::object::SectionedAddress> {
2449- addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection (index);
2450- if (address == LLDB_INVALID_ADDRESS)
2451- return std::nullopt ;
2452- return llvm::object::SectionedAddress{address};
2453- };
2454- auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) {
2455- if (!loc) {
2456- LLDB_LOG_ERROR (log, loc.takeError (), " {0}" );
2457- return true ;
2458- }
2459- auto buffer_sp =
2460- std::make_shared<DataBufferHeap>(loc->Expr .data (), loc->Expr .size ());
2461- DWARFExpression expr = DWARFExpression (DataExtractor (
2462- buffer_sp, data.GetByteOrder (), data.GetAddressByteSize ()));
2463- location_list->AddExpression (loc->Range ->LowPC , loc->Range ->HighPC , expr);
2464- return true ;
2465- };
2466- llvm::Error error = loctable_up->visitAbsoluteLocationList (
2467- 0 , llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress ()},
2468- lookup_addr, process_list);
2469- location_list->Sort ();
2470- if (error) {
2471- LLDB_LOG_ERROR (log, std::move (error), " {0}" );
2472- return false ;
2473- }
2474- return true ;
2475- }
2476-
24772425bool DWARFExpression::MatchesOperand (
24782426 StackFrame &frame, const Instruction::Operand &operand) const {
24792427 using namespace OperandMatchers ;
0 commit comments