from typing import Optional
from .frames import frame_for_id
-from .server import request
+from .server import capability, request
from .startup import send_gdb_with_response, in_gdb_thread
from .varref import find_variable, VariableReference
return ref.to_object()
+# Helper function to perform an assignment.
+@in_gdb_thread
+def _set_expression(expression, value, frame_id):
+ global_context = True
+ if frame_id is not None:
+ frame = frame_for_id(frame_id)
+ frame.select()
+ global_context = False
+ lhs = gdb.parse_and_eval(expression, global_context=global_context)
+ rhs = gdb.parse_and_eval(value, global_context=global_context)
+ lhs.assign(rhs)
+ return EvaluateResult(lhs).to_object()
+
+
# Helper function to evaluate a gdb command in a certain frame.
@in_gdb_thread
def _repl(command, frame_id):
lambda: _variables(variablesReference, start, count)
)
return {"variables": result}
+
+
+@capability("supportsSetExpression")
+@request("setExpression")
+def set_expression(
+ *, expression: str, value: str, frameId: Optional[int] = None, **args
+):
+ return send_gdb_with_response(lambda: _set_expression(expression, value, frameId))
dap_match_values "global value in main" [lindex $obj 0] \
"body result" 25
+set obj [dap_check_request_and_response "set global in main" \
+ setExpression {o expression [s global_variable] value [s 23]}]
+dap_match_values "global value in main after set" [lindex $obj 0] \
+ "body result" 23
+
set obj [dap_request_and_response \
evaluate {o expression [s nosuchvariable]}]
set response [lindex $obj 0]