script_debugger_remote.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*************************************************************************/
  2. /* script_debugger_remote.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_debugger_remote.h"
  30. #include "os/os.h"
  31. #include "io/ip.h"
  32. #include "globals.h"
  33. void ScriptDebuggerRemote::_send_video_memory() {
  34. List<ResourceUsage> usage;
  35. if (resource_usage_func)
  36. resource_usage_func(&usage);
  37. usage.sort();
  38. packet_peer_stream->put_var("message:video_mem");
  39. packet_peer_stream->put_var(usage.size()*4);
  40. for(List<ResourceUsage>::Element *E=usage.front();E;E=E->next()) {
  41. packet_peer_stream->put_var(E->get().path);
  42. packet_peer_stream->put_var(E->get().type);
  43. packet_peer_stream->put_var(E->get().format);
  44. packet_peer_stream->put_var(E->get().vram);
  45. }
  46. }
  47. Error ScriptDebuggerRemote::connect_to_host(const String& p_host,uint16_t p_port) {
  48. IP_Address ip;
  49. if (p_host.is_valid_ip_address())
  50. ip=p_host;
  51. else
  52. ip = IP::get_singleton()->resolve_hostname(p_host);
  53. int port = p_port;
  54. int tries = 3;
  55. tcp_client->connect(ip, port);
  56. while (tries--) {
  57. if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  58. break;
  59. } else {
  60. OS::get_singleton()->delay_usec(1000000);
  61. print_line("Remote Debugger: Connection failed with status: " + String::num(tcp_client->get_status())+"'', retrying in 1 sec.");
  62. };
  63. };
  64. if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  65. print_line("Remote Debugger: Unable to connect");
  66. return FAILED;
  67. };
  68. // print_line("Remote Debugger: Connection OK!");
  69. packet_peer_stream->set_stream_peer(tcp_client);
  70. return OK;
  71. }
  72. static int _ScriptDebuggerRemote_found_id=0;
  73. static Object* _ScriptDebuggerRemote_find=NULL;
  74. static void _ScriptDebuggerRemote_debug_func(Object *p_obj) {
  75. if (_ScriptDebuggerRemote_find==p_obj) {
  76. _ScriptDebuggerRemote_found_id=p_obj->get_instance_ID();
  77. }
  78. }
  79. static ObjectID safe_get_instance_id(const Variant& p_v) {
  80. Object *o = p_v;
  81. if (o==NULL)
  82. return 0;
  83. else {
  84. REF r = p_v;
  85. if (r.is_valid()) {
  86. return r->get_instance_ID();
  87. } else {
  88. _ScriptDebuggerRemote_found_id=0;
  89. _ScriptDebuggerRemote_find=NULL;
  90. ObjectDB::debug_objects(_ScriptDebuggerRemote_debug_func);
  91. return _ScriptDebuggerRemote_found_id;
  92. }
  93. }
  94. }
  95. void ScriptDebuggerRemote::debug(ScriptLanguage *p_script,bool p_can_continue) {
  96. if (!tcp_client->is_connected()) {
  97. ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway.");
  98. ERR_FAIL();
  99. }
  100. packet_peer_stream->put_var("debug_enter");
  101. packet_peer_stream->put_var(2);
  102. packet_peer_stream->put_var(p_can_continue);
  103. packet_peer_stream->put_var(p_script->debug_get_error());
  104. while(true) {
  105. _get_output();
  106. if (packet_peer_stream->get_available_packet_count()>0) {
  107. Variant var;
  108. Error err = packet_peer_stream->get_var(var);
  109. ERR_CONTINUE( err != OK);
  110. ERR_CONTINUE( var.get_type()!=Variant::ARRAY );
  111. Array cmd = var;
  112. ERR_CONTINUE( cmd.size()==0);
  113. ERR_CONTINUE( cmd[0].get_type()!=Variant::STRING );
  114. String command = cmd[0];
  115. if (command=="get_stack_dump") {
  116. packet_peer_stream->put_var("stack_dump");
  117. int slc = p_script->debug_get_stack_level_count();
  118. packet_peer_stream->put_var( slc );
  119. for(int i=0;i<slc;i++) {
  120. Dictionary d;
  121. d["file"]=p_script->debug_get_stack_level_source(i);
  122. d["line"]=p_script->debug_get_stack_level_line(i);
  123. d["function"]=p_script->debug_get_stack_level_function(i);
  124. //d["id"]=p_script->debug_get_stack_level_
  125. d["id"]=0;
  126. packet_peer_stream->put_var( d );
  127. }
  128. } else if (command=="get_stack_frame_vars") {
  129. cmd.remove(0);
  130. ERR_CONTINUE( cmd.size()!=1 );
  131. int lv = cmd[0];
  132. List<String> members;
  133. List<Variant> member_vals;
  134. p_script->debug_get_stack_level_members(lv,&members,&member_vals);
  135. ERR_CONTINUE( members.size() !=member_vals.size() );
  136. List<String> locals;
  137. List<Variant> local_vals;
  138. p_script->debug_get_stack_level_locals(lv,&locals,&local_vals);
  139. ERR_CONTINUE( locals.size() !=local_vals.size() );
  140. packet_peer_stream->put_var("stack_frame_vars");
  141. packet_peer_stream->put_var(2+locals.size()*2+members.size()*2);
  142. { //members
  143. packet_peer_stream->put_var(members.size());
  144. List<String>::Element *E=members.front();
  145. List<Variant>::Element *F=member_vals.front();
  146. while(E) {
  147. if (F->get().get_type()==Variant::OBJECT) {
  148. packet_peer_stream->put_var("*"+E->get());
  149. packet_peer_stream->put_var(safe_get_instance_id(F->get()));
  150. } else {
  151. packet_peer_stream->put_var(E->get());
  152. packet_peer_stream->put_var(F->get());
  153. }
  154. E=E->next();
  155. F=F->next();
  156. }
  157. }
  158. { //locals
  159. packet_peer_stream->put_var(locals.size());
  160. List<String>::Element *E=locals.front();
  161. List<Variant>::Element *F=local_vals.front();
  162. while(E) {
  163. if (F->get().get_type()==Variant::OBJECT) {
  164. packet_peer_stream->put_var("*"+E->get());
  165. packet_peer_stream->put_var(safe_get_instance_id(F->get()));
  166. } else {
  167. packet_peer_stream->put_var(E->get());
  168. packet_peer_stream->put_var(F->get());
  169. }
  170. E=E->next();
  171. F=F->next();
  172. }
  173. }
  174. } else if (command=="step") {
  175. set_depth(-1);
  176. set_lines_left(1);
  177. break;
  178. } else if (command=="next") {
  179. set_depth(0);
  180. set_lines_left(1);
  181. break;
  182. } else if (command=="continue") {
  183. set_depth(-1);
  184. set_lines_left(-1);
  185. break;
  186. } else if (command=="break") {
  187. ERR_PRINT("Got break when already broke!");
  188. break;
  189. } else if (command=="request_scene_tree") {
  190. if (request_scene_tree)
  191. request_scene_tree(request_scene_tree_ud);
  192. } else if (command=="request_video_mem") {
  193. _send_video_memory();
  194. } else if (command=="breakpoint") {
  195. bool set = cmd[3];
  196. if (set)
  197. insert_breakpoint(cmd[2],cmd[1]);
  198. else
  199. remove_breakpoint(cmd[2],cmd[1]);
  200. } else {
  201. _parse_live_edit(cmd);
  202. }
  203. } else {
  204. OS::get_singleton()->delay_usec(10000);
  205. }
  206. }
  207. packet_peer_stream->put_var("debug_exit");
  208. packet_peer_stream->put_var(0);
  209. }
  210. void ScriptDebuggerRemote::_get_output() {
  211. mutex->lock();
  212. if (output_strings.size()) {
  213. locking=true;
  214. packet_peer_stream->put_var("output");
  215. packet_peer_stream->put_var(output_strings .size());
  216. while(output_strings.size()) {
  217. packet_peer_stream->put_var(output_strings.front()->get());
  218. output_strings.pop_front();
  219. }
  220. locking=false;
  221. }
  222. while (messages.size()) {
  223. locking=true;
  224. packet_peer_stream->put_var("message:"+messages.front()->get().message);
  225. packet_peer_stream->put_var(messages.front()->get().data.size());
  226. for(int i=0;i<messages.front()->get().data.size();i++) {
  227. packet_peer_stream->put_var(messages.front()->get().data[i]);
  228. }
  229. messages.pop_front();
  230. locking=false;
  231. }
  232. while (errors.size()) {
  233. locking=true;
  234. packet_peer_stream->put_var("error");
  235. OutputError oe = errors.front()->get();
  236. packet_peer_stream->put_var(oe.callstack.size()+2);
  237. Array error_data;
  238. error_data.push_back(oe.hr);
  239. error_data.push_back(oe.min);
  240. error_data.push_back(oe.sec);
  241. error_data.push_back(oe.msec);
  242. error_data.push_back(oe.source_func);
  243. error_data.push_back(oe.source_file);
  244. error_data.push_back(oe.source_line);
  245. error_data.push_back(oe.error);
  246. error_data.push_back(oe.error_descr);
  247. error_data.push_back(oe.warning);
  248. packet_peer_stream->put_var(error_data);
  249. packet_peer_stream->put_var(oe.callstack.size());
  250. for(int i=0;i<oe.callstack.size();i++) {
  251. packet_peer_stream->put_var(oe.callstack[i]);
  252. }
  253. errors.pop_front();
  254. locking=false;
  255. }
  256. mutex->unlock();
  257. }
  258. void ScriptDebuggerRemote::line_poll() {
  259. //the purpose of this is just processing events every now and then when the script might get too busy
  260. //otherwise bugs like infinite loops cant be catched
  261. if (poll_every%512==0)
  262. _poll_events();
  263. poll_every++;
  264. }
  265. 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) {
  266. if (p_type==ERR_HANDLER_SCRIPT)
  267. return; //ignore script errors, those go through debugger
  268. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote*)ud;
  269. OutputError oe;
  270. oe.error=p_err;
  271. oe.error_descr=p_descr;
  272. oe.source_file=p_file;
  273. oe.source_line=p_line;
  274. oe.source_func=p_func;
  275. oe.warning=p_type==ERR_HANDLER_WARNING;
  276. uint64_t time = OS::get_singleton()->get_ticks_msec();
  277. oe.hr=time/3600000;
  278. oe.min=(time/60000)%60;
  279. oe.sec=(time/1000)%60;
  280. oe.msec=time%1000;
  281. Array cstack;
  282. Vector<ScriptLanguage::StackInfo> si;
  283. for(int i=0;i<ScriptServer::get_language_count();i++) {
  284. si=ScriptServer::get_language(i)->debug_get_current_stack_info();
  285. if (si.size())
  286. break;
  287. }
  288. cstack.resize(si.size()*2);
  289. for(int i=0;i<si.size();i++) {
  290. String path;
  291. int line=0;
  292. if (si[i].script.is_valid()) {
  293. path=si[i].script->get_path();
  294. line=si[i].line;
  295. }
  296. cstack[i*2+0]=path;
  297. cstack[i*2+1]=line;
  298. }
  299. oe.callstack=cstack;
  300. sdr->mutex->lock();
  301. if (!sdr->locking && sdr->tcp_client->is_connected()) {
  302. sdr->errors.push_back(oe);
  303. }
  304. sdr->mutex->unlock();
  305. }
  306. bool ScriptDebuggerRemote::_parse_live_edit(const Array& cmd) {
  307. String cmdstr = cmd[0];
  308. if (!live_edit_funcs || !cmdstr.begins_with("live_"))
  309. return false;
  310. //print_line(Variant(cmd).get_construct_string());
  311. if (cmdstr=="live_set_root") {
  312. if (!live_edit_funcs->root_func)
  313. return true;
  314. //print_line("root: "+Variant(cmd).get_construct_string());
  315. live_edit_funcs->root_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  316. } else if (cmdstr=="live_node_path") {
  317. if (!live_edit_funcs->node_path_func)
  318. return true;
  319. //print_line("path: "+Variant(cmd).get_construct_string());
  320. live_edit_funcs->node_path_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  321. } else if (cmdstr=="live_res_path") {
  322. if (!live_edit_funcs->res_path_func)
  323. return true;
  324. live_edit_funcs->res_path_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  325. } else if (cmdstr=="live_node_prop_res") {
  326. if (!live_edit_funcs->node_set_res_func)
  327. return true;
  328. live_edit_funcs->node_set_res_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  329. } else if (cmdstr=="live_node_prop") {
  330. if (!live_edit_funcs->node_set_func)
  331. return true;
  332. live_edit_funcs->node_set_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  333. } else if (cmdstr=="live_res_prop_res") {
  334. if (!live_edit_funcs->res_set_res_func)
  335. return true;
  336. live_edit_funcs->res_set_res_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  337. } else if (cmdstr=="live_res_prop") {
  338. if (!live_edit_funcs->res_set_func)
  339. return true;
  340. live_edit_funcs->res_set_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  341. } else if (cmdstr=="live_node_call") {
  342. if (!live_edit_funcs->node_call_func)
  343. return true;
  344. live_edit_funcs->node_call_func(live_edit_funcs->udata,cmd[1],cmd[2], cmd[3],cmd[4],cmd[5],cmd[6],cmd[7]);
  345. } else if (cmdstr=="live_res_call") {
  346. if (!live_edit_funcs->res_call_func)
  347. return true;
  348. live_edit_funcs->res_call_func(live_edit_funcs->udata,cmd[1],cmd[2], cmd[3],cmd[4],cmd[5],cmd[6],cmd[7]);
  349. } else if (cmdstr=="live_create_node") {
  350. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  351. } else if (cmdstr=="live_instance_node") {
  352. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  353. } else if (cmdstr=="live_remove_node") {
  354. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata,cmd[1]);
  355. } else if (cmdstr=="live_remove_and_keep_node") {
  356. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  357. } else if (cmdstr=="live_restore_node") {
  358. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  359. } else if (cmdstr=="live_duplicate_node") {
  360. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  361. } else if (cmdstr=="live_reparent_node") {
  362. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3],cmd[4]);
  363. } else {
  364. return false;
  365. }
  366. return true;
  367. }
  368. void ScriptDebuggerRemote::_poll_events() {
  369. while(packet_peer_stream->get_available_packet_count()>0) {
  370. _get_output();
  371. //send over output_strings
  372. Variant var;
  373. Error err = packet_peer_stream->get_var(var);
  374. ERR_CONTINUE( err != OK);
  375. ERR_CONTINUE( var.get_type()!=Variant::ARRAY );
  376. Array cmd = var;
  377. ERR_CONTINUE( cmd.size()==0);
  378. ERR_CONTINUE( cmd[0].get_type()!=Variant::STRING );
  379. String command = cmd[0];
  380. //cmd.remove(0);
  381. if (command=="break") {
  382. if (get_break_language())
  383. debug(get_break_language());
  384. } else if (command=="request_scene_tree") {
  385. if (request_scene_tree)
  386. request_scene_tree(request_scene_tree_ud);
  387. } else if (command=="request_video_mem") {
  388. _send_video_memory();
  389. } else if (command=="breakpoint") {
  390. bool set = cmd[3];
  391. if (set)
  392. insert_breakpoint(cmd[2],cmd[1]);
  393. else
  394. remove_breakpoint(cmd[2],cmd[1]);
  395. } else {
  396. _parse_live_edit(cmd);
  397. }
  398. }
  399. }
  400. void ScriptDebuggerRemote::idle_poll() {
  401. _get_output();
  402. if (requested_quit) {
  403. packet_peer_stream->put_var("kill_me");
  404. packet_peer_stream->put_var(0);
  405. requested_quit=false;
  406. }
  407. if (performance) {
  408. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  409. if (pt-last_perf_time > 1000) {
  410. last_perf_time=pt;
  411. int max = performance->get("MONITOR_MAX");
  412. Array arr;
  413. arr.resize(max);
  414. for(int i=0;i<max;i++) {
  415. arr[i]=performance->call("get_monitor",i);
  416. }
  417. packet_peer_stream->put_var("performance");
  418. packet_peer_stream->put_var(1);
  419. packet_peer_stream->put_var(arr);
  420. }
  421. }
  422. _poll_events();
  423. }
  424. void ScriptDebuggerRemote::send_message(const String& p_message, const Array &p_args) {
  425. mutex->lock();
  426. if (!locking && tcp_client->is_connected()) {
  427. Message msg;
  428. msg.message=p_message;
  429. msg.data=p_args;
  430. messages.push_back(msg);
  431. }
  432. mutex->unlock();
  433. }
  434. void ScriptDebuggerRemote::_print_handler(void *p_this,const String& p_string) {
  435. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote*)p_this;
  436. uint64_t ticks = OS::get_singleton()->get_ticks_usec()/1000;
  437. sdr->msec_count+=ticks-sdr->last_msec;
  438. sdr->last_msec=ticks;
  439. if (sdr->msec_count>1000) {
  440. sdr->char_count=0;
  441. sdr->msec_count=0;
  442. }
  443. String s = p_string;
  444. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count,0), s.length());
  445. if (allowed_chars==0)
  446. return;
  447. if (allowed_chars<s.length()) {
  448. s=s.substr(0,allowed_chars);
  449. }
  450. sdr->char_count+=allowed_chars;
  451. if (sdr->char_count>=sdr->max_cps) {
  452. s+="\n[output overflow, print less text!]\n";
  453. }
  454. sdr->mutex->lock();
  455. if (!sdr->locking && sdr->tcp_client->is_connected()) {
  456. sdr->output_strings.push_back(s);
  457. }
  458. sdr->mutex->unlock();
  459. }
  460. void ScriptDebuggerRemote::request_quit() {
  461. requested_quit=true;
  462. }
  463. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  464. request_scene_tree=p_func;
  465. request_scene_tree_ud=p_udata;
  466. }
  467. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  468. live_edit_funcs=p_funcs;
  469. }
  470. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func=NULL;
  471. ScriptDebuggerRemote::ScriptDebuggerRemote() {
  472. tcp_client = StreamPeerTCP::create_ref();
  473. packet_peer_stream = Ref<PacketPeerStream>( memnew(PacketPeerStream) );
  474. packet_peer_stream->set_stream_peer(tcp_client);
  475. mutex = Mutex::create();
  476. locking=false;
  477. phl.printfunc=_print_handler;
  478. phl.userdata=this;
  479. add_print_handler(&phl);
  480. requested_quit=false;
  481. performance = Globals::get_singleton()->get_singleton_object("Performance");
  482. last_perf_time=0;
  483. poll_every=0;
  484. request_scene_tree=NULL;
  485. live_edit_funcs=NULL;
  486. max_cps = GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048);
  487. char_count=0;
  488. msec_count=0;
  489. last_msec=0;
  490. eh.errfunc=_err_handler;
  491. eh.userdata=this;
  492. add_error_handler(&eh);
  493. }
  494. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  495. remove_print_handler(&phl);
  496. remove_error_handler(&eh);
  497. memdelete(mutex);
  498. }