|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "DAP.h"
|
10 |
| -#include "EventHelper.h" |
11 |
| -#include "JSONUtils.h" |
| 10 | +#include "Protocol/ProtocolRequests.h" |
12 | 11 | #include "RequestHandler.h"
|
13 | 12 | #include "lldb/API/SBInstruction.h"
|
| 13 | +#include "lldb/lldb-defines.h" |
14 | 14 |
|
| 15 | +using namespace lldb_dap::protocol; |
15 | 16 | namespace lldb_dap {
|
16 | 17 |
|
17 |
| -// "StepInTargetsRequest": { |
18 |
| -// "allOf": [ { "$ref": "#/definitions/Request" }, { |
19 |
| -// "type": "object", |
20 |
| -// "description": "This request retrieves the possible step-in targets for |
21 |
| -// the specified stack frame.\nThese targets can be used in the `stepIn` |
22 |
| -// request.\nClients should only call this request if the corresponding |
23 |
| -// capability `supportsStepInTargetsRequest` is true.", "properties": { |
24 |
| -// "command": { |
25 |
| -// "type": "string", |
26 |
| -// "enum": [ "stepInTargets" ] |
27 |
| -// }, |
28 |
| -// "arguments": { |
29 |
| -// "$ref": "#/definitions/StepInTargetsArguments" |
30 |
| -// } |
31 |
| -// }, |
32 |
| -// "required": [ "command", "arguments" ] |
33 |
| -// }] |
34 |
| -// }, |
35 |
| -// "StepInTargetsArguments": { |
36 |
| -// "type": "object", |
37 |
| -// "description": "Arguments for `stepInTargets` request.", |
38 |
| -// "properties": { |
39 |
| -// "frameId": { |
40 |
| -// "type": "integer", |
41 |
| -// "description": "The stack frame for which to retrieve the possible |
42 |
| -// step-in targets." |
43 |
| -// } |
44 |
| -// }, |
45 |
| -// "required": [ "frameId" ] |
46 |
| -// }, |
47 |
| -// "StepInTargetsResponse": { |
48 |
| -// "allOf": [ { "$ref": "#/definitions/Response" }, { |
49 |
| -// "type": "object", |
50 |
| -// "description": "Response to `stepInTargets` request.", |
51 |
| -// "properties": { |
52 |
| -// "body": { |
53 |
| -// "type": "object", |
54 |
| -// "properties": { |
55 |
| -// "targets": { |
56 |
| -// "type": "array", |
57 |
| -// "items": { |
58 |
| -// "$ref": "#/definitions/StepInTarget" |
59 |
| -// }, |
60 |
| -// "description": "The possible step-in targets of the specified |
61 |
| -// source location." |
62 |
| -// } |
63 |
| -// }, |
64 |
| -// "required": [ "targets" ] |
65 |
| -// } |
66 |
| -// }, |
67 |
| -// "required": [ "body" ] |
68 |
| -// }] |
69 |
| -// } |
70 |
| -void StepInTargetsRequestHandler::operator()( |
71 |
| - const llvm::json::Object &request) const { |
72 |
| - llvm::json::Object response; |
73 |
| - FillResponse(request, response); |
74 |
| - const auto *arguments = request.getObject("arguments"); |
75 |
| - |
| 18 | +// This request retrieves the possible step-in targets for the specified stack |
| 19 | +// frame. |
| 20 | +// These targets can be used in the `stepIn` request. |
| 21 | +// Clients should only call this request if the corresponding capability |
| 22 | +// `supportsStepInTargetsRequest` is true. |
| 23 | +llvm::Expected<StepInTargetsResponseBody> |
| 24 | +StepInTargetsRequestHandler::Run(const StepInTargetsArguments &args) const { |
76 | 25 | dap.step_in_targets.clear();
|
77 |
| - lldb::SBFrame frame = dap.GetLLDBFrame(*arguments); |
78 |
| - if (frame.IsValid()) { |
79 |
| - lldb::SBAddress pc_addr = frame.GetPCAddress(); |
80 |
| - lldb::SBAddress line_end_addr = |
81 |
| - pc_addr.GetLineEntry().GetSameLineContiguousAddressRangeEnd(true); |
82 |
| - lldb::SBInstructionList insts = dap.target.ReadInstructions( |
83 |
| - pc_addr, line_end_addr, /*flavor_string=*/nullptr); |
84 |
| - |
85 |
| - if (!insts.IsValid()) { |
86 |
| - response["success"] = false; |
87 |
| - response["message"] = "Failed to get instructions for frame."; |
88 |
| - dap.SendJSON(llvm::json::Value(std::move(response))); |
89 |
| - return; |
90 |
| - } |
| 26 | + const lldb::SBFrame frame = dap.GetLLDBFrame(args.frameId); |
| 27 | + if (!frame.IsValid()) |
| 28 | + return llvm::make_error<DAPError>("Failed to get frame for input frameId."); |
| 29 | + |
| 30 | + lldb::SBAddress pc_addr = frame.GetPCAddress(); |
| 31 | + lldb::SBAddress line_end_addr = |
| 32 | + pc_addr.GetLineEntry().GetSameLineContiguousAddressRangeEnd(true); |
| 33 | + lldb::SBInstructionList insts = dap.target.ReadInstructions( |
| 34 | + pc_addr, line_end_addr, /*flavor_string=*/nullptr); |
| 35 | + |
| 36 | + if (!insts.IsValid()) |
| 37 | + return llvm::make_error<DAPError>("Failed to get instructions for frame."); |
| 38 | + |
| 39 | + StepInTargetsResponseBody body; |
| 40 | + const size_t num_insts = insts.GetSize(); |
| 41 | + for (size_t i = 0; i < num_insts; ++i) { |
| 42 | + lldb::SBInstruction inst = insts.GetInstructionAtIndex(i); |
| 43 | + if (!inst.IsValid()) |
| 44 | + break; |
| 45 | + |
| 46 | + const lldb::addr_t inst_addr = inst.GetAddress().GetLoadAddress(dap.target); |
| 47 | + if (inst_addr == LLDB_INVALID_ADDRESS) |
| 48 | + break; |
| 49 | + |
| 50 | + // Note: currently only x86/x64 supports flow kind. |
| 51 | + const lldb::InstructionControlFlowKind flow_kind = |
| 52 | + inst.GetControlFlowKind(dap.target); |
| 53 | + |
| 54 | + if (flow_kind == lldb::eInstructionControlFlowKindCall) { |
| 55 | + |
| 56 | + const llvm::StringRef call_operand_name = inst.GetOperands(dap.target); |
| 57 | + lldb::addr_t call_target_addr = LLDB_INVALID_ADDRESS; |
| 58 | + if (call_operand_name.getAsInteger(0, call_target_addr)) |
| 59 | + continue; |
| 60 | + |
| 61 | + const lldb::SBAddress call_target_load_addr = |
| 62 | + dap.target.ResolveLoadAddress(call_target_addr); |
| 63 | + if (!call_target_load_addr.IsValid()) |
| 64 | + continue; |
| 65 | + |
| 66 | + // The existing ThreadPlanStepInRange only accept step in target |
| 67 | + // function with debug info. |
| 68 | + lldb::SBSymbolContext sc = dap.target.ResolveSymbolContextForAddress( |
| 69 | + call_target_load_addr, lldb::eSymbolContextFunction); |
| 70 | + |
| 71 | + // The existing ThreadPlanStepInRange only accept step in target |
| 72 | + // function with debug info. |
| 73 | + llvm::StringRef step_in_target_name; |
| 74 | + if (sc.IsValid() && sc.GetFunction().IsValid()) |
| 75 | + step_in_target_name = sc.GetFunction().GetDisplayName(); |
| 76 | + |
| 77 | + // Skip call sites if we fail to resolve its symbol name. |
| 78 | + if (step_in_target_name.empty()) |
| 79 | + continue; |
91 | 80 |
|
92 |
| - llvm::json::Array step_in_targets; |
93 |
| - const auto num_insts = insts.GetSize(); |
94 |
| - for (size_t i = 0; i < num_insts; ++i) { |
95 |
| - lldb::SBInstruction inst = insts.GetInstructionAtIndex(i); |
96 |
| - if (!inst.IsValid()) |
97 |
| - break; |
98 |
| - |
99 |
| - lldb::addr_t inst_addr = inst.GetAddress().GetLoadAddress(dap.target); |
100 |
| - |
101 |
| - // Note: currently only x86/x64 supports flow kind. |
102 |
| - lldb::InstructionControlFlowKind flow_kind = |
103 |
| - inst.GetControlFlowKind(dap.target); |
104 |
| - if (flow_kind == lldb::eInstructionControlFlowKindCall) { |
105 |
| - // Use call site instruction address as id which is easy to debug. |
106 |
| - llvm::json::Object step_in_target; |
107 |
| - step_in_target["id"] = inst_addr; |
108 |
| - |
109 |
| - llvm::StringRef call_operand_name = inst.GetOperands(dap.target); |
110 |
| - lldb::addr_t call_target_addr; |
111 |
| - if (call_operand_name.getAsInteger(0, call_target_addr)) |
112 |
| - continue; |
113 |
| - |
114 |
| - lldb::SBAddress call_target_load_addr = |
115 |
| - dap.target.ResolveLoadAddress(call_target_addr); |
116 |
| - if (!call_target_load_addr.IsValid()) |
117 |
| - continue; |
118 |
| - |
119 |
| - // The existing ThreadPlanStepInRange only accept step in target |
120 |
| - // function with debug info. |
121 |
| - lldb::SBSymbolContext sc = dap.target.ResolveSymbolContextForAddress( |
122 |
| - call_target_load_addr, lldb::eSymbolContextFunction); |
123 |
| - |
124 |
| - // The existing ThreadPlanStepInRange only accept step in target |
125 |
| - // function with debug info. |
126 |
| - std::string step_in_target_name; |
127 |
| - if (sc.IsValid() && sc.GetFunction().IsValid()) |
128 |
| - step_in_target_name = sc.GetFunction().GetDisplayName(); |
129 |
| - |
130 |
| - // Skip call sites if we fail to resolve its symbol name. |
131 |
| - if (step_in_target_name.empty()) |
132 |
| - continue; |
133 |
| - |
134 |
| - dap.step_in_targets.try_emplace(inst_addr, step_in_target_name); |
135 |
| - step_in_target.try_emplace("label", step_in_target_name); |
136 |
| - step_in_targets.emplace_back(std::move(step_in_target)); |
137 |
| - } |
| 81 | + StepInTarget target; |
| 82 | + target.id = inst_addr; |
| 83 | + target.label = step_in_target_name; |
| 84 | + dap.step_in_targets.try_emplace(inst_addr, step_in_target_name); |
| 85 | + body.targets.emplace_back(std::move(target)); |
138 | 86 | }
|
139 |
| - llvm::json::Object body; |
140 |
| - body.try_emplace("targets", std::move(step_in_targets)); |
141 |
| - response.try_emplace("body", std::move(body)); |
142 |
| - } else { |
143 |
| - response["success"] = llvm::json::Value(false); |
144 |
| - response["message"] = "Failed to get frame for input frameId."; |
145 | 87 | }
|
146 |
| - dap.SendJSON(llvm::json::Value(std::move(response))); |
147 |
| -} |
| 88 | + return body; |
| 89 | +}; |
148 | 90 |
|
149 | 91 | } // namespace lldb_dap
|
0 commit comments