2
0

script_debugger_remote.cpp 27 KB

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