script_debugger_remote.cpp 33 KB

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