engine_debugger.cpp 8.0 KB

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