script_debugger_remote.cpp 28 KB

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