Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/debug_adapter/debug_adapter_protocol.h
9906 views
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
#pragma once
32
33
#include "core/debugger/debugger_marshalls.h"
34
#include "core/io/stream_peer_tcp.h"
35
#include "core/io/tcp_server.h"
36
37
#include "debug_adapter_parser.h"
38
#include "debug_adapter_types.h"
39
#include "scene/debugger/scene_debugger.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 has_header = 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
Dictionary pending_launch;
67
68
Error handle_data();
69
Error send_data();
70
Vector<uint8_t> format_output(const Dictionary &p_params) const;
71
};
72
73
class DebugAdapterProtocol : public Object {
74
GDCLASS(DebugAdapterProtocol, Object)
75
76
friend class DebugAdapterParser;
77
78
using DAPVarID = int;
79
using DAPStackFrameID = int;
80
81
private:
82
static DebugAdapterProtocol *singleton;
83
DebugAdapterParser *parser = nullptr;
84
85
List<Ref<DAPeer>> clients;
86
Ref<TCPServer> server;
87
88
Error on_client_connected();
89
void on_client_disconnected(const Ref<DAPeer> &p_peer);
90
void on_debug_paused();
91
void on_debug_stopped();
92
void on_debug_output(const String &p_message, int p_type);
93
void on_debug_breaked(const bool &p_reallydid, const bool &p_can_debug, const String &p_reason, const bool &p_has_stackdump);
94
void on_debug_breakpoint_toggled(const String &p_path, const int &p_line, const bool &p_enabled);
95
void on_debug_stack_dump(const Array &p_stack_dump);
96
void on_debug_stack_frame_vars(const int &p_size);
97
void on_debug_stack_frame_var(const Array &p_data);
98
void on_debug_data(const String &p_msg, const Array &p_data);
99
100
void reset_current_info();
101
void reset_ids();
102
void reset_stack_info();
103
104
int parse_variant(const Variant &p_var);
105
void parse_object(SceneDebuggerObject &p_obj);
106
const Variant parse_object_variable(const SceneDebuggerObject::SceneDebuggerProperty &p_property);
107
void parse_evaluation(DebuggerMarshalls::ScriptStackVariable &p_var);
108
109
ObjectID search_object_id(DAPVarID p_var_id);
110
bool request_remote_object(const ObjectID &p_object_id);
111
bool request_remote_evaluate(const String &p_eval, int p_stack_frame);
112
113
const DAP::Source &fetch_source(const String &p_path);
114
void update_source(const String &p_path);
115
116
bool _initialized = false;
117
bool _processing_breakpoint = false;
118
bool _stepping = false;
119
bool _processing_stackdump = false;
120
int _remaining_vars = 0;
121
int _current_frame = 0;
122
uint64_t _request_timeout = 5000;
123
bool _sync_breakpoints = false;
124
125
String _current_request;
126
Ref<DAPeer> _current_peer;
127
128
int breakpoint_id = 0;
129
int stackframe_id = 0;
130
DAPVarID variable_id = 0;
131
List<DAP::Breakpoint> breakpoint_list;
132
HashMap<String, DAP::Source> breakpoint_source_list;
133
List<DAP::StackFrame> stackframe_list;
134
HashMap<DAPStackFrameID, Vector<int>> scope_list;
135
HashMap<DAPVarID, Array> variable_list;
136
137
HashMap<ObjectID, DAPVarID> object_list;
138
HashSet<ObjectID> object_pending_set;
139
140
HashMap<String, DAP::Variable> eval_list;
141
HashSet<String> eval_pending_list;
142
143
public:
144
friend class DebugAdapterServer;
145
146
_FORCE_INLINE_ static DebugAdapterProtocol *get_singleton() { return singleton; }
147
_FORCE_INLINE_ bool is_active() const { return _initialized && clients.size() > 0; }
148
149
bool process_message(const String &p_text);
150
151
String get_current_request() const { return _current_request; }
152
Ref<DAPeer> get_current_peer() const { return _current_peer; }
153
154
void notify_initialized();
155
void notify_process();
156
void notify_terminated();
157
void notify_exited(const int &p_exitcode = 0);
158
void notify_stopped_paused();
159
void notify_stopped_exception(const String &p_error);
160
void notify_stopped_breakpoint(const int &p_id);
161
void notify_stopped_step();
162
void notify_continued();
163
void notify_output(const String &p_message, RemoteDebugger::MessageType p_type);
164
void notify_custom_data(const String &p_msg, const Array &p_data);
165
void notify_breakpoint(const DAP::Breakpoint &p_breakpoint, const bool &p_enabled);
166
167
Array update_breakpoints(const String &p_path, const Array &p_lines);
168
169
void poll();
170
Error start(int p_port, const IPAddress &p_bind_ip);
171
void stop();
172
173
DebugAdapterProtocol();
174
~DebugAdapterProtocol();
175
};
176
177