2
0

script_debugger_remote.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*************************************************************************/
  2. /* script_debugger_remote.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_debugger_remote.h"
  31. #include "core/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. }