2
0

script_debugger_remote.cpp 23 KB

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