engine_debugger.cpp 7.2 KB

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