script_debugger_remote.cpp 32 KB

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