script_debugger_remote.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. while (messages.size()) {
  268. locking = true;
  269. packet_peer_stream->put_var("message:" + messages.front()->get().message);
  270. packet_peer_stream->put_var(messages.front()->get().data.size());
  271. for (int i = 0; i < messages.front()->get().data.size(); i++) {
  272. packet_peer_stream->put_var(messages.front()->get().data[i]);
  273. }
  274. messages.pop_front();
  275. locking = false;
  276. }
  277. while (errors.size()) {
  278. locking = true;
  279. packet_peer_stream->put_var("error");
  280. OutputError oe = errors.front()->get();
  281. packet_peer_stream->put_var(oe.callstack.size() + 2);
  282. Array error_data;
  283. error_data.push_back(oe.hr);
  284. error_data.push_back(oe.min);
  285. error_data.push_back(oe.sec);
  286. error_data.push_back(oe.msec);
  287. error_data.push_back(oe.source_func);
  288. error_data.push_back(oe.source_file);
  289. error_data.push_back(oe.source_line);
  290. error_data.push_back(oe.error);
  291. error_data.push_back(oe.error_descr);
  292. error_data.push_back(oe.warning);
  293. packet_peer_stream->put_var(error_data);
  294. packet_peer_stream->put_var(oe.callstack.size());
  295. for (int i = 0; i < oe.callstack.size(); i++) {
  296. packet_peer_stream->put_var(oe.callstack[i]);
  297. }
  298. errors.pop_front();
  299. locking = false;
  300. }
  301. mutex->unlock();
  302. }
  303. void ScriptDebuggerRemote::line_poll() {
  304. //the purpose of this is just processing events every now and then when the script might get too busy
  305. //otherwise bugs like infinite loops can't be caught
  306. if (poll_every % 2048 == 0)
  307. _poll_events();
  308. poll_every++;
  309. }
  310. 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) {
  311. if (p_type == ERR_HANDLER_SCRIPT)
  312. return; //ignore script errors, those go through debugger
  313. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud;
  314. OutputError oe;
  315. oe.error = p_err;
  316. oe.error_descr = p_descr;
  317. oe.source_file = p_file;
  318. oe.source_line = p_line;
  319. oe.source_func = p_func;
  320. oe.warning = p_type == ERR_HANDLER_WARNING;
  321. uint64_t time = OS::get_singleton()->get_ticks_msec();
  322. oe.hr = time / 3600000;
  323. oe.min = (time / 60000) % 60;
  324. oe.sec = (time / 1000) % 60;
  325. oe.msec = time % 1000;
  326. Array cstack;
  327. Vector<ScriptLanguage::StackInfo> si;
  328. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  329. si = ScriptServer::get_language(i)->debug_get_current_stack_info();
  330. if (si.size())
  331. break;
  332. }
  333. cstack.resize(si.size() * 2);
  334. for (int i = 0; i < si.size(); i++) {
  335. String path;
  336. int line = 0;
  337. if (si[i].script.is_valid()) {
  338. path = si[i].script->get_path();
  339. line = si[i].line;
  340. }
  341. cstack[i * 2 + 0] = path;
  342. cstack[i * 2 + 1] = line;
  343. }
  344. oe.callstack = cstack;
  345. sdr->mutex->lock();
  346. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  347. sdr->errors.push_back(oe);
  348. }
  349. sdr->mutex->unlock();
  350. }
  351. bool ScriptDebuggerRemote::_parse_live_edit(const Array &p_command) {
  352. String cmdstr = p_command[0];
  353. if (!live_edit_funcs || !cmdstr.begins_with("live_"))
  354. return false;
  355. //print_line(Variant(cmd).get_construct_string());
  356. if (cmdstr == "live_set_root") {
  357. if (!live_edit_funcs->root_func)
  358. return true;
  359. //print_line("root: "+Variant(cmd).get_construct_string());
  360. live_edit_funcs->root_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  361. } else if (cmdstr == "live_node_path") {
  362. if (!live_edit_funcs->node_path_func)
  363. return true;
  364. //print_line("path: "+Variant(cmd).get_construct_string());
  365. live_edit_funcs->node_path_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  366. } else if (cmdstr == "live_res_path") {
  367. if (!live_edit_funcs->res_path_func)
  368. return true;
  369. live_edit_funcs->res_path_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  370. } else if (cmdstr == "live_node_prop_res") {
  371. if (!live_edit_funcs->node_set_res_func)
  372. return true;
  373. live_edit_funcs->node_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  374. } else if (cmdstr == "live_node_prop") {
  375. if (!live_edit_funcs->node_set_func)
  376. return true;
  377. live_edit_funcs->node_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  378. } else if (cmdstr == "live_res_prop_res") {
  379. if (!live_edit_funcs->res_set_res_func)
  380. return true;
  381. live_edit_funcs->res_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  382. } else if (cmdstr == "live_res_prop") {
  383. if (!live_edit_funcs->res_set_func)
  384. return true;
  385. live_edit_funcs->res_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  386. } else if (cmdstr == "live_node_call") {
  387. if (!live_edit_funcs->node_call_func)
  388. return true;
  389. 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]);
  390. } else if (cmdstr == "live_res_call") {
  391. if (!live_edit_funcs->res_call_func)
  392. return true;
  393. 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]);
  394. } else if (cmdstr == "live_create_node") {
  395. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  396. } else if (cmdstr == "live_instance_node") {
  397. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  398. } else if (cmdstr == "live_remove_node") {
  399. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata, p_command[1]);
  400. } else if (cmdstr == "live_remove_and_keep_node") {
  401. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  402. } else if (cmdstr == "live_restore_node") {
  403. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]);
  404. } else if (cmdstr == "live_duplicate_node") {
  405. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata, p_command[1], p_command[2]);
  406. } else if (cmdstr == "live_reparent_node") {
  407. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3], p_command[4]);
  408. } else {
  409. return false;
  410. }
  411. return true;
  412. }
  413. void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) {
  414. Object *obj = ObjectDB::get_instance(p_id);
  415. if (!obj)
  416. return;
  417. typedef Pair<PropertyInfo, Variant> PropertyDesc;
  418. List<PropertyDesc> properties;
  419. if (ScriptInstance *si = obj->get_script_instance()) {
  420. if (!si->get_script().is_null()) {
  421. Set<StringName> members;
  422. si->get_script()->get_members(&members);
  423. for (Set<StringName>::Element *E = members.front(); E; E = E->next()) {
  424. Variant m;
  425. if (si->get(E->get(), m)) {
  426. PropertyInfo pi(m.get_type(), String("Members/") + E->get());
  427. properties.push_back(PropertyDesc(pi, m));
  428. }
  429. }
  430. Map<StringName, Variant> constants;
  431. si->get_script()->get_constants(&constants);
  432. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  433. PropertyInfo pi(E->value().get_type(), (String("Constants/") + E->key()));
  434. properties.push_back(PropertyDesc(pi, E->value()));
  435. }
  436. }
  437. }
  438. if (Node *node = Object::cast_to<Node>(obj)) {
  439. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  440. properties.push_front(PropertyDesc(pi, node->get_path()));
  441. } else if (Resource *res = Object::cast_to<Resource>(obj)) {
  442. if (Script *s = Object::cast_to<Script>(res)) {
  443. Map<StringName, Variant> constants;
  444. s->get_constants(&constants);
  445. for (Map<StringName, Variant>::Element *E = constants.front(); E; E = E->next()) {
  446. PropertyInfo pi(E->value().get_type(), String("Constants/") + E->key());
  447. properties.push_front(PropertyDesc(pi, E->value()));
  448. }
  449. }
  450. }
  451. List<PropertyInfo> pinfo;
  452. obj->get_property_list(&pinfo, true);
  453. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  454. if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  455. properties.push_back(PropertyDesc(E->get(), obj->get(E->get().name)));
  456. }
  457. }
  458. Array send_props;
  459. for (int i = 0; i < properties.size(); i++) {
  460. const PropertyInfo &pi = properties[i].first;
  461. const Variant &var = properties[i].second;
  462. RES res = var;
  463. Array prop;
  464. prop.push_back(pi.name);
  465. prop.push_back(pi.type);
  466. //only send information that can be sent..
  467. int len = 0; //test how big is this to encode
  468. encode_variant(var, NULL, len);
  469. if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size
  470. prop.push_back(PROPERTY_HINT_OBJECT_TOO_BIG);
  471. prop.push_back("");
  472. prop.push_back(pi.usage);
  473. prop.push_back(Variant());
  474. } else {
  475. prop.push_back(pi.hint);
  476. if (res.is_null())
  477. prop.push_back(pi.hint_string);
  478. else
  479. prop.push_back(String("RES:") + res->get_path());
  480. prop.push_back(pi.usage);
  481. prop.push_back(var);
  482. }
  483. send_props.push_back(prop);
  484. }
  485. packet_peer_stream->put_var("message:inspect_object");
  486. packet_peer_stream->put_var(3);
  487. packet_peer_stream->put_var(p_id);
  488. packet_peer_stream->put_var(obj->get_class());
  489. packet_peer_stream->put_var(send_props);
  490. }
  491. void ScriptDebuggerRemote::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  492. Object *obj = ObjectDB::get_instance(p_id);
  493. if (!obj)
  494. return;
  495. String prop_name = p_property;
  496. if (p_property.begins_with("Members/"))
  497. prop_name = p_property.substr(8, p_property.length());
  498. obj->set(prop_name, p_value);
  499. }
  500. void ScriptDebuggerRemote::_poll_events() {
  501. //this si called from ::idle_poll, happens only when running the game,
  502. //does not get called while on debug break
  503. while (packet_peer_stream->get_available_packet_count() > 0) {
  504. _get_output();
  505. //send over output_strings
  506. Variant var;
  507. Error err = packet_peer_stream->get_var(var);
  508. ERR_CONTINUE(err != OK);
  509. ERR_CONTINUE(var.get_type() != Variant::ARRAY);
  510. Array cmd = var;
  511. ERR_CONTINUE(cmd.size() == 0);
  512. ERR_CONTINUE(cmd[0].get_type() != Variant::STRING);
  513. String command = cmd[0];
  514. //cmd.remove(0);
  515. if (command == "break") {
  516. if (get_break_language())
  517. debug(get_break_language());
  518. } else if (command == "request_scene_tree") {
  519. if (request_scene_tree)
  520. request_scene_tree(request_scene_tree_ud);
  521. } else if (command == "request_video_mem") {
  522. _send_video_memory();
  523. } else if (command == "inspect_object") {
  524. ObjectID id = cmd[1];
  525. _send_object_id(id);
  526. } else if (command == "set_object_property") {
  527. _set_object_property(cmd[1], cmd[2], cmd[3]);
  528. } else if (command == "start_profiling") {
  529. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  530. ScriptServer::get_language(i)->profiling_start();
  531. }
  532. max_frame_functions = cmd[1];
  533. profiler_function_signature_map.clear();
  534. profiling = true;
  535. frame_time = 0;
  536. idle_time = 0;
  537. physics_time = 0;
  538. physics_frame_time = 0;
  539. print_line("PROFILING ALRIGHT!");
  540. } else if (command == "stop_profiling") {
  541. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  542. ScriptServer::get_language(i)->profiling_stop();
  543. }
  544. profiling = false;
  545. _send_profiling_data(false);
  546. print_line("PROFILING END!");
  547. } else if (command == "reload_scripts") {
  548. reload_all_scripts = true;
  549. } else if (command == "breakpoint") {
  550. bool set = cmd[3];
  551. if (set)
  552. insert_breakpoint(cmd[2], cmd[1]);
  553. else
  554. remove_breakpoint(cmd[2], cmd[1]);
  555. } else {
  556. _parse_live_edit(cmd);
  557. }
  558. }
  559. }
  560. void ScriptDebuggerRemote::_send_profiling_data(bool p_for_frame) {
  561. int ofs = 0;
  562. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  563. if (p_for_frame)
  564. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&profile_info[ofs], profile_info.size() - ofs);
  565. else
  566. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&profile_info[ofs], profile_info.size() - ofs);
  567. }
  568. for (int i = 0; i < ofs; i++) {
  569. profile_info_ptrs[i] = &profile_info[i];
  570. }
  571. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  572. sa.sort(profile_info_ptrs.ptrw(), ofs);
  573. int to_send = MIN(ofs, max_frame_functions);
  574. //check signatures first
  575. uint64_t total_script_time = 0;
  576. for (int i = 0; i < to_send; i++) {
  577. if (!profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  578. int idx = profiler_function_signature_map.size();
  579. packet_peer_stream->put_var("profile_sig");
  580. packet_peer_stream->put_var(2);
  581. packet_peer_stream->put_var(profile_info_ptrs[i]->signature);
  582. packet_peer_stream->put_var(idx);
  583. profiler_function_signature_map[profile_info_ptrs[i]->signature] = idx;
  584. }
  585. total_script_time += profile_info_ptrs[i]->self_time;
  586. }
  587. //send frames then
  588. if (p_for_frame) {
  589. packet_peer_stream->put_var("profile_frame");
  590. packet_peer_stream->put_var(8 + profile_frame_data.size() * 2 + to_send * 4);
  591. } else {
  592. packet_peer_stream->put_var("profile_total");
  593. packet_peer_stream->put_var(8 + to_send * 4);
  594. }
  595. packet_peer_stream->put_var(Engine::get_singleton()->get_frames_drawn()); //total frame time
  596. packet_peer_stream->put_var(frame_time); //total frame time
  597. packet_peer_stream->put_var(idle_time); //idle frame time
  598. packet_peer_stream->put_var(physics_time); //fixed frame time
  599. packet_peer_stream->put_var(physics_frame_time); //fixed frame time
  600. packet_peer_stream->put_var(USEC_TO_SEC(total_script_time)); //total script execution time
  601. if (p_for_frame) {
  602. packet_peer_stream->put_var(profile_frame_data.size()); //how many profile framedatas to send
  603. packet_peer_stream->put_var(to_send); //how many script functions to send
  604. for (int i = 0; i < profile_frame_data.size(); i++) {
  605. packet_peer_stream->put_var(profile_frame_data[i].name);
  606. packet_peer_stream->put_var(profile_frame_data[i].data);
  607. }
  608. } else {
  609. packet_peer_stream->put_var(0); //how many script functions to send
  610. packet_peer_stream->put_var(to_send); //how many script functions to send
  611. }
  612. for (int i = 0; i < to_send; i++) {
  613. int sig_id = -1;
  614. if (profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  615. sig_id = profiler_function_signature_map[profile_info_ptrs[i]->signature];
  616. }
  617. packet_peer_stream->put_var(sig_id);
  618. packet_peer_stream->put_var(profile_info_ptrs[i]->call_count);
  619. packet_peer_stream->put_var(profile_info_ptrs[i]->total_time / 1000000.0);
  620. packet_peer_stream->put_var(profile_info_ptrs[i]->self_time / 1000000.0);
  621. }
  622. if (p_for_frame) {
  623. profile_frame_data.clear();
  624. }
  625. }
  626. void ScriptDebuggerRemote::idle_poll() {
  627. // this function is called every frame, except when there is a debugger break (::debug() in this class)
  628. // execution stops and remains in the ::debug function
  629. _get_output();
  630. if (requested_quit) {
  631. packet_peer_stream->put_var("kill_me");
  632. packet_peer_stream->put_var(0);
  633. requested_quit = false;
  634. }
  635. if (performance) {
  636. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  637. if (pt - last_perf_time > 1000) {
  638. last_perf_time = pt;
  639. int max = performance->get("MONITOR_MAX");
  640. Array arr;
  641. arr.resize(max);
  642. for (int i = 0; i < max; i++) {
  643. arr[i] = performance->call("get_monitor", i);
  644. }
  645. packet_peer_stream->put_var("performance");
  646. packet_peer_stream->put_var(1);
  647. packet_peer_stream->put_var(arr);
  648. }
  649. }
  650. if (profiling) {
  651. if (skip_profile_frame) {
  652. skip_profile_frame = false;
  653. } else {
  654. //send profiling info normally
  655. _send_profiling_data(true);
  656. }
  657. }
  658. if (reload_all_scripts) {
  659. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  660. ScriptServer::get_language(i)->reload_all_scripts();
  661. }
  662. reload_all_scripts = false;
  663. }
  664. _poll_events();
  665. }
  666. void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) {
  667. mutex->lock();
  668. if (!locking && tcp_client->is_connected_to_host()) {
  669. Message msg;
  670. msg.message = p_message;
  671. msg.data = p_args;
  672. messages.push_back(msg);
  673. }
  674. mutex->unlock();
  675. }
  676. void ScriptDebuggerRemote::_print_handler(void *p_this, const String &p_string, bool p_error) {
  677. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)p_this;
  678. uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000;
  679. sdr->msec_count += ticks - sdr->last_msec;
  680. sdr->last_msec = ticks;
  681. if (sdr->msec_count > 1000) {
  682. sdr->char_count = 0;
  683. sdr->msec_count = 0;
  684. }
  685. String s = p_string;
  686. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count, 0), s.length());
  687. if (allowed_chars == 0)
  688. return;
  689. if (allowed_chars < s.length()) {
  690. s = s.substr(0, allowed_chars);
  691. }
  692. sdr->char_count += allowed_chars;
  693. bool overflowed = sdr->char_count >= sdr->max_cps;
  694. sdr->mutex->lock();
  695. if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) {
  696. if (overflowed)
  697. s += "[...]";
  698. sdr->output_strings.push_back(s);
  699. if (overflowed) {
  700. sdr->output_strings.push_back("[output overflow, print less text!]");
  701. }
  702. }
  703. sdr->mutex->unlock();
  704. }
  705. void ScriptDebuggerRemote::request_quit() {
  706. requested_quit = true;
  707. }
  708. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  709. request_scene_tree = p_func;
  710. request_scene_tree_ud = p_udata;
  711. }
  712. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  713. live_edit_funcs = p_funcs;
  714. }
  715. bool ScriptDebuggerRemote::is_profiling() const {
  716. return profiling;
  717. }
  718. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName &p_name, const Array &p_data) {
  719. int idx = -1;
  720. for (int i = 0; i < profile_frame_data.size(); i++) {
  721. if (profile_frame_data[i].name == p_name) {
  722. idx = i;
  723. break;
  724. }
  725. }
  726. FrameData fd;
  727. fd.name = p_name;
  728. fd.data = p_data;
  729. if (idx == -1) {
  730. profile_frame_data.push_back(fd);
  731. } else {
  732. profile_frame_data[idx] = fd;
  733. }
  734. }
  735. void ScriptDebuggerRemote::profiling_start() {
  736. //ignores this, uses it via connnection
  737. }
  738. void ScriptDebuggerRemote::profiling_end() {
  739. //ignores this, uses it via connnection
  740. }
  741. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  742. frame_time = p_frame_time;
  743. idle_time = p_idle_time;
  744. physics_time = p_physics_time;
  745. physics_frame_time = p_physics_frame_time;
  746. }
  747. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = NULL;
  748. ScriptDebuggerRemote::ScriptDebuggerRemote() :
  749. profiling(false),
  750. max_frame_functions(16),
  751. skip_profile_frame(false),
  752. reload_all_scripts(false),
  753. tcp_client(StreamPeerTCP::create_ref()),
  754. packet_peer_stream(Ref<PacketPeerStream>(memnew(PacketPeerStream))),
  755. last_perf_time(0),
  756. performance(Engine::get_singleton()->get_singleton_object("Performance")),
  757. requested_quit(false),
  758. mutex(Mutex::create()),
  759. max_cps(GLOBAL_GET("network/limits/debugger_stdout/max_chars_per_second")),
  760. char_count(0),
  761. last_msec(0),
  762. msec_count(0),
  763. locking(false),
  764. poll_every(0),
  765. request_scene_tree(NULL),
  766. live_edit_funcs(NULL) {
  767. packet_peer_stream->set_stream_peer(tcp_client);
  768. packet_peer_stream->set_output_buffer_max_size(1024 * 1024 * 8); //8mb should be way more than enough
  769. phl.printfunc = _print_handler;
  770. phl.userdata = this;
  771. add_print_handler(&phl);
  772. eh.errfunc = _err_handler;
  773. eh.userdata = this;
  774. add_error_handler(&eh);
  775. profile_info.resize(CLAMP(int(ProjectSettings::get_singleton()->get("debug/settings/profiler/max_functions")), 128, 65535));
  776. profile_info_ptrs.resize(profile_info.size());
  777. }
  778. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  779. remove_print_handler(&phl);
  780. remove_error_handler(&eh);
  781. memdelete(mutex);
  782. }