1 | /**************************************************************************/ |
2 | /* debug_adapter_protocol.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef DEBUG_ADAPTER_PROTOCOL_H |
32 | #define DEBUG_ADAPTER_PROTOCOL_H |
33 | |
34 | #include "core/io/stream_peer.h" |
35 | #include "core/io/stream_peer_tcp.h" |
36 | #include "core/io/tcp_server.h" |
37 | |
38 | #include "debug_adapter_parser.h" |
39 | #include "debug_adapter_types.h" |
40 | |
41 | #define DAP_MAX_BUFFER_SIZE 4194304 // 4MB |
42 | #define DAP_MAX_CLIENTS 8 |
43 | |
44 | class DebugAdapterParser; |
45 | |
46 | struct DAPeer : RefCounted { |
47 | Ref<StreamPeerTCP> connection; |
48 | |
49 | uint8_t req_buf[DAP_MAX_BUFFER_SIZE]; |
50 | int req_pos = 0; |
51 | bool = false; |
52 | int content_length = 0; |
53 | List<Dictionary> res_queue; |
54 | int seq = 0; |
55 | uint64_t timestamp = 0; |
56 | |
57 | // Client specific info |
58 | bool linesStartAt1 = false; |
59 | bool columnsStartAt1 = false; |
60 | bool supportsVariableType = false; |
61 | bool supportsInvalidatedEvent = false; |
62 | bool supportsCustomData = false; |
63 | |
64 | // Internal client info |
65 | bool attached = false; |
66 | |
67 | Error handle_data(); |
68 | Error send_data(); |
69 | String 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 | private: |
78 | static DebugAdapterProtocol *singleton; |
79 | DebugAdapterParser *parser = nullptr; |
80 | |
81 | List<Ref<DAPeer>> clients; |
82 | Ref<TCPServer> server; |
83 | |
84 | Error on_client_connected(); |
85 | void on_client_disconnected(const Ref<DAPeer> &p_peer); |
86 | void on_debug_paused(); |
87 | void on_debug_stopped(); |
88 | void on_debug_output(const String &p_message); |
89 | void on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump); |
90 | void on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled); |
91 | void on_debug_stack_dump(const Array &p_stack_dump); |
92 | void on_debug_stack_frame_vars(const int &p_size); |
93 | void on_debug_stack_frame_var(const Array &p_data); |
94 | void on_debug_data(const String &p_msg, const Array &p_data); |
95 | |
96 | void reset_current_info(); |
97 | void reset_ids(); |
98 | void reset_stack_info(); |
99 | |
100 | int parse_variant(const Variant &p_var); |
101 | |
102 | bool _initialized = false; |
103 | bool _processing_breakpoint = false; |
104 | bool _stepping = false; |
105 | bool _processing_stackdump = false; |
106 | int _remaining_vars = 0; |
107 | int _current_frame = 0; |
108 | uint64_t _request_timeout = 1000; |
109 | bool _sync_breakpoints = false; |
110 | |
111 | String _current_request; |
112 | Ref<DAPeer> _current_peer; |
113 | |
114 | int breakpoint_id = 0; |
115 | int stackframe_id = 0; |
116 | int variable_id = 0; |
117 | List<DAP::Breakpoint> breakpoint_list; |
118 | HashMap<DAP::StackFrame, List<int>, DAP::StackFrame> stackframe_list; |
119 | HashMap<int, Array> variable_list; |
120 | |
121 | public: |
122 | friend class DebugAdapterServer; |
123 | |
124 | _FORCE_INLINE_ static DebugAdapterProtocol *get_singleton() { return singleton; } |
125 | _FORCE_INLINE_ bool is_active() const { return _initialized && clients.size() > 0; } |
126 | |
127 | bool process_message(const String &p_text); |
128 | |
129 | String get_current_request() const { return _current_request; } |
130 | Ref<DAPeer> get_current_peer() const { return _current_peer; } |
131 | |
132 | void notify_initialized(); |
133 | void notify_process(); |
134 | void notify_terminated(); |
135 | void notify_exited(const int &p_exitcode = 0); |
136 | void notify_stopped_paused(); |
137 | void notify_stopped_exception(const String &p_error); |
138 | void notify_stopped_breakpoint(const int &p_id); |
139 | void notify_stopped_step(); |
140 | void notify_continued(); |
141 | void notify_output(const String &p_message); |
142 | void notify_custom_data(const String &p_msg, const Array &p_data); |
143 | void notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled); |
144 | |
145 | Array update_breakpoints(const String &p_path, const Array &p_lines); |
146 | |
147 | void poll(); |
148 | Error start(int p_port, const IPAddress &p_bind_ip); |
149 | void stop(); |
150 | |
151 | DebugAdapterProtocol(); |
152 | ~DebugAdapterProtocol(); |
153 | }; |
154 | |
155 | #endif // DEBUG_ADAPTER_PROTOCOL_H |
156 | |