engine_debugger.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**************************************************************************/
  2. /* engine_debugger.cpp */
  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. #include "engine_debugger.h"
  31. #include "core/debugger/local_debugger.h"
  32. #include "core/debugger/remote_debugger.h"
  33. #include "core/debugger/remote_debugger_peer.h"
  34. #include "core/debugger/script_debugger.h"
  35. #include "core/os/os.h"
  36. EngineDebugger *EngineDebugger::singleton = nullptr;
  37. ScriptDebugger *EngineDebugger::script_debugger = nullptr;
  38. HashMap<StringName, EngineDebugger::Profiler> EngineDebugger::profilers;
  39. HashMap<StringName, EngineDebugger::Capture> EngineDebugger::captures;
  40. HashMap<String, EngineDebugger::CreatePeerFunc> EngineDebugger::protocols;
  41. void (*EngineDebugger::allow_focus_steal_fn)();
  42. void EngineDebugger::register_profiler(const StringName &p_name, const Profiler &p_func) {
  43. ERR_FAIL_COND_MSG(profilers.has(p_name), "Profiler already registered: " + p_name);
  44. profilers.insert(p_name, p_func);
  45. }
  46. void EngineDebugger::unregister_profiler(const StringName &p_name) {
  47. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  48. Profiler &p = profilers[p_name];
  49. if (p.active && p.toggle) {
  50. p.toggle(p.data, false, Array());
  51. p.active = false;
  52. }
  53. profilers.erase(p_name);
  54. }
  55. void EngineDebugger::register_message_capture(const StringName &p_name, Capture p_func) {
  56. ERR_FAIL_COND_MSG(captures.has(p_name), "Capture already registered: " + p_name);
  57. captures.insert(p_name, p_func);
  58. }
  59. void EngineDebugger::unregister_message_capture(const StringName &p_name) {
  60. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  61. captures.erase(p_name);
  62. }
  63. void EngineDebugger::register_uri_handler(const String &p_protocol, CreatePeerFunc p_func) {
  64. ERR_FAIL_COND_MSG(protocols.has(p_protocol), "Protocol handler already registered: " + p_protocol);
  65. protocols.insert(p_protocol, p_func);
  66. }
  67. void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  68. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Can't change profiler state, no profiler: " + p_name);
  69. Profiler &p = profilers[p_name];
  70. if (p.toggle) {
  71. p.toggle(p.data, p_enabled, p_opts);
  72. }
  73. p.active = p_enabled;
  74. }
  75. void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  76. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Can't add frame data, no profiler: " + p_name);
  77. Profiler &p = profilers[p_name];
  78. if (p.add) {
  79. p.add(p.data, p_data);
  80. }
  81. }
  82. bool EngineDebugger::is_profiling(const StringName &p_name) {
  83. return profilers.has(p_name) && profilers[p_name].active;
  84. }
  85. bool EngineDebugger::has_profiler(const StringName &p_name) {
  86. return profilers.has(p_name);
  87. }
  88. bool EngineDebugger::has_capture(const StringName &p_name) {
  89. return captures.has(p_name);
  90. }
  91. Error EngineDebugger::capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured) {
  92. r_captured = false;
  93. ERR_FAIL_COND_V_MSG(!captures.has(p_name), ERR_UNCONFIGURED, "Capture not registered: " + p_name);
  94. const Capture &cap = captures[p_name];
  95. return cap.capture(cap.data, p_msg, p_args, r_captured);
  96. }
  97. void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time) {
  98. frame_time = USEC_TO_SEC(p_frame_ticks);
  99. process_time = USEC_TO_SEC(p_process_ticks);
  100. physics_time = USEC_TO_SEC(p_physics_ticks);
  101. physics_frame_time = p_physics_frame_time;
  102. // Notify tick to running profilers
  103. for (KeyValue<StringName, Profiler> &E : profilers) {
  104. Profiler &p = E.value;
  105. if (!p.active || !p.tick) {
  106. continue;
  107. }
  108. p.tick(p.data, frame_time, process_time, physics_time, physics_frame_time);
  109. }
  110. singleton->poll_events(true);
  111. }
  112. void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, const Vector<String> &p_breakpoints, void (*p_allow_focus_steal_fn)()) {
  113. register_uri_handler("tcp://", RemoteDebuggerPeerTCP::create); // TCP is the default protocol. Platforms/modules can add more.
  114. if (p_uri.is_empty()) {
  115. return;
  116. }
  117. if (p_uri == "local://") {
  118. singleton = memnew(LocalDebugger);
  119. script_debugger = memnew(ScriptDebugger);
  120. // Tell the OS that we want to handle termination signals.
  121. OS::get_singleton()->initialize_debugging();
  122. } else if (p_uri.contains("://")) {
  123. const String proto = p_uri.substr(0, p_uri.find("://") + 3);
  124. if (!protocols.has(proto)) {
  125. return;
  126. }
  127. RemoteDebuggerPeer *peer = protocols[proto](p_uri);
  128. if (!peer) {
  129. return;
  130. }
  131. singleton = memnew(RemoteDebugger(Ref<RemoteDebuggerPeer>(peer)));
  132. script_debugger = memnew(ScriptDebugger);
  133. // Notify editor of our pid (to allow focus stealing).
  134. Array msg;
  135. msg.push_back(OS::get_singleton()->get_process_id());
  136. singleton->send_message("set_pid", msg);
  137. }
  138. if (!singleton) {
  139. return;
  140. }
  141. // There is a debugger, parse breakpoints.
  142. ScriptDebugger *singleton_script_debugger = singleton->get_script_debugger();
  143. singleton_script_debugger->set_skip_breakpoints(p_skip_breakpoints);
  144. for (int i = 0; i < p_breakpoints.size(); i++) {
  145. const String &bp = p_breakpoints[i];
  146. int sp = bp.rfind(":");
  147. ERR_CONTINUE_MSG(sp == -1, "Invalid breakpoint: '" + bp + "', expected file:line format.");
  148. singleton_script_debugger->insert_breakpoint(bp.substr(sp + 1, bp.length()).to_int(), bp.substr(0, sp));
  149. }
  150. allow_focus_steal_fn = p_allow_focus_steal_fn;
  151. }
  152. void EngineDebugger::deinitialize() {
  153. if (singleton) {
  154. // Stop all profilers
  155. for (const KeyValue<StringName, Profiler> &E : profilers) {
  156. if (E.value.active) {
  157. singleton->profiler_enable(E.key, false);
  158. }
  159. }
  160. // Flush any remaining message
  161. singleton->poll_events(false);
  162. memdelete(singleton);
  163. singleton = nullptr;
  164. }
  165. // Clear profilers/captures/protocol handlers.
  166. profilers.clear();
  167. captures.clear();
  168. protocols.clear();
  169. }
  170. EngineDebugger::~EngineDebugger() {
  171. if (script_debugger) {
  172. memdelete(script_debugger);
  173. }
  174. script_debugger = nullptr;
  175. singleton = nullptr;
  176. }