engine_debugger.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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::line_poll() {
  98. // The purpose of this is just processing events every now and then when the script might get too busy otherwise bugs like infinite loops can't be caught
  99. if (poll_every % 2048 == 0) {
  100. poll_events(false);
  101. }
  102. poll_every++;
  103. }
  104. void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, double p_physics_frame_time) {
  105. frame_time = USEC_TO_SEC(p_frame_ticks);
  106. process_time = USEC_TO_SEC(p_process_ticks);
  107. physics_time = USEC_TO_SEC(p_physics_ticks);
  108. physics_frame_time = p_physics_frame_time;
  109. // Notify tick to running profilers
  110. for (KeyValue<StringName, Profiler> &E : profilers) {
  111. Profiler &p = E.value;
  112. if (!p.active || !p.tick) {
  113. continue;
  114. }
  115. p.tick(p.data, frame_time, process_time, physics_time, physics_frame_time);
  116. }
  117. singleton->poll_events(true);
  118. }
  119. void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints, void (*p_allow_focus_steal_fn)()) {
  120. register_uri_handler("tcp://", RemoteDebuggerPeerTCP::create); // TCP is the default protocol. Platforms/modules can add more.
  121. if (p_uri.is_empty()) {
  122. return;
  123. }
  124. if (p_uri == "local://") {
  125. singleton = memnew(LocalDebugger);
  126. script_debugger = memnew(ScriptDebugger);
  127. // Tell the OS that we want to handle termination signals.
  128. OS::get_singleton()->initialize_debugging();
  129. } else if (p_uri.find("://") >= 0) {
  130. const String proto = p_uri.substr(0, p_uri.find("://") + 3);
  131. if (!protocols.has(proto)) {
  132. return;
  133. }
  134. RemoteDebuggerPeer *peer = protocols[proto](p_uri);
  135. if (!peer) {
  136. return;
  137. }
  138. singleton = memnew(RemoteDebugger(Ref<RemoteDebuggerPeer>(peer)));
  139. script_debugger = memnew(ScriptDebugger);
  140. // Notify editor of our pid (to allow focus stealing).
  141. Array msg;
  142. msg.push_back(OS::get_singleton()->get_process_id());
  143. singleton->send_message("set_pid", msg);
  144. }
  145. if (!singleton) {
  146. return;
  147. }
  148. // There is a debugger, parse breakpoints.
  149. ScriptDebugger *singleton_script_debugger = singleton->get_script_debugger();
  150. singleton_script_debugger->set_skip_breakpoints(p_skip_breakpoints);
  151. for (int i = 0; i < p_breakpoints.size(); i++) {
  152. String bp = p_breakpoints[i];
  153. int sp = bp.rfind(":");
  154. ERR_CONTINUE_MSG(sp == -1, "Invalid breakpoint: '" + bp + "', expected file:line format.");
  155. singleton_script_debugger->insert_breakpoint(bp.substr(sp + 1, bp.length()).to_int(), bp.substr(0, sp));
  156. }
  157. allow_focus_steal_fn = p_allow_focus_steal_fn;
  158. }
  159. void EngineDebugger::deinitialize() {
  160. if (singleton) {
  161. // Stop all profilers
  162. for (const KeyValue<StringName, Profiler> &E : profilers) {
  163. if (E.value.active) {
  164. singleton->profiler_enable(E.key, false);
  165. }
  166. }
  167. // Flush any remaining message
  168. singleton->poll_events(false);
  169. memdelete(singleton);
  170. singleton = nullptr;
  171. }
  172. // Clear profilers/captures/protocol handlers.
  173. profilers.clear();
  174. captures.clear();
  175. protocols.clear();
  176. }
  177. EngineDebugger::~EngineDebugger() {
  178. if (script_debugger) {
  179. memdelete(script_debugger);
  180. }
  181. script_debugger = nullptr;
  182. singleton = nullptr;
  183. }