script_debugger_remote.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*************************************************************************/
  2. /* script_debugger_remote.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_debugger_remote.h"
  30. #include "global_config.h"
  31. #include "io/ip.h"
  32. #include "os/input.h"
  33. #include "os/os.h"
  34. void ScriptDebuggerRemote::_send_video_memory() {
  35. List<ResourceUsage> usage;
  36. if (resource_usage_func)
  37. resource_usage_func(&usage);
  38. usage.sort();
  39. packet_peer_stream->put_var("message:video_mem");
  40. packet_peer_stream->put_var(usage.size() * 4);
  41. for (List<ResourceUsage>::Element *E = usage.front(); E; E = E->next()) {
  42. packet_peer_stream->put_var(E->get().path);
  43. packet_peer_stream->put_var(E->get().type);
  44. packet_peer_stream->put_var(E->get().format);
  45. packet_peer_stream->put_var(E->get().vram);
  46. }
  47. }
  48. Error ScriptDebuggerRemote::connect_to_host(const String &p_host, uint16_t p_port) {
  49. IP_Address ip;
  50. if (p_host.is_valid_ip_address())
  51. ip = p_host;
  52. else
  53. ip = IP::get_singleton()->resolve_hostname(p_host);
  54. int port = p_port;
  55. int tries = 3;
  56. tcp_client->connect_to_host(ip, port);
  57. while (tries--) {
  58. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  59. break;
  60. } else {
  61. OS::get_singleton()->delay_usec(1000000);
  62. print_line("Remote Debugger: Connection failed with status: " + String::num(tcp_client->get_status()) + "'', retrying in 1 sec.");
  63. };
  64. };
  65. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  66. print_line("Remote Debugger: Unable to connect");
  67. return FAILED;
  68. };
  69. // print_line("Remote Debugger: Connection OK!");
  70. packet_peer_stream->set_stream_peer(tcp_client);
  71. return OK;
  72. }
  73. static int _ScriptDebuggerRemote_found_id = 0;
  74. static Object *_ScriptDebuggerRemote_find = NULL;
  75. static void _ScriptDebuggerRemote_debug_func(Object *p_obj) {
  76. if (_ScriptDebuggerRemote_find == p_obj) {
  77. _ScriptDebuggerRemote_found_id = p_obj->get_instance_ID();
  78. }
  79. }
  80. static ObjectID safe_get_instance_id(const Variant &p_v) {
  81. Object *o = p_v;
  82. if (o == NULL)
  83. return 0;
  84. else {
  85. REF r = p_v;
  86. if (r.is_valid()) {
  87. return r->get_instance_ID();
  88. } else {
  89. _ScriptDebuggerRemote_found_id = 0;
  90. _ScriptDebuggerRemote_find = NULL;
  91. ObjectDB::debug_objects(_ScriptDebuggerRemote_debug_func);
  92. return _ScriptDebuggerRemote_found_id;
  93. }
  94. }
  95. }
  96. void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) {
  97. //this function is called when there is a debugger break (bug on script)
  98. //or when execution is paused from editor
  99. if (!tcp_client->is_connected_to_host()) {
  100. ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway.");
  101. ERR_FAIL();
  102. }
  103. OS::get_singleton()->enable_for_stealing_focus(GlobalConfig::get_singleton()->get("editor_pid"));
  104. packet_peer_stream->put_var("debug_enter");
  105. packet_peer_stream->put_var(2);
  106. packet_peer_stream->put_var(p_can_continue);
  107. packet_peer_stream->put_var(p_script->debug_get_error());
  108. skip_profile_frame = true; // to avoid super long frame time for the frame
  109. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  110. if (mouse_mode != Input::MOUSE_MODE_VISIBLE)
  111. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  112. while (true) {
  113. _get_output();
  114. if (packet_peer_stream->get_available_packet_count() > 0) {
  115. Variant var;
  116. Error err = packet_peer_stream->get_var(var);
  117. ERR_CONTINUE(err != OK);
  118. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  119. Array cmd = var;
  120. ERR_CONTINUE(cmd.size() == 0);
  121. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  122. String command = cmd[0];
  123. if (command == "get_stack_dump") {
  124. packet_peer_stream->put_var("stack_dump");
  125. int slc = p_script->debug_get_stack_level_count();
  126. packet_peer_stream->put_var(slc);
  127. for (int i = 0; i < slc; i++) {
  128. Dictionary d;
  129. d["file"] = p_script->debug_get_stack_level_source(i);
  130. d["line"] = p_script->debug_get_stack_level_line(i);
  131. d["function"] = p_script->debug_get_stack_level_function(i);
  132. //d["id"]=p_script->debug_get_stack_level_
  133. d["id"] = 0;
  134. packet_peer_stream->put_var(d);
  135. }
  136. } else if (command == "get_stack_frame_vars") {
  137. cmd.remove(0);
  138. ERR_CONTINUE(cmd.size() != 1);
  139. int lv = cmd[0];
  140. List<String> members;
  141. List<Variant> member_vals;
  142. p_script->debug_get_stack_level_members(lv, &members, &member_vals);
  143. ERR_CONTINUE(members.size() != member_vals.size());
  144. List<String> locals;
  145. List<Variant> local_vals;
  146. p_script->debug_get_stack_level_locals(lv, &locals, &local_vals);
  147. ERR_CONTINUE(locals.size() != local_vals.size());
  148. packet_peer_stream->put_var("stack_frame_vars");
  149. packet_peer_stream->put_var(2 + locals.size() * 2 + members.size() * 2);
  150. { //members
  151. packet_peer_stream->put_var(members.size());
  152. List<String>::Element *E = members.front();
  153. List<Variant>::Element *F = member_vals.front();
  154. while (E) {
  155. if (F->get().get_type() == Variant::OBJECT) {
  156. packet_peer_stream->put_var("*" + E->get());
  157. String pretty_print = F->get().operator String();
  158. packet_peer_stream->put_var(pretty_print.ascii().get_data());
  159. } else {
  160. packet_peer_stream->put_var(E->get());
  161. packet_peer_stream->put_var(F->get());
  162. }
  163. E = E->next();
  164. F = F->next();
  165. }
  166. }
  167. { //locals
  168. packet_peer_stream->put_var(locals.size());
  169. List<String>::Element *E = locals.front();
  170. List<Variant>::Element *F = local_vals.front();
  171. while (E) {
  172. if (F->get().get_type() == Variant::OBJECT) {
  173. packet_peer_stream->put_var("*" + E->get());
  174. String pretty_print = F->get().operator String();
  175. packet_peer_stream->put_var(pretty_print.ascii().get_data());
  176. } else {
  177. packet_peer_stream->put_var(E->get());
  178. packet_peer_stream->put_var(F->get());
  179. }
  180. E = E->next();
  181. F = F->next();
  182. }
  183. }
  184. } else if (command == "step") {
  185. set_depth(-1);
  186. set_lines_left(1);
  187. break;
  188. } else if (command == "next") {
  189. set_depth(0);
  190. set_lines_left(1);
  191. break;
  192. } else if (command == "continue") {
  193. set_depth(-1);
  194. set_lines_left(-1);
  195. OS::get_singleton()->move_window_to_foreground();
  196. break;
  197. } else if (command == "break") {
  198. ERR_PRINT("Got break when already broke!");
  199. break;
  200. } else if (command == "request_scene_tree") {
  201. if (request_scene_tree)
  202. request_scene_tree(request_scene_tree_ud);
  203. } else if (command == "request_video_mem") {
  204. _send_video_memory();
  205. } else if (command == "inspect_object") {
  206. ObjectID id = cmd[1];
  207. _send_object_id(id);
  208. } else if (command == "set_object_property") {
  209. _set_object_property(cmd[1], cmd[2], cmd[3]);
  210. } else if (command == "reload_scripts") {
  211. reload_all_scripts = true;
  212. } else if (command == "breakpoint") {
  213. bool set = cmd[3];
  214. if (set)
  215. insert_breakpoint(cmd[2], cmd[1]);
  216. else
  217. remove_breakpoint(cmd[2], cmd[1]);
  218. } else {
  219. _parse_live_edit(cmd);
  220. }
  221. } else {
  222. OS::get_singleton()->delay_usec(10000);
  223. }
  224. }
  225. packet_peer_stream->put_var("debug_exit");
  226. packet_peer_stream->put_var(0);
  227. if (mouse_mode != Input::MOUSE_MODE_VISIBLE)
  228. Input::get_singleton()->set_mouse_mode(mouse_mode);
  229. }
  230. void ScriptDebuggerRemote::_get_output() {
  231. mutex->lock();
  232. if (output_strings.size()) {
  233. locking = true;
  234. packet_peer_stream->put_var("output");
  235. packet_peer_stream->put_var(output_strings.size());
  236. while (output_strings.size()) {
  237. packet_peer_stream->put_var(output_strings.front()->get());
  238. output_strings.pop_front();
  239. }
  240. locking = false;
  241. }
  242. while (messages.size()) {
  243. locking = true;
  244. packet_peer_stream->put_var("message:" + messages.front()->get().message);
  245. packet_peer_stream->put_var(messages.front()->get().data.size());
  246. for (int i = 0; i < messages.front()->get().data.size(); i++) {
  247. packet_peer_stream->put_var(messages.front()->get().data[i]);
  248. }
  249. messages.pop_front();
  250. locking = false;
  251. }
  252. while (errors.size()) {
  253. locking = true;
  254. packet_peer_stream->put_var("error");
  255. OutputError oe = errors.front()->get();
  256. packet_peer_stream->put_var(oe.callstack.size() + 2);
  257. Array error_data;
  258. error_data.push_back(oe.hr);
  259. error_data.push_back(oe.min);
  260. error_data.push_back(oe.sec);
  261. error_data.push_back(oe.msec);
  262. error_data.push_back(oe.source_func);
  263. error_data.push_back(oe.source_file);
  264. error_data.push_back(oe.source_line);
  265. error_data.push_back(oe.error);
  266. error_data.push_back(oe.error_descr);
  267. error_data.push_back(oe.warning);
  268. packet_peer_stream->put_var(error_data);
  269. packet_peer_stream->put_var(oe.callstack.size());
  270. for (int i = 0; i < oe.callstack.size(); i++) {
  271. packet_peer_stream->put_var(oe.callstack[i]);
  272. }
  273. errors.pop_front();
  274. locking = false;
  275. }
  276. mutex->unlock();
  277. }
  278. void ScriptDebuggerRemote::line_poll() {
  279. //the purpose of this is just processing events every now and then when the script might get too busy
  280. //otherwise bugs like infinite loops can't be caught
  281. if (poll_every % 2048 == 0)
  282. _poll_events();
  283. poll_every++;
  284. }
  285. 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) {
  286. if (p_type == ERR_HANDLER_SCRIPT)
  287. return; //ignore script errors, those go through debugger
  288. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud;
  289. OutputError oe;
  290. oe.error = p_err;
  291. oe.error_descr = p_descr;
  292. oe.source_file = p_file;
  293. oe.source_line = p_line;
  294. oe.source_func = p_func;
  295. oe.warning = p_type == ERR_HANDLER_WARNING;
  296. uint64_t time = OS::get_singleton()->get_ticks_msec();
  297. oe.hr = time / 3600000;
  298. oe.min = (time / 60000) % 60;
  299. oe.sec = (time / 1000) % 60;
  300. oe.msec = time % 1000;
  301. Array cstack;
  302. Vector<ScriptLanguage::StackInfo> si;
  303. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  304. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  305. if (si.size())
  306. break;
  307. }
  308. cstack.resize(si.size() * 2);
  309. for (int i = 0; i < si.size(); i++) {
  310. String path;
  311. int line = 0;
  312. if (si[i].script.is_valid()) {
  313. path = si[i].script->get_path();
  314. line = si[i].line;
  315. }
  316. cstack[i * 2 + 0] = path;
  317. cstack[i * 2 + 1] = line;
  318. }
  319. oe.callstack = cstack;
  320. sdr->mutex->lock();
  321. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  322. sdr->errors.push_back(oe);
  323. }
  324. sdr->mutex->unlock();
  325. }
  326. bool ScriptDebuggerRemote::_parse_live_edit(const Array &cmd) {
  327. String cmdstr = cmd[0];
  328. if (!live_edit_funcs || !cmdstr.begins_with("live_"))
  329. return false;
  330. //print_line(Variant(cmd).get_construct_string());
  331. if (cmdstr == "live_set_root") {
  332. if (!live_edit_funcs->root_func)
  333. return true;
  334. //print_line("root: "+Variant(cmd).get_construct_string());
  335. live_edit_funcs->root_func(live_edit_funcs->udata, cmd[1], cmd[2]);
  336. } else if (cmdstr == "live_node_path") {
  337. if (!live_edit_funcs->node_path_func)
  338. return true;
  339. //print_line("path: "+Variant(cmd).get_construct_string());
  340. live_edit_funcs->node_path_func(live_edit_funcs->udata, cmd[1], cmd[2]);
  341. } else if (cmdstr == "live_res_path") {
  342. if (!live_edit_funcs->res_path_func)
  343. return true;
  344. live_edit_funcs->res_path_func(live_edit_funcs->udata, cmd[1], cmd[2]);
  345. } else if (cmdstr == "live_node_prop_res") {
  346. if (!live_edit_funcs->node_set_res_func)
  347. return true;
  348. live_edit_funcs->node_set_res_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  349. } else if (cmdstr == "live_node_prop") {
  350. if (!live_edit_funcs->node_set_func)
  351. return true;
  352. live_edit_funcs->node_set_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  353. } else if (cmdstr == "live_res_prop_res") {
  354. if (!live_edit_funcs->res_set_res_func)
  355. return true;
  356. live_edit_funcs->res_set_res_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  357. } else if (cmdstr == "live_res_prop") {
  358. if (!live_edit_funcs->res_set_func)
  359. return true;
  360. live_edit_funcs->res_set_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  361. } else if (cmdstr == "live_node_call") {
  362. if (!live_edit_funcs->node_call_func)
  363. return true;
  364. live_edit_funcs->node_call_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7]);
  365. } else if (cmdstr == "live_res_call") {
  366. if (!live_edit_funcs->res_call_func)
  367. return true;
  368. live_edit_funcs->res_call_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7]);
  369. } else if (cmdstr == "live_create_node") {
  370. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  371. } else if (cmdstr == "live_instance_node") {
  372. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  373. } else if (cmdstr == "live_remove_node") {
  374. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata, cmd[1]);
  375. } else if (cmdstr == "live_remove_and_keep_node") {
  376. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata, cmd[1], cmd[2]);
  377. } else if (cmdstr == "live_restore_node") {
  378. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3]);
  379. } else if (cmdstr == "live_duplicate_node") {
  380. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata, cmd[1], cmd[2]);
  381. } else if (cmdstr == "live_reparent_node") {
  382. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata, cmd[1], cmd[2], cmd[3], cmd[4]);
  383. } else {
  384. return false;
  385. }
  386. return true;
  387. }
  388. void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) {
  389. Object *obj = ObjectDB::get_instance(p_id);
  390. if (!obj)
  391. return;
  392. List<PropertyInfo> pinfo;
  393. obj->get_property_list(&pinfo, true);
  394. int props_to_send = 0;
  395. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  396. if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  397. props_to_send++;
  398. }
  399. }
  400. packet_peer_stream->put_var("message:inspect_object");
  401. packet_peer_stream->put_var(props_to_send * 5 + 4);
  402. packet_peer_stream->put_var(p_id);
  403. packet_peer_stream->put_var(obj->get_class());
  404. if (obj->is_class("Resource") || obj->is_class("Node"))
  405. packet_peer_stream->put_var(obj->call("get_path"));
  406. else
  407. packet_peer_stream->put_var("");
  408. packet_peer_stream->put_var(props_to_send);
  409. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  410. if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  411. if (E->get().usage & PROPERTY_USAGE_CATEGORY) {
  412. packet_peer_stream->put_var("*" + E->get().name);
  413. } else {
  414. packet_peer_stream->put_var(E->get().name);
  415. }
  416. Variant var = obj->get(E->get().name);
  417. if (E->get().type == Variant::OBJECT || var.get_type() == Variant::OBJECT) {
  418. ObjectID id2;
  419. Object *obj = var;
  420. if (obj) {
  421. id2 = obj->get_instance_ID();
  422. } else {
  423. id2 = 0;
  424. }
  425. packet_peer_stream->put_var(Variant::INT); //hint string
  426. packet_peer_stream->put_var(PROPERTY_HINT_OBJECT_ID); //hint
  427. packet_peer_stream->put_var(E->get().hint_string); //hint string
  428. packet_peer_stream->put_var(id2); //value
  429. } else {
  430. packet_peer_stream->put_var(E->get().type);
  431. packet_peer_stream->put_var(E->get().hint);
  432. packet_peer_stream->put_var(E->get().hint_string);
  433. //only send information that can be sent..
  434. if (var.get_type() == Variant::IMAGE) {
  435. var = Image();
  436. }
  437. if (var.get_type() >= Variant::DICTIONARY) {
  438. var = Array(); //send none for now, may be to big
  439. }
  440. packet_peer_stream->put_var(var);
  441. }
  442. }
  443. }
  444. }
  445. void ScriptDebuggerRemote::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  446. Object *obj = ObjectDB::get_instance(p_id);
  447. if (!obj)
  448. return;
  449. obj->set(p_property, p_value);
  450. }
  451. void ScriptDebuggerRemote::_poll_events() {
  452. //this si called from ::idle_poll, happens only when running the game,
  453. //does not get called while on debug break
  454. while (packet_peer_stream->get_available_packet_count() > 0) {
  455. _get_output();
  456. //send over output_strings
  457. Variant var;
  458. Error err = packet_peer_stream->get_var(var);
  459. ERR_CONTINUE(err != OK);
  460. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  461. Array cmd = var;
  462. ERR_CONTINUE(cmd.size() == 0);
  463. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  464. String command = cmd[0];
  465. //cmd.remove(0);
  466. if (command == "break") {
  467. if (get_break_language())
  468. debug(get_break_language());
  469. } else if (command == "request_scene_tree") {
  470. if (request_scene_tree)
  471. request_scene_tree(request_scene_tree_ud);
  472. } else if (command == "request_video_mem") {
  473. _send_video_memory();
  474. } else if (command == "inspect_object") {
  475. ObjectID id = cmd[1];
  476. _send_object_id(id);
  477. } else if (command == "set_object_property") {
  478. _set_object_property(cmd[1], cmd[2], cmd[3]);
  479. } else if (command == "start_profiling") {
  480. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  481. ScriptServer::get_language(i)->profiling_start();
  482. }
  483. max_frame_functions = cmd[1];
  484. profiler_function_signature_map.clear();
  485. profiling = true;
  486. frame_time = 0;
  487. idle_time = 0;
  488. fixed_time = 0;
  489. fixed_frame_time = 0;
  490. print_line("PROFILING ALRIGHT!");
  491. } else if (command == "stop_profiling") {
  492. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  493. ScriptServer::get_language(i)->profiling_stop();
  494. }
  495. profiling = false;
  496. _send_profiling_data(false);
  497. print_line("PROFILING END!");
  498. } else if (command == "reload_scripts") {
  499. reload_all_scripts = true;
  500. } else if (command == "breakpoint") {
  501. bool set = cmd[3];
  502. if (set)
  503. insert_breakpoint(cmd[2], cmd[1]);
  504. else
  505. remove_breakpoint(cmd[2], cmd[1]);
  506. } else {
  507. _parse_live_edit(cmd);
  508. }
  509. }
  510. }
  511. void ScriptDebuggerRemote::_send_profiling_data(bool p_for_frame) {
  512. int ofs = 0;
  513. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  514. if (p_for_frame)
  515. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&profile_info[ofs], profile_info.size() - ofs);
  516. else
  517. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&profile_info[ofs], profile_info.size() - ofs);
  518. }
  519. for (int i = 0; i < ofs; i++) {
  520. profile_info_ptrs[i] = &profile_info[i];
  521. }
  522. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  523. sa.sort(profile_info_ptrs.ptr(), ofs);
  524. int to_send = MIN(ofs, max_frame_functions);
  525. //check signatures first
  526. uint64_t total_script_time = 0;
  527. for (int i = 0; i < to_send; i++) {
  528. if (!profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  529. int idx = profiler_function_signature_map.size();
  530. packet_peer_stream->put_var("profile_sig");
  531. packet_peer_stream->put_var(2);
  532. packet_peer_stream->put_var(profile_info_ptrs[i]->signature);
  533. packet_peer_stream->put_var(idx);
  534. profiler_function_signature_map[profile_info_ptrs[i]->signature] = idx;
  535. }
  536. total_script_time += profile_info_ptrs[i]->self_time;
  537. }
  538. //send frames then
  539. if (p_for_frame) {
  540. packet_peer_stream->put_var("profile_frame");
  541. packet_peer_stream->put_var(8 + profile_frame_data.size() * 2 + to_send * 4);
  542. } else {
  543. packet_peer_stream->put_var("profile_total");
  544. packet_peer_stream->put_var(8 + to_send * 4);
  545. }
  546. packet_peer_stream->put_var(Engine::get_singleton()->get_frames_drawn()); //total frame time
  547. packet_peer_stream->put_var(frame_time); //total frame time
  548. packet_peer_stream->put_var(idle_time); //idle frame time
  549. packet_peer_stream->put_var(fixed_time); //fixed frame time
  550. packet_peer_stream->put_var(fixed_frame_time); //fixed frame time
  551. packet_peer_stream->put_var(USEC_TO_SEC(total_script_time)); //total script execution time
  552. if (p_for_frame) {
  553. packet_peer_stream->put_var(profile_frame_data.size()); //how many profile framedatas to send
  554. packet_peer_stream->put_var(to_send); //how many script functions to send
  555. for (int i = 0; i < profile_frame_data.size(); i++) {
  556. packet_peer_stream->put_var(profile_frame_data[i].name);
  557. packet_peer_stream->put_var(profile_frame_data[i].data);
  558. }
  559. } else {
  560. packet_peer_stream->put_var(0); //how many script functions to send
  561. packet_peer_stream->put_var(to_send); //how many script functions to send
  562. }
  563. for (int i = 0; i < to_send; i++) {
  564. int sig_id = -1;
  565. if (profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  566. sig_id = profiler_function_signature_map[profile_info_ptrs[i]->signature];
  567. }
  568. packet_peer_stream->put_var(sig_id);
  569. packet_peer_stream->put_var(profile_info_ptrs[i]->call_count);
  570. packet_peer_stream->put_var(profile_info_ptrs[i]->total_time / 1000000.0);
  571. packet_peer_stream->put_var(profile_info_ptrs[i]->self_time / 1000000.0);
  572. }
  573. if (p_for_frame) {
  574. profile_frame_data.clear();
  575. }
  576. }
  577. void ScriptDebuggerRemote::idle_poll() {
  578. // this function is called every frame, except when there is a debugger break (::debug() in this class)
  579. // execution stops and remains in the ::debug function
  580. _get_output();
  581. if (requested_quit) {
  582. packet_peer_stream->put_var("kill_me");
  583. packet_peer_stream->put_var(0);
  584. requested_quit = false;
  585. }
  586. if (performance) {
  587. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  588. if (pt - last_perf_time > 1000) {
  589. last_perf_time = pt;
  590. int max = performance->get("MONITOR_MAX");
  591. Array arr;
  592. arr.resize(max);
  593. for (int i = 0; i < max; i++) {
  594. arr[i] = performance->call("get_monitor", i);
  595. }
  596. packet_peer_stream->put_var("performance");
  597. packet_peer_stream->put_var(1);
  598. packet_peer_stream->put_var(arr);
  599. }
  600. }
  601. if (profiling) {
  602. if (skip_profile_frame) {
  603. skip_profile_frame = false;
  604. } else {
  605. //send profiling info normally
  606. _send_profiling_data(true);
  607. }
  608. }
  609. if (reload_all_scripts) {
  610. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  611. ScriptServer::get_language(i)->reload_all_scripts();
  612. }
  613. reload_all_scripts = false;
  614. }
  615. _poll_events();
  616. }
  617. void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) {
  618. mutex->lock();
  619. if (!locking && tcp_client->is_connected_to_host()) {
  620. Message msg;
  621. msg.message = p_message;
  622. msg.data = p_args;
  623. messages.push_back(msg);
  624. }
  625. mutex->unlock();
  626. }
  627. void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string) {
  628. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)p_this;
  629. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  630. sdr->msec_count += ticks - sdr->last_msec;
  631. sdr->last_msec = ticks;
  632. if (sdr->msec_count > 1000) {
  633. sdr->char_count = 0;
  634. sdr->msec_count = 0;
  635. }
  636. String s = p_string;
  637. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count, 0), s.length());
  638. if (allowed_chars == 0)
  639. return;
  640. if (allowed_chars < s.length()) {
  641. s = s.substr(0, allowed_chars);
  642. }
  643. sdr->char_count += allowed_chars;
  644. if (sdr->char_count >= sdr->max_cps) {
  645. s += "\n[output overflow, print less text!]\n";
  646. }
  647. sdr->mutex->lock();
  648. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  649. sdr->output_strings.push_back(s);
  650. }
  651. sdr->mutex->unlock();
  652. }
  653. void ScriptDebuggerRemote::request_quit() {
  654. requested_quit = true;
  655. }
  656. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  657. request_scene_tree = p_func;
  658. request_scene_tree_ud = p_udata;
  659. }
  660. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  661. live_edit_funcs = p_funcs;
  662. }
  663. bool ScriptDebuggerRemote::is_profiling() const {
  664. return profiling;
  665. }
  666. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName &p_name, const Array &p_data) {
  667. int idx = -1;
  668. for (int i = 0; i < profile_frame_data.size(); i++) {
  669. if (profile_frame_data[i].name == p_name) {
  670. idx = i;
  671. break;
  672. }
  673. }
  674. FrameData fd;
  675. fd.name = p_name;
  676. fd.data = p_data;
  677. if (idx == -1) {
  678. profile_frame_data.push_back(fd);
  679. } else {
  680. profile_frame_data[idx] = fd;
  681. }
  682. }
  683. void ScriptDebuggerRemote::profiling_start() {
  684. //ignores this, uses it via connnection
  685. }
  686. void ScriptDebuggerRemote::profiling_end() {
  687. //ignores this, uses it via connnection
  688. }
  689. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_fixed_time, float p_fixed_frame_time) {
  690. frame_time = p_frame_time;
  691. idle_time = p_idle_time;
  692. fixed_time = p_fixed_time;
  693. fixed_frame_time = p_fixed_frame_time;
  694. }
  695. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = NULL;
  696. ScriptDebuggerRemote::ScriptDebuggerRemote() {
  697. tcp_client = StreamPeerTCP::create_ref();
  698. packet_peer_stream = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  699. packet_peer_stream->set_stream_peer(tcp_client);
  700. mutex = Mutex::create();
  701. locking = false;
  702. phl.printfunc = _print_handler;
  703. phl.userdata = this;
  704. add_print_handler(&phl);
  705. requested_quit = false;
  706. performance = GlobalConfig::get_singleton()->get_singleton_object("Performance");
  707. last_perf_time = 0;
  708. poll_every = 0;
  709. request_scene_tree = NULL;
  710. live_edit_funcs = NULL;
  711. max_cps = GLOBAL_DEF("network/debug/max_remote_stdout_chars_per_second", 2048);
  712. char_count = 0;
  713. msec_count = 0;
  714. last_msec = 0;
  715. skip_profile_frame = false;
  716. eh.errfunc = _err_handler;
  717. eh.userdata = this;
  718. add_error_handler(&eh);
  719. profile_info.resize(CLAMP(int(GlobalConfig::get_singleton()->get("debug/profiler/max_functions")), 128, 65535));
  720. profile_info_ptrs.resize(profile_info.size());
  721. profiling = false;
  722. max_frame_functions = 16;
  723. reload_all_scripts = false;
  724. }
  725. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  726. remove_print_handler(&phl);
  727. remove_error_handler(&eh);
  728. memdelete(mutex);
  729. }