2
0

script_debugger_remote.cpp 27 KB

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