engine_debugger.cpp 7.9 KB

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