script_debugger_remote.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*************************************************************************/
  2. /* script_debugger_remote.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef SCRIPT_DEBUGGER_REMOTE_H
  31. #define SCRIPT_DEBUGGER_REMOTE_H
  32. #include "core/io/packet_peer.h"
  33. #include "core/io/stream_peer_tcp.h"
  34. #include "core/list.h"
  35. #include "core/os/os.h"
  36. #include "core/script_language.h"
  37. class ScriptDebuggerRemote : public ScriptDebugger {
  38. public:
  39. class ResourceInfo {
  40. public:
  41. String path;
  42. String format;
  43. String type;
  44. RID id;
  45. int vram;
  46. bool operator<(const ResourceInfo &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  47. ResourceInfo() {
  48. vram = 0;
  49. }
  50. };
  51. class ResourceUsage {
  52. public:
  53. List<ResourceInfo> infos;
  54. Array serialize();
  55. bool deserialize(const Array &p_arr);
  56. };
  57. class FrameInfo {
  58. public:
  59. StringName name;
  60. float self_time;
  61. float total_time;
  62. FrameInfo() {
  63. self_time = 0;
  64. total_time = 0;
  65. }
  66. };
  67. class FrameFunction {
  68. public:
  69. int sig_id;
  70. int call_count;
  71. StringName name;
  72. float self_time;
  73. float total_time;
  74. FrameFunction() {
  75. sig_id = -1;
  76. call_count = 0;
  77. self_time = 0;
  78. total_time = 0;
  79. }
  80. };
  81. class ScriptStackVariable {
  82. public:
  83. String name;
  84. Variant value;
  85. int type;
  86. ScriptStackVariable() {
  87. type = -1;
  88. }
  89. Array serialize(int max_size = 1 << 20); // 1 MiB default.
  90. bool deserialize(const Array &p_arr);
  91. };
  92. class ScriptStackDump {
  93. public:
  94. List<ScriptLanguage::StackInfo> frames;
  95. ScriptStackDump() {}
  96. Array serialize();
  97. bool deserialize(const Array &p_arr);
  98. };
  99. class Message {
  100. public:
  101. String message;
  102. Array data;
  103. Message() {}
  104. };
  105. class OutputError {
  106. public:
  107. int hr;
  108. int min;
  109. int sec;
  110. int msec;
  111. String source_file;
  112. String source_func;
  113. int source_line;
  114. String error;
  115. String error_descr;
  116. bool warning;
  117. Vector<ScriptLanguage::StackInfo> callstack;
  118. OutputError() {
  119. hr = -1;
  120. min = -1;
  121. sec = -1;
  122. msec = -1;
  123. source_line = -1;
  124. warning = false;
  125. }
  126. Array serialize();
  127. bool deserialize(const Array &p_arr);
  128. };
  129. struct FrameData {
  130. StringName name;
  131. Array data;
  132. };
  133. class ProfilerSignature {
  134. public:
  135. StringName name;
  136. int id;
  137. Array serialize();
  138. bool deserialize(const Array &p_arr);
  139. ProfilerSignature() {
  140. id = -1;
  141. };
  142. };
  143. class ProfilerFrame {
  144. public:
  145. int frame_number;
  146. float frame_time;
  147. float idle_time;
  148. float physics_time;
  149. float physics_frame_time;
  150. float script_time;
  151. Vector<FrameData> frames_data;
  152. Vector<FrameFunction> frame_functions;
  153. ProfilerFrame() {
  154. frame_number = 0;
  155. frame_time = 0;
  156. idle_time = 0;
  157. physics_time = 0;
  158. physics_frame_time = 0;
  159. }
  160. Array serialize();
  161. bool deserialize(const Array &p_arr);
  162. };
  163. class NetworkProfilerFrame {
  164. public:
  165. Vector<MultiplayerAPI::ProfilingInfo> infos;
  166. Array serialize();
  167. bool deserialize(const Array &p_arr);
  168. NetworkProfilerFrame(){};
  169. };
  170. protected:
  171. struct ProfileInfoSort {
  172. bool operator()(ScriptLanguage::ProfilingInfo *A, ScriptLanguage::ProfilingInfo *B) const {
  173. return A->total_time < B->total_time;
  174. }
  175. };
  176. Vector<ScriptLanguage::ProfilingInfo> profile_info;
  177. Vector<ScriptLanguage::ProfilingInfo *> profile_info_ptrs;
  178. Vector<MultiplayerAPI::ProfilingInfo> network_profile_info;
  179. Map<StringName, int> profiler_function_signature_map;
  180. float frame_time, idle_time, physics_time, physics_frame_time;
  181. bool profiling;
  182. bool visual_profiling;
  183. bool network_profiling;
  184. int max_frame_functions;
  185. bool skip_profile_frame;
  186. bool reload_all_scripts;
  187. Ref<StreamPeerTCP> tcp_client;
  188. Ref<PacketPeerStream> packet_peer_stream;
  189. uint64_t last_perf_time;
  190. uint64_t last_net_prof_time;
  191. uint64_t last_net_bandwidth_time;
  192. Object *performance;
  193. bool requested_quit;
  194. Mutex *mutex;
  195. List<String> output_strings;
  196. List<Message> messages;
  197. int max_messages_per_frame;
  198. int n_messages_dropped;
  199. List<OutputError> errors;
  200. int max_errors_per_second;
  201. int max_warnings_per_second;
  202. int n_errors_dropped;
  203. int n_warnings_dropped;
  204. int max_cps;
  205. int char_count;
  206. int err_count;
  207. int warn_count;
  208. uint64_t last_msec;
  209. uint64_t msec_count;
  210. bool locking; //hack to avoid a deadloop
  211. static void _print_handler(void *p_this, const String &p_string, bool p_error);
  212. PrintHandlerList phl;
  213. void _get_output();
  214. void _poll_events();
  215. uint32_t poll_every;
  216. void _parse_message(const String p_command, const Array &p_data, ScriptLanguage *p_script = NULL);
  217. void _set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value);
  218. void _send_object_id(ObjectID p_id);
  219. void _send_video_memory();
  220. Ref<MultiplayerAPI> multiplayer;
  221. ErrorHandlerList eh;
  222. static void _err_handler(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
  223. void _put_msg(String p_message, Array p_data);
  224. void _send_profiling_data(bool p_for_frame);
  225. void _send_network_profiling_data();
  226. void _send_network_bandwidth_usage();
  227. Vector<FrameData> profile_frame_data;
  228. bool skip_breakpoints;
  229. public:
  230. typedef void (*ResourceUsageFunc)(ResourceUsage *);
  231. typedef Error (*ParseMessageFunc)(const String &p_name, const Array &p_msg); // Returns true if something was found (stopping propagation).
  232. static ResourceUsageFunc resource_usage_func;
  233. static ParseMessageFunc scene_tree_parse_func; // Could be made into list, extensible...
  234. Error connect_to_host(const String &p_host, uint16_t p_port);
  235. bool is_peer_connected();
  236. virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true, bool p_is_error_breakpoint = false);
  237. virtual void idle_poll();
  238. virtual void line_poll();
  239. virtual bool is_remote() const { return true; }
  240. virtual void request_quit();
  241. virtual void send_message(const String &p_message, const Array &p_args);
  242. virtual void 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);
  243. virtual void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer);
  244. virtual bool is_profiling() const;
  245. virtual void add_profiling_frame_data(const StringName &p_name, const Array &p_data);
  246. virtual void profiling_start();
  247. virtual void profiling_end();
  248. virtual void profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time);
  249. virtual void set_skip_breakpoints(bool p_skip_breakpoints);
  250. ScriptDebuggerRemote();
  251. ~ScriptDebuggerRemote();
  252. };
  253. #endif // SCRIPT_DEBUGGER_REMOTE_H