script_debugger_remote.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. const int tries = 6;
  60. int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 };
  61. tcp_client->connect_to_host(ip, port);
  62. for (int i = 0; i < tries; i++) {
  63. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  64. break;
  65. } else {
  66. const int ms = waits[i];
  67. OS::get_singleton()->delay_usec(ms * 1000);
  68. print_line("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in " + String::num(ms) + " msec.");
  69. };
  70. };
  71. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  72. print_line("Remote Debugger: Unable to connect");
  73. return FAILED;
  74. };
  75. // print_line("Remote Debugger: Connection OK!");
  76. packet_peer_stream->set_stream_peer(tcp_client);
  77. return OK;
  78. }
  79. static int _ScriptDebuggerRemote_found_id = 0;
  80. static Object *_ScriptDebuggerRemote_find = NULL;
  81. static void _ScriptDebuggerRemote_debug_func(Object *p_obj) {
  82. if (_ScriptDebuggerRemote_find == p_obj) {
  83. _ScriptDebuggerRemote_found_id = p_obj->get_instance_id();
  84. }
  85. }
  86. static ObjectID safe_get_instance_id(const Variant &p_v) {
  87. Object *o = p_v;
  88. if (o == NULL)
  89. return 0;
  90. else {
  91. REF r = p_v;
  92. if (r.is_valid()) {
  93. return r->get_instance_id();
  94. } else {
  95. _ScriptDebuggerRemote_found_id = 0;
  96. _ScriptDebuggerRemote_find = NULL;
  97. ObjectDB::debug_objects(_ScriptDebuggerRemote_debug_func);
  98. return _ScriptDebuggerRemote_found_id;
  99. }
  100. }
  101. }
  102. void ScriptDebuggerRemote::_put_variable(const String &p_name, const Variant &p_variable) {
  103. packet_peer_stream->put_var(p_name);
  104. Variant var = p_variable;
  105. if (p_variable.get_type() == Variant::OBJECT && !ObjectDB::instance_validate(p_variable)) {
  106. var = Variant();
  107. }
  108. int len = 0;
  109. Error err = encode_variant(var, NULL, len);
  110. if (err != OK)
  111. ERR_PRINT("Failed to encode variant");
  112. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  113. packet_peer_stream->put_var(Variant());
  114. } else {
  115. packet_peer_stream->put_var(var);
  116. }
  117. }
  118. void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) {
  119. //this function is called when there is a debugger break (bug on script)
  120. //or when execution is paused from editor
  121. if (!tcp_client->is_connected_to_host()) {
  122. ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway.");
  123. ERR_FAIL();
  124. }
  125. packet_peer_stream->put_var("debug_enter");
  126. packet_peer_stream->put_var(2);
  127. packet_peer_stream->put_var(p_can_continue);
  128. packet_peer_stream->put_var(p_script->debug_get_error());
  129. skip_profile_frame = true; // to avoid super long frame time for the frame
  130. Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode();
  131. if (mouse_mode != Input::MOUSE_MODE_VISIBLE)
  132. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  133. while (true) {
  134. _get_output();
  135. if (packet_peer_stream->get_available_packet_count() > 0) {
  136. Variant var;
  137. Error err = packet_peer_stream->get_var(var);
  138. ERR_CONTINUE(err != OK);
  139. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  140. Array cmd = var;
  141. ERR_CONTINUE(cmd.size() == 0);
  142. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  143. String command = cmd[0];
  144. if (command == "get_stack_dump") {
  145. packet_peer_stream->put_var("stack_dump");
  146. int slc = p_script->debug_get_stack_level_count();
  147. packet_peer_stream->put_var(slc);
  148. for (int i = 0; i < slc; i++) {
  149. Dictionary d;
  150. d["file"] = p_script->debug_get_stack_level_source(i);
  151. d["line"] = p_script->debug_get_stack_level_line(i);
  152. d["function"] = p_script->debug_get_stack_level_function(i);
  153. //d["id"]=p_script->debug_get_stack_level_
  154. d["id"] = 0;
  155. packet_peer_stream->put_var(d);
  156. }
  157. } else if (command == "get_stack_frame_vars") {
  158. cmd.remove(0);
  159. ERR_CONTINUE(cmd.size() != 1);
  160. int lv = cmd[0];
  161. List<String> members;
  162. List<Variant> member_vals;
  163. if (ScriptInstance *inst = p_script->debug_get_stack_level_instance(lv)) {
  164. members.push_back("self");
  165. member_vals.push_back(inst->get_owner());
  166. }
  167. p_script->debug_get_stack_level_members(lv, &members, &member_vals);
  168. ERR_CONTINUE(members.size() != member_vals.size());
  169. List<String> locals;
  170. List<Variant> local_vals;
  171. p_script->debug_get_stack_level_locals(lv, &locals, &local_vals);
  172. ERR_CONTINUE(locals.size() != local_vals.size());
  173. List<String> globals;
  174. List<Variant> globals_vals;
  175. p_script->debug_get_globals(&globals, &globals_vals);
  176. ERR_CONTINUE(globals.size() != globals_vals.size());
  177. packet_peer_stream->put_var("stack_frame_vars");
  178. packet_peer_stream->put_var(3 + (locals.size() + members.size() + globals.size()) * 2);
  179. { //locals
  180. packet_peer_stream->put_var(locals.size());
  181. List<String>::Element *E = locals.front();
  182. List<Variant>::Element *F = local_vals.front();
  183. while (E) {
  184. _put_variable(E->get(), F->get());
  185. E = E->next();
  186. F = F->next();
  187. }
  188. }
  189. { //members
  190. packet_peer_stream->put_var(members.size());
  191. List<String>::Element *E = members.front();
  192. List<Variant>::Element *F = member_vals.front();
  193. while (E) {
  194. _put_variable(E->get(), F->get());
  195. E = E->next();
  196. F = F->next();
  197. }
  198. }
  199. { //globals
  200. packet_peer_stream->put_var(globals.size());
  201. List<String>::Element *E = globals.front();
  202. List<Variant>::Element *F = globals_vals.front();
  203. while (E) {
  204. _put_variable(E->get(), F->get());
  205. E = E->next();
  206. F = F->next();
  207. }
  208. }
  209. } else if (command == "step") {
  210. set_depth(-1);
  211. set_lines_left(1);
  212. break;
  213. } else if (command == "next") {
  214. set_depth(0);
  215. set_lines_left(1);
  216. break;
  217. } else if (command == "continue") {
  218. set_depth(-1);
  219. set_lines_left(-1);
  220. OS::get_singleton()->move_window_to_foreground();
  221. break;
  222. } else if (command == "break") {
  223. ERR_PRINT("Got break when already broke!");
  224. break;
  225. } else if (command == "request_scene_tree") {
  226. if (request_scene_tree)
  227. request_scene_tree(request_scene_tree_ud);
  228. } else if (command == "request_video_mem") {
  229. _send_video_memory();
  230. } else if (command == "inspect_object") {
  231. ObjectID id = cmd[1];
  232. _send_object_id(id);
  233. } else if (command == "set_object_property") {
  234. _set_object_property(cmd[1], cmd[2], cmd[3]);
  235. } else if (command == "reload_scripts") {
  236. reload_all_scripts = true;
  237. } else if (command == "breakpoint") {
  238. bool set = cmd[3];
  239. if (set)
  240. insert_breakpoint(cmd[2], cmd[1]);
  241. else
  242. remove_breakpoint(cmd[2], cmd[1]);
  243. } else {
  244. _parse_live_edit(cmd);
  245. }
  246. } else {
  247. OS::get_singleton()->delay_usec(10000);
  248. }
  249. }
  250. packet_peer_stream->put_var("debug_exit");
  251. packet_peer_stream->put_var(0);
  252. if (mouse_mode != Input::MOUSE_MODE_VISIBLE)
  253. Input::get_singleton()->set_mouse_mode(mouse_mode);
  254. }
  255. void ScriptDebuggerRemote::_get_output() {
  256. mutex->lock();
  257. if (output_strings.size()) {
  258. locking = true;
  259. packet_peer_stream->put_var("output");
  260. packet_peer_stream->put_var(output_strings.size());
  261. while (output_strings.size()) {
  262. packet_peer_stream->put_var(output_strings.front()->get());
  263. output_strings.pop_front();
  264. }
  265. locking = false;
  266. }
  267. if (n_messages_dropped > 0) {
  268. Message msg;
  269. msg.message = "Too many messages! " + String::num_int64(n_messages_dropped) + " messages were dropped.";
  270. messages.push_back(msg);
  271. n_messages_dropped = 0;
  272. }
  273. while (messages.size()) {
  274. locking = true;
  275. packet_peer_stream->put_var("message:" + messages.front()->get().message);
  276. packet_peer_stream->put_var(messages.front()->get().data.size());
  277. for (int i = 0; i < messages.front()->get().data.size(); i++) {
  278. packet_peer_stream->put_var(messages.front()->get().data[i]);
  279. }
  280. messages.pop_front();
  281. locking = false;
  282. }
  283. if (n_errors_dropped > 0) {
  284. OutputError oe;
  285. oe.error = "TOO_MANY_ERRORS";
  286. oe.error_descr = "Too many errors! " + String::num_int64(n_errors_dropped) + " errors were dropped.";
  287. oe.warning = false;
  288. uint64_t time = OS::get_singleton()->get_ticks_msec();
  289. oe.hr = time / 3600000;
  290. oe.min = (time / 60000) % 60;
  291. oe.sec = (time / 1000) % 60;
  292. oe.msec = time % 1000;
  293. errors.push_back(oe);
  294. n_errors_dropped = 0;
  295. }
  296. while (errors.size()) {
  297. locking = true;
  298. packet_peer_stream->put_var("error");
  299. OutputError oe = errors.front()->get();
  300. packet_peer_stream->put_var(oe.callstack.size() + 2);
  301. Array error_data;
  302. error_data.push_back(oe.hr);
  303. error_data.push_back(oe.min);
  304. error_data.push_back(oe.sec);
  305. error_data.push_back(oe.msec);
  306. error_data.push_back(oe.source_func);
  307. error_data.push_back(oe.source_file);
  308. error_data.push_back(oe.source_line);
  309. error_data.push_back(oe.error);
  310. error_data.push_back(oe.error_descr);
  311. error_data.push_back(oe.warning);
  312. packet_peer_stream->put_var(error_data);
  313. packet_peer_stream->put_var(oe.callstack.size());
  314. for (int i = 0; i < oe.callstack.size(); i++) {
  315. packet_peer_stream->put_var(oe.callstack[i]);
  316. }
  317. errors.pop_front();
  318. locking = false;
  319. }
  320. mutex->unlock();
  321. }
  322. void ScriptDebuggerRemote::line_poll() {
  323. //the purpose of this is just processing events every now and then when the script might get too busy
  324. //otherwise bugs like infinite loops can't be caught
  325. if (poll_every % 2048 == 0)
  326. _poll_events();
  327. poll_every++;
  328. }
  329. 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) {
  330. if (p_type == ERR_HANDLER_SCRIPT)
  331. return; //ignore script errors, those go through debugger
  332. Vector<ScriptLanguage::StackInfo> si;
  333. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  334. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  335. if (si.size())
  336. break;
  337. }
  338. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud;
  339. sdr->send_error(p_func, p_file, p_line, p_err, p_descr, p_type, si);
  340. }
  341. bool ScriptDebuggerRemote::_parse_live_edit(const Array &p_command) {
  342. String cmdstr = p_command[0];
  343. if (!live_edit_funcs || !cmdstr.begins_with("live_"))
  344. return false;
  345. //print_line(Variant(cmd).get_construct_string());
  346. if (cmdstr == "live_set_root") {
  347. if (!live_edit_funcs->root_func)
  348. return true;
  349. //print_line("root: "+Variant(cmd).get_construct_string());
  350. live_edit_funcs->root_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  351. } else if (cmdstr == "live_node_path") {
  352. if (!live_edit_funcs->node_path_func)
  353. return true;
  354. //print_line("path: "+Variant(cmd).get_construct_string());
  355. live_edit_funcs->node_path_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  356. } else if (cmdstr == "live_res_path") {
  357. if (!live_edit_funcs->res_path_func)
  358. return true;
  359. live_edit_funcs->res_path_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  360. } else if (cmdstr == "live_node_prop_res") {
  361. if (!live_edit_funcs->node_set_res_func)
  362. return true;
  363. live_edit_funcs->node_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  364. } else if (cmdstr == "live_node_prop") {
  365. if (!live_edit_funcs->node_set_func)
  366. return true;
  367. live_edit_funcs->node_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  368. } else if (cmdstr == "live_res_prop_res") {
  369. if (!live_edit_funcs->res_set_res_func)
  370. return true;
  371. live_edit_funcs->res_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  372. } else if (cmdstr == "live_res_prop") {
  373. if (!live_edit_funcs->res_set_func)
  374. return true;
  375. live_edit_funcs->res_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  376. } else if (cmdstr == "live_node_call") {
  377. if (!live_edit_funcs->node_call_func)
  378. return true;
  379. 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]);
  380. } else if (cmdstr == "live_res_call") {
  381. if (!live_edit_funcs->res_call_func)
  382. return true;
  383. 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]);
  384. } else if (cmdstr == "live_create_node") {
  385. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  386. } else if (cmdstr == "live_instance_node") {
  387. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  388. } else if (cmdstr == "live_remove_node") {
  389. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata, p_command[1]);
  390. } else if (cmdstr == "live_remove_and_keep_node") {
  391. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  392. } else if (cmdstr == "live_restore_node") {
  393. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  394. } else if (cmdstr == "live_duplicate_node") {
  395. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  396. } else if (cmdstr == "live_reparent_node") {
  397. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3], p_command[4]);
  398. } else {
  399. return false;
  400. }
  401. return true;
  402. }
  403. void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) {
  404. Object *obj = ObjectDB::get_instance(p_id);
  405. if (!obj)
  406. return;
  407. typedef Pair<PropertyInfo, Variant> PropertyDesc;
  408. List<PropertyDesc> properties;
  409. if (ScriptInstance *si = obj->get_script_instance()) {
  410. if (!si->get_script().is_null()) {
  411. Set<StringName> members;
  412. si->get_script()->get_members(&members);
  413. for (Set<StringName>::Element *E = members.front(); E; E = E->next()) {
  414. Variant m;
  415. if (si->get(E->get(), m)) {
  416. PropertyInfo pi(m.get_type(), String("Members/") + E->get());
  417. properties.push_back(PropertyDesc(pi, m));
  418. }
  419. }
  420. Map<StringName, Variant> constants;
  421. si->get_script()->get_constants(&constants);
  422. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  423. PropertyInfo pi(E->value().get_type(), (String("Constants/") + E->key()));
  424. properties.push_back(PropertyDesc(pi, E->value()));
  425. }
  426. }
  427. }
  428. if (Node *node = Object::cast_to<Node>(obj)) {
  429. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  430. properties.push_front(PropertyDesc(pi, node->get_path()));
  431. } else if (Resource *res = Object::cast_to<Resource>(obj)) {
  432. if (Script *s = Object::cast_to<Script>(res)) {
  433. Map<StringName, Variant> constants;
  434. s->get_constants(&constants);
  435. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  436. PropertyInfo pi(E->value().get_type(), String("Constants/") + E->key());
  437. properties.push_front(PropertyDesc(pi, E->value()));
  438. }
  439. }
  440. }
  441. List<PropertyInfo> pinfo;
  442. obj->get_property_list(&pinfo, true);
  443. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  444. if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  445. properties.push_back(PropertyDesc(E->get(), obj->get(E->get().name)));
  446. }
  447. }
  448. Array send_props;
  449. for (int i = 0; i < properties.size(); i++) {
  450. const PropertyInfo &pi = properties[i].first;
  451. Variant &var = properties[i].second;
  452. WeakRef *ref = Object::cast_to<WeakRef>(var);
  453. if (ref) {
  454. var = ref->get_ref();
  455. }
  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.ptrw(), 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. if (messages.size() >= max_messages_per_frame) {
  664. n_messages_dropped++;
  665. } else {
  666. Message msg;
  667. msg.message = p_message;
  668. msg.data = p_args;
  669. messages.push_back(msg);
  670. }
  671. }
  672. mutex->unlock();
  673. }
  674. void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type, const Vector<ScriptLanguage::StackInfo> &p_stack_info) {
  675. OutputError oe;
  676. oe.error = p_err;
  677. oe.error_descr = p_descr;
  678. oe.source_file = p_file;
  679. oe.source_line = p_line;
  680. oe.source_func = p_func;
  681. oe.warning = p_type == ERR_HANDLER_WARNING;
  682. uint64_t time = OS::get_singleton()->get_ticks_msec();
  683. oe.hr = time / 3600000;
  684. oe.min = (time / 60000) % 60;
  685. oe.sec = (time / 1000) % 60;
  686. oe.msec = time % 1000;
  687. Array cstack;
  688. cstack.resize(p_stack_info.size() * 3);
  689. for (int i = 0; i < p_stack_info.size(); i++) {
  690. cstack[i * 3 + 0] = p_stack_info[i].file;
  691. cstack[i * 3 + 1] = p_stack_info[i].func;
  692. cstack[i * 3 + 2] = p_stack_info[i].line;
  693. }
  694. oe.callstack = cstack;
  695. mutex->lock();
  696. if (!locking && tcp_client->is_connected_to_host()) {
  697. if (errors.size() >= max_errors_per_frame) {
  698. n_errors_dropped++;
  699. } else {
  700. errors.push_back(oe);
  701. }
  702. }
  703. mutex->unlock();
  704. }
  705. void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string, bool p_error) {
  706. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)p_this;
  707. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  708. sdr->msec_count += ticks - sdr->last_msec;
  709. sdr->last_msec = ticks;
  710. if (sdr->msec_count > 1000) {
  711. sdr->char_count = 0;
  712. sdr->msec_count = 0;
  713. }
  714. String s = p_string;
  715. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count, 0), s.length());
  716. if (allowed_chars == 0)
  717. return;
  718. if (allowed_chars < s.length()) {
  719. s = s.substr(0, allowed_chars);
  720. }
  721. sdr->char_count += allowed_chars;
  722. bool overflowed = sdr->char_count >= sdr->max_cps;
  723. sdr->mutex->lock();
  724. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  725. if (overflowed)
  726. s += "[...]";
  727. sdr->output_strings.push_back(s);
  728. if (overflowed) {
  729. sdr->output_strings.push_back("[output overflow, print less text!]");
  730. }
  731. }
  732. sdr->mutex->unlock();
  733. }
  734. void ScriptDebuggerRemote::request_quit() {
  735. requested_quit = true;
  736. }
  737. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  738. request_scene_tree = p_func;
  739. request_scene_tree_ud = p_udata;
  740. }
  741. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  742. live_edit_funcs = p_funcs;
  743. }
  744. bool ScriptDebuggerRemote::is_profiling() const {
  745. return profiling;
  746. }
  747. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName &p_name, const Array &p_data) {
  748. int idx = -1;
  749. for (int i = 0; i < profile_frame_data.size(); i++) {
  750. if (profile_frame_data[i].name == p_name) {
  751. idx = i;
  752. break;
  753. }
  754. }
  755. FrameData fd;
  756. fd.name = p_name;
  757. fd.data = p_data;
  758. if (idx == -1) {
  759. profile_frame_data.push_back(fd);
  760. } else {
  761. profile_frame_data[idx] = fd;
  762. }
  763. }
  764. void ScriptDebuggerRemote::profiling_start() {
  765. //ignores this, uses it via connection
  766. }
  767. void ScriptDebuggerRemote::profiling_end() {
  768. //ignores this, uses it via connection
  769. }
  770. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  771. frame_time = p_frame_time;
  772. idle_time = p_idle_time;
  773. physics_time = p_physics_time;
  774. physics_frame_time = p_physics_frame_time;
  775. }
  776. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = NULL;
  777. ScriptDebuggerRemote::ScriptDebuggerRemote() :
  778. profiling(false),
  779. max_frame_functions(16),
  780. skip_profile_frame(false),
  781. reload_all_scripts(false),
  782. tcp_client(StreamPeerTCP::create_ref()),
  783. packet_peer_stream(Ref<PacketPeerStream>(memnew(PacketPeerStream))),
  784. last_perf_time(0),
  785. performance(Engine::get_singleton()->get_singleton_object("Performance")),
  786. requested_quit(false),
  787. mutex(Mutex::create()),
  788. max_cps(GLOBAL_GET("network/limits/debugger_stdout/max_chars_per_second")),
  789. max_messages_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_messages_per_frame")),
  790. max_errors_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_errors_per_frame")),
  791. char_count(0),
  792. n_messages_dropped(0),
  793. n_errors_dropped(0),
  794. last_msec(0),
  795. msec_count(0),
  796. locking(false),
  797. poll_every(0),
  798. request_scene_tree(NULL),
  799. live_edit_funcs(NULL) {
  800. packet_peer_stream->set_stream_peer(tcp_client);
  801. packet_peer_stream->set_output_buffer_max_size(1024 * 1024 * 8); //8mb should be way more than enough
  802. phl.printfunc = _print_handler;
  803. phl.userdata = this;
  804. add_print_handler(&phl);
  805. eh.errfunc = _err_handler;
  806. eh.userdata = this;
  807. add_error_handler(&eh);
  808. profile_info.resize(CLAMP(int(ProjectSettings::get_singleton()->get("debug/settings/profiler/max_functions")), 128, 65535));
  809. profile_info_ptrs.resize(profile_info.size());
  810. }
  811. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  812. remove_print_handler(&phl);
  813. remove_error_handler(&eh);
  814. memdelete(mutex);
  815. }