script_debugger_remote.cpp 29 KB

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