script_debugger_remote.cpp 31 KB

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