script_debugger_remote.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /**************************************************************************/
  2. /* script_debugger_remote.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. #include "script_debugger_remote.h"
  31. #include "core/engine.h"
  32. #include "core/io/ip.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/os/input.h"
  35. #include "core/os/os.h"
  36. #include "core/project_settings.h"
  37. #include "scene/main/node.h"
  38. #include "scene/main/scene_tree.h"
  39. #include "scene/main/viewport.h"
  40. #include "scene/resources/packed_scene.h"
  41. #include "servers/visual_server.h"
  42. void ScriptDebuggerRemote::_send_video_memory() {
  43. List<ResourceUsage> usage;
  44. if (resource_usage_func) {
  45. resource_usage_func(&usage);
  46. }
  47. usage.sort();
  48. packet_peer_stream->put_var("message:video_mem");
  49. packet_peer_stream->put_var(usage.size() * 4);
  50. for (List<ResourceUsage>::Element *E = usage.front(); E; E = E->next()) {
  51. packet_peer_stream->put_var(E->get().path);
  52. packet_peer_stream->put_var(E->get().type);
  53. packet_peer_stream->put_var(E->get().format);
  54. packet_peer_stream->put_var(E->get().vram);
  55. }
  56. }
  57. Error ScriptDebuggerRemote::connect_to_host(const String &p_host, uint16_t p_port) {
  58. IP_Address ip;
  59. if (p_host.is_valid_ip_address()) {
  60. ip = p_host;
  61. } else {
  62. ip = IP::get_singleton()->resolve_hostname(p_host);
  63. }
  64. int port = p_port;
  65. const int tries = 6;
  66. int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
  67. tcp_client->connect_to_host(ip, port);
  68. for (int i = 0; i < tries; i++) {
  69. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  70. print_verbose("Remote Debugger: Connected!");
  71. break;
  72. } else {
  73. const int ms = waits[i];
  74. OS::get_singleton()->delay_usec(ms * 1000);
  75. print_verbose("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in " + String::num(ms) + " msec.");
  76. };
  77. };
  78. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  79. ERR_PRINT("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + ".");
  80. return FAILED;
  81. };
  82. packet_peer_stream->set_stream_peer(tcp_client);
  83. return OK;
  84. }
  85. void ScriptDebuggerRemote::_put_variable(const String &p_name, const Variant &p_variable) {
  86. packet_peer_stream->put_var(p_name);
  87. Variant var = p_variable;
  88. if (p_variable.get_type() == Variant::OBJECT && p_variable.operator Object *() == nullptr) {
  89. var = Variant();
  90. }
  91. int len = 0;
  92. Error err = encode_variant(var, nullptr, len, true);
  93. if (err != OK)
  94. ERR_PRINT("Failed to encode variant.");
  95. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  96. packet_peer_stream->put_var(Variant());
  97. } else {
  98. packet_peer_stream->put_var(var);
  99. }
  100. }
  101. void ScriptDebuggerRemote::_save_node(ObjectID id, const String &p_path) {
  102. Node *node = ObjectDB::get_instance<Node>(id);
  103. ERR_FAIL_COND(!node);
  104. Ref<PackedScene> ps = memnew(PackedScene);
  105. ps->pack(node);
  106. ResourceSaver::save(p_path, ps);
  107. }
  108. void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue, bool p_is_error_breakpoint) {
  109. //this function is called when there is a debugger break (bug on script)
  110. //or when execution is paused from editor
  111. if (skip_breakpoints && !p_is_error_breakpoint) {
  112. return;
  113. }
  114. ERR_FAIL_COND_MSG(!tcp_client->is_connected_to_host(), "Script Debugger failed to connect, but being used anyway.");
  115. if (allow_focus_steal_pid) {
  116. OS::get_singleton()->enable_for_stealing_focus(allow_focus_steal_pid);
  117. }
  118. packet_peer_stream->put_var("debug_enter");
  119. packet_peer_stream->put_var(2);
  120. packet_peer_stream->put_var(p_can_continue);
  121. packet_peer_stream->put_var(p_script->debug_get_error());
  122. skip_profile_frame = true; // to avoid super long frame time for the frame
  123. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  124. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  125. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  126. }
  127. uint64_t loop_begin_usec = 0;
  128. uint64_t loop_time_sec = 0;
  129. while (true) {
  130. loop_begin_usec = OS::get_singleton()->get_ticks_usec();
  131. _get_output();
  132. if (packet_peer_stream->get_available_packet_count() > 0) {
  133. Variant var;
  134. Error err = packet_peer_stream->get_var(var);
  135. ERR_CONTINUE(err != OK);
  136. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  137. Array cmd = var;
  138. ERR_CONTINUE(cmd.size() == 0);
  139. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  140. String command = cmd[0];
  141. if (command == "get_stack_dump") {
  142. packet_peer_stream->put_var("stack_dump");
  143. int slc = p_script->debug_get_stack_level_count();
  144. packet_peer_stream->put_var(slc);
  145. for (int i = 0; i < slc; i++) {
  146. Dictionary d;
  147. d["file"] = p_script->debug_get_stack_level_source(i);
  148. d["line"] = p_script->debug_get_stack_level_line(i);
  149. d["function"] = p_script->debug_get_stack_level_function(i);
  150. //d["id"]=p_script->debug_get_stack_level_
  151. d["id"] = 0;
  152. packet_peer_stream->put_var(d);
  153. }
  154. } else if (command == "get_stack_frame_vars") {
  155. cmd.remove(0);
  156. ERR_CONTINUE(cmd.size() != 1);
  157. int lv = cmd[0];
  158. List<String> members;
  159. List<Variant> member_vals;
  160. if (ScriptInstance *inst = p_script->debug_get_stack_level_instance(lv)) {
  161. members.push_back("self");
  162. member_vals.push_back(inst->get_owner());
  163. }
  164. p_script->debug_get_stack_level_members(lv, &members, &member_vals);
  165. ERR_CONTINUE(members.size() != member_vals.size());
  166. List<String> locals;
  167. List<Variant> local_vals;
  168. p_script->debug_get_stack_level_locals(lv, &locals, &local_vals);
  169. ERR_CONTINUE(locals.size() != local_vals.size());
  170. List<String> globals;
  171. List<Variant> globals_vals;
  172. p_script->debug_get_globals(&globals, &globals_vals);
  173. ERR_CONTINUE(globals.size() != globals_vals.size());
  174. packet_peer_stream->put_var("stack_frame_vars");
  175. packet_peer_stream->put_var(3 + (locals.size() + members.size() + globals.size()) * 2);
  176. { //locals
  177. packet_peer_stream->put_var(locals.size());
  178. List<String>::Element *E = locals.front();
  179. List<Variant>::Element *F = local_vals.front();
  180. while (E) {
  181. _put_variable(E->get(), F->get());
  182. E = E->next();
  183. F = F->next();
  184. }
  185. }
  186. { //members
  187. packet_peer_stream->put_var(members.size());
  188. List<String>::Element *E = members.front();
  189. List<Variant>::Element *F = member_vals.front();
  190. while (E) {
  191. _put_variable(E->get(), F->get());
  192. E = E->next();
  193. F = F->next();
  194. }
  195. }
  196. { //globals
  197. packet_peer_stream->put_var(globals.size());
  198. List<String>::Element *E = globals.front();
  199. List<Variant>::Element *F = globals_vals.front();
  200. while (E) {
  201. _put_variable(E->get(), F->get());
  202. E = E->next();
  203. F = F->next();
  204. }
  205. }
  206. } else if (command == "step") {
  207. set_depth(-1);
  208. set_lines_left(1);
  209. break;
  210. } else if (command == "next") {
  211. set_depth(0);
  212. set_lines_left(1);
  213. break;
  214. } else if (command == "continue") {
  215. set_depth(-1);
  216. set_lines_left(-1);
  217. OS::get_singleton()->move_window_to_foreground();
  218. break;
  219. } else if (command == "break") {
  220. ERR_PRINT("Got break when already broke!");
  221. break;
  222. } else if (command == "request_scene_tree") {
  223. #ifdef DEBUG_ENABLED
  224. if (scene_tree) {
  225. scene_tree->_debugger_request_tree();
  226. }
  227. #endif
  228. } else if (command == "request_video_mem") {
  229. _send_video_memory();
  230. } else if (command == "inspect_object") {
  231. ObjectID id = cmd[1];
  232. _send_object_id(id);
  233. } else if (command == "set_object_property") {
  234. _set_object_property(cmd[1], cmd[2], cmd[3]);
  235. } else if (command == "override_camera_2D:set") {
  236. bool enforce = cmd[1];
  237. if (scene_tree) {
  238. scene_tree->get_root()->enable_canvas_transform_override(enforce);
  239. }
  240. } else if (command == "override_camera_2D:transform") {
  241. Transform2D transform = cmd[1];
  242. if (scene_tree) {
  243. scene_tree->get_root()->set_canvas_transform_override(transform);
  244. }
  245. } else if (command == "override_camera_3D:set") {
  246. bool enable = cmd[1];
  247. if (scene_tree) {
  248. scene_tree->get_root()->enable_camera_override(enable);
  249. }
  250. } else if (command == "override_camera_3D:transform") {
  251. Transform transform = cmd[1];
  252. bool is_perspective = cmd[2];
  253. float size_or_fov = cmd[3];
  254. float near = cmd[4];
  255. float far = cmd[5];
  256. if (scene_tree) {
  257. if (is_perspective) {
  258. scene_tree->get_root()->set_camera_override_perspective(size_or_fov, near, far);
  259. } else {
  260. scene_tree->get_root()->set_camera_override_orthogonal(size_or_fov, near, far);
  261. }
  262. scene_tree->get_root()->set_camera_override_transform(transform);
  263. }
  264. } else if (command == "reload_scripts") {
  265. reload_all_scripts = true;
  266. } else if (command == "breakpoint") {
  267. bool set = cmd[3];
  268. if (set) {
  269. insert_breakpoint(cmd[2], cmd[1]);
  270. } else {
  271. remove_breakpoint(cmd[2], cmd[1]);
  272. }
  273. } else if (command == "save_node") {
  274. _save_node(cmd[1], cmd[2]);
  275. } else if (command == "set_skip_breakpoints") {
  276. skip_breakpoints = cmd[1];
  277. } else {
  278. _parse_live_edit(cmd);
  279. }
  280. } else {
  281. OS::get_singleton()->delay_usec(10000);
  282. OS::get_singleton()->process_and_drop_events();
  283. }
  284. // This is for the camera override to stay live even when the game is paused from the editor
  285. loop_time_sec = (OS::get_singleton()->get_ticks_usec() - loop_begin_usec) / 1000000.0f;
  286. VisualServer::get_singleton()->sync();
  287. if (VisualServer::get_singleton()->has_changed()) {
  288. VisualServer::get_singleton()->draw(true, loop_time_sec * Engine::get_singleton()->get_time_scale());
  289. }
  290. }
  291. packet_peer_stream->put_var("debug_exit");
  292. packet_peer_stream->put_var(0);
  293. if (mouse_mode != Input::MOUSE_MODE_VISIBLE) {
  294. Input::get_singleton()->set_mouse_mode(mouse_mode);
  295. }
  296. }
  297. void ScriptDebuggerRemote::_get_output() {
  298. mutex.lock();
  299. if (output_strings.size()) {
  300. locking = true;
  301. packet_peer_stream->put_var("output");
  302. packet_peer_stream->put_var(output_strings.size());
  303. while (output_strings.size()) {
  304. const OutputString &output_string = output_strings.front()->get();
  305. Array msg_data;
  306. msg_data.push_back(output_string.message);
  307. msg_data.push_back(output_string.type);
  308. packet_peer_stream->put_var(msg_data);
  309. output_strings.pop_front();
  310. }
  311. locking = false;
  312. }
  313. if (n_messages_dropped > 0) {
  314. Message msg;
  315. msg.message = "Too many messages! " + String::num_int64(n_messages_dropped) + " messages were dropped.";
  316. messages.push_back(msg);
  317. n_messages_dropped = 0;
  318. }
  319. while (messages.size()) {
  320. locking = true;
  321. packet_peer_stream->put_var("message:" + messages.front()->get().message);
  322. packet_peer_stream->put_var(messages.front()->get().data.size());
  323. for (int i = 0; i < messages.front()->get().data.size(); i++) {
  324. packet_peer_stream->put_var(messages.front()->get().data[i]);
  325. }
  326. messages.pop_front();
  327. locking = false;
  328. }
  329. if (n_errors_dropped == 1) {
  330. // Only print one message about dropping per second
  331. OutputError oe;
  332. oe.error = "TOO_MANY_ERRORS";
  333. oe.error_descr = "Too many errors! Ignoring errors for up to 1 second.";
  334. oe.warning = false;
  335. uint64_t time = OS::get_singleton()->get_ticks_msec();
  336. oe.hr = time / 3600000;
  337. oe.min = (time / 60000) % 60;
  338. oe.sec = (time / 1000) % 60;
  339. oe.msec = time % 1000;
  340. errors.push_back(oe);
  341. }
  342. if (n_warnings_dropped == 1) {
  343. // Only print one message about dropping per second
  344. OutputError oe;
  345. oe.error = "TOO_MANY_WARNINGS";
  346. oe.error_descr = "Too many warnings! Ignoring warnings for up to 1 second.";
  347. oe.warning = true;
  348. uint64_t time = OS::get_singleton()->get_ticks_msec();
  349. oe.hr = time / 3600000;
  350. oe.min = (time / 60000) % 60;
  351. oe.sec = (time / 1000) % 60;
  352. oe.msec = time % 1000;
  353. errors.push_back(oe);
  354. }
  355. while (errors.size()) {
  356. locking = true;
  357. packet_peer_stream->put_var("error");
  358. OutputError oe = errors.front()->get();
  359. packet_peer_stream->put_var(oe.callstack.size() + 2);
  360. Array error_data;
  361. error_data.push_back(oe.hr);
  362. error_data.push_back(oe.min);
  363. error_data.push_back(oe.sec);
  364. error_data.push_back(oe.msec);
  365. error_data.push_back(oe.source_func);
  366. error_data.push_back(oe.source_file);
  367. error_data.push_back(oe.source_line);
  368. error_data.push_back(oe.error);
  369. error_data.push_back(oe.error_descr);
  370. error_data.push_back(oe.warning);
  371. packet_peer_stream->put_var(error_data);
  372. packet_peer_stream->put_var(oe.callstack.size());
  373. for (int i = 0; i < oe.callstack.size(); i++) {
  374. packet_peer_stream->put_var(oe.callstack[i]);
  375. }
  376. errors.pop_front();
  377. locking = false;
  378. }
  379. mutex.unlock();
  380. }
  381. void ScriptDebuggerRemote::line_poll() {
  382. //the purpose of this is just processing events every now and then when the script might get too busy
  383. //otherwise bugs like infinite loops can't be caught
  384. if (poll_every % 2048 == 0) {
  385. _poll_events();
  386. }
  387. poll_every++;
  388. }
  389. void ScriptDebuggerRemote::_err_handler(void *ud, const char *p_func, const char *p_file, int p_line, const char *p_err, const char *p_descr, ErrorHandlerType p_type) {
  390. if (p_type == ERR_HANDLER_SCRIPT) {
  391. return; //ignore script errors, those go through debugger
  392. }
  393. Vector<ScriptLanguage::StackInfo> si;
  394. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  395. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  396. if (si.size()) {
  397. break;
  398. }
  399. }
  400. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud;
  401. sdr->send_error(String::utf8(p_func), String::utf8(p_file), p_line, String::utf8(p_err), String::utf8(p_descr), p_type, si);
  402. }
  403. bool ScriptDebuggerRemote::_parse_live_edit(const Array &p_command) {
  404. #ifdef DEBUG_ENABLED
  405. String cmdstr = p_command[0];
  406. if (!scene_tree || !cmdstr.begins_with("live_")) {
  407. return false;
  408. }
  409. if (cmdstr == "live_set_root") {
  410. scene_tree->_live_edit_root_func(p_command[1], p_command[2]);
  411. } else if (cmdstr == "live_node_path") {
  412. scene_tree->_live_edit_node_path_func(p_command[1], p_command[2]);
  413. } else if (cmdstr == "live_res_path") {
  414. scene_tree->_live_edit_res_path_func(p_command[1], p_command[2]);
  415. } else if (cmdstr == "live_node_prop_res") {
  416. scene_tree->_live_edit_node_set_res_func(p_command[1], p_command[2], p_command[3]);
  417. } else if (cmdstr == "live_node_prop") {
  418. scene_tree->_live_edit_node_set_func(p_command[1], p_command[2], p_command[3]);
  419. } else if (cmdstr == "live_res_prop_res") {
  420. scene_tree->_live_edit_res_set_res_func(p_command[1], p_command[2], p_command[3]);
  421. } else if (cmdstr == "live_res_prop") {
  422. scene_tree->_live_edit_res_set_func(p_command[1], p_command[2], p_command[3]);
  423. } else if (cmdstr == "live_node_call") {
  424. scene_tree->_live_edit_node_call_func(p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[7], p_command[8], p_command[9], p_command[10]);
  425. } else if (cmdstr == "live_res_call") {
  426. scene_tree->_live_edit_res_call_func(p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[7], p_command[8], p_command[9], p_command[10]);
  427. } else if (cmdstr == "live_create_node") {
  428. scene_tree->_live_edit_create_node_func(p_command[1], p_command[2], p_command[3]);
  429. } else if (cmdstr == "live_instance_node") {
  430. scene_tree->_live_edit_instance_node_func(p_command[1], p_command[2], p_command[3]);
  431. } else if (cmdstr == "live_remove_node") {
  432. scene_tree->_live_edit_remove_node_func(p_command[1]);
  433. } else if (cmdstr == "live_remove_and_keep_node") {
  434. scene_tree->_live_edit_remove_and_keep_node_func(p_command[1], p_command[2]);
  435. } else if (cmdstr == "live_restore_node") {
  436. scene_tree->_live_edit_restore_node_func(p_command[1], p_command[2], p_command[3]);
  437. } else if (cmdstr == "live_duplicate_node") {
  438. scene_tree->_live_edit_duplicate_node_func(p_command[1], p_command[2]);
  439. } else if (cmdstr == "live_reparent_node") {
  440. scene_tree->_live_edit_reparent_node_func(p_command[1], p_command[2], p_command[3], p_command[4]);
  441. } else {
  442. return false;
  443. }
  444. return true;
  445. #else
  446. return false;
  447. #endif
  448. }
  449. void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) {
  450. Object *obj = ObjectDB::get_instance(p_id);
  451. if (!obj) {
  452. return;
  453. }
  454. typedef Pair<PropertyInfo, Variant> PropertyDesc;
  455. List<PropertyDesc> properties;
  456. if (ScriptInstance *si = obj->get_script_instance()) {
  457. if (!si->get_script().is_null()) {
  458. typedef Map<const Script *, Set<StringName>> ScriptMemberMap;
  459. typedef Map<const Script *, Map<StringName, Variant>> ScriptConstantsMap;
  460. ScriptMemberMap members;
  461. members[si->get_script().ptr()] = Set<StringName>();
  462. si->get_script()->get_members(&(members[si->get_script().ptr()]));
  463. ScriptConstantsMap constants;
  464. constants[si->get_script().ptr()] = Map<StringName, Variant>();
  465. si->get_script()->get_constants(&(constants[si->get_script().ptr()]));
  466. Ref<Script> base = si->get_script()->get_base_script();
  467. while (base.is_valid()) {
  468. members[base.ptr()] = Set<StringName>();
  469. base->get_members(&(members[base.ptr()]));
  470. constants[base.ptr()] = Map<StringName, Variant>();
  471. base->get_constants(&(constants[base.ptr()]));
  472. base = base->get_base_script();
  473. }
  474. for (ScriptMemberMap::Element *sm = members.front(); sm; sm = sm->next()) {
  475. for (Set<StringName>::Element *E = sm->get().front(); E; E = E->next()) {
  476. Variant m;
  477. if (si->get(E->get(), m)) {
  478. String script_path = sm->key() == si->get_script().ptr() ? "" : sm->key()->get_path().get_file() + "/";
  479. PropertyInfo pi(m.get_type(), "Members/" + script_path + E->get());
  480. properties.push_back(PropertyDesc(pi, m));
  481. }
  482. }
  483. }
  484. for (ScriptConstantsMap::Element *sc = constants.front(); sc; sc = sc->next()) {
  485. for (Map<StringName, Variant>::Element *E = sc->get().front(); E; E = E->next()) {
  486. String script_path = sc->key() == si->get_script().ptr() ? "" : sc->key()->get_path().get_file() + "/";
  487. if (E->value().get_type() == Variant::OBJECT) {
  488. Variant id = ((Object *)E->value())->get_instance_id();
  489. PropertyInfo pi(id.get_type(), "Constants/" + E->key(), PROPERTY_HINT_OBJECT_ID, "Object");
  490. properties.push_back(PropertyDesc(pi, id));
  491. } else {
  492. PropertyInfo pi(E->value().get_type(), "Constants/" + script_path + E->key());
  493. properties.push_back(PropertyDesc(pi, E->value()));
  494. }
  495. }
  496. }
  497. }
  498. }
  499. if (Node *node = Object::cast_to<Node>(obj)) {
  500. // in some cases node will not be in tree here
  501. // for instance where it created as variable and not yet added to tree
  502. // in such cases we can't ask for it's path
  503. if (node->is_inside_tree()) {
  504. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  505. properties.push_front(PropertyDesc(pi, node->get_path()));
  506. } else {
  507. PropertyInfo pi(Variant::STRING, String("Node/path"));
  508. properties.push_front(PropertyDesc(pi, "[Orphan]"));
  509. }
  510. } else if (Resource *res = Object::cast_to<Resource>(obj)) {
  511. if (Script *s = Object::cast_to<Script>(res)) {
  512. Map<StringName, Variant> constants;
  513. s->get_constants(&constants);
  514. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  515. if (E->value().get_type() == Variant::OBJECT) {
  516. Variant id = ((Object *)E->value())->get_instance_id();
  517. PropertyInfo pi(id.get_type(), "Constants/" + E->key(), PROPERTY_HINT_OBJECT_ID, "Object");
  518. properties.push_front(PropertyDesc(pi, E->value()));
  519. } else {
  520. PropertyInfo pi(E->value().get_type(), String("Constants/") + E->key());
  521. properties.push_front(PropertyDesc(pi, E->value()));
  522. }
  523. }
  524. }
  525. }
  526. List<PropertyInfo> pinfo;
  527. obj->get_property_list(&pinfo, true);
  528. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  529. if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  530. properties.push_back(PropertyDesc(E->get(), obj->get(E->get().name)));
  531. }
  532. }
  533. Array send_props;
  534. for (int i = 0; i < properties.size(); i++) {
  535. const PropertyInfo &pi = properties[i].first;
  536. Variant &var = properties[i].second;
  537. RES res = var;
  538. Array prop;
  539. prop.push_back(pi.name);
  540. prop.push_back(pi.type);
  541. //only send information that can be sent..
  542. int len = 0; //test how big is this to encode
  543. encode_variant(var, nullptr, len);
  544. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  545. prop.push_back(PROPERTY_HINT_OBJECT_TOO_BIG);
  546. prop.push_back("");
  547. prop.push_back(pi.usage);
  548. prop.push_back(Variant());
  549. } else {
  550. prop.push_back(pi.hint);
  551. prop.push_back(pi.hint_string);
  552. prop.push_back(pi.usage);
  553. if (!res.is_null() && !res->get_path().empty()) {
  554. var = res->get_path();
  555. }
  556. prop.push_back(var);
  557. }
  558. send_props.push_back(prop);
  559. }
  560. packet_peer_stream->put_var("message:inspect_object");
  561. packet_peer_stream->put_var(3);
  562. packet_peer_stream->put_var(p_id);
  563. packet_peer_stream->put_var(obj->get_class());
  564. packet_peer_stream->put_var(send_props);
  565. }
  566. void ScriptDebuggerRemote::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  567. Object *obj = ObjectDB::get_instance(p_id);
  568. if (!obj) {
  569. return;
  570. }
  571. String prop_name = p_property;
  572. if (p_property.begins_with("Members/")) {
  573. Vector<String> ss = p_property.split("/");
  574. prop_name = ss[ss.size() - 1];
  575. }
  576. obj->set(prop_name, p_value);
  577. }
  578. void ScriptDebuggerRemote::_poll_events() {
  579. //this si called from ::idle_poll, happens only when running the game,
  580. //does not get called while on debug break
  581. while (packet_peer_stream->get_available_packet_count() > 0) {
  582. _get_output();
  583. //send over output_strings
  584. Variant var;
  585. Error err = packet_peer_stream->get_var(var);
  586. ERR_CONTINUE(err != OK);
  587. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  588. Array cmd = var;
  589. ERR_CONTINUE(cmd.size() == 0);
  590. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  591. String command = cmd[0];
  592. //cmd.remove(0);
  593. if (command == "break") {
  594. if (get_break_language()) {
  595. debug(get_break_language());
  596. }
  597. } else if (command == "save_node") {
  598. _save_node(cmd[1], cmd[2]);
  599. } else if (command == "request_scene_tree") {
  600. #ifdef DEBUG_ENABLED
  601. if (scene_tree) {
  602. scene_tree->_debugger_request_tree();
  603. }
  604. #endif
  605. } else if (command == "request_video_mem") {
  606. _send_video_memory();
  607. } else if (command == "inspect_object") {
  608. ObjectID id = cmd[1];
  609. _send_object_id(id);
  610. } else if (command == "set_object_property") {
  611. _set_object_property(cmd[1], cmd[2], cmd[3]);
  612. } else if (command == "start_profiling") {
  613. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  614. ScriptServer::get_language(i)->profiling_start();
  615. }
  616. max_frame_functions = cmd[1];
  617. profiler_function_signature_map.clear();
  618. profiling = true;
  619. frame_time = 0;
  620. process_time = 0;
  621. physics_time = 0;
  622. physics_frame_time = 0;
  623. print_verbose("Starting profiling.");
  624. } else if (command == "stop_profiling") {
  625. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  626. ScriptServer::get_language(i)->profiling_stop();
  627. }
  628. profiling = false;
  629. _send_profiling_data(false);
  630. print_verbose("Ending profiling.");
  631. } else if (command == "start_network_profiling") {
  632. multiplayer->profiling_start();
  633. profiling_network = true;
  634. } else if (command == "stop_network_profiling") {
  635. multiplayer->profiling_end();
  636. profiling_network = false;
  637. } else if (command == "override_camera_2D:set") {
  638. bool enforce = cmd[1];
  639. if (scene_tree) {
  640. scene_tree->get_root()->enable_canvas_transform_override(enforce);
  641. }
  642. } else if (command == "override_camera_2D:transform") {
  643. Transform2D transform = cmd[1];
  644. if (scene_tree) {
  645. scene_tree->get_root()->set_canvas_transform_override(transform);
  646. }
  647. } else if (command == "override_camera_3D:set") {
  648. bool enable = cmd[1];
  649. if (scene_tree) {
  650. scene_tree->get_root()->enable_camera_override(enable);
  651. }
  652. } else if (command == "override_camera_3D:transform") {
  653. Transform transform = cmd[1];
  654. bool is_perspective = cmd[2];
  655. float size_or_fov = cmd[3];
  656. float near = cmd[4];
  657. float far = cmd[5];
  658. if (scene_tree) {
  659. if (is_perspective) {
  660. scene_tree->get_root()->set_camera_override_perspective(size_or_fov, near, far);
  661. } else {
  662. scene_tree->get_root()->set_camera_override_orthogonal(size_or_fov, near, far);
  663. }
  664. scene_tree->get_root()->set_camera_override_transform(transform);
  665. }
  666. } else if (command == "reload_scripts") {
  667. reload_all_scripts = true;
  668. } else if (command == "breakpoint") {
  669. bool set = cmd[3];
  670. if (set) {
  671. insert_breakpoint(cmd[2], cmd[1]);
  672. } else {
  673. remove_breakpoint(cmd[2], cmd[1]);
  674. }
  675. } else if (command == "set_skip_breakpoints") {
  676. skip_breakpoints = cmd[1];
  677. } else {
  678. _parse_live_edit(cmd);
  679. }
  680. }
  681. }
  682. void ScriptDebuggerRemote::_send_profiling_data(bool p_for_frame) {
  683. int ofs = 0;
  684. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  685. if (p_for_frame) {
  686. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&profile_info.write[ofs], profile_info.size() - ofs);
  687. } else {
  688. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&profile_info.write[ofs], profile_info.size() - ofs);
  689. }
  690. }
  691. for (int i = 0; i < ofs; i++) {
  692. profile_info_ptrs.write[i] = &profile_info.write[i];
  693. }
  694. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  695. sa.sort(profile_info_ptrs.ptrw(), ofs);
  696. int to_send = MIN(ofs, max_frame_functions);
  697. //check signatures first
  698. uint64_t total_script_time = 0;
  699. for (int i = 0; i < to_send; i++) {
  700. if (!profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  701. int idx = profiler_function_signature_map.size();
  702. packet_peer_stream->put_var("profile_sig");
  703. packet_peer_stream->put_var(2);
  704. packet_peer_stream->put_var(profile_info_ptrs[i]->signature);
  705. packet_peer_stream->put_var(idx);
  706. profiler_function_signature_map[profile_info_ptrs[i]->signature] = idx;
  707. }
  708. total_script_time += profile_info_ptrs[i]->self_time;
  709. }
  710. //send frames then
  711. if (p_for_frame) {
  712. packet_peer_stream->put_var("profile_frame");
  713. packet_peer_stream->put_var(8 + profile_frame_data.size() * 2 + to_send * 4);
  714. } else {
  715. packet_peer_stream->put_var("profile_total");
  716. packet_peer_stream->put_var(8 + to_send * 4);
  717. }
  718. packet_peer_stream->put_var(Engine::get_singleton()->get_idle_frames()); //total frame time
  719. packet_peer_stream->put_var(frame_time); //total frame time
  720. packet_peer_stream->put_var(process_time); //idle frame time
  721. packet_peer_stream->put_var(physics_time); //fixed frame time
  722. packet_peer_stream->put_var(physics_frame_time); //fixed frame time
  723. packet_peer_stream->put_var(USEC_TO_SEC(total_script_time)); //total script execution time
  724. if (p_for_frame) {
  725. packet_peer_stream->put_var(profile_frame_data.size()); //how many profile framedatas to send
  726. packet_peer_stream->put_var(to_send); //how many script functions to send
  727. for (int i = 0; i < profile_frame_data.size(); i++) {
  728. packet_peer_stream->put_var(profile_frame_data[i].name);
  729. packet_peer_stream->put_var(profile_frame_data[i].data);
  730. }
  731. } else {
  732. packet_peer_stream->put_var(0); //how many script functions to send
  733. packet_peer_stream->put_var(to_send); //how many script functions to send
  734. }
  735. for (int i = 0; i < to_send; i++) {
  736. int sig_id = -1;
  737. if (profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  738. sig_id = profiler_function_signature_map[profile_info_ptrs[i]->signature];
  739. }
  740. packet_peer_stream->put_var(sig_id);
  741. packet_peer_stream->put_var(profile_info_ptrs[i]->call_count);
  742. packet_peer_stream->put_var(profile_info_ptrs[i]->total_time / 1000000.0);
  743. packet_peer_stream->put_var(profile_info_ptrs[i]->self_time / 1000000.0);
  744. }
  745. if (p_for_frame) {
  746. profile_frame_data.clear();
  747. }
  748. }
  749. void ScriptDebuggerRemote::idle_poll() {
  750. // this function is called every frame, except when there is a debugger break (::debug() in this class)
  751. // execution stops and remains in the ::debug function
  752. _get_output();
  753. if (requested_quit) {
  754. packet_peer_stream->put_var("kill_me");
  755. packet_peer_stream->put_var(0);
  756. requested_quit = false;
  757. }
  758. if (performance) {
  759. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  760. if (pt - last_perf_time > 1000) {
  761. last_perf_time = pt;
  762. int max = performance->get("MONITOR_MAX");
  763. Array arr;
  764. arr.resize(max);
  765. for (int i = 0; i < max; i++) {
  766. arr[i] = performance->call("get_monitor", i);
  767. }
  768. packet_peer_stream->put_var("performance");
  769. packet_peer_stream->put_var(1);
  770. packet_peer_stream->put_var(arr);
  771. }
  772. }
  773. if (profiling) {
  774. if (skip_profile_frame) {
  775. skip_profile_frame = false;
  776. } else {
  777. //send profiling info normally
  778. _send_profiling_data(true);
  779. }
  780. }
  781. if (profiling_network) {
  782. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  783. if (pt - last_net_bandwidth_time > 200) {
  784. last_net_bandwidth_time = pt;
  785. _send_network_bandwidth_usage();
  786. }
  787. if (pt - last_net_prof_time > 100) {
  788. last_net_prof_time = pt;
  789. _send_network_profiling_data();
  790. }
  791. }
  792. if (reload_all_scripts) {
  793. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  794. ScriptServer::get_language(i)->reload_all_scripts();
  795. }
  796. reload_all_scripts = false;
  797. }
  798. _poll_events();
  799. }
  800. void ScriptDebuggerRemote::_send_network_profiling_data() {
  801. ERR_FAIL_COND(multiplayer.is_null());
  802. int n_nodes = multiplayer->get_profiling_frame(&network_profile_info.write[0]);
  803. packet_peer_stream->put_var("network_profile");
  804. packet_peer_stream->put_var(n_nodes * 6);
  805. for (int i = 0; i < n_nodes; ++i) {
  806. packet_peer_stream->put_var(network_profile_info[i].node);
  807. packet_peer_stream->put_var(network_profile_info[i].node_path);
  808. packet_peer_stream->put_var(network_profile_info[i].incoming_rpc);
  809. packet_peer_stream->put_var(network_profile_info[i].incoming_rset);
  810. packet_peer_stream->put_var(network_profile_info[i].outgoing_rpc);
  811. packet_peer_stream->put_var(network_profile_info[i].outgoing_rset);
  812. }
  813. }
  814. void ScriptDebuggerRemote::_send_network_bandwidth_usage() {
  815. ERR_FAIL_COND(multiplayer.is_null());
  816. int incoming_bandwidth = multiplayer->get_incoming_bandwidth_usage();
  817. int outgoing_bandwidth = multiplayer->get_outgoing_bandwidth_usage();
  818. packet_peer_stream->put_var("network_bandwidth");
  819. packet_peer_stream->put_var(2);
  820. packet_peer_stream->put_var(incoming_bandwidth);
  821. packet_peer_stream->put_var(outgoing_bandwidth);
  822. }
  823. void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) {
  824. mutex.lock();
  825. if (!locking && tcp_client->is_connected_to_host()) {
  826. if (messages.size() >= max_messages_per_frame) {
  827. n_messages_dropped++;
  828. } else {
  829. Message msg;
  830. msg.message = p_message;
  831. msg.data = p_args;
  832. messages.push_back(msg);
  833. }
  834. }
  835. mutex.unlock();
  836. }
  837. void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type, const Vector<ScriptLanguage::StackInfo> &p_stack_info) {
  838. OutputError oe;
  839. oe.error = p_err;
  840. oe.error_descr = p_descr;
  841. oe.source_file = p_file;
  842. oe.source_line = p_line;
  843. oe.source_func = p_func;
  844. oe.warning = p_type == ERR_HANDLER_WARNING;
  845. uint64_t time = OS::get_singleton()->get_ticks_msec();
  846. oe.hr = time / 3600000;
  847. oe.min = (time / 60000) % 60;
  848. oe.sec = (time / 1000) % 60;
  849. oe.msec = time % 1000;
  850. Array cstack;
  851. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  852. msec_count += ticks - last_msec;
  853. last_msec = ticks;
  854. if (msec_count > 1000) {
  855. msec_count = 0;
  856. err_count = 0;
  857. n_errors_dropped = 0;
  858. warn_count = 0;
  859. n_warnings_dropped = 0;
  860. }
  861. cstack.resize(p_stack_info.size() * 3);
  862. for (int i = 0; i < p_stack_info.size(); i++) {
  863. cstack[i * 3 + 0] = p_stack_info[i].file;
  864. cstack[i * 3 + 1] = p_stack_info[i].func;
  865. cstack[i * 3 + 2] = p_stack_info[i].line;
  866. }
  867. oe.callstack = cstack;
  868. if (oe.warning) {
  869. warn_count++;
  870. } else {
  871. err_count++;
  872. }
  873. mutex.lock();
  874. if (!locking && tcp_client->is_connected_to_host()) {
  875. if (oe.warning) {
  876. if (warn_count > max_warnings_per_second) {
  877. n_warnings_dropped++;
  878. } else {
  879. errors.push_back(oe);
  880. }
  881. } else {
  882. if (err_count > max_errors_per_second) {
  883. n_errors_dropped++;
  884. } else {
  885. errors.push_back(oe);
  886. }
  887. }
  888. }
  889. mutex.unlock();
  890. }
  891. void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string, bool p_error) {
  892. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)p_this;
  893. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  894. sdr->msec_count += ticks - sdr->last_msec;
  895. sdr->last_msec = ticks;
  896. if (sdr->msec_count > 1000) {
  897. sdr->char_count = 0;
  898. sdr->msec_count = 0;
  899. }
  900. String s = p_string;
  901. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count, 0), s.length());
  902. if (allowed_chars == 0 && s.length() > 0) {
  903. return;
  904. }
  905. if (allowed_chars < s.length()) {
  906. s = s.substr(0, allowed_chars);
  907. }
  908. sdr->char_count += allowed_chars;
  909. bool overflowed = sdr->char_count >= sdr->max_cps;
  910. sdr->mutex.lock();
  911. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  912. if (overflowed) {
  913. s += "[...]";
  914. }
  915. OutputString output_string;
  916. output_string.message = s;
  917. output_string.type = p_error ? MESSAGE_TYPE_ERROR : MESSAGE_TYPE_LOG;
  918. sdr->output_strings.push_back(output_string);
  919. if (overflowed) {
  920. output_string.message = "[output overflow, print less text!]";
  921. output_string.type = MESSAGE_TYPE_ERROR;
  922. sdr->output_strings.push_back(output_string);
  923. }
  924. }
  925. sdr->mutex.unlock();
  926. }
  927. void ScriptDebuggerRemote::request_quit() {
  928. requested_quit = true;
  929. }
  930. void ScriptDebuggerRemote::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  931. multiplayer = p_multiplayer;
  932. }
  933. bool ScriptDebuggerRemote::is_profiling() const {
  934. return profiling;
  935. }
  936. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName &p_name, const Array &p_data) {
  937. int idx = -1;
  938. for (int i = 0; i < profile_frame_data.size(); i++) {
  939. if (profile_frame_data[i].name == p_name) {
  940. idx = i;
  941. break;
  942. }
  943. }
  944. FrameData fd;
  945. fd.name = p_name;
  946. fd.data = p_data;
  947. if (idx == -1) {
  948. profile_frame_data.push_back(fd);
  949. } else {
  950. profile_frame_data.write[idx] = fd;
  951. }
  952. }
  953. void ScriptDebuggerRemote::profiling_start() {
  954. //ignores this, uses it via connection
  955. }
  956. void ScriptDebuggerRemote::profiling_end() {
  957. //ignores this, uses it via connection
  958. }
  959. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_process_time, float p_physics_time, float p_physics_frame_time) {
  960. frame_time = p_frame_time;
  961. process_time = p_process_time;
  962. physics_time = p_physics_time;
  963. physics_frame_time = p_physics_frame_time;
  964. }
  965. void ScriptDebuggerRemote::set_skip_breakpoints(bool p_skip_breakpoints) {
  966. skip_breakpoints = p_skip_breakpoints;
  967. }
  968. void ScriptDebuggerRemote::set_allow_focus_steal_pid(OS::ProcessID p_pid) {
  969. allow_focus_steal_pid = p_pid;
  970. }
  971. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = nullptr;
  972. ScriptDebuggerRemote::ScriptDebuggerRemote() :
  973. profiling(false),
  974. profiling_network(false),
  975. max_frame_functions(16),
  976. skip_profile_frame(false),
  977. reload_all_scripts(false),
  978. tcp_client(Ref<StreamPeerTCP>(memnew(StreamPeerTCP))),
  979. packet_peer_stream(Ref<PacketPeerStream>(memnew(PacketPeerStream))),
  980. last_perf_time(0),
  981. last_net_prof_time(0),
  982. last_net_bandwidth_time(0),
  983. performance(Engine::get_singleton()->get_singleton_object("Performance")),
  984. requested_quit(false),
  985. max_messages_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_messages_per_frame")),
  986. n_messages_dropped(0),
  987. max_errors_per_second(GLOBAL_GET("network/limits/debugger_stdout/max_errors_per_second")),
  988. max_warnings_per_second(GLOBAL_GET("network/limits/debugger_stdout/max_warnings_per_second")),
  989. n_errors_dropped(0),
  990. n_warnings_dropped(0),
  991. max_cps(GLOBAL_GET("network/limits/debugger_stdout/max_chars_per_second")),
  992. char_count(0),
  993. err_count(0),
  994. warn_count(0),
  995. last_msec(0),
  996. msec_count(0),
  997. allow_focus_steal_pid(0),
  998. locking(false),
  999. poll_every(0),
  1000. scene_tree(nullptr) {
  1001. packet_peer_stream->set_stream_peer(tcp_client);
  1002. packet_peer_stream->set_output_buffer_max_size((1024 * 1024 * 8) - 4); // 8 MiB should be way more than enough, minus 4 bytes for separator.
  1003. phl.printfunc = _print_handler;
  1004. phl.userdata = this;
  1005. add_print_handler(&phl);
  1006. eh.errfunc = _err_handler;
  1007. eh.userdata = this;
  1008. add_error_handler(&eh);
  1009. profile_info.resize(GLOBAL_GET("debug/settings/profiler/max_functions"));
  1010. network_profile_info.resize(GLOBAL_GET("debug/settings/profiler/max_functions"));
  1011. profile_info_ptrs.resize(profile_info.size());
  1012. }
  1013. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  1014. remove_print_handler(&phl);
  1015. remove_error_handler(&eh);
  1016. }