engine_debugger.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*************************************************************************/
  2. /* engine_debugger.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 ENGINE_DEBUGGER_H
  31. #define ENGINE_DEBUGGER_H
  32. #include "core/array.h"
  33. #include "core/map.h"
  34. #include "core/string_name.h"
  35. #include "core/ustring.h"
  36. #include "core/variant.h"
  37. #include "core/vector.h"
  38. class ScriptDebugger;
  39. class EngineDebugger {
  40. public:
  41. typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts);
  42. typedef void (*ProfilingTick)(void *p_user, float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time);
  43. typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr);
  44. typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);
  45. class Profiler {
  46. friend class EngineDebugger;
  47. ProfilingToggle toggle = nullptr;
  48. ProfilingAdd add = nullptr;
  49. ProfilingTick tick = nullptr;
  50. void *data = nullptr;
  51. bool active = false;
  52. public:
  53. Profiler() {}
  54. Profiler(void *p_data, ProfilingToggle p_toggle, ProfilingAdd p_add, ProfilingTick p_tick) {
  55. data = p_data;
  56. toggle = p_toggle;
  57. add = p_add;
  58. tick = p_tick;
  59. }
  60. };
  61. class Capture {
  62. friend class EngineDebugger;
  63. CaptureFunc capture = nullptr;
  64. void *data = nullptr;
  65. public:
  66. Capture() {}
  67. Capture(void *p_data, CaptureFunc p_capture) {
  68. data = p_data;
  69. capture = p_capture;
  70. }
  71. };
  72. private:
  73. float frame_time = 0.0;
  74. float idle_time = 0.0;
  75. float physics_time = 0.0;
  76. float physics_frame_time = 0.0;
  77. uint32_t poll_every = 0;
  78. protected:
  79. static EngineDebugger *singleton;
  80. static ScriptDebugger *script_debugger;
  81. static Map<StringName, Profiler> profilers;
  82. static Map<StringName, Capture> captures;
  83. public:
  84. _FORCE_INLINE_ static EngineDebugger *get_singleton() { return singleton; }
  85. _FORCE_INLINE_ static bool is_active() { return singleton != nullptr && script_debugger != nullptr; }
  86. _FORCE_INLINE_ static ScriptDebugger *get_script_debugger() { return script_debugger; };
  87. static void initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints);
  88. static void deinitialize();
  89. static void register_profiler(const StringName &p_name, const Profiler &p_profiler);
  90. static void unregister_profiler(const StringName &p_name);
  91. static bool is_profiling(const StringName &p_name);
  92. static bool has_profiler(const StringName &p_name);
  93. static void profiler_add_frame_data(const StringName &p_name, const Array &p_data);
  94. static void register_message_capture(const StringName &p_name, Capture p_func);
  95. static void unregister_message_capture(const StringName &p_name);
  96. static bool has_capture(const StringName &p_name);
  97. void iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, uint64_t p_physics_ticks, float p_physics_frame_time);
  98. void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array());
  99. Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured);
  100. void line_poll();
  101. virtual void poll_events(bool p_is_idle) {}
  102. virtual void send_message(const String &p_msg, const Array &p_data) = 0;
  103. 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) = 0;
  104. virtual void debug(bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0;
  105. virtual ~EngineDebugger();
  106. };
  107. #endif // ENGINE_DEBUGGER_H