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