script_debugger_remote.cpp 34 KB

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