script_debugger_remote.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. String pretty_print = F->get().operator String();
  157. packet_peer_stream->put_var(pretty_print.ascii().get_data());
  158. } else {
  159. packet_peer_stream->put_var(E->get());
  160. packet_peer_stream->put_var(F->get());
  161. }
  162. E=E->next();
  163. F=F->next();
  164. }
  165. }
  166. { //locals
  167. packet_peer_stream->put_var(locals.size());
  168. List<String>::Element *E=locals.front();
  169. List<Variant>::Element *F=local_vals.front();
  170. while(E) {
  171. if (F->get().get_type()==Variant::OBJECT) {
  172. packet_peer_stream->put_var("*"+E->get());
  173. String pretty_print = F->get().operator String();
  174. packet_peer_stream->put_var(pretty_print.ascii().get_data());
  175. } else {
  176. packet_peer_stream->put_var(E->get());
  177. packet_peer_stream->put_var(F->get());
  178. }
  179. E=E->next();
  180. F=F->next();
  181. }
  182. }
  183. } else if (command=="step") {
  184. set_depth(-1);
  185. set_lines_left(1);
  186. break;
  187. } else if (command=="next") {
  188. set_depth(0);
  189. set_lines_left(1);
  190. break;
  191. } else if (command=="continue") {
  192. set_depth(-1);
  193. set_lines_left(-1);
  194. break;
  195. } else if (command=="break") {
  196. ERR_PRINT("Got break when already broke!");
  197. break;
  198. } else if (command=="request_scene_tree") {
  199. if (request_scene_tree)
  200. request_scene_tree(request_scene_tree_ud);
  201. } else if (command=="request_video_mem") {
  202. _send_video_memory();
  203. } else if (command=="inspect_object") {
  204. ObjectID id = cmd[1];
  205. _send_object_id(id);
  206. } else if (command=="set_object_property") {
  207. _set_object_property(cmd[1],cmd[2],cmd[3]);
  208. } else if (command=="reload_scripts") {
  209. reload_all_scripts=true;
  210. } else if (command=="breakpoint") {
  211. bool set = cmd[3];
  212. if (set)
  213. insert_breakpoint(cmd[2],cmd[1]);
  214. else
  215. remove_breakpoint(cmd[2],cmd[1]);
  216. } else {
  217. _parse_live_edit(cmd);
  218. }
  219. } else {
  220. OS::get_singleton()->delay_usec(10000);
  221. }
  222. }
  223. packet_peer_stream->put_var("debug_exit");
  224. packet_peer_stream->put_var(0);
  225. if (mouse_mode!=Input::MOUSE_MODE_VISIBLE)
  226. Input::get_singleton()->set_mouse_mode(mouse_mode);
  227. }
  228. void ScriptDebuggerRemote::_get_output() {
  229. mutex->lock();
  230. if (output_strings.size()) {
  231. locking=true;
  232. packet_peer_stream->put_var("output");
  233. packet_peer_stream->put_var(output_strings .size());
  234. while(output_strings.size()) {
  235. packet_peer_stream->put_var(output_strings.front()->get());
  236. output_strings.pop_front();
  237. }
  238. locking=false;
  239. }
  240. while (messages.size()) {
  241. locking=true;
  242. packet_peer_stream->put_var("message:"+messages.front()->get().message);
  243. packet_peer_stream->put_var(messages.front()->get().data.size());
  244. for(int i=0;i<messages.front()->get().data.size();i++) {
  245. packet_peer_stream->put_var(messages.front()->get().data[i]);
  246. }
  247. messages.pop_front();
  248. locking=false;
  249. }
  250. while (errors.size()) {
  251. locking=true;
  252. packet_peer_stream->put_var("error");
  253. OutputError oe = errors.front()->get();
  254. packet_peer_stream->put_var(oe.callstack.size()+2);
  255. Array error_data;
  256. error_data.push_back(oe.hr);
  257. error_data.push_back(oe.min);
  258. error_data.push_back(oe.sec);
  259. error_data.push_back(oe.msec);
  260. error_data.push_back(oe.source_func);
  261. error_data.push_back(oe.source_file);
  262. error_data.push_back(oe.source_line);
  263. error_data.push_back(oe.error);
  264. error_data.push_back(oe.error_descr);
  265. error_data.push_back(oe.warning);
  266. packet_peer_stream->put_var(error_data);
  267. packet_peer_stream->put_var(oe.callstack.size());
  268. for(int i=0;i<oe.callstack.size();i++) {
  269. packet_peer_stream->put_var(oe.callstack[i]);
  270. }
  271. errors.pop_front();
  272. locking=false;
  273. }
  274. mutex->unlock();
  275. }
  276. void ScriptDebuggerRemote::line_poll() {
  277. //the purpose of this is just processing events every now and then when the script might get too busy
  278. //otherwise bugs like infinite loops cant be catched
  279. if (poll_every%2048==0)
  280. _poll_events();
  281. poll_every++;
  282. }
  283. 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) {
  284. if (p_type==ERR_HANDLER_SCRIPT)
  285. return; //ignore script errors, those go through debugger
  286. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote*)ud;
  287. OutputError oe;
  288. oe.error=p_err;
  289. oe.error_descr=p_descr;
  290. oe.source_file=p_file;
  291. oe.source_line=p_line;
  292. oe.source_func=p_func;
  293. oe.warning=p_type==ERR_HANDLER_WARNING;
  294. uint64_t time = OS::get_singleton()->get_ticks_msec();
  295. oe.hr=time/3600000;
  296. oe.min=(time/60000)%60;
  297. oe.sec=(time/1000)%60;
  298. oe.msec=time%1000;
  299. Array cstack;
  300. Vector<ScriptLanguage::StackInfo> si;
  301. for(int i=0;i<ScriptServer::get_language_count();i++) {
  302. si=ScriptServer::get_language(i)->debug_get_current_stack_info();
  303. if (si.size())
  304. break;
  305. }
  306. cstack.resize(si.size()*2);
  307. for(int i=0;i<si.size();i++) {
  308. String path;
  309. int line=0;
  310. if (si[i].script.is_valid()) {
  311. path=si[i].script->get_path();
  312. line=si[i].line;
  313. }
  314. cstack[i*2+0]=path;
  315. cstack[i*2+1]=line;
  316. }
  317. oe.callstack=cstack;
  318. sdr->mutex->lock();
  319. if (!sdr->locking && sdr->tcp_client->is_connected()) {
  320. sdr->errors.push_back(oe);
  321. }
  322. sdr->mutex->unlock();
  323. }
  324. bool ScriptDebuggerRemote::_parse_live_edit(const Array& cmd) {
  325. String cmdstr = cmd[0];
  326. if (!live_edit_funcs || !cmdstr.begins_with("live_"))
  327. return false;
  328. //print_line(Variant(cmd).get_construct_string());
  329. if (cmdstr=="live_set_root") {
  330. if (!live_edit_funcs->root_func)
  331. return true;
  332. //print_line("root: "+Variant(cmd).get_construct_string());
  333. live_edit_funcs->root_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  334. } else if (cmdstr=="live_node_path") {
  335. if (!live_edit_funcs->node_path_func)
  336. return true;
  337. //print_line("path: "+Variant(cmd).get_construct_string());
  338. live_edit_funcs->node_path_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  339. } else if (cmdstr=="live_res_path") {
  340. if (!live_edit_funcs->res_path_func)
  341. return true;
  342. live_edit_funcs->res_path_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  343. } else if (cmdstr=="live_node_prop_res") {
  344. if (!live_edit_funcs->node_set_res_func)
  345. return true;
  346. live_edit_funcs->node_set_res_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  347. } else if (cmdstr=="live_node_prop") {
  348. if (!live_edit_funcs->node_set_func)
  349. return true;
  350. live_edit_funcs->node_set_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  351. } else if (cmdstr=="live_res_prop_res") {
  352. if (!live_edit_funcs->res_set_res_func)
  353. return true;
  354. live_edit_funcs->res_set_res_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  355. } else if (cmdstr=="live_res_prop") {
  356. if (!live_edit_funcs->res_set_func)
  357. return true;
  358. live_edit_funcs->res_set_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  359. } else if (cmdstr=="live_node_call") {
  360. if (!live_edit_funcs->node_call_func)
  361. return true;
  362. live_edit_funcs->node_call_func(live_edit_funcs->udata,cmd[1],cmd[2], cmd[3],cmd[4],cmd[5],cmd[6],cmd[7]);
  363. } else if (cmdstr=="live_res_call") {
  364. if (!live_edit_funcs->res_call_func)
  365. return true;
  366. live_edit_funcs->res_call_func(live_edit_funcs->udata,cmd[1],cmd[2], cmd[3],cmd[4],cmd[5],cmd[6],cmd[7]);
  367. } else if (cmdstr=="live_create_node") {
  368. live_edit_funcs->tree_create_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  369. } else if (cmdstr=="live_instance_node") {
  370. live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  371. } else if (cmdstr=="live_remove_node") {
  372. live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata,cmd[1]);
  373. } else if (cmdstr=="live_remove_and_keep_node") {
  374. live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  375. } else if (cmdstr=="live_restore_node") {
  376. live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3]);
  377. } else if (cmdstr=="live_duplicate_node") {
  378. live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata,cmd[1],cmd[2]);
  379. } else if (cmdstr=="live_reparent_node") {
  380. live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata,cmd[1],cmd[2],cmd[3],cmd[4]);
  381. } else {
  382. return false;
  383. }
  384. return true;
  385. }
  386. void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) {
  387. Object* obj = ObjectDB::get_instance(p_id);
  388. if (!obj)
  389. return;
  390. List<PropertyInfo> pinfo;
  391. obj->get_property_list(&pinfo,true);
  392. int props_to_send=0;
  393. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  394. if (E->get().usage&(PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CATEGORY)) {
  395. props_to_send++;
  396. }
  397. }
  398. packet_peer_stream->put_var("message:inspect_object");
  399. packet_peer_stream->put_var(props_to_send*5+4);
  400. packet_peer_stream->put_var(p_id);
  401. packet_peer_stream->put_var(obj->get_type());
  402. if (obj->is_type("Resource") || obj->is_type("Node"))
  403. packet_peer_stream->put_var(obj->call("get_path"));
  404. else
  405. packet_peer_stream->put_var("");
  406. packet_peer_stream->put_var(props_to_send);
  407. for (List<PropertyInfo>::Element *E=pinfo.front();E;E=E->next()) {
  408. if (E->get().usage&(PROPERTY_USAGE_EDITOR|PROPERTY_USAGE_CATEGORY)) {
  409. if (E->get().usage&PROPERTY_USAGE_CATEGORY) {
  410. packet_peer_stream->put_var("*"+E->get().name);
  411. } else {
  412. packet_peer_stream->put_var(E->get().name);
  413. }
  414. Variant var = obj->get(E->get().name);
  415. if (E->get().type==Variant::OBJECT || var.get_type()==Variant::OBJECT) {
  416. ObjectID id2;
  417. Object *obj=var;
  418. if (obj) {
  419. id2=obj->get_instance_ID();
  420. } else {
  421. id2=0;
  422. }
  423. packet_peer_stream->put_var(Variant::INT); //hint string
  424. packet_peer_stream->put_var(PROPERTY_HINT_OBJECT_ID); //hint
  425. packet_peer_stream->put_var(E->get().hint_string); //hint string
  426. packet_peer_stream->put_var(id2); //value
  427. } else {
  428. packet_peer_stream->put_var(E->get().type);
  429. packet_peer_stream->put_var(E->get().hint);
  430. packet_peer_stream->put_var(E->get().hint_string);
  431. //only send information that can be sent..
  432. if (var.get_type()==Variant::IMAGE) {
  433. var=Image();
  434. }
  435. if (var.get_type()>=Variant::DICTIONARY) {
  436. var=Array(); //send none for now, may be to big
  437. }
  438. packet_peer_stream->put_var(var);
  439. }
  440. }
  441. }
  442. }
  443. void ScriptDebuggerRemote::_set_object_property(ObjectID p_id, const String& p_property, const Variant& p_value) {
  444. Object* obj = ObjectDB::get_instance(p_id);
  445. if (!obj)
  446. return;
  447. obj->set(p_property,p_value);
  448. }
  449. void ScriptDebuggerRemote::_poll_events() {
  450. //this si called from ::idle_poll, happens only when running the game,
  451. //does not get called while on debug break
  452. while(packet_peer_stream->get_available_packet_count()>0) {
  453. _get_output();
  454. //send over output_strings
  455. Variant var;
  456. Error err = packet_peer_stream->get_var(var);
  457. ERR_CONTINUE( err != OK);
  458. ERR_CONTINUE( var.get_type()!=Variant::ARRAY );
  459. Array cmd = var;
  460. ERR_CONTINUE( cmd.size()==0);
  461. ERR_CONTINUE( cmd[0].get_type()!=Variant::STRING );
  462. String command = cmd[0];
  463. //cmd.remove(0);
  464. if (command=="break") {
  465. if (get_break_language())
  466. debug(get_break_language());
  467. } else if (command=="request_scene_tree") {
  468. if (request_scene_tree)
  469. request_scene_tree(request_scene_tree_ud);
  470. } else if (command=="request_video_mem") {
  471. _send_video_memory();
  472. } else if (command=="inspect_object") {
  473. ObjectID id = cmd[1];
  474. _send_object_id(id);
  475. } else if (command=="set_object_property") {
  476. _set_object_property(cmd[1],cmd[2],cmd[3]);
  477. } else if (command=="start_profiling") {
  478. for(int i=0;i<ScriptServer::get_language_count();i++) {
  479. ScriptServer::get_language(i)->profiling_start();
  480. }
  481. max_frame_functions=cmd[1];
  482. profiler_function_signature_map.clear();
  483. profiling=true;
  484. frame_time=0;
  485. idle_time=0;
  486. fixed_time=0;
  487. fixed_frame_time=0;
  488. print_line("PROFILING ALRIGHT!");
  489. } else if (command=="stop_profiling") {
  490. for(int i=0;i<ScriptServer::get_language_count();i++) {
  491. ScriptServer::get_language(i)->profiling_stop();
  492. }
  493. profiling=false;
  494. _send_profiling_data(false);
  495. print_line("PROFILING END!");
  496. } else if (command=="reload_scripts") {
  497. reload_all_scripts=true;
  498. } else if (command=="breakpoint") {
  499. bool set = cmd[3];
  500. if (set)
  501. insert_breakpoint(cmd[2],cmd[1]);
  502. else
  503. remove_breakpoint(cmd[2],cmd[1]);
  504. } else {
  505. _parse_live_edit(cmd);
  506. }
  507. }
  508. }
  509. void ScriptDebuggerRemote::_send_profiling_data(bool p_for_frame) {
  510. int ofs=0;
  511. for(int i=0;i<ScriptServer::get_language_count();i++) {
  512. if (p_for_frame)
  513. ofs+=ScriptServer::get_language(i)->profiling_get_frame_data(&profile_info[ofs],profile_info.size()-ofs);
  514. else
  515. ofs+=ScriptServer::get_language(i)->profiling_get_accumulated_data(&profile_info[ofs],profile_info.size()-ofs);
  516. }
  517. for(int i=0;i<ofs;i++) {
  518. profile_info_ptrs[i]=&profile_info[i];
  519. }
  520. SortArray<ScriptLanguage::ProfilingInfo*,ProfileInfoSort> sa;
  521. sa.sort(profile_info_ptrs.ptr(),ofs);
  522. int to_send=MIN(ofs,max_frame_functions);
  523. //check signatures first
  524. uint64_t total_script_time=0;
  525. for(int i=0;i<to_send;i++) {
  526. if (!profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  527. int idx = profiler_function_signature_map.size();
  528. packet_peer_stream->put_var("profile_sig");
  529. packet_peer_stream->put_var(2);
  530. packet_peer_stream->put_var(profile_info_ptrs[i]->signature);
  531. packet_peer_stream->put_var(idx);
  532. profiler_function_signature_map[profile_info_ptrs[i]->signature]=idx;
  533. }
  534. total_script_time+=profile_info_ptrs[i]->self_time;
  535. }
  536. //send frames then
  537. if (p_for_frame) {
  538. packet_peer_stream->put_var("profile_frame");
  539. packet_peer_stream->put_var(8+profile_frame_data.size()*2+to_send*4);
  540. } else {
  541. packet_peer_stream->put_var("profile_total");
  542. packet_peer_stream->put_var(8+to_send*4);
  543. }
  544. packet_peer_stream->put_var(OS::get_singleton()->get_frames_drawn()); //total frame time
  545. packet_peer_stream->put_var(frame_time); //total frame time
  546. packet_peer_stream->put_var(idle_time); //idle frame time
  547. packet_peer_stream->put_var(fixed_time); //fixed frame time
  548. packet_peer_stream->put_var(fixed_frame_time); //fixed frame time
  549. packet_peer_stream->put_var(USEC_TO_SEC(total_script_time)); //total script execution time
  550. if (p_for_frame) {
  551. packet_peer_stream->put_var(profile_frame_data.size()); //how many profile framedatas to send
  552. packet_peer_stream->put_var(to_send); //how many script functions to send
  553. for (int i=0;i<profile_frame_data.size();i++) {
  554. packet_peer_stream->put_var(profile_frame_data[i].name);
  555. packet_peer_stream->put_var(profile_frame_data[i].data);
  556. }
  557. } else {
  558. packet_peer_stream->put_var(0); //how many script functions to send
  559. packet_peer_stream->put_var(to_send); //how many script functions to send
  560. }
  561. for(int i=0;i<to_send;i++) {
  562. int sig_id=-1;
  563. if (profiler_function_signature_map.has(profile_info_ptrs[i]->signature)) {
  564. sig_id=profiler_function_signature_map[profile_info_ptrs[i]->signature];
  565. }
  566. packet_peer_stream->put_var(sig_id);
  567. packet_peer_stream->put_var(profile_info_ptrs[i]->call_count);
  568. packet_peer_stream->put_var(profile_info_ptrs[i]->total_time/1000000.0);
  569. packet_peer_stream->put_var(profile_info_ptrs[i]->self_time/1000000.0);
  570. }
  571. if (p_for_frame) {
  572. profile_frame_data.clear();
  573. }
  574. }
  575. void ScriptDebuggerRemote::idle_poll() {
  576. // this function is called every frame, except when there is a debugger break (::debug() in this class)
  577. // execution stops and remains in the ::debug function
  578. _get_output();
  579. if (requested_quit) {
  580. packet_peer_stream->put_var("kill_me");
  581. packet_peer_stream->put_var(0);
  582. requested_quit=false;
  583. }
  584. if (performance) {
  585. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  586. if (pt-last_perf_time > 1000) {
  587. last_perf_time=pt;
  588. int max = performance->get("MONITOR_MAX");
  589. Array arr;
  590. arr.resize(max);
  591. for(int i=0;i<max;i++) {
  592. arr[i]=performance->call("get_monitor",i);
  593. }
  594. packet_peer_stream->put_var("performance");
  595. packet_peer_stream->put_var(1);
  596. packet_peer_stream->put_var(arr);
  597. }
  598. }
  599. if (profiling) {
  600. if (skip_profile_frame) {
  601. skip_profile_frame=false;
  602. } else {
  603. //send profiling info normally
  604. _send_profiling_data(true);
  605. }
  606. }
  607. if (reload_all_scripts) {
  608. for(int i=0;i<ScriptServer::get_language_count();i++) {
  609. ScriptServer::get_language(i)->reload_all_scripts();
  610. }
  611. reload_all_scripts=false;
  612. }
  613. _poll_events();
  614. }
  615. void ScriptDebuggerRemote::send_message(const String& p_message, const Array &p_args) {
  616. mutex->lock();
  617. if (!locking && tcp_client->is_connected()) {
  618. Message msg;
  619. msg.message=p_message;
  620. msg.data=p_args;
  621. messages.push_back(msg);
  622. }
  623. mutex->unlock();
  624. }
  625. void ScriptDebuggerRemote::_print_handler(void *p_this,const String& p_string) {
  626. ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote*)p_this;
  627. uint64_t ticks = OS::get_singleton()->get_ticks_usec()/1000;
  628. sdr->msec_count+=ticks-sdr->last_msec;
  629. sdr->last_msec=ticks;
  630. if (sdr->msec_count>1000) {
  631. sdr->char_count=0;
  632. sdr->msec_count=0;
  633. }
  634. String s = p_string;
  635. int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count,0), s.length());
  636. if (allowed_chars==0)
  637. return;
  638. if (allowed_chars<s.length()) {
  639. s=s.substr(0,allowed_chars);
  640. }
  641. sdr->char_count+=allowed_chars;
  642. if (sdr->char_count>=sdr->max_cps) {
  643. s+="\n[output overflow, print less text!]\n";
  644. }
  645. sdr->mutex->lock();
  646. if (!sdr->locking && sdr->tcp_client->is_connected()) {
  647. sdr->output_strings.push_back(s);
  648. }
  649. sdr->mutex->unlock();
  650. }
  651. void ScriptDebuggerRemote::request_quit() {
  652. requested_quit=true;
  653. }
  654. void ScriptDebuggerRemote::set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {
  655. request_scene_tree=p_func;
  656. request_scene_tree_ud=p_udata;
  657. }
  658. void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) {
  659. live_edit_funcs=p_funcs;
  660. }
  661. bool ScriptDebuggerRemote::is_profiling() const {
  662. return profiling;
  663. }
  664. void ScriptDebuggerRemote::add_profiling_frame_data(const StringName& p_name,const Array& p_data){
  665. int idx=-1;
  666. for(int i=0;i<profile_frame_data.size();i++) {
  667. if (profile_frame_data[i].name==p_name) {
  668. idx=i;
  669. break;
  670. }
  671. }
  672. FrameData fd;
  673. fd.name=p_name;
  674. fd.data=p_data;
  675. if (idx==-1) {
  676. profile_frame_data.push_back(fd);
  677. } else {
  678. profile_frame_data[idx]=fd;
  679. }
  680. }
  681. void ScriptDebuggerRemote::profiling_start() {
  682. //ignores this, uses it via connnection
  683. }
  684. void ScriptDebuggerRemote::profiling_end() {
  685. //ignores this, uses it via connnection
  686. }
  687. void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_fixed_time, float p_fixed_frame_time) {
  688. frame_time=p_frame_time;
  689. idle_time=p_idle_time;
  690. fixed_time=p_fixed_time;
  691. fixed_frame_time=p_fixed_frame_time;
  692. }
  693. ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func=NULL;
  694. ScriptDebuggerRemote::ScriptDebuggerRemote() {
  695. tcp_client = StreamPeerTCP::create_ref();
  696. packet_peer_stream = Ref<PacketPeerStream>( memnew(PacketPeerStream) );
  697. packet_peer_stream->set_stream_peer(tcp_client);
  698. mutex = Mutex::create();
  699. locking=false;
  700. phl.printfunc=_print_handler;
  701. phl.userdata=this;
  702. add_print_handler(&phl);
  703. requested_quit=false;
  704. performance = Globals::get_singleton()->get_singleton_object("Performance");
  705. last_perf_time=0;
  706. poll_every=0;
  707. request_scene_tree=NULL;
  708. live_edit_funcs=NULL;
  709. max_cps = GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048);
  710. char_count=0;
  711. msec_count=0;
  712. last_msec=0;
  713. skip_profile_frame=false;
  714. eh.errfunc=_err_handler;
  715. eh.userdata=this;
  716. add_error_handler(&eh);
  717. profile_info.resize(CLAMP(int(Globals::get_singleton()->get("debug/profiler_max_functions")),128,65535));
  718. profile_info_ptrs.resize(profile_info.size());
  719. profiling=false;
  720. max_frame_functions=16;
  721. reload_all_scripts=false;
  722. }
  723. ScriptDebuggerRemote::~ScriptDebuggerRemote() {
  724. remove_print_handler(&phl);
  725. remove_error_handler(&eh);
  726. memdelete(mutex);
  727. }