llvm-project/lldb/tools/lldb-dap/Handler/SetVariableRequestHandler.cpp
Ebuka Ezike 47a969ef88
[lldb-dap] Refactor variablesReference storage and scope management. (#179262)
This commit refactors the Variables class into a
VariableReferenceStorage with variableReferences of different
ReferenceKinds.

The variablesReference is now a uint32_t.
The most significant byte (bits 24 - 31) holds the reference kind and
the remaining 3 bytes (bits 0 -23) holds the actual reference.

We have (at the moment) 3 reference kinds.
Temporary => 0b0000 => 0x00
Permanent => 0b0001 => 0x01
Scope     => 0b0010 => 0x03

The actual variablesReference can be used to get a `VariableStore`.

VariableStore holds variables in a group.
It has two implementations:
- ScopeStore: Holds variables within frame scopes (locals, globals,
registers). This is lazy-loaded and only fetched when variable(s) in the
scope is requested.
- ExpandableValueStore: Holds SBValue and fetches it's variable children
when requested.

ReferenceKindPool:
   The variablesReference created starts from 1 with the mask of the
   Reference kind applied.
   It holds vector of VariableStore of one Referencekind,
   This allows constant lookup of a reference

Example:
```md

| maskedVariablesReference | Mask | variablesReference | RefrenceKind | VariableStore |
|--------------------------|------|--------------------|--------------|---------------|
| 20        -> 0x00000014  | 0x00 | 20 -> 0x00000014   | temporary    | ValueStore    |
| 268435476 -> 0x01000014  | 0x01 | 20 -> 0x00000014   | permanent    | ValueStore    |
| 536870932 -> 0x01000014  | 0x02 | 20 -> 0x00000014   | scope        | ScopeStore    |
```
2026-02-17 12:55:19 +00:00

96 lines
3.3 KiB
C++

//===-- SetVariableRequestHandler.cpp -------------------------------------===//
//
// 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 "DAP.h"
#include "EventHelper.h"
#include "JSONUtils.h"
#include "Protocol/DAPTypes.h"
#include "Protocol/ProtocolEvents.h"
#include "Protocol/ProtocolTypes.h"
#include "RequestHandler.h"
using namespace lldb_dap::protocol;
namespace lldb_dap {
/// Set the variable with the given name in the variable container to a new
/// value. Clients should only call this request if the corresponding capability
/// `supportsSetVariable` is true.
///
/// If a debug adapter implements both `setVariable` and `setExpression`,
/// a client will only use `setExpression` if the variable has an evaluateName
/// property.
llvm::Expected<SetVariableResponseBody>
SetVariableRequestHandler::Run(const SetVariableArguments &args) const {
const auto args_name = llvm::StringRef(args.name);
if (args.variablesReference.Kind() == eReferenceKindInvalid) {
return llvm::make_error<DAPError>(
llvm::formatv("invalid reference {}",
args.variablesReference.AsUInt32())
.str(),
llvm::inconvertibleErrorCode(),
/*show_user=*/false);
}
constexpr llvm::StringRef return_value_name = "(Return Value)";
if (args_name == return_value_name)
return llvm::make_error<DAPError>(
"cannot change the value of the return value");
lldb::SBValue variable =
dap.reference_storage.FindVariable(args.variablesReference, args_name);
if (!variable.IsValid())
return llvm::make_error<DAPError>("could not find variable in scope");
lldb::SBError error;
const bool success = variable.SetValueFromCString(args.value.c_str(), error);
if (!success)
return llvm::make_error<DAPError>(error.GetCString());
VariableDescription desc(variable,
dap.configuration.enableAutoVariableSummaries);
SetVariableResponseBody body;
body.value = desc.display_value;
body.type = desc.display_type_name;
// We don't know the index of the variable in our dap.variables
// so always insert a new one to get its variablesReference.
// is_permanent is false because debug console does not support
// setVariable request.
const var_ref_t new_var_ref =
dap.reference_storage.InsertVariable(variable, /*is_permanent=*/false);
if (variable.MightHaveChildren()) {
body.variablesReference = new_var_ref;
if (desc.type_obj.IsArrayType())
body.indexedVariables = variable.GetNumChildren();
else
body.namedVariables = variable.GetNumChildren();
}
if (const lldb::addr_t addr = variable.GetLoadAddress();
addr != LLDB_INVALID_ADDRESS)
body.memoryReference = addr;
if (ValuePointsToCode(variable))
body.valueLocationReference = new_var_ref.AsUInt32();
// Also send invalidated event to signal client that some variables
// (e.g. references) can be changed.
SendInvalidatedEvent(dap, {InvalidatedEventBody::eAreaVariables});
// Also send memory event to signal client that variable memory was changed.
SendMemoryEvent(dap, variable);
return body;
}
} // namespace lldb_dap