@@ -589,7 +589,7 @@ bool DWARFExpression::LinkThreadLocalStorage(
589589 return true ;
590590}
591591
592- static llvm::Error Evaluate_DW_OP_entry_value (std::vector<Value> &stack,
592+ static llvm::Error Evaluate_DW_OP_entry_value (DWARFExpression::Stack &stack,
593593 ExecutionContext *exe_ctx,
594594 RegisterContext *reg_ctx,
595595 const DataExtractor &opcodes,
@@ -654,7 +654,7 @@ static llvm::Error Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
654654 addr_t return_pc = LLDB_INVALID_ADDRESS;
655655 uint32_t current_frame_idx = current_frame->GetFrameIndex ();
656656
657- for (uint32_t parent_frame_idx = current_frame_idx + 1 ;;parent_frame_idx++) {
657+ for (uint32_t parent_frame_idx = current_frame_idx + 1 ;; parent_frame_idx++) {
658658 parent_frame = thread->GetStackFrameAtIndex (parent_frame_idx);
659659 // If this is null, we're at the end of the stack.
660660 if (!parent_frame)
@@ -860,6 +860,64 @@ ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp,
860860 return load_addr;
861861}
862862
863+ static llvm::Error Evaluate_DW_OP_deref (DWARFExpression::Stack &stack,
864+ ExecutionContext *exe_ctx,
865+ lldb::ModuleSP module_sp,
866+ Process *process) {
867+ if (stack.empty ())
868+ return llvm::createStringError (" expression stack empty for DW_OP_deref" );
869+
870+ const Value::ValueType value_type = stack.back ().GetValueType ();
871+ switch (value_type) {
872+ case Value::ValueType::HostAddress: {
873+ void *src = (void *)stack.back ().GetScalar ().ULongLong ();
874+ intptr_t ptr;
875+ ::memcpy (&ptr, src, sizeof (void *));
876+ stack.back ().GetScalar () = ptr;
877+ stack.back ().ClearContext ();
878+ } break ;
879+ case Value::ValueType::FileAddress: {
880+ auto file_addr = stack.back ().GetScalar ().ULongLong (LLDB_INVALID_ADDRESS);
881+ Address so_addr;
882+ auto maybe_load_addr = ResolveLoadAddress (exe_ctx, module_sp, " DW_OP_deref" ,
883+ file_addr, so_addr);
884+ if (!maybe_load_addr)
885+ return maybe_load_addr.takeError ();
886+ stack.back ().GetScalar () = *maybe_load_addr;
887+ // Fall through to load address promotion code below.
888+ }
889+ [[fallthrough]];
890+ case Value::ValueType::Scalar:
891+ // Promote Scalar to LoadAddress and fall through.
892+ stack.back ().SetValueType (Value::ValueType::LoadAddress);
893+ [[fallthrough]];
894+ case Value::ValueType::LoadAddress: {
895+ if (!exe_ctx)
896+ return llvm::createStringError (" NULL execution context for DW_OP_deref" );
897+ if (!process)
898+ return llvm::createStringError (" NULL process for DW_OP_deref" );
899+ lldb::addr_t pointer_addr =
900+ stack.back ().GetScalar ().ULongLong (LLDB_INVALID_ADDRESS);
901+ Status error;
902+ lldb::addr_t pointer_value =
903+ process->ReadPointerFromMemory (pointer_addr, error);
904+ if (pointer_value == LLDB_INVALID_ADDRESS)
905+ return llvm::createStringError (
906+ " Failed to dereference pointer from 0x%" PRIx64
907+ " for DW_OP_deref: %s\n " ,
908+ pointer_addr, error.AsCString ());
909+ if (ABISP abi_sp = process->GetABI ())
910+ pointer_value = abi_sp->FixCodeAddress (pointer_value);
911+ stack.back ().GetScalar () = pointer_value;
912+ stack.back ().ClearContext ();
913+ } break ;
914+ case Value::ValueType::Invalid:
915+ return llvm::createStringError (" invalid value type for DW_OP_deref" );
916+ }
917+
918+ return llvm::Error::success ();
919+ }
920+
863921// / Helper function to move common code used to load sized data from a uint8_t
864922// / buffer.
865923// /
@@ -890,7 +948,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
890948 if (opcodes.GetByteSize () == 0 )
891949 return llvm::createStringError (
892950 " no location, value may have been optimized out" );
893- std::vector<Value> stack;
951+
952+ Stack stack;
894953
895954 Process *process = nullptr ;
896955 StackFrame *frame = nullptr ;
@@ -1019,69 +1078,9 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
10191078 // retrieved from the dereferenced address is the size of an address on the
10201079 // target machine.
10211080 case DW_OP_deref: {
1022- if (stack.empty ())
1023- return llvm::createStringError (
1024- " expression stack empty for DW_OP_deref" );
1025- Value::ValueType value_type = stack.back ().GetValueType ();
1026- switch (value_type) {
1027- case Value::ValueType::HostAddress: {
1028- void *src = (void *)stack.back ().GetScalar ().ULongLong ();
1029- intptr_t ptr;
1030- ::memcpy (&ptr, src, sizeof (void *));
1031- stack.back ().GetScalar () = ptr;
1032- stack.back ().ClearContext ();
1033- } break ;
1034- case Value::ValueType::FileAddress: {
1035- auto file_addr = stack.back ().GetScalar ().ULongLong (
1036- LLDB_INVALID_ADDRESS);
1037-
1038- Address so_addr;
1039- auto maybe_load_addr = ResolveLoadAddress (
1040- exe_ctx, module_sp, " DW_OP_deref" , file_addr, so_addr);
1041-
1042- if (!maybe_load_addr)
1043- return maybe_load_addr.takeError ();
1044-
1045- stack.back ().GetScalar () = *maybe_load_addr;
1046- // Fall through to load address promotion code below.
1047- }
1048- [[fallthrough]];
1049- case Value::ValueType::Scalar:
1050- // Promote Scalar to LoadAddress and fall through.
1051- stack.back ().SetValueType (Value::ValueType::LoadAddress);
1052- [[fallthrough]];
1053- case Value::ValueType::LoadAddress:
1054- if (exe_ctx) {
1055- if (process) {
1056- lldb::addr_t pointer_addr =
1057- stack.back ().GetScalar ().ULongLong (LLDB_INVALID_ADDRESS);
1058- Status error;
1059- lldb::addr_t pointer_value =
1060- process->ReadPointerFromMemory (pointer_addr, error);
1061- if (pointer_value != LLDB_INVALID_ADDRESS) {
1062- if (ABISP abi_sp = process->GetABI ())
1063- pointer_value = abi_sp->FixCodeAddress (pointer_value);
1064- stack.back ().GetScalar () = pointer_value;
1065- stack.back ().ClearContext ();
1066- } else {
1067- return llvm::createStringError (
1068- " Failed to dereference pointer from 0x%" PRIx64
1069- " for DW_OP_deref: %s\n " ,
1070- pointer_addr, error.AsCString ());
1071- }
1072- } else {
1073- return llvm::createStringError (" NULL process for DW_OP_deref" );
1074- }
1075- } else {
1076- return llvm::createStringError (
1077- " NULL execution context for DW_OP_deref" );
1078- }
1079- break ;
1080-
1081- case Value::ValueType::Invalid:
1082- return llvm::createStringError (" invalid value type for DW_OP_deref" );
1083- }
1084-
1081+ if (llvm::Error err =
1082+ Evaluate_DW_OP_deref (stack, exe_ctx, module_sp, process))
1083+ return err;
10851084 } break ;
10861085
10871086 // OPCODE: DW_OP_deref_size
@@ -1966,8 +1965,7 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
19661965 case Value::ValueType::Scalar: {
19671966 uint32_t bit_size = piece_byte_size * 8 ;
19681967 uint32_t bit_offset = 0 ;
1969- if (!scalar.ExtractBitfield (
1970- bit_size, bit_offset)) {
1968+ if (!scalar.ExtractBitfield (bit_size, bit_offset)) {
19711969 return llvm::createStringError (
19721970 " unable to extract %" PRIu64 " bytes from a %" PRIu64
19731971 " byte scalar value." ,
@@ -2409,8 +2407,7 @@ bool DWARFExpression::MatchesOperand(
24092407 return MatchUnaryOp (
24102408 MatchOpType (Instruction::Operand::Type::Dereference),
24112409 MatchBinaryOp (MatchOpType (Instruction::Operand::Type::Sum),
2412- MatchRegOp (*reg),
2413- MatchImmOp (offset)))(operand);
2410+ MatchRegOp (*reg), MatchImmOp (offset)))(operand);
24142411 } else {
24152412 return MatchRegOp (*reg)(operand);
24162413 }
0 commit comments