/*************************************************************************/ /* script_debugger_remote.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "script_debugger_remote.h" #include "core/engine.h" #include "core/io/ip.h" #include "core/io/marshalls.h" #include "core/os/input.h" #include "core/os/os.h" #include "core/project_settings.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" #include "scene/main/viewport.h" #include "scene/resources/packed_scene.h" #include "servers/visual_server.h" void ScriptDebuggerRemote::_send_video_memory() { List usage; if (resource_usage_func) resource_usage_func(&usage); usage.sort(); packet_peer_stream->put_var("message:video_mem"); packet_peer_stream->put_var(usage.size() * 4); for (List::Element *E = usage.front(); E; E = E->next()) { packet_peer_stream->put_var(E->get().path); packet_peer_stream->put_var(E->get().type); packet_peer_stream->put_var(E->get().format); packet_peer_stream->put_var(E->get().vram); } } Error ScriptDebuggerRemote::connect_to_host(const String &p_host, uint16_t p_port) { IP_Address ip; if (p_host.is_valid_ip_address()) ip = p_host; else ip = IP::get_singleton()->resolve_hostname(p_host); int port = p_port; const int tries = 6; int waits[tries] = { 1, 10, 100, 1000, 1000, 1000 }; tcp_client->connect_to_host(ip, port); for (int i = 0; i < tries; i++) { if (tcp_client->get_status() == StreamPeerTCP::STATUS_CONNECTED) { print_verbose("Remote Debugger: Connected!"); break; } else { const int ms = waits[i]; OS::get_singleton()->delay_usec(ms * 1000); print_verbose("Remote Debugger: Connection failed with status: '" + String::num(tcp_client->get_status()) + "', retrying in " + String::num(ms) + " msec."); }; }; if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) { ERR_PRINTS("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + "."); return FAILED; }; packet_peer_stream->set_stream_peer(tcp_client); return OK; } void ScriptDebuggerRemote::_put_variable(const String &p_name, const Variant &p_variable) { packet_peer_stream->put_var(p_name); Variant var = p_variable; if (p_variable.get_type() == Variant::OBJECT && !ObjectDB::instance_validate(p_variable)) { var = Variant(); } int len = 0; Error err = encode_variant(var, NULL, len, true); if (err != OK) ERR_PRINT("Failed to encode variant."); if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size packet_peer_stream->put_var(Variant()); } else { packet_peer_stream->put_var(var); } } void ScriptDebuggerRemote::_save_node(ObjectID id, const String &p_path) { Node *node = Object::cast_to(ObjectDB::get_instance(id)); ERR_FAIL_COND(!node); Ref ps = memnew(PackedScene); ps->pack(node); ResourceSaver::save(p_path, ps); } void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue, bool p_is_error_breakpoint) { //this function is called when there is a debugger break (bug on script) //or when execution is paused from editor if (skip_breakpoints && !p_is_error_breakpoint) return; ERR_FAIL_COND_MSG(!tcp_client->is_connected_to_host(), "Script Debugger failed to connect, but being used anyway."); if (allow_focus_steal_pid) { OS::get_singleton()->enable_for_stealing_focus(allow_focus_steal_pid); } packet_peer_stream->put_var("debug_enter"); packet_peer_stream->put_var(2); packet_peer_stream->put_var(p_can_continue); packet_peer_stream->put_var(p_script->debug_get_error()); skip_profile_frame = true; // to avoid super long frame time for the frame Input::MouseMode mouse_mode = Input::get_singleton()->get_mouse_mode(); if (mouse_mode != Input::MOUSE_MODE_VISIBLE) Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); uint64_t loop_begin_usec = 0; uint64_t loop_time_sec = 0; while (true) { loop_begin_usec = OS::get_singleton()->get_ticks_usec(); _get_output(); if (packet_peer_stream->get_available_packet_count() > 0) { Variant var; Error err = packet_peer_stream->get_var(var); ERR_CONTINUE(err != OK); ERR_CONTINUE(var.get_type() != Variant::ARRAY); Array cmd = var; ERR_CONTINUE(cmd.size() == 0); ERR_CONTINUE(cmd[0].get_type() != Variant::STRING); String command = cmd[0]; if (command == "get_stack_dump") { packet_peer_stream->put_var("stack_dump"); int slc = p_script->debug_get_stack_level_count(); packet_peer_stream->put_var(slc); for (int i = 0; i < slc; i++) { Dictionary d; d["file"] = p_script->debug_get_stack_level_source(i); d["line"] = p_script->debug_get_stack_level_line(i); d["function"] = p_script->debug_get_stack_level_function(i); //d["id"]=p_script->debug_get_stack_level_ d["id"] = 0; packet_peer_stream->put_var(d); } } else if (command == "get_stack_frame_vars") { cmd.remove(0); ERR_CONTINUE(cmd.size() != 1); int lv = cmd[0]; List members; List member_vals; if (ScriptInstance *inst = p_script->debug_get_stack_level_instance(lv)) { members.push_back("self"); member_vals.push_back(inst->get_owner()); } p_script->debug_get_stack_level_members(lv, &members, &member_vals); ERR_CONTINUE(members.size() != member_vals.size()); List locals; List local_vals; p_script->debug_get_stack_level_locals(lv, &locals, &local_vals); ERR_CONTINUE(locals.size() != local_vals.size()); List globals; List globals_vals; p_script->debug_get_globals(&globals, &globals_vals); ERR_CONTINUE(globals.size() != globals_vals.size()); packet_peer_stream->put_var("stack_frame_vars"); packet_peer_stream->put_var(3 + (locals.size() + members.size() + globals.size()) * 2); { //locals packet_peer_stream->put_var(locals.size()); List::Element *E = locals.front(); List::Element *F = local_vals.front(); while (E) { _put_variable(E->get(), F->get()); E = E->next(); F = F->next(); } } { //members packet_peer_stream->put_var(members.size()); List::Element *E = members.front(); List::Element *F = member_vals.front(); while (E) { _put_variable(E->get(), F->get()); E = E->next(); F = F->next(); } } { //globals packet_peer_stream->put_var(globals.size()); List::Element *E = globals.front(); List::Element *F = globals_vals.front(); while (E) { _put_variable(E->get(), F->get()); E = E->next(); F = F->next(); } } } else if (command == "step") { set_depth(-1); set_lines_left(1); break; } else if (command == "next") { set_depth(0); set_lines_left(1); break; } else if (command == "continue") { set_depth(-1); set_lines_left(-1); OS::get_singleton()->move_window_to_foreground(); break; } else if (command == "break") { ERR_PRINT("Got break when already broke!"); break; } else if (command == "request_scene_tree") { #ifdef DEBUG_ENABLED if (scene_tree) scene_tree->_debugger_request_tree(); #endif } else if (command == "request_video_mem") { _send_video_memory(); } else if (command == "inspect_object") { ObjectID id = cmd[1]; _send_object_id(id); } else if (command == "set_object_property") { _set_object_property(cmd[1], cmd[2], cmd[3]); } else if (command == "override_camera_2D:set") { bool enforce = cmd[1]; if (scene_tree) { scene_tree->get_root()->enable_canvas_transform_override(enforce); } } else if (command == "override_camera_2D:transform") { Transform2D transform = cmd[1]; if (scene_tree) { scene_tree->get_root()->set_canvas_transform_override(transform); } } else if (command == "override_camera_3D:set") { bool enable = cmd[1]; if (scene_tree) { scene_tree->get_root()->enable_camera_override(enable); } } else if (command == "override_camera_3D:transform") { Transform transform = cmd[1]; bool is_perspective = cmd[2]; float size_or_fov = cmd[3]; float near = cmd[4]; float far = cmd[5]; if (scene_tree) { if (is_perspective) { scene_tree->get_root()->set_camera_override_perspective(size_or_fov, near, far); } else { scene_tree->get_root()->set_camera_override_orthogonal(size_or_fov, near, far); } scene_tree->get_root()->set_camera_override_transform(transform); } } else if (command == "reload_scripts") { reload_all_scripts = true; } else if (command == "breakpoint") { bool set = cmd[3]; if (set) insert_breakpoint(cmd[2], cmd[1]); else remove_breakpoint(cmd[2], cmd[1]); } else if (command == "save_node") { _save_node(cmd[1], cmd[2]); } else if (command == "set_skip_breakpoints") { skip_breakpoints = cmd[1]; } else { _parse_live_edit(cmd); } } else { OS::get_singleton()->delay_usec(10000); OS::get_singleton()->process_and_drop_events(); } // This is for the camera override to stay live even when the game is paused from the editor loop_time_sec = (OS::get_singleton()->get_ticks_usec() - loop_begin_usec) / 1000000.0f; VisualServer::get_singleton()->sync(); if (VisualServer::get_singleton()->has_changed()) { VisualServer::get_singleton()->draw(true, loop_time_sec * Engine::get_singleton()->get_time_scale()); } } packet_peer_stream->put_var("debug_exit"); packet_peer_stream->put_var(0); if (mouse_mode != Input::MOUSE_MODE_VISIBLE) Input::get_singleton()->set_mouse_mode(mouse_mode); } void ScriptDebuggerRemote::_get_output() { mutex->lock(); if (output_strings.size()) { locking = true; packet_peer_stream->put_var("output"); packet_peer_stream->put_var(output_strings.size()); while (output_strings.size()) { const OutputString &output_string = output_strings.front()->get(); Array msg_data; msg_data.push_back(output_string.message); msg_data.push_back(output_string.type); packet_peer_stream->put_var(msg_data); output_strings.pop_front(); } locking = false; } if (n_messages_dropped > 0) { Message msg; msg.message = "Too many messages! " + String::num_int64(n_messages_dropped) + " messages were dropped."; messages.push_back(msg); n_messages_dropped = 0; } while (messages.size()) { locking = true; packet_peer_stream->put_var("message:" + messages.front()->get().message); packet_peer_stream->put_var(messages.front()->get().data.size()); for (int i = 0; i < messages.front()->get().data.size(); i++) { packet_peer_stream->put_var(messages.front()->get().data[i]); } messages.pop_front(); locking = false; } if (n_errors_dropped == 1) { // Only print one message about dropping per second OutputError oe; oe.error = "TOO_MANY_ERRORS"; oe.error_descr = "Too many errors! Ignoring errors for up to 1 second."; oe.warning = false; uint64_t time = OS::get_singleton()->get_ticks_msec(); oe.hr = time / 3600000; oe.min = (time / 60000) % 60; oe.sec = (time / 1000) % 60; oe.msec = time % 1000; errors.push_back(oe); } if (n_warnings_dropped == 1) { // Only print one message about dropping per second OutputError oe; oe.error = "TOO_MANY_WARNINGS"; oe.error_descr = "Too many warnings! Ignoring warnings for up to 1 second."; oe.warning = true; uint64_t time = OS::get_singleton()->get_ticks_msec(); oe.hr = time / 3600000; oe.min = (time / 60000) % 60; oe.sec = (time / 1000) % 60; oe.msec = time % 1000; errors.push_back(oe); } while (errors.size()) { locking = true; packet_peer_stream->put_var("error"); OutputError oe = errors.front()->get(); packet_peer_stream->put_var(oe.callstack.size() + 2); Array error_data; error_data.push_back(oe.hr); error_data.push_back(oe.min); error_data.push_back(oe.sec); error_data.push_back(oe.msec); error_data.push_back(oe.source_func); error_data.push_back(oe.source_file); error_data.push_back(oe.source_line); error_data.push_back(oe.error); error_data.push_back(oe.error_descr); error_data.push_back(oe.warning); packet_peer_stream->put_var(error_data); packet_peer_stream->put_var(oe.callstack.size()); for (int i = 0; i < oe.callstack.size(); i++) { packet_peer_stream->put_var(oe.callstack[i]); } errors.pop_front(); locking = false; } mutex->unlock(); } void ScriptDebuggerRemote::line_poll() { //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 if (poll_every % 2048 == 0) _poll_events(); poll_every++; } void ScriptDebuggerRemote::_err_handler(void *ud, const char *p_func, const char *p_file, int p_line, const char *p_err, const char *p_descr, ErrorHandlerType p_type) { if (p_type == ERR_HANDLER_SCRIPT) return; //ignore script errors, those go through debugger Vector si; for (int i = 0; i < ScriptServer::get_language_count(); i++) { si = ScriptServer::get_language(i)->debug_get_current_stack_info(); if (si.size()) break; } ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud; sdr->send_error(p_func, p_file, p_line, p_err, p_descr, p_type, si); } bool ScriptDebuggerRemote::_parse_live_edit(const Array &p_command) { #ifdef DEBUG_ENABLED String cmdstr = p_command[0]; if (!scene_tree || !cmdstr.begins_with("live_")) return false; if (cmdstr == "live_set_root") { scene_tree->_live_edit_root_func(p_command[1], p_command[2]); } else if (cmdstr == "live_node_path") { scene_tree->_live_edit_node_path_func(p_command[1], p_command[2]); } else if (cmdstr == "live_res_path") { scene_tree->_live_edit_res_path_func(p_command[1], p_command[2]); } else if (cmdstr == "live_node_prop_res") { scene_tree->_live_edit_node_set_res_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_node_prop") { scene_tree->_live_edit_node_set_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_res_prop_res") { scene_tree->_live_edit_res_set_res_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_res_prop") { scene_tree->_live_edit_res_set_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_node_call") { scene_tree->_live_edit_node_call_func(p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[7]); } else if (cmdstr == "live_res_call") { scene_tree->_live_edit_res_call_func(p_command[1], p_command[2], p_command[3], p_command[4], p_command[5], p_command[6], p_command[7]); } else if (cmdstr == "live_create_node") { scene_tree->_live_edit_create_node_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_instance_node") { scene_tree->_live_edit_instance_node_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_remove_node") { scene_tree->_live_edit_remove_node_func(p_command[1]); } else if (cmdstr == "live_remove_and_keep_node") { scene_tree->_live_edit_remove_and_keep_node_func(p_command[1], p_command[2]); } else if (cmdstr == "live_restore_node") { scene_tree->_live_edit_restore_node_func(p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_duplicate_node") { scene_tree->_live_edit_duplicate_node_func(p_command[1], p_command[2]); } else if (cmdstr == "live_reparent_node") { scene_tree->_live_edit_reparent_node_func(p_command[1], p_command[2], p_command[3], p_command[4]); } else { return false; } return true; #else return false; #endif } void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) { Object *obj = ObjectDB::get_instance(p_id); if (!obj) return; typedef Pair PropertyDesc; List properties; if (ScriptInstance *si = obj->get_script_instance()) { if (!si->get_script().is_null()) { typedef Map > ScriptMemberMap; typedef Map > ScriptConstantsMap; ScriptMemberMap members; members[si->get_script().ptr()] = Set(); si->get_script()->get_members(&(members[si->get_script().ptr()])); ScriptConstantsMap constants; constants[si->get_script().ptr()] = Map(); si->get_script()->get_constants(&(constants[si->get_script().ptr()])); Ref