Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/debugger/remote_debugger.cpp
10277 views
1
/**************************************************************************/
2
/* remote_debugger.cpp */
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
#include "remote_debugger.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/debugger/debugger_marshalls.h"
35
#include "core/debugger/engine_debugger.h"
36
#include "core/debugger/engine_profiler.h"
37
#include "core/debugger/script_debugger.h"
38
#include "core/input/input.h"
39
#include "core/io/resource_loader.h"
40
#include "core/math/expression.h"
41
#include "core/object/script_language.h"
42
#include "core/os/os.h"
43
#include "servers/display_server.h"
44
45
class RemoteDebugger::PerformanceProfiler : public EngineProfiler {
46
Object *performance = nullptr;
47
int last_perf_time = 0;
48
uint64_t last_monitor_modification_time = 0;
49
50
public:
51
void toggle(bool p_enable, const Array &p_opts) {}
52
void add(const Array &p_data) {}
53
void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
54
if (!performance) {
55
return;
56
}
57
58
uint64_t pt = OS::get_singleton()->get_ticks_msec();
59
if (pt - last_perf_time < 1000) {
60
return;
61
}
62
last_perf_time = pt;
63
64
Array custom_monitor_names = performance->call("get_custom_monitor_names");
65
66
uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
67
if (monitor_modification_time > last_monitor_modification_time) {
68
last_monitor_modification_time = monitor_modification_time;
69
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
70
}
71
72
int max = performance->get("MONITOR_MAX");
73
Array arr;
74
arr.resize(max + custom_monitor_names.size());
75
for (int i = 0; i < max; i++) {
76
arr[i] = performance->call("get_monitor", i);
77
}
78
79
for (int i = 0; i < custom_monitor_names.size(); i++) {
80
Variant monitor_value = performance->call("get_custom_monitor", custom_monitor_names[i]);
81
if (!monitor_value.is_num()) {
82
ERR_PRINT(vformat("Value of custom monitor '%s' is not a number.", String(custom_monitor_names[i])));
83
arr[i + max] = Variant();
84
} else {
85
arr[i + max] = monitor_value;
86
}
87
}
88
89
EngineDebugger::get_singleton()->send_message("performance:profile_frame", arr);
90
}
91
92
explicit PerformanceProfiler(Object *p_performance) {
93
performance = p_performance;
94
}
95
};
96
97
Error RemoteDebugger::_put_msg(const String &p_message, const Array &p_data) {
98
Array msg = { p_message, Thread::get_caller_id(), p_data };
99
Error err = peer->put_message(msg);
100
if (err != OK) {
101
n_messages_dropped++;
102
}
103
return err;
104
}
105
106
void RemoteDebugger::_err_handler(void *p_this, const char *p_func, const char *p_file, int p_line, const char *p_err, const char *p_descr, bool p_editor_notify, ErrorHandlerType p_type) {
107
RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
108
if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive errors during flush.
109
return;
110
}
111
112
Vector<ScriptLanguage::StackInfo> si;
113
114
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
115
si = ScriptServer::get_language(i)->debug_get_current_stack_info();
116
if (si.size()) {
117
break;
118
}
119
}
120
121
// send_error will lock internally.
122
rd->script_debugger->send_error(String::utf8(p_func), String::utf8(p_file), p_line, String::utf8(p_err), String::utf8(p_descr), p_editor_notify, p_type, si);
123
}
124
125
void RemoteDebugger::_print_handler(void *p_this, const String &p_string, bool p_error, bool p_rich) {
126
RemoteDebugger *rd = static_cast<RemoteDebugger *>(p_this);
127
128
if (rd->flushing && Thread::get_caller_id() == rd->flush_thread) { // Can't handle recursive prints during flush.
129
return;
130
}
131
132
String s = p_string;
133
int allowed_chars = MIN(MAX(rd->max_chars_per_second - rd->char_count, 0), s.length());
134
135
if (allowed_chars == 0 && s.length() > 0) {
136
return;
137
}
138
139
if (allowed_chars < s.length()) {
140
s = s.substr(0, allowed_chars);
141
}
142
143
MutexLock lock(rd->mutex);
144
145
rd->char_count += allowed_chars;
146
bool overflowed = rd->char_count >= rd->max_chars_per_second;
147
if (rd->is_peer_connected()) {
148
if (overflowed) {
149
s += "[...]";
150
}
151
152
OutputString output_string;
153
output_string.message = s;
154
if (p_error) {
155
output_string.type = MESSAGE_TYPE_ERROR;
156
} else if (p_rich) {
157
output_string.type = MESSAGE_TYPE_LOG_RICH;
158
} else {
159
output_string.type = MESSAGE_TYPE_LOG;
160
}
161
rd->output_strings.push_back(output_string);
162
163
if (overflowed) {
164
output_string.message = "[output overflow, print less text!]";
165
output_string.type = MESSAGE_TYPE_ERROR;
166
rd->output_strings.push_back(output_string);
167
}
168
}
169
}
170
171
RemoteDebugger::ErrorMessage RemoteDebugger::_create_overflow_error(const String &p_what, const String &p_descr) {
172
ErrorMessage oe;
173
oe.error = p_what;
174
oe.error_descr = p_descr;
175
oe.warning = false;
176
uint64_t time = OS::get_singleton()->get_ticks_msec();
177
oe.hr = time / 3600000;
178
oe.min = (time / 60000) % 60;
179
oe.sec = (time / 1000) % 60;
180
oe.msec = time % 1000;
181
return oe;
182
}
183
184
void RemoteDebugger::flush_output() {
185
MutexLock lock(mutex);
186
flush_thread = Thread::get_caller_id();
187
flushing = true;
188
if (!is_peer_connected()) {
189
return;
190
}
191
192
if (n_messages_dropped > 0) {
193
ErrorMessage err_msg = _create_overflow_error("TOO_MANY_MESSAGES", "Too many messages! " + String::num_int64(n_messages_dropped) + " messages were dropped. Profiling might misbheave, try raising 'network/limits/debugger/max_queued_messages' in project setting.");
194
if (_put_msg("error", err_msg.serialize()) == OK) {
195
n_messages_dropped = 0;
196
}
197
}
198
199
if (output_strings.size()) {
200
// Join output strings so we generate less messages.
201
Vector<String> joined_log_strings;
202
Vector<String> strings;
203
Vector<int> types;
204
for (const OutputString &output_string : output_strings) {
205
if (output_string.type == MESSAGE_TYPE_ERROR) {
206
if (!joined_log_strings.is_empty()) {
207
strings.push_back(String("\n").join(joined_log_strings));
208
types.push_back(MESSAGE_TYPE_LOG);
209
joined_log_strings.clear();
210
}
211
strings.push_back(output_string.message);
212
types.push_back(MESSAGE_TYPE_ERROR);
213
} else if (output_string.type == MESSAGE_TYPE_LOG_RICH) {
214
if (!joined_log_strings.is_empty()) {
215
strings.push_back(String("\n").join(joined_log_strings));
216
types.push_back(MESSAGE_TYPE_LOG_RICH);
217
joined_log_strings.clear();
218
}
219
strings.push_back(output_string.message);
220
types.push_back(MESSAGE_TYPE_LOG_RICH);
221
} else {
222
joined_log_strings.push_back(output_string.message);
223
}
224
}
225
226
if (!joined_log_strings.is_empty()) {
227
strings.push_back(String("\n").join(joined_log_strings));
228
types.push_back(MESSAGE_TYPE_LOG);
229
}
230
231
Array arr = { strings, types };
232
_put_msg("output", arr);
233
output_strings.clear();
234
}
235
236
while (errors.size()) {
237
ErrorMessage oe = errors.front()->get();
238
_put_msg("error", oe.serialize());
239
errors.pop_front();
240
}
241
242
// Update limits
243
uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
244
245
if (ticks - last_reset > 1000) {
246
last_reset = ticks;
247
char_count = 0;
248
err_count = 0;
249
n_errors_dropped = 0;
250
warn_count = 0;
251
n_warnings_dropped = 0;
252
}
253
flushing = false;
254
}
255
256
void RemoteDebugger::send_message(const String &p_message, const Array &p_args) {
257
MutexLock lock(mutex);
258
if (is_peer_connected()) {
259
_put_msg(p_message, p_args);
260
}
261
}
262
263
void RemoteDebugger::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type) {
264
ErrorMessage oe;
265
oe.error = p_err;
266
oe.error_descr = p_descr;
267
oe.source_file = p_file;
268
oe.source_line = p_line;
269
oe.source_func = p_func;
270
oe.warning = p_type == ERR_HANDLER_WARNING;
271
uint64_t time = OS::get_singleton()->get_ticks_msec();
272
oe.hr = time / 3600000;
273
oe.min = (time / 60000) % 60;
274
oe.sec = (time / 1000) % 60;
275
oe.msec = time % 1000;
276
oe.callstack.append_array(script_debugger->get_error_stack_info());
277
278
if (flushing && Thread::get_caller_id() == flush_thread) { // Can't handle recursive errors during flush.
279
return;
280
}
281
282
MutexLock lock(mutex);
283
284
if (oe.warning) {
285
warn_count++;
286
} else {
287
err_count++;
288
}
289
290
if (is_peer_connected()) {
291
if (oe.warning) {
292
if (warn_count > max_warnings_per_second) {
293
n_warnings_dropped++;
294
if (n_warnings_dropped == 1) {
295
// Only print one message about dropping per second
296
ErrorMessage overflow = _create_overflow_error("TOO_MANY_WARNINGS", "Too many warnings! Ignoring warnings for up to 1 second.");
297
errors.push_back(overflow);
298
}
299
} else {
300
errors.push_back(oe);
301
}
302
} else {
303
if (err_count > max_errors_per_second) {
304
n_errors_dropped++;
305
if (n_errors_dropped == 1) {
306
// Only print one message about dropping per second
307
ErrorMessage overflow = _create_overflow_error("TOO_MANY_ERRORS", "Too many errors! Ignoring errors for up to 1 second.");
308
errors.push_back(overflow);
309
}
310
} else {
311
errors.push_back(oe);
312
}
313
}
314
}
315
}
316
317
void RemoteDebugger::_send_stack_vars(List<String> &p_names, List<Variant> &p_vals, int p_type) {
318
DebuggerMarshalls::ScriptStackVariable stvar;
319
List<String>::Element *E = p_names.front();
320
List<Variant>::Element *F = p_vals.front();
321
while (E) {
322
stvar.name = E->get();
323
stvar.value = F->get();
324
stvar.type = p_type;
325
send_message("stack_frame_var", stvar.serialize());
326
E = E->next();
327
F = F->next();
328
}
329
}
330
331
Error RemoteDebugger::_try_capture(const String &p_msg, const Array &p_data, bool &r_captured) {
332
const int idx = p_msg.find_char(':');
333
r_captured = false;
334
if (idx < 0) { // No prefix, unknown message.
335
return OK;
336
}
337
const String cap = p_msg.substr(0, idx);
338
if (!has_capture(cap)) {
339
return ERR_UNAVAILABLE; // Unknown message...
340
}
341
const String msg = p_msg.substr(idx + 1);
342
return capture_parse(cap, msg, p_data, r_captured);
343
}
344
345
void RemoteDebugger::_poll_messages() {
346
MutexLock mutex_lock(mutex);
347
348
peer->poll();
349
while (peer->has_message()) {
350
Array cmd = peer->get_message();
351
ERR_CONTINUE(cmd.size() != 3);
352
ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
353
ERR_CONTINUE(cmd[1].get_type() != Variant::INT);
354
ERR_CONTINUE(cmd[2].get_type() != Variant::ARRAY);
355
356
Thread::ID thread = cmd[1];
357
358
if (!messages.has(thread)) {
359
continue; // This thread is not around to receive the messages
360
}
361
362
Message msg;
363
msg.message = cmd[0];
364
msg.data = cmd[2];
365
messages[thread].push_back(msg);
366
}
367
}
368
369
bool RemoteDebugger::_has_messages() {
370
MutexLock mutex_lock(mutex);
371
return messages.has(Thread::get_caller_id()) && !messages[Thread::get_caller_id()].is_empty();
372
}
373
374
Array RemoteDebugger::_get_message() {
375
MutexLock mutex_lock(mutex);
376
ERR_FAIL_COND_V(!messages.has(Thread::get_caller_id()), Array());
377
List<Message> &message_list = messages[Thread::get_caller_id()];
378
ERR_FAIL_COND_V(message_list.is_empty(), Array());
379
380
Array msg;
381
msg.resize(2);
382
msg[0] = message_list.front()->get().message;
383
msg[1] = message_list.front()->get().data;
384
message_list.pop_front();
385
return msg;
386
}
387
388
void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
389
//this function is called when there is a debugger break (bug on script)
390
//or when execution is paused from editor
391
392
{
393
MutexLock lock(mutex);
394
// Tests that require mutex.
395
if (script_debugger->is_skipping_breakpoints() && !p_is_error_breakpoint) {
396
return;
397
}
398
399
ERR_FAIL_COND_MSG(!is_peer_connected(), "Script Debugger failed to connect, but being used anyway.");
400
401
if (!peer->can_block()) {
402
return; // Peer does not support blocking IO. We could at least send the error though.
403
}
404
}
405
406
if (p_is_error_breakpoint && script_debugger->is_ignoring_error_breaks()) {
407
return;
408
}
409
410
ScriptLanguage *script_lang = script_debugger->get_break_language();
411
ERR_FAIL_NULL(script_lang);
412
413
Array msg = {
414
p_can_continue,
415
script_lang->debug_get_error(),
416
script_lang->debug_get_stack_level_count() > 0,
417
Thread::get_caller_id()
418
};
419
if (allow_focus_steal_fn) {
420
allow_focus_steal_fn();
421
}
422
send_message("debug_enter", msg);
423
424
Input::MouseMode mouse_mode = Input::MOUSE_MODE_VISIBLE;
425
426
if (Thread::get_caller_id() == Thread::get_main_id()) {
427
mouse_mode = Input::get_singleton()->get_mouse_mode();
428
if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
429
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
430
}
431
} else {
432
MutexLock mutex_lock(mutex);
433
messages.insert(Thread::get_caller_id(), List<Message>());
434
}
435
436
while (is_peer_connected()) {
437
flush_output();
438
439
_poll_messages();
440
441
if (_has_messages()) {
442
Array cmd = _get_message();
443
444
ERR_CONTINUE(cmd.size() != 2);
445
ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
446
ERR_CONTINUE(cmd[1].get_type() != Variant::ARRAY);
447
448
String command = cmd[0];
449
Array data = cmd[1];
450
451
if (command == "step") {
452
script_debugger->set_depth(-1);
453
script_debugger->set_lines_left(1);
454
break;
455
456
} else if (command == "next") {
457
script_debugger->set_depth(0);
458
script_debugger->set_lines_left(1);
459
break;
460
461
} else if (command == "continue") {
462
script_debugger->set_depth(-1);
463
script_debugger->set_lines_left(-1);
464
break;
465
466
} else if (command == "break") {
467
ERR_PRINT("Got break when already broke!");
468
break;
469
470
} else if (command == "get_stack_dump") {
471
DebuggerMarshalls::ScriptStackDump dump;
472
int slc = script_lang->debug_get_stack_level_count();
473
for (int i = 0; i < slc; i++) {
474
ScriptLanguage::StackInfo frame;
475
frame.file = script_lang->debug_get_stack_level_source(i);
476
frame.line = script_lang->debug_get_stack_level_line(i);
477
frame.func = script_lang->debug_get_stack_level_function(i);
478
dump.frames.push_back(frame);
479
}
480
send_message("stack_dump", dump.serialize());
481
482
} else if (command == "get_stack_frame_vars") {
483
ERR_FAIL_COND(data.size() != 1);
484
ERR_FAIL_NULL(script_lang);
485
int lv = data[0];
486
487
List<String> members;
488
List<Variant> member_vals;
489
if (ScriptInstance *inst = script_lang->debug_get_stack_level_instance(lv)) {
490
members.push_back("self");
491
member_vals.push_back(inst->get_owner());
492
}
493
script_lang->debug_get_stack_level_members(lv, &members, &member_vals);
494
ERR_FAIL_COND(members.size() != member_vals.size());
495
496
List<String> locals;
497
List<Variant> local_vals;
498
script_lang->debug_get_stack_level_locals(lv, &locals, &local_vals);
499
ERR_FAIL_COND(locals.size() != local_vals.size());
500
501
List<String> globals;
502
List<Variant> globals_vals;
503
script_lang->debug_get_globals(&globals, &globals_vals);
504
ERR_FAIL_COND(globals.size() != globals_vals.size());
505
506
Array var_size = { local_vals.size() + member_vals.size() + globals_vals.size() };
507
send_message("stack_frame_vars", var_size);
508
_send_stack_vars(locals, local_vals, 0);
509
_send_stack_vars(members, member_vals, 1);
510
_send_stack_vars(globals, globals_vals, 2);
511
512
} else if (command == "reload_scripts") {
513
script_paths_to_reload = data;
514
} else if (command == "reload_all_scripts") {
515
reload_all_scripts = true;
516
} else if (command == "breakpoint") {
517
ERR_FAIL_COND(data.size() < 3);
518
bool set = data[2];
519
if (set) {
520
script_debugger->insert_breakpoint(data[1], data[0]);
521
} else {
522
script_debugger->remove_breakpoint(data[1], data[0]);
523
}
524
525
} else if (command == "set_skip_breakpoints") {
526
ERR_FAIL_COND(data.is_empty());
527
script_debugger->set_skip_breakpoints(data[0]);
528
} else if (command == "set_ignore_error_breaks") {
529
ERR_FAIL_COND(data.is_empty());
530
script_debugger->set_ignore_error_breaks(data[0]);
531
} else if (command == "evaluate") {
532
String expression_str = data[0];
533
int frame = data[1];
534
535
ScriptInstance *breaked_instance = script_debugger->get_break_language()->debug_get_stack_level_instance(frame);
536
if (!breaked_instance) {
537
break;
538
}
539
540
PackedStringArray input_names;
541
Array input_vals;
542
543
List<String> locals;
544
List<Variant> local_vals;
545
script_debugger->get_break_language()->debug_get_stack_level_locals(frame, &locals, &local_vals);
546
ERR_FAIL_COND(locals.size() != local_vals.size());
547
548
for (const String &S : locals) {
549
input_names.append(S);
550
}
551
552
for (const Variant &V : local_vals) {
553
input_vals.append(V);
554
}
555
556
List<String> globals;
557
List<Variant> globals_vals;
558
script_debugger->get_break_language()->debug_get_globals(&globals, &globals_vals);
559
ERR_FAIL_COND(globals.size() != globals_vals.size());
560
561
for (const String &S : globals) {
562
input_names.append(S);
563
}
564
565
for (const Variant &V : globals_vals) {
566
input_vals.append(V);
567
}
568
569
List<StringName> native_types;
570
ClassDB::get_class_list(&native_types);
571
for (const StringName &E : native_types) {
572
if (!ClassDB::is_class_exposed(E) || !Engine::get_singleton()->has_singleton(E) || Engine::get_singleton()->is_singleton_editor_only(E)) {
573
continue;
574
}
575
576
input_names.append(E);
577
input_vals.append(Engine::get_singleton()->get_singleton_object(E));
578
}
579
580
List<StringName> user_types;
581
ScriptServer::get_global_class_list(&user_types);
582
for (const StringName &S : user_types) {
583
String scr_path = ScriptServer::get_global_class_path(S);
584
Ref<Script> scr = ResourceLoader::load(scr_path, "Script");
585
ERR_CONTINUE_MSG(scr.is_null(), vformat(R"(Could not load the global class %s from resource path: "%s".)", S, scr_path));
586
587
input_names.append(S);
588
input_vals.append(scr);
589
}
590
591
Expression expression;
592
expression.parse(expression_str, input_names);
593
const Variant return_val = expression.execute(input_vals, breaked_instance->get_owner());
594
595
DebuggerMarshalls::ScriptStackVariable stvar;
596
stvar.name = expression_str;
597
stvar.value = return_val;
598
stvar.type = 3;
599
600
send_message("evaluation_return", stvar.serialize());
601
} else {
602
bool captured = false;
603
ERR_CONTINUE(_try_capture(command, data, captured) != OK);
604
if (!captured) {
605
WARN_PRINT(vformat("Unknown message received from debugger: %s.", command));
606
}
607
}
608
} else {
609
OS::get_singleton()->delay_usec(10000);
610
if (Thread::get_caller_id() == Thread::get_main_id()) {
611
// If this is a busy loop on the main thread, events still need to be processed.
612
DisplayServer::get_singleton()->force_process_and_drop_events();
613
}
614
}
615
}
616
617
send_message("debug_exit", Array());
618
619
if (Thread::get_caller_id() == Thread::get_main_id()) {
620
if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
621
Input::get_singleton()->set_mouse_mode(mouse_mode);
622
}
623
} else {
624
MutexLock mutex_lock(mutex);
625
messages.erase(Thread::get_caller_id());
626
}
627
}
628
629
void RemoteDebugger::poll_events(bool p_is_idle) {
630
if (peer.is_null()) {
631
return;
632
}
633
634
flush_output();
635
636
_poll_messages();
637
638
while (_has_messages()) {
639
Array arr = _get_message();
640
641
ERR_CONTINUE(arr.size() != 2);
642
ERR_CONTINUE(arr[0].get_type() != Variant::STRING);
643
ERR_CONTINUE(arr[1].get_type() != Variant::ARRAY);
644
645
const String cmd = arr[0];
646
const int idx = cmd.find_char(':');
647
bool parsed = false;
648
if (idx < 0) { // Not prefix, use scripts capture.
649
capture_parse("core", cmd, arr[1], parsed);
650
continue;
651
}
652
653
const String cap = cmd.substr(0, idx);
654
if (!has_capture(cap)) {
655
continue; // Unknown message...
656
}
657
658
const String msg = cmd.substr(idx + 1);
659
capture_parse(cap, msg, arr[1], parsed);
660
}
661
662
// Reload scripts during idle poll only.
663
if (p_is_idle) {
664
if (reload_all_scripts) {
665
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
666
ScriptServer::get_language(i)->reload_all_scripts();
667
}
668
reload_all_scripts = false;
669
} else if (!script_paths_to_reload.is_empty()) {
670
Array scripts_to_reload;
671
for (int i = 0; i < script_paths_to_reload.size(); ++i) {
672
String path = script_paths_to_reload[i];
673
Error err = OK;
674
Ref<Script> script = ResourceLoader::load(path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
675
ERR_CONTINUE_MSG(err != OK, vformat("Could not reload script '%s': %s", path, error_names[err]));
676
ERR_CONTINUE_MSG(script.is_null(), vformat("Could not reload script '%s': Not a script!", path, error_names[err]));
677
scripts_to_reload.push_back(script);
678
}
679
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
680
ScriptServer::get_language(i)->reload_scripts(scripts_to_reload, true);
681
}
682
}
683
script_paths_to_reload.clear();
684
}
685
}
686
687
Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
688
r_captured = true;
689
if (p_cmd == "reload_scripts") {
690
script_paths_to_reload = p_data;
691
} else if (p_cmd == "reload_all_scripts") {
692
reload_all_scripts = true;
693
} else if (p_cmd == "breakpoint") {
694
ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA);
695
bool set = p_data[2];
696
if (set) {
697
script_debugger->insert_breakpoint(p_data[1], p_data[0]);
698
} else {
699
script_debugger->remove_breakpoint(p_data[1], p_data[0]);
700
}
701
702
} else if (p_cmd == "set_skip_breakpoints") {
703
ERR_FAIL_COND_V(p_data.is_empty(), ERR_INVALID_DATA);
704
script_debugger->set_skip_breakpoints(p_data[0]);
705
} else if (p_cmd == "set_ignore_error_breaks") {
706
ERR_FAIL_COND_V(p_data.is_empty(), ERR_INVALID_DATA);
707
script_debugger->set_ignore_error_breaks(p_data[0]);
708
} else if (p_cmd == "break") {
709
script_debugger->debug(script_debugger->get_break_language());
710
} else {
711
r_captured = false;
712
}
713
return OK;
714
}
715
716
Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data, bool &r_captured) {
717
r_captured = false;
718
ERR_FAIL_COND_V(p_data.is_empty(), ERR_INVALID_DATA);
719
ERR_FAIL_COND_V(p_data[0].get_type() != Variant::BOOL, ERR_INVALID_DATA);
720
ERR_FAIL_COND_V(!has_profiler(p_cmd), ERR_UNAVAILABLE);
721
Array opts;
722
if (p_data.size() > 1) { // Optional profiler parameters.
723
ERR_FAIL_COND_V(p_data[1].get_type() != Variant::ARRAY, ERR_INVALID_DATA);
724
opts = p_data[1];
725
}
726
r_captured = true;
727
profiler_enable(p_cmd, p_data[0], opts);
728
return OK;
729
}
730
731
RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
732
peer = p_peer;
733
max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
734
max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second");
735
max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second");
736
737
// Performance Profiler
738
Object *perf = Engine::get_singleton()->get_singleton_object("Performance");
739
if (perf) {
740
performance_profiler.instantiate(perf);
741
performance_profiler->bind("performance");
742
profiler_enable("performance", true);
743
}
744
745
// Core and profiler captures.
746
Capture core_cap(this,
747
[](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
748
return static_cast<RemoteDebugger *>(p_user)->_core_capture(p_cmd, p_data, r_captured);
749
});
750
register_message_capture("core", core_cap);
751
Capture profiler_cap(this,
752
[](void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
753
return static_cast<RemoteDebugger *>(p_user)->_profiler_capture(p_cmd, p_data, r_captured);
754
});
755
register_message_capture("profiler", profiler_cap);
756
757
// Error handlers
758
phl.printfunc = _print_handler;
759
phl.userdata = this;
760
add_print_handler(&phl);
761
762
eh.errfunc = _err_handler;
763
eh.userdata = this;
764
add_error_handler(&eh);
765
766
messages.insert(Thread::get_main_id(), List<Message>());
767
}
768
769
RemoteDebugger::~RemoteDebugger() {
770
remove_print_handler(&phl);
771
remove_error_handler(&eh);
772
}
773
774