script_debugger_remote.cpp 32 KB

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