/*************************************************************************/ /* script_debugger_remote.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2018 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 "engine.h" #include "io/ip.h" #include "io/marshalls.h" #include "os/input.h" #include "os/os.h" #include "project_settings.h" #include "scene/main/node.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) { break; } else { const int ms = waits[i]; OS::get_singleton()->delay_usec(ms * 1000); print_line("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) { print_line("Remote Debugger: Unable to connect"); return FAILED; }; // print_line("Remote Debugger: Connection OK!"); packet_peer_stream->set_stream_peer(tcp_client); return OK; } static int _ScriptDebuggerRemote_found_id = 0; static Object *_ScriptDebuggerRemote_find = NULL; static void _ScriptDebuggerRemote_debug_func(Object *p_obj) { if (_ScriptDebuggerRemote_find == p_obj) { _ScriptDebuggerRemote_found_id = p_obj->get_instance_id(); } } static ObjectID safe_get_instance_id(const Variant &p_v) { Object *o = p_v; if (o == NULL) return 0; else { REF r = p_v; if (r.is_valid()) { return r->get_instance_id(); } else { _ScriptDebuggerRemote_found_id = 0; _ScriptDebuggerRemote_find = NULL; ObjectDB::debug_objects(_ScriptDebuggerRemote_debug_func); return _ScriptDebuggerRemote_found_id; } } } 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); 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::debug(ScriptLanguage *p_script, bool p_can_continue) { //this function is called when there is a debugger break (bug on script) //or when execution is paused from editor if (!tcp_client->is_connected_to_host()) { ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway."); ERR_FAIL(); } 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); while (true) { _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") { if (request_scene_tree) request_scene_tree(request_scene_tree_ud); } 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 == "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 { _parse_live_edit(cmd); } } else { OS::get_singleton()->delay_usec(10000); } } 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()) { packet_peer_stream->put_var(output_strings.front()->get()); output_strings.pop_front(); } locking = false; } 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; } 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 ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote *)ud; OutputError oe; oe.error = p_err; oe.error_descr = p_descr; oe.source_file = p_file; oe.source_line = p_line; oe.source_func = p_func; oe.warning = p_type == ERR_HANDLER_WARNING; 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; Array cstack; 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; } cstack.resize(si.size() * 2); for (int i = 0; i < si.size(); i++) { String path; int line = 0; if (si[i].script.is_valid()) { path = si[i].script->get_path(); line = si[i].line; } cstack[i * 2 + 0] = path; cstack[i * 2 + 1] = line; } oe.callstack = cstack; sdr->mutex->lock(); if (!sdr->locking && sdr->tcp_client->is_connected_to_host()) { sdr->errors.push_back(oe); } sdr->mutex->unlock(); } bool ScriptDebuggerRemote::_parse_live_edit(const Array &p_command) { String cmdstr = p_command[0]; if (!live_edit_funcs || !cmdstr.begins_with("live_")) return false; //print_line(Variant(cmd).get_construct_string()); if (cmdstr == "live_set_root") { if (!live_edit_funcs->root_func) return true; //print_line("root: "+Variant(cmd).get_construct_string()); live_edit_funcs->root_func(live_edit_funcs->udata, p_command[1], p_command[2]); } else if (cmdstr == "live_node_path") { if (!live_edit_funcs->node_path_func) return true; //print_line("path: "+Variant(cmd).get_construct_string()); live_edit_funcs->node_path_func(live_edit_funcs->udata, p_command[1], p_command[2]); } else if (cmdstr == "live_res_path") { if (!live_edit_funcs->res_path_func) return true; live_edit_funcs->res_path_func(live_edit_funcs->udata, p_command[1], p_command[2]); } else if (cmdstr == "live_node_prop_res") { if (!live_edit_funcs->node_set_res_func) return true; live_edit_funcs->node_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_node_prop") { if (!live_edit_funcs->node_set_func) return true; live_edit_funcs->node_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_res_prop_res") { if (!live_edit_funcs->res_set_res_func) return true; live_edit_funcs->res_set_res_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_res_prop") { if (!live_edit_funcs->res_set_func) return true; live_edit_funcs->res_set_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_node_call") { if (!live_edit_funcs->node_call_func) return true; live_edit_funcs->node_call_func(live_edit_funcs->udata, 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") { if (!live_edit_funcs->res_call_func) return true; live_edit_funcs->res_call_func(live_edit_funcs->udata, 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") { live_edit_funcs->tree_create_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_instance_node") { live_edit_funcs->tree_instance_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_remove_node") { live_edit_funcs->tree_remove_node_func(live_edit_funcs->udata, p_command[1]); } else if (cmdstr == "live_remove_and_keep_node") { live_edit_funcs->tree_remove_and_keep_node_func(live_edit_funcs->udata, p_command[1], p_command[2]); } else if (cmdstr == "live_restore_node") { live_edit_funcs->tree_restore_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3]); } else if (cmdstr == "live_duplicate_node") { live_edit_funcs->tree_duplicate_node_func(live_edit_funcs->udata, p_command[1], p_command[2]); } else if (cmdstr == "live_reparent_node") { live_edit_funcs->tree_reparent_node_func(live_edit_funcs->udata, p_command[1], p_command[2], p_command[3], p_command[4]); } else { return false; } return true; } 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()) { Set members; si->get_script()->get_members(&members); for (Set::Element *E = members.front(); E; E = E->next()) { Variant m; if (si->get(E->get(), m)) { PropertyInfo pi(m.get_type(), String("Members/") + E->get()); properties.push_back(PropertyDesc(pi, m)); } } Map constants; si->get_script()->get_constants(&constants); for (Map::Element *E = constants.front(); E; E = E->next()) { PropertyInfo pi(E->value().get_type(), (String("Constants/") + E->key())); properties.push_back(PropertyDesc(pi, E->value())); } } } if (Node *node = Object::cast_to(obj)) { PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); properties.push_front(PropertyDesc(pi, node->get_path())); } else if (Resource *res = Object::cast_to(obj)) { if (Script *s = Object::cast_to