script_debugger_remote.cpp 30 KB

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