script_debugger_remote.cpp 29 KB

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