2
0

script_debugger_remote.cpp 33 KB

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