script_debugger_remote.cpp 33 KB

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