script_debugger_remote.cpp 27 KB

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