script_debugger_remote.cpp 31 KB

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