| 1 | /**************************************************************************/ |
| 2 | /* editor_debugger_plugin.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 | |
| 31 | #include "editor_debugger_plugin.h" |
| 32 | |
| 33 | #include "editor/debugger/script_editor_debugger.h" |
| 34 | |
| 35 | void EditorDebuggerSession::_breaked(bool p_really_did, bool p_can_debug, String p_message, bool p_has_stackdump) { |
| 36 | if (p_really_did) { |
| 37 | emit_signal(SNAME("breaked" ), p_can_debug); |
| 38 | } else { |
| 39 | emit_signal(SNAME("continued" )); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void EditorDebuggerSession::_started() { |
| 44 | emit_signal(SNAME("started" )); |
| 45 | } |
| 46 | |
| 47 | void EditorDebuggerSession::_stopped() { |
| 48 | emit_signal(SNAME("stopped" )); |
| 49 | } |
| 50 | |
| 51 | void EditorDebuggerSession::_bind_methods() { |
| 52 | ClassDB::bind_method(D_METHOD("send_message" , "message" , "data" ), &EditorDebuggerSession::send_message, DEFVAL(Array())); |
| 53 | ClassDB::bind_method(D_METHOD("toggle_profiler" , "profiler" , "enable" , "data" ), &EditorDebuggerSession::toggle_profiler, DEFVAL(Array())); |
| 54 | ClassDB::bind_method(D_METHOD("is_breaked" ), &EditorDebuggerSession::is_breaked); |
| 55 | ClassDB::bind_method(D_METHOD("is_debuggable" ), &EditorDebuggerSession::is_debuggable); |
| 56 | ClassDB::bind_method(D_METHOD("is_active" ), &EditorDebuggerSession::is_active); |
| 57 | ClassDB::bind_method(D_METHOD("add_session_tab" , "control" ), &EditorDebuggerSession::add_session_tab); |
| 58 | ClassDB::bind_method(D_METHOD("remove_session_tab" , "control" ), &EditorDebuggerSession::remove_session_tab); |
| 59 | |
| 60 | ADD_SIGNAL(MethodInfo("started" )); |
| 61 | ADD_SIGNAL(MethodInfo("stopped" )); |
| 62 | ADD_SIGNAL(MethodInfo("breaked" , PropertyInfo(Variant::BOOL, "can_debug" ))); |
| 63 | ADD_SIGNAL(MethodInfo("continued" )); |
| 64 | } |
| 65 | |
| 66 | void EditorDebuggerSession::add_session_tab(Control *p_tab) { |
| 67 | ERR_FAIL_COND(!p_tab || !debugger); |
| 68 | debugger->add_debugger_tab(p_tab); |
| 69 | tabs.insert(p_tab); |
| 70 | } |
| 71 | |
| 72 | void EditorDebuggerSession::remove_session_tab(Control *p_tab) { |
| 73 | ERR_FAIL_COND(!p_tab || !debugger); |
| 74 | debugger->remove_debugger_tab(p_tab); |
| 75 | tabs.erase(p_tab); |
| 76 | } |
| 77 | |
| 78 | void EditorDebuggerSession::send_message(const String &p_message, const Array &p_args) { |
| 79 | ERR_FAIL_NULL_MSG(debugger, "Plugin is not attached to debugger." ); |
| 80 | debugger->send_message(p_message, p_args); |
| 81 | } |
| 82 | |
| 83 | void EditorDebuggerSession::toggle_profiler(const String &p_profiler, bool p_enable, const Array &p_data) { |
| 84 | ERR_FAIL_NULL_MSG(debugger, "Plugin is not attached to debugger." ); |
| 85 | debugger->toggle_profiler(p_profiler, p_enable, p_data); |
| 86 | } |
| 87 | |
| 88 | bool EditorDebuggerSession::is_breaked() { |
| 89 | ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger." ); |
| 90 | return debugger->is_breaked(); |
| 91 | } |
| 92 | |
| 93 | bool EditorDebuggerSession::is_debuggable() { |
| 94 | ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger." ); |
| 95 | return debugger->is_debuggable(); |
| 96 | } |
| 97 | |
| 98 | bool EditorDebuggerSession::is_active() { |
| 99 | ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger." ); |
| 100 | return debugger->is_session_active(); |
| 101 | } |
| 102 | |
| 103 | void EditorDebuggerSession::detach_debugger() { |
| 104 | if (!debugger) { |
| 105 | return; |
| 106 | } |
| 107 | debugger->disconnect("started" , callable_mp(this, &EditorDebuggerSession::_started)); |
| 108 | debugger->disconnect("stopped" , callable_mp(this, &EditorDebuggerSession::_stopped)); |
| 109 | debugger->disconnect("breaked" , callable_mp(this, &EditorDebuggerSession::_breaked)); |
| 110 | debugger->disconnect("tree_exited" , callable_mp(this, &EditorDebuggerSession::_debugger_gone_away)); |
| 111 | for (Control *tab : tabs) { |
| 112 | debugger->remove_debugger_tab(tab); |
| 113 | } |
| 114 | tabs.clear(); |
| 115 | debugger = nullptr; |
| 116 | } |
| 117 | |
| 118 | void EditorDebuggerSession::_debugger_gone_away() { |
| 119 | debugger = nullptr; |
| 120 | tabs.clear(); |
| 121 | } |
| 122 | |
| 123 | EditorDebuggerSession::EditorDebuggerSession(ScriptEditorDebugger *p_debugger) { |
| 124 | ERR_FAIL_NULL(p_debugger); |
| 125 | debugger = p_debugger; |
| 126 | debugger->connect("started" , callable_mp(this, &EditorDebuggerSession::_started)); |
| 127 | debugger->connect("stopped" , callable_mp(this, &EditorDebuggerSession::_stopped)); |
| 128 | debugger->connect("breaked" , callable_mp(this, &EditorDebuggerSession::_breaked)); |
| 129 | debugger->connect("tree_exited" , callable_mp(this, &EditorDebuggerSession::_debugger_gone_away), CONNECT_ONE_SHOT); |
| 130 | } |
| 131 | |
| 132 | EditorDebuggerSession::~EditorDebuggerSession() { |
| 133 | detach_debugger(); |
| 134 | } |
| 135 | |
| 136 | /// EditorDebuggerPlugin |
| 137 | |
| 138 | EditorDebuggerPlugin::~EditorDebuggerPlugin() { |
| 139 | clear(); |
| 140 | } |
| 141 | |
| 142 | void EditorDebuggerPlugin::clear() { |
| 143 | for (int i = 0; i < sessions.size(); i++) { |
| 144 | sessions[i]->detach_debugger(); |
| 145 | } |
| 146 | sessions.clear(); |
| 147 | } |
| 148 | |
| 149 | void EditorDebuggerPlugin::create_session(ScriptEditorDebugger *p_debugger) { |
| 150 | sessions.push_back(Ref<EditorDebuggerSession>(memnew(EditorDebuggerSession(p_debugger)))); |
| 151 | setup_session(sessions.size() - 1); |
| 152 | } |
| 153 | |
| 154 | void EditorDebuggerPlugin::setup_session(int p_idx) { |
| 155 | GDVIRTUAL_CALL(_setup_session, p_idx); |
| 156 | } |
| 157 | |
| 158 | Ref<EditorDebuggerSession> EditorDebuggerPlugin::get_session(int p_idx) { |
| 159 | ERR_FAIL_INDEX_V(p_idx, sessions.size(), nullptr); |
| 160 | return sessions[p_idx]; |
| 161 | } |
| 162 | |
| 163 | Array EditorDebuggerPlugin::get_sessions() { |
| 164 | Array ret; |
| 165 | for (int i = 0; i < sessions.size(); i++) { |
| 166 | ret.push_back(sessions[i]); |
| 167 | } |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | bool EditorDebuggerPlugin::has_capture(const String &p_message) const { |
| 172 | bool ret = false; |
| 173 | if (GDVIRTUAL_CALL(_has_capture, p_message, ret)) { |
| 174 | return ret; |
| 175 | } |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | bool EditorDebuggerPlugin::capture(const String &p_message, const Array &p_data, int p_session_id) { |
| 180 | bool ret = false; |
| 181 | if (GDVIRTUAL_CALL(_capture, p_message, p_data, p_session_id, ret)) { |
| 182 | return ret; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | void EditorDebuggerPlugin::_bind_methods() { |
| 188 | GDVIRTUAL_BIND(_setup_session, "session_id" ); |
| 189 | GDVIRTUAL_BIND(_has_capture, "capture" ); |
| 190 | GDVIRTUAL_BIND(_capture, "message" , "data" , "session_id" ); |
| 191 | ClassDB::bind_method(D_METHOD("get_session" , "id" ), &EditorDebuggerPlugin::get_session); |
| 192 | ClassDB::bind_method(D_METHOD("get_sessions" ), &EditorDebuggerPlugin::get_sessions); |
| 193 | } |
| 194 | |