88
99#include " DAP.h"
1010#include " EventHelper.h"
11+ #include " Handler/RequestHandler.h"
1112#include " JSONUtils.h"
12- #include " RequestHandler.h"
13+ #include " ProtocolUtils.h"
14+
15+ using namespace llvm ;
16+ using namespace lldb_dap ::protocol;
1317
1418namespace lldb_dap {
1519
16- // "VariablesRequest": {
17- // "allOf": [ { "$ref": "#/definitions/Request" }, {
18- // "type": "object",
19- // "description": "Variables request; value of command field is 'variables'.
20- // Retrieves all child variables for the given variable reference. An
21- // optional filter can be used to limit the fetched children to either named
22- // or indexed children.", "properties": {
23- // "command": {
24- // "type": "string",
25- // "enum": [ "variables" ]
26- // },
27- // "arguments": {
28- // "$ref": "#/definitions/VariablesArguments"
29- // }
30- // },
31- // "required": [ "command", "arguments" ]
32- // }]
33- // },
34- // "VariablesArguments": {
35- // "type": "object",
36- // "description": "Arguments for 'variables' request.",
37- // "properties": {
38- // "variablesReference": {
39- // "type": "integer",
40- // "description": "The Variable reference."
41- // },
42- // "filter": {
43- // "type": "string",
44- // "enum": [ "indexed", "named" ],
45- // "description": "Optional filter to limit the child variables to either
46- // named or indexed. If ommited, both types are fetched."
47- // },
48- // "start": {
49- // "type": "integer",
50- // "description": "The index of the first variable to return; if omitted
51- // children start at 0."
52- // },
53- // "count": {
54- // "type": "integer",
55- // "description": "The number of variables to return. If count is missing
56- // or 0, all variables are returned."
57- // },
58- // "format": {
59- // "$ref": "#/definitions/ValueFormat",
60- // "description": "Specifies details on how to format the Variable
61- // values."
62- // }
63- // },
64- // "required": [ "variablesReference" ]
65- // },
66- // "VariablesResponse": {
67- // "allOf": [ { "$ref": "#/definitions/Response" }, {
68- // "type": "object",
69- // "description": "Response to 'variables' request.",
70- // "properties": {
71- // "body": {
72- // "type": "object",
73- // "properties": {
74- // "variables": {
75- // "type": "array",
76- // "items": {
77- // "$ref": "#/definitions/Variable"
78- // },
79- // "description": "All (or a range) of variables for the given
80- // variable reference."
81- // }
82- // },
83- // "required": [ "variables" ]
84- // }
85- // },
86- // "required": [ "body" ]
87- // }]
88- // }
89- void VariablesRequestHandler::operator ()(
90- const llvm::json::Object &request) const {
91- llvm::json::Object response;
92- FillResponse (request, response);
93- llvm::json::Array variables;
94- const auto *arguments = request.getObject (" arguments" );
95- const auto variablesReference =
96- GetInteger<uint64_t >(arguments, " variablesReference" ).value_or (0 );
97- const auto start = GetInteger<int64_t >(arguments, " start" ).value_or (0 );
98- const auto count = GetInteger<int64_t >(arguments, " count" ).value_or (0 );
20+ // / Retrieves all child variables for the given variable reference.
21+ // /
22+ // / A filter can be used to limit the fetched children to either named or
23+ // / indexed children.
24+ Expected<VariablesResponseBody>
25+ VariablesRequestHandler::Run (const VariablesArguments &arguments) const {
26+ const uint64_t var_ref = arguments.variablesReference ;
27+ const uint64_t count = arguments.count ;
28+ const uint64_t start = arguments.start ;
9929 bool hex = false ;
100- const auto *format = arguments->getObject (" format" );
101- if (format)
102- hex = GetBoolean (format, " hex" ).value_or (false );
30+ if (arguments.format )
31+ hex = arguments.format ->hex ;
32+
33+ std::vector<Variable> variables;
10334
104- if (lldb::SBValueList *top_scope =
105- dap.variables .GetTopLevelScope (variablesReference)) {
35+ if (lldb::SBValueList *top_scope = dap.variables .GetTopLevelScope (var_ref)) {
10636 // variablesReference is one of our scopes, not an actual variable it is
10737 // asking for the list of args, locals or globals.
10838 int64_t start_idx = 0 ;
10939 int64_t num_children = 0 ;
11040
111- if (variablesReference == VARREF_REGS) {
41+ if (var_ref == VARREF_REGS) {
11242 // Change the default format of any pointer sized registers in the first
11343 // register set to be the lldb::eFormatAddressInfo so we show the pointer
11444 // and resolve what the pointer resolves to. Only change the format if the
@@ -128,7 +58,7 @@ void VariablesRequestHandler::operator()(
12858 }
12959
13060 num_children = top_scope->GetSize ();
131- if (num_children == 0 && variablesReference == VARREF_LOCALS) {
61+ if (num_children == 0 && var_ref == VARREF_LOCALS) {
13262 // Check for an error in the SBValueList that might explain why we don't
13363 // have locals. If we have an error display it as the sole value in the
13464 // the locals.
@@ -145,12 +75,11 @@ void VariablesRequestHandler::operator()(
14575 // errors are only set when there is a problem that the user could
14676 // fix, so no error will show up when you have no debug info, only when
14777 // we do have debug info and something that is fixable can be done.
148- llvm::json::Object object;
149- EmplaceSafeString (object, " name" , " <error>" );
150- EmplaceSafeString (object, " type" , " const char *" );
151- EmplaceSafeString (object, " value" , var_err);
152- object.try_emplace (" variablesReference" , (int64_t )0 );
153- variables.emplace_back (std::move (object));
78+ Variable var;
79+ var.name = " <error>" ;
80+ var.type = " const char *" ;
81+ var.value = var_err;
82+ variables.emplace_back (var);
15483 }
15584 }
15685 const int64_t end_idx = start_idx + ((count == 0 ) ? num_children : count);
@@ -165,7 +94,7 @@ void VariablesRequestHandler::operator()(
16594 }
16695
16796 // Show return value if there is any ( in the local top frame )
168- if (variablesReference == VARREF_LOCALS) {
97+ if (var_ref == VARREF_LOCALS) {
16998 auto process = dap.target .GetProcess ();
17099 auto selected_thread = process.GetSelectedThread ();
171100 lldb::SBValue stop_return_value = selected_thread.GetStopReturnValue ();
@@ -194,32 +123,35 @@ void VariablesRequestHandler::operator()(
194123 if (!variable.IsValid ())
195124 break ;
196125
197- int64_t var_ref =
126+ const int64_t frame_var_ref =
198127 dap.variables .InsertVariable (variable, /* is_permanent=*/ false );
199128 variables.emplace_back (CreateVariable (
200- variable, var_ref, hex, dap.configuration .enableAutoVariableSummaries ,
129+ variable, frame_var_ref, hex,
130+ dap.configuration .enableAutoVariableSummaries ,
201131 dap.configuration .enableSyntheticChildDebugging ,
202132 variable_name_counts[GetNonNullVariableName (variable)] > 1 ));
203133 }
204134 } else {
205135 // We are expanding a variable that has children, so we will return its
206136 // children.
207- lldb::SBValue variable = dap.variables .GetVariable (variablesReference );
137+ lldb::SBValue variable = dap.variables .GetVariable (var_ref );
208138 if (variable.IsValid ()) {
139+ const bool is_permanent =
140+ dap.variables .IsPermanentVariableReference (var_ref);
209141 auto addChild = [&](lldb::SBValue child,
210142 std::optional<std::string> custom_name = {}) {
211143 if (!child.IsValid ())
212144 return ;
213- bool is_permanent =
214- dap.variables .IsPermanentVariableReference (variablesReference );
215- int64_t var_ref = dap. variables .InsertVariable (child, is_permanent);
216- variables. emplace_back ( CreateVariable (
217- child, var_ref, hex, dap.configuration .enableAutoVariableSummaries ,
218- dap.configuration .enableSyntheticChildDebugging ,
219- /* is_name_duplicated=*/ false , custom_name));
145+ const int64_t child_var_ref =
146+ dap.variables .InsertVariable (child, is_permanent );
147+ variables.emplace_back (
148+ CreateVariable (child, child_var_ref, hex,
149+ dap.configuration .enableAutoVariableSummaries ,
150+ dap.configuration .enableSyntheticChildDebugging ,
151+ /* is_name_duplicated=*/ false , custom_name));
220152 };
221153 const int64_t num_children = variable.GetNumChildren ();
222- int64_t end_idx = start + ((count == 0 ) ? num_children : count);
154+ const int64_t end_idx = start + ((count == 0 ) ? num_children : count);
223155 int64_t i = start;
224156 for (; i < end_idx && i < num_children; ++i)
225157 addChild (variable.GetChildAtIndex (i));
@@ -233,10 +165,8 @@ void VariablesRequestHandler::operator()(
233165 addChild (variable.GetNonSyntheticValue (), " [raw]" );
234166 }
235167 }
236- llvm::json::Object body;
237- body.try_emplace (" variables" , std::move (variables));
238- response.try_emplace (" body" , std::move (body));
239- dap.SendJSON (llvm::json::Value (std::move (response)));
168+
169+ return VariablesResponseBody{variables};
240170}
241171
242172} // namespace lldb_dap
0 commit comments