script_debugger_remote.cpp 31 KB

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