engine_debugger.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* engine_debugger.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 ENGINE_DEBUGGER_H
  31. #define ENGINE_DEBUGGER_H
  32. #include "core/string/string_name.h"
  33. #include "core/string/ustring.h"
  34. #include "core/templates/hash_map.h"
  35. #include "core/templates/vector.h"
  36. #include "core/variant/array.h"
  37. #include "core/variant/variant.h"
  38. class RemoteDebuggerPeer;
  39. class ScriptDebugger;
  40. class EngineDebugger {
  41. public:
  42. typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts);
  43. typedef void (*ProfilingTick)(void *p_user, double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time);
  44. typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr);
  45. typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
  46. typedef RemoteDebuggerPeer *(*CreatePeerFunc)(const String &p_uri);
  47. class Profiler {
  48. friend class EngineDebugger;
  49. ProfilingToggle toggle = nullptr;
  50. ProfilingAdd add = nullptr;
  51. ProfilingTick tick = nullptr;
  52. void *data = nullptr;
  53. bool active = false;
  54. public:
  55. Profiler() {}
  56. Profiler(void *p_data, ProfilingToggle p_toggle, ProfilingAdd p_add, ProfilingTick p_tick) {
  57. data = p_data;
  58. toggle = p_toggle;
  59. add = p_add;
  60. tick = p_tick;
  61. }
  62. };
  63. class Capture {
  64. friend class EngineDebugger;
  65. CaptureFunc capture = nullptr;
  66. void *data = nullptr;
  67. public:
  68. Capture() {}
  69. Capture(void *p_data, CaptureFunc p_capture) {
  70. data = p_data;
  71. capture = p_capture;
  72. }
  73. };
  74. private:
  75. double frame_time = 0.0;
  76. double process_time = 0.0;
  77. double physics_time = 0.0;
  78. double physics_frame_time = 0.0;
  79. uint32_t poll_every = 0;
  80. protected:
  81. static EngineDebugger *singleton;
  82. static ScriptDebugger *script_debugger;
  83. static HashMap<StringName, Profiler> profilers;
  84. static HashMap<StringName, Capture> captures;
  85. static HashMap<String, CreatePeerFunc> protocols;
  86. static void (*allow_focus_steal_fn)();
  87. public:
  88. _FORCE_INLINE_ static EngineDebugger *get_singleton() { return singleton; }
  89. _FORCE_INLINE_ static bool is_active() { return singleton != nullptr && script_debugger != nullptr; }
  90. _FORCE_INLINE_ static ScriptDebugger *get_script_debugger() { return script_debugger; };
  91. static void initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints, void (*p_allow_focus_steal_fn)());
  92. static void deinitialize();
  93. static void register_profiler(const StringName &p_name, const Profiler &p_profiler);
  94. static void unregister_profiler(const StringName &p_name);
  95. static bool is_profiling(const StringName &p_name);
  96. static bool has_profiler(const StringName &p_name);
  97. static void profiler_add_frame_data(const StringName &p_name, const Array &p_data);
  98. static void register_message_capture(const StringName &p_name, Capture p_func);
  99. static void unregister_message_capture(const StringName &p_name);
  100. static bool has_capture(const StringName &p_name);
  101. static void register_uri_handler(const String &p_protocol, CreatePeerFunc p_func);
  102. void iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time);
  103. void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array());
  104. Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured);
  105. void line_poll();
  106. virtual void poll_events(bool p_is_idle) {}
  107. virtual void send_message(const String &p_msg, const Array &p_data) = 0;
  108. virtual void send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, bool p_editor_notify, ErrorHandlerType p_type) = 0;
  109. virtual void debug(bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0;
  110. virtual ~EngineDebugger();
  111. };
  112. #endif // ENGINE_DEBUGGER_H