script_debugger_remote.cpp 32 KB

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