Path: blob/master/editor/debugger/editor_debugger_plugin.cpp
9902 views
/**************************************************************************/1/* editor_debugger_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_debugger_plugin.h"3132#include "editor/debugger/script_editor_debugger.h"3334void EditorDebuggerSession::_breaked(bool p_really_did, bool p_can_debug, const String &p_message, bool p_has_stackdump) {35if (p_really_did) {36emit_signal(SNAME("breaked"), p_can_debug);37} else {38emit_signal(SNAME("continued"));39}40}4142void EditorDebuggerSession::_started() {43emit_signal(SNAME("started"));44}4546void EditorDebuggerSession::_stopped() {47emit_signal(SNAME("stopped"));48}4950void EditorDebuggerSession::_bind_methods() {51ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EditorDebuggerSession::send_message, DEFVAL(Array()));52ClassDB::bind_method(D_METHOD("toggle_profiler", "profiler", "enable", "data"), &EditorDebuggerSession::toggle_profiler, DEFVAL(Array()));53ClassDB::bind_method(D_METHOD("is_breaked"), &EditorDebuggerSession::is_breaked);54ClassDB::bind_method(D_METHOD("is_debuggable"), &EditorDebuggerSession::is_debuggable);55ClassDB::bind_method(D_METHOD("is_active"), &EditorDebuggerSession::is_active);56ClassDB::bind_method(D_METHOD("add_session_tab", "control"), &EditorDebuggerSession::add_session_tab);57ClassDB::bind_method(D_METHOD("remove_session_tab", "control"), &EditorDebuggerSession::remove_session_tab);58ClassDB::bind_method(D_METHOD("set_breakpoint", "path", "line", "enabled"), &EditorDebuggerSession::set_breakpoint);5960ADD_SIGNAL(MethodInfo("started"));61ADD_SIGNAL(MethodInfo("stopped"));62ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "can_debug")));63ADD_SIGNAL(MethodInfo("continued"));64}6566void EditorDebuggerSession::add_session_tab(Control *p_tab) {67ERR_FAIL_COND(!p_tab || !debugger);68debugger->add_debugger_tab(p_tab);69tabs.insert(p_tab);70}7172void EditorDebuggerSession::remove_session_tab(Control *p_tab) {73ERR_FAIL_COND(!p_tab || !debugger);74debugger->remove_debugger_tab(p_tab);75tabs.erase(p_tab);76}7778void EditorDebuggerSession::send_message(const String &p_message, const Array &p_args) {79ERR_FAIL_NULL_MSG(debugger, "Plugin is not attached to debugger.");80debugger->send_message(p_message, p_args);81}8283void EditorDebuggerSession::toggle_profiler(const String &p_profiler, bool p_enable, const Array &p_data) {84ERR_FAIL_NULL_MSG(debugger, "Plugin is not attached to debugger.");85debugger->toggle_profiler(p_profiler, p_enable, p_data);86}8788bool EditorDebuggerSession::is_breaked() {89ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger.");90return debugger->is_breaked();91}9293bool EditorDebuggerSession::is_debuggable() {94ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger.");95return debugger->is_debuggable();96}9798bool EditorDebuggerSession::is_active() {99ERR_FAIL_NULL_V_MSG(debugger, false, "Plugin is not attached to debugger.");100return debugger->is_session_active();101}102103void EditorDebuggerSession::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {104ERR_FAIL_NULL_MSG(debugger, "Plugin is not attached to debugger.");105debugger->set_breakpoint(p_path, p_line, p_enabled);106}107108void EditorDebuggerSession::detach_debugger() {109if (!debugger) {110return;111}112debugger->disconnect("started", callable_mp(this, &EditorDebuggerSession::_started));113debugger->disconnect("stopped", callable_mp(this, &EditorDebuggerSession::_stopped));114debugger->disconnect("breaked", callable_mp(this, &EditorDebuggerSession::_breaked));115debugger->disconnect(SceneStringName(tree_exited), callable_mp(this, &EditorDebuggerSession::_debugger_gone_away));116for (Control *tab : tabs) {117debugger->remove_debugger_tab(tab);118}119tabs.clear();120debugger = nullptr;121}122123void EditorDebuggerSession::_debugger_gone_away() {124debugger = nullptr;125tabs.clear();126}127128EditorDebuggerSession::EditorDebuggerSession(ScriptEditorDebugger *p_debugger) {129ERR_FAIL_NULL(p_debugger);130debugger = p_debugger;131debugger->connect("started", callable_mp(this, &EditorDebuggerSession::_started));132debugger->connect("stopped", callable_mp(this, &EditorDebuggerSession::_stopped));133debugger->connect("breaked", callable_mp(this, &EditorDebuggerSession::_breaked));134debugger->connect(SceneStringName(tree_exited), callable_mp(this, &EditorDebuggerSession::_debugger_gone_away), CONNECT_ONE_SHOT);135}136137EditorDebuggerSession::~EditorDebuggerSession() {138detach_debugger();139}140141/// EditorDebuggerPlugin142143EditorDebuggerPlugin::~EditorDebuggerPlugin() {144clear();145}146147void EditorDebuggerPlugin::clear() {148for (Ref<EditorDebuggerSession> &session : sessions) {149session->detach_debugger();150}151sessions.clear();152}153154void EditorDebuggerPlugin::create_session(ScriptEditorDebugger *p_debugger) {155sessions.push_back(Ref<EditorDebuggerSession>(memnew(EditorDebuggerSession(p_debugger))));156setup_session(sessions.size() - 1);157}158159void EditorDebuggerPlugin::setup_session(int p_idx) {160GDVIRTUAL_CALL(_setup_session, p_idx);161}162163Ref<EditorDebuggerSession> EditorDebuggerPlugin::get_session(int p_idx) {164ERR_FAIL_INDEX_V(p_idx, sessions.size(), nullptr);165return sessions.get(p_idx);166}167168Array EditorDebuggerPlugin::get_sessions() {169Array ret;170for (const Ref<EditorDebuggerSession> &session : sessions) {171ret.push_back(session);172}173return ret;174}175176bool EditorDebuggerPlugin::has_capture(const String &p_message) const {177bool ret = false;178if (GDVIRTUAL_CALL(_has_capture, p_message, ret)) {179return ret;180}181return false;182}183184bool EditorDebuggerPlugin::capture(const String &p_message, const Array &p_data, int p_session_id) {185bool ret = false;186if (GDVIRTUAL_CALL(_capture, p_message, p_data, p_session_id, ret)) {187return ret;188}189return false;190}191192void EditorDebuggerPlugin::goto_script_line(const Ref<Script> &p_script, int p_line) {193GDVIRTUAL_CALL(_goto_script_line, p_script, p_line);194}195196void EditorDebuggerPlugin::breakpoints_cleared_in_tree() {197GDVIRTUAL_CALL(_breakpoints_cleared_in_tree);198}199200void EditorDebuggerPlugin::breakpoint_set_in_tree(const Ref<Script> &p_script, int p_line, bool p_enabled) {201GDVIRTUAL_CALL(_breakpoint_set_in_tree, p_script, p_line, p_enabled);202}203204void EditorDebuggerPlugin::_bind_methods() {205GDVIRTUAL_BIND(_setup_session, "session_id");206GDVIRTUAL_BIND(_has_capture, "capture");207GDVIRTUAL_BIND(_capture, "message", "data", "session_id");208GDVIRTUAL_BIND(_goto_script_line, "script", "line");209GDVIRTUAL_BIND(_breakpoints_cleared_in_tree);210GDVIRTUAL_BIND(_breakpoint_set_in_tree, "script", "line", "enabled");211ClassDB::bind_method(D_METHOD("get_session", "id"), &EditorDebuggerPlugin::get_session);212ClassDB::bind_method(D_METHOD("get_sessions"), &EditorDebuggerPlugin::get_sessions);213}214215EditorDebuggerPlugin::EditorDebuggerPlugin() {216EditorDebuggerNode::get_singleton()->connect("goto_script_line", callable_mp(this, &EditorDebuggerPlugin::goto_script_line));217EditorDebuggerNode::get_singleton()->connect("breakpoints_cleared_in_tree", callable_mp(this, &EditorDebuggerPlugin::breakpoints_cleared_in_tree));218EditorDebuggerNode::get_singleton()->connect("breakpoint_set_in_tree", callable_mp(this, &EditorDebuggerPlugin::breakpoint_set_in_tree));219}220221222