script_debugger_remote.cpp 38 KB

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