script_debugger_remote.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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-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 "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. void ScriptDebuggerRemote::_send_video_memory() {
  38. List<ResourceUsage> usage;
  39. if (resource_usage_func)
  40. resource_usage_func(&usage);
  41. usage.sort();
  42. packet_peer_stream->put_var("message:video_mem");
  43. packet_peer_stream->put_var(usage.size() * 4);
  44. for (List<ResourceUsage>::Element *E = usage.front(); E; E = E->next()) {
  45. packet_peer_stream->put_var(E->get().path);
  46. packet_peer_stream->put_var(E->get().type);
  47. packet_peer_stream->put_var(E->get().format);
  48. packet_peer_stream->put_var(E->get().vram);
  49. }
  50. }
  51. Error ScriptDebuggerRemote::connect_to_host(const String &p_host, uint16_t p_port) {
  52. IP_Address ip;
  53. if (p_host.is_valid_ip_address())
  54. ip = p_host;
  55. else
  56. ip = IP::get_singleton()->resolve_hostname(p_host);
  57. int port = p_port;
  58. int tries = 3;
  59. tcp_client->connect_to_host(ip, port);
  60. while (tries--) {
  61. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  62. break;
  63. } else {
  64. OS::get_singleton()->delay_usec(1000000);
  65. print_line("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in 1 sec.");
  66. };
  67. };
  68. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  69. print_line("Remote Debugger: Unable to connect");
  70. return FAILED;
  71. };
  72. // print_line("Remote Debugger: Connection OK!");
  73. packet_peer_stream->set_stream_peer(tcp_client);
  74. return OK;
  75. }
  76. static int _ScriptDebuggerRemote_found_id = 0;
  77. static Object *_ScriptDebuggerRemote_find = NULL;
  78. static void _ScriptDebuggerRemote_debug_func(Object *p_obj) {
  79. if (_ScriptDebuggerRemote_find == p_obj) {
  80. _ScriptDebuggerRemote_found_id = p_obj->get_instance_id();
  81. }
  82. }
  83. static ObjectID safe_get_instance_id(const Variant &p_v) {
  84. Object *o = p_v;
  85. if (o == NULL)
  86. return 0;
  87. else {
  88. REF r = p_v;
  89. if (r.is_valid()) {
  90. return r->get_instance_id();
  91. } else {
  92. _ScriptDebuggerRemote_found_id = 0;
  93. _ScriptDebuggerRemote_find = NULL;
  94. ObjectDB::debug_objects(_ScriptDebuggerRemote_debug_func);
  95. return _ScriptDebuggerRemote_found_id;
  96. }
  97. }
  98. }
  99. void ScriptDebuggerRemote::_put_variable(const String &p_name, const Variant &p_variable) {
  100. packet_peer_stream->put_var(p_name);
  101. int len = 0;
  102. Error err = encode_variant(p_variable, NULL, len);
  103. if (err != OK)
  104. ERR_PRINT("Failed to encode variant");
  105. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  106. packet_peer_stream->put_var(Variant());
  107. } else {
  108. packet_peer_stream->put_var(p_variable);
  109. }
  110. }
  111. void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) {
  112. //this function is called when there is a debugger break (bug on script)
  113. //or when execution is paused from editor
  114. if (!tcp_client->is_connected_to_host()) {
  115. ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway.");
  116. ERR_FAIL();
  117. }
  118. packet_peer_stream->put_var("debug_enter");
  119. packet_peer_stream->put_var(2);
  120. packet_peer_stream->put_var(p_can_continue);
  121. packet_peer_stream->put_var(p_script->debug_get_error());
  122. skip_profile_frame = true; // to avoid super long frame time for the frame
  123. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  124. if (mouse_mode != Input::MOUSE_MODE_VISIBLE)
  125. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  126. while (true) {
  127. _get_output();
  128. if (packet_peer_stream->get_available_packet_count() > 0) {
  129. Variant var;
  130. Error err = packet_peer_stream->get_var(var);
  131. ERR_CONTINUE(err != OK);
  132. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  133. Array cmd = var;
  134. ERR_CONTINUE(cmd.size() == 0);
  135. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  136. String command = cmd[0];
  137. if (command == "get_stack_dump") {
  138. packet_peer_stream->put_var("stack_dump");
  139. int slc = p_script->debug_get_stack_level_count();
  140. packet_peer_stream->put_var(slc);
  141. for (int i = 0; i < slc; i++) {
  142. Dictionary d;
  143. d["file"] = p_script->debug_get_stack_level_source(i);
  144. d["line"] = p_script->debug_get_stack_level_line(i);
  145. d["function"] = p_script->debug_get_stack_level_function(i);
  146. //d["id"]=p_script->debug_get_stack_level_
  147. d["id"] = 0;
  148. packet_peer_stream->put_var(d);
  149. }
  150. } else if (command == "get_stack_frame_vars") {
  151. cmd.remove(0);
  152. ERR_CONTINUE(cmd.size() != 1);
  153. int lv = cmd[0];
  154. List<String> members;
  155. List<Variant> member_vals;
  156. p_script->debug_get_stack_level_members(lv, &members, &member_vals);
  157. ERR_CONTINUE(members.size() != member_vals.size());
  158. List<String> locals;
  159. List<Variant> local_vals;
  160. p_script->debug_get_stack_level_locals(lv, &locals, &local_vals);
  161. ERR_CONTINUE(locals.size() != local_vals.size());
  162. packet_peer_stream->put_var("stack_frame_vars");
  163. packet_peer_stream->put_var(2 + locals.size() * 2 + members.size() * 2);
  164. { //members
  165. packet_peer_stream->put_var(members.size());
  166. List<String>::Element *E = members.front();
  167. List<Variant>::Element *F = member_vals.front();
  168. while (E) {
  169. _put_variable(E->get(), F->get());
  170. E = E->next();
  171. F = F->next();
  172. }
  173. }
  174. { //locals
  175. packet_peer_stream->put_var(locals.size());
  176. List<String>::Element *E = locals.front();
  177. List<Variant>::Element *F = local_vals.front();
  178. while (E) {
  179. _put_variable(E->get(), F->get());
  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 &p_command) {
  327. String cmdstr = p_command[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, p_command[1], p_command[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, p_command[1], p_command[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, p_command[1], p_command[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, p_command[1], p_command[2], p_command[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, p_command[1], p_command[2], p_command[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, p_command[1], p_command[2], p_command[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, p_command[1], p_command[2], p_command[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, p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[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, p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[7]);
  369. } else if (cmdstr == "live_create_node") {
  370. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  371. } else if (cmdstr == "live_instance_node") {
  372. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  373. } else if (cmdstr == "live_remove_node") {
  374. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata, p_command[1]);
  375. } else if (cmdstr == "live_remove_and_keep_node") {
  376. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  377. } else if (cmdstr == "live_restore_node") {
  378. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  379. } else if (cmdstr == "live_duplicate_node") {
  380. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  381. } else if (cmdstr == "live_reparent_node") {
  382. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3], p_command[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. packet_peer_stream->put_var(E->get().type);
  418. //only send information that can be sent..
  419. int len = 0; //test how big is this to encode
  420. encode_variant(var, NULL, len);
  421. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  422. packet_peer_stream->put_var(PROPERTY_HINT_OBJECT_TOO_BIG);
  423. packet_peer_stream->put_var("");
  424. packet_peer_stream->put_var(Variant());
  425. } else {
  426. packet_peer_stream->put_var(E->get().hint);
  427. packet_peer_stream->put_var(E->get().hint_string);
  428. packet_peer_stream->put_var(var);
  429. }
  430. }
  431. }
  432. }
  433. void ScriptDebuggerRemote::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  434. Object *obj = ObjectDB::get_instance(p_id);
  435. if (!obj)
  436. return;
  437. obj->set(p_property, p_value);
  438. }
  439. void ScriptDebuggerRemote::_poll_events() {
  440. //this si called from ::idle_poll, happens only when running the game,
  441. //does not get called while on debug break
  442. while (packet_peer_stream->get_available_packet_count() > 0) {
  443. _get_output();
  444. //send over output_strings
  445. Variant var;
  446. Error err = packet_peer_stream->get_var(var);
  447. ERR_CONTINUE(err != OK);
  448. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  449. Array cmd = var;
  450. ERR_CONTINUE(cmd.size() == 0);
  451. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  452. String command = cmd[0];
  453. //cmd.remove(0);
  454. if (command == "break") {
  455. if (get_break_language())
  456. debug(get_break_language());
  457. } else if (command == "request_scene_tree") {
  458. if (request_scene_tree)
  459. request_scene_tree(request_scene_tree_ud);
  460. } else if (command == "request_video_mem") {
  461. _send_video_memory();
  462. } else if (command == "inspect_object") {
  463. ObjectID id = cmd[1];
  464. _send_object_id(id);
  465. } else if (command == "set_object_property") {
  466. _set_object_property(cmd[1], cmd[2], cmd[3]);
  467. } else if (command == "start_profiling") {
  468. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  469. ScriptServer::get_language(i)->profiling_start();
  470. }
  471. max_frame_functions = cmd[1];
  472. profiler_function_signature_map.clear();
  473. profiling = true;
  474. frame_time = 0;
  475. idle_time = 0;
  476. physics_time = 0;
  477. physics_frame_time = 0;
  478. print_line("PROFILING ALRIGHT!");
  479. } else if (command == "stop_profiling") {
  480. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  481. ScriptServer::get_language(i)->profiling_stop();
  482. }
  483. profiling = false;
  484. _send_profiling_data(false);
  485. print_line("PROFILING END!");
  486. } else if (command == "reload_scripts") {
  487. reload_all_scripts = true;
  488. } else if (command == "breakpoint") {
  489. bool set = cmd[3];
  490. if (set)
  491. insert_breakpoint(cmd[2], cmd[1]);
  492. else
  493. remove_breakpoint(cmd[2], cmd[1]);
  494. } else {
  495. _parse_live_edit(cmd);
  496. }
  497. }
  498. }
  499. void ScriptDebuggerRemote::_send_profiling_data(bool p_for_frame) {
  500. int ofs = 0;
  501. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  502. if (p_for_frame)
  503. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&profile_info[ofs], profile_info.size() - ofs);
  504. else
  505. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&profile_info[ofs], profile_info.size() - ofs);
  506. }
  507. for (int i = 0; i < ofs; i++) {
  508. profile_info_ptrs[i] = &profile_info[i];
  509. }
  510. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  511. sa.sort(profile_info_ptrs.ptr(), ofs);
  512. int to_send = MIN(ofs, max_frame_functions);
  513. //check signatures first
  514. uint64_t total_script_time = 0;
  515. for (int i = 0; i < to_send; i++) {
  516. if (!profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  517. int idx = profiler_function_signature_map.size();
  518. packet_peer_stream->put_var("profile_sig");
  519. packet_peer_stream->put_var(2);
  520. packet_peer_stream->put_var(profile_info_ptrs[i]->signature);
  521. packet_peer_stream->put_var(idx);
  522. profiler_function_signature_map[profile_info_ptrs[i]->signature] = idx;
  523. }
  524. total_script_time += profile_info_ptrs[i]->self_time;
  525. }
  526. //send frames then
  527. if (p_for_frame) {
  528. packet_peer_stream->put_var("profile_frame");
  529. packet_peer_stream->put_var(8 + profile_frame_data.size() * 2 + to_send * 4);
  530. } else {
  531. packet_peer_stream->put_var("profile_total");
  532. packet_peer_stream->put_var(8 + to_send * 4);
  533. }
  534. packet_peer_stream->put_var(Engine::get_singleton()->get_frames_drawn()); //total frame time
  535. packet_peer_stream->put_var(frame_time); //total frame time
  536. packet_peer_stream->put_var(idle_time); //idle frame time
  537. packet_peer_stream->put_var(physics_time); //fixed frame time
  538. packet_peer_stream->put_var(physics_frame_time); //fixed frame time
  539. packet_peer_stream->put_var(USEC_TO_SEC(total_script_time)); //total script execution time
  540. if (p_for_frame) {
  541. packet_peer_stream->put_var(profile_frame_data.size()); //how many profile framedatas to send
  542. packet_peer_stream->put_var(to_send); //how many script functions to send
  543. for (int i = 0; i < profile_frame_data.size(); i++) {
  544. packet_peer_stream->put_var(profile_frame_data[i].name);
  545. packet_peer_stream->put_var(profile_frame_data[i].data);
  546. }
  547. } else {
  548. packet_peer_stream->put_var(0); //how many script functions to send
  549. packet_peer_stream->put_var(to_send); //how many script functions to send
  550. }
  551. for (int i = 0; i < to_send; i++) {
  552. int sig_id = -1;
  553. if (profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  554. sig_id = profiler_function_signature_map[profile_info_ptrs[i]->signature];
  555. }
  556. packet_peer_stream->put_var(sig_id);
  557. packet_peer_stream->put_var(profile_info_ptrs[i]->call_count);
  558. packet_peer_stream->put_var(profile_info_ptrs[i]->total_time / 1000000.0);
  559. packet_peer_stream->put_var(profile_info_ptrs[i]->self_time / 1000000.0);
  560. }
  561. if (p_for_frame) {
  562. profile_frame_data.clear();
  563. }
  564. }
  565. void ScriptDebuggerRemote::idle_poll() {
  566. // this function is called every frame, except when there is a debugger break (::debug() in this class)
  567. // execution stops and remains in the ::debug function
  568. _get_output();
  569. if (requested_quit) {
  570. packet_peer_stream->put_var("kill_me");
  571. packet_peer_stream->put_var(0);
  572. requested_quit = false;
  573. }
  574. if (performance) {
  575. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  576. if (pt - last_perf_time > 1000) {
  577. last_perf_time = pt;
  578. int max = performance->get("MONITOR_MAX");
  579. Array arr;
  580. arr.resize(max);
  581. for (int i = 0; i < max; i++) {
  582. arr[i] = performance->call("get_monitor", i);
  583. }
  584. packet_peer_stream->put_var("performance");
  585. packet_peer_stream->put_var(1);
  586. packet_peer_stream->put_var(arr);
  587. }
  588. }
  589. if (profiling) {
  590. if (skip_profile_frame) {
  591. skip_profile_frame = false;
  592. } else {
  593. //send profiling info normally
  594. _send_profiling_data(true);
  595. }
  596. }
  597. if (reload_all_scripts) {
  598. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  599. ScriptServer::get_language(i)->reload_all_scripts();
  600. }
  601. reload_all_scripts = false;
  602. }
  603. _poll_events();
  604. }
  605. void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) {
  606. mutex->lock();
  607. if (!locking && tcp_client->is_connected_to_host()) {
  608. Message msg;
  609. msg.message = p_message;
  610. msg.data = p_args;
  611. messages.push_back(msg);
  612. }
  613. mutex->unlock();
  614. }
  615. void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string) {
  616. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)p_this;
  617. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  618. sdr->msec_count += ticks - sdr->last_msec;
  619. sdr->last_msec = ticks;
  620. if (sdr->msec_count > 1000) {
  621. sdr->char_count = 0;
  622. sdr->msec_count = 0;
  623. }
  624. String s = p_string;
  625. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count, 0), s.length());
  626. if (allowed_chars == 0)
  627. return;
  628. if (allowed_chars < s.length()) {
  629. s = s.substr(0, allowed_chars);
  630. }
  631. sdr->char_count += allowed_chars;
  632. bool overflowed = sdr->char_count >= sdr->max_cps;
  633. sdr->mutex->lock();
  634. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  635. if (overflowed)
  636. s += "[...]";
  637. sdr->output_strings.push_back(s);
  638. if (overflowed) {
  639. sdr->output_strings.push_back("[output overflow, print less text!]");
  640. }
  641. }
  642. sdr->mutex->unlock();
  643. }
  644. void ScriptDebuggerRemote::request_quit() {
  645. requested_quit = true;
  646. }
  647. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  648. request_scene_tree = p_func;
  649. request_scene_tree_ud = p_udata;
  650. }
  651. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  652. live_edit_funcs = p_funcs;
  653. }
  654. bool ScriptDebuggerRemote::is_profiling() const {
  655. return profiling;
  656. }
  657. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName &p_name, const Array &p_data) {
  658. int idx = -1;
  659. for (int i = 0; i < profile_frame_data.size(); i++) {
  660. if (profile_frame_data[i].name == p_name) {
  661. idx = i;
  662. break;
  663. }
  664. }
  665. FrameData fd;
  666. fd.name = p_name;
  667. fd.data = p_data;
  668. if (idx == -1) {
  669. profile_frame_data.push_back(fd);
  670. } else {
  671. profile_frame_data[idx] = fd;
  672. }
  673. }
  674. void ScriptDebuggerRemote::profiling_start() {
  675. //ignores this, uses it via connnection
  676. }
  677. void ScriptDebuggerRemote::profiling_end() {
  678. //ignores this, uses it via connnection
  679. }
  680. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  681. frame_time = p_frame_time;
  682. idle_time = p_idle_time;
  683. physics_time = p_physics_time;
  684. physics_frame_time = p_physics_frame_time;
  685. }
  686. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = NULL;
  687. ScriptDebuggerRemote::ScriptDebuggerRemote()
  688. : profiling(false),
  689. max_frame_functions(16),
  690. skip_profile_frame(false),
  691. reload_all_scripts(false),
  692. tcp_client(StreamPeerTCP::create_ref()),
  693. packet_peer_stream(Ref<PacketPeerStream>(memnew(PacketPeerStream))),
  694. last_perf_time(0),
  695. performance(Engine::get_singleton()->get_singleton_object("Performance")),
  696. requested_quit(false),
  697. mutex(Mutex::create()),
  698. max_cps(GLOBAL_GET("network/limits/debugger_stdout/max_chars_per_second")),
  699. char_count(0),
  700. last_msec(0),
  701. msec_count(0),
  702. locking(false),
  703. poll_every(0),
  704. request_scene_tree(NULL),
  705. live_edit_funcs(NULL) {
  706. packet_peer_stream->set_stream_peer(tcp_client);
  707. packet_peer_stream->set_output_buffer_max_size(1024 * 1024 * 8); //8mb should be way more than enough
  708. phl.printfunc = _print_handler;
  709. phl.userdata = this;
  710. add_print_handler(&phl);
  711. eh.errfunc = _err_handler;
  712. eh.userdata = this;
  713. add_error_handler(&eh);
  714. profile_info.resize(CLAMP(int(ProjectSettings::get_singleton()->get("debug/settings/profiler/max_functions")), 128, 65535));
  715. profile_info_ptrs.resize(profile_info.size());
  716. }
  717. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  718. remove_print_handler(&phl);
  719. remove_error_handler(&eh);
  720. memdelete(mutex);
  721. }