| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/debugger/debugger_marshalls.h" |
| 34 |
#include "core/debugger/remote_debugger.h" |
| 35 |
#include "core/io/stream_peer_tcp.h" |
| 36 |
#include "core/io/tcp_server.h" |
| 37 |
#include "editor/debugger/debug_adapter/debug_adapter_types.h" |
| 38 |
#include "scene/debugger/scene_debugger.h" |
| 39 |
|
| 40 |
#define DAP_MAX_BUFFER_SIZE 4194304 // 4MB |
| 41 |
#define DAP_MAX_CLIENTS 8 |
| 42 |
|
| 43 |
class DebugAdapterParser; |
| 44 |
|
| 45 |
struct DAPeer : RefCounted { |
| 46 |
Ref<StreamPeerTCP> connection; |
| 47 |
|
| 48 |
uint8_t req_buf[DAP_MAX_BUFFER_SIZE]; |
| 49 |
int req_pos = 0; |
| 50 |
bool has_header = false; |
| 51 |
int content_length = 0; |
| 52 |
List<Dictionary> res_queue; |
| 53 |
int seq = 0; |
| 54 |
uint64_t timestamp = 0; |
| 55 |
|
| 56 |
|
| 57 |
bool linesStartAt1 = false; |
| 58 |
bool columnsStartAt1 = false; |
| 59 |
bool supportsVariableType = false; |
| 60 |
bool supportsInvalidatedEvent = false; |
| 61 |
bool supportsCustomData = false; |
| 62 |
|
| 63 |
|
| 64 |
bool attached = false; |
| 65 |
Dictionary pending_launch; |
| 66 |
|
| 67 |
Error handle_data(); |
| 68 |
Error send_data(); |
| 69 |
Vector<uint8_t> format_output(const Dictionary &p_params) const; |
| 70 |
}; |
| 71 |
|
| 72 |
class DebugAdapterProtocol : public Object { |
| 73 |
GDCLASS(DebugAdapterProtocol, Object) |
| 74 |
|
| 75 |
friend class DebugAdapterParser; |
| 76 |
|
| 77 |
using DAPVarID = int; |
| 78 |
using DAPStackFrameID = int; |
| 79 |
|
| 80 |
private: |
| 81 |
static DebugAdapterProtocol *singleton; |
| 82 |
DebugAdapterParser *parser = nullptr; |
| 83 |
|
| 84 |
List<Ref<DAPeer>> clients; |
| 85 |
Ref<TCPServer> server; |
| 86 |
|
| 87 |
Error on_client_connected(); |
| 88 |
void on_client_disconnected(const Ref<DAPeer> &p_peer); |
| 89 |
void on_debug_paused(); |
| 90 |
void on_debug_stopped(); |
| 91 |
void on_debug_output(const String &p_message, int p_type); |
| 92 |
void on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump); |
| 93 |
void on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled); |
| 94 |
void on_debug_stack_dump(const Array &p_stack_dump); |
| 95 |
void on_debug_stack_frame_vars(const int &p_size); |
| 96 |
void on_debug_stack_frame_var(const Array &p_data); |
| 97 |
void on_debug_data(const String &p_msg, const Array &p_data); |
| 98 |
|
| 99 |
void reset_current_info(); |
| 100 |
void reset_ids(); |
| 101 |
void reset_stack_info(); |
| 102 |
|
| 103 |
int parse_variant(const Variant &p_var); |
| 104 |
void parse_object(SceneDebuggerObject &p_obj); |
| 105 |
const Variant parse_object_variable(const SceneDebuggerObject::SceneDebuggerProperty &p_property); |
| 106 |
void parse_evaluation(DebuggerMarshalls::ScriptStackVariable &p_var); |
| 107 |
|
| 108 |
ObjectID search_object_id(DAPVarID p_var_id); |
| 109 |
bool request_remote_object(const ObjectID &p_object_id); |
| 110 |
bool request_remote_evaluate(const String &p_eval, int p_stack_frame); |
| 111 |
|
| 112 |
const DAP::Source &fetch_source(const String &p_path); |
| 113 |
void update_source(const String &p_path); |
| 114 |
|
| 115 |
bool _initialized = false; |
| 116 |
bool _processing_breakpoint = false; |
| 117 |
bool _stepping = false; |
| 118 |
bool _processing_stackdump = false; |
| 119 |
int _remaining_vars = 0; |
| 120 |
int _current_frame = 0; |
| 121 |
uint64_t _request_timeout = 5000; |
| 122 |
bool _sync_breakpoints = false; |
| 123 |
|
| 124 |
String _current_request; |
| 125 |
Ref<DAPeer> _current_peer; |
| 126 |
|
| 127 |
int breakpoint_id = 0; |
| 128 |
int stackframe_id = 0; |
| 129 |
DAPVarID variable_id = 0; |
| 130 |
List<DAP::Breakpoint> breakpoint_list; |
| 131 |
HashMap<String, DAP::Source> breakpoint_source_list; |
| 132 |
List<DAP::StackFrame> stackframe_list; |
| 133 |
HashMap<DAPStackFrameID, Vector<int>> scope_list; |
| 134 |
HashMap<DAPVarID, Array> variable_list; |
| 135 |
|
| 136 |
HashMap<ObjectID, DAPVarID> object_list; |
| 137 |
HashSet<ObjectID> object_pending_set; |
| 138 |
|
| 139 |
HashMap<String, DAP::Variable> eval_list; |
| 140 |
HashSet<String> eval_pending_list; |
| 141 |
|
| 142 |
public: |
| 143 |
friend class DebugAdapterServer; |
| 144 |
|
| 145 |
_FORCE_INLINE_ static DebugAdapterProtocol *get_singleton() { return singleton; } |
| 146 |
_FORCE_INLINE_ bool is_active() const { return _initialized && clients.size() > 0; } |
| 147 |
|
| 148 |
bool process_message(const String &p_text); |
| 149 |
|
| 150 |
String get_current_request() const { return _current_request; } |
| 151 |
Ref<DAPeer> get_current_peer() const { return _current_peer; } |
| 152 |
|
| 153 |
void notify_initialized(); |
| 154 |
void notify_process(); |
| 155 |
void notify_terminated(); |
| 156 |
void notify_exited(const int &p_exitcode = 0); |
| 157 |
void notify_stopped_paused(); |
| 158 |
void notify_stopped_exception(const String &p_error); |
| 159 |
void notify_stopped_breakpoint(const int &p_id); |
| 160 |
void notify_stopped_step(); |
| 161 |
void notify_continued(); |
| 162 |
void notify_output(const String &p_message, RemoteDebugger::MessageType p_type); |
| 163 |
void notify_custom_data(const String &p_msg, const Array &p_data); |
| 164 |
void notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled); |
| 165 |
|
| 166 |
Array update_breakpoints(const String &p_path, const Array &p_lines); |
| 167 |
|
| 168 |
void poll(); |
| 169 |
Error start(int p_port, const IPAddress &p_bind_ip); |
| 170 |
void stop(); |
| 171 |
|
| 172 |
DebugAdapterProtocol(); |
| 173 |
~DebugAdapterProtocol(); |
| 174 |
}; |