/*************************************************************************/ /* script_editor_debugger.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ /* */ /* 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_editor_debugger.h" #include "scene/gui/separator.h" #include "scene/gui/label.h" #include "scene/gui/split_container.h" #include "scene/gui/tree.h" #include "scene/gui/texture_button.h" #include "scene/gui/tab_container.h" #include "scene/gui/line_edit.h" #include "scene/gui/dialogs.h" #include "scene/gui/rich_text_label.h" #include "scene/gui/margin_container.h" #include "property_editor.h" #include "globals.h" #include "editor_node.h" #include "main/performance.h" #include "editor_profiler.h" #include "editor_settings.h" class ScriptEditorDebuggerVariables : public Object { OBJ_TYPE( ScriptEditorDebuggerVariables, Object ); List props; Map values; protected: bool _set(const StringName& p_name, const Variant& p_value) { return false; } bool _get(const StringName& p_name,Variant &r_ret) const { if (!values.has(p_name)) return false; r_ret=values[p_name]; return true; } void _get_property_list( List *p_list) const { for(const List::Element *E=props.front();E;E=E->next() ) p_list->push_back(E->get()); } public: void clear() { props.clear(); values.clear(); } String get_var_value(const String& p_var) const { for(Map::Element *E=values.front();E;E=E->next()) { String v = E->key().operator String().get_slice("/",1); if (v==p_var) return E->get(); } return ""; } void add_property(const String &p_name, const Variant& p_value) { PropertyInfo pinfo; pinfo.name=p_name; pinfo.type=p_value.get_type(); props.push_back(pinfo); values[p_name]=p_value; } void update() { _change_notify(); } ScriptEditorDebuggerVariables() { } }; void ScriptEditorDebugger::debug_next() { ERR_FAIL_COND(!breaked); ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("next"); ppeer->put_var(msg); stack_dump->clear(); inspector->edit(NULL); } void ScriptEditorDebugger::debug_step() { ERR_FAIL_COND(!breaked); ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("step"); ppeer->put_var(msg); stack_dump->clear(); inspector->edit(NULL); } void ScriptEditorDebugger::debug_break() { ERR_FAIL_COND(breaked); ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("break"); ppeer->put_var(msg); } void ScriptEditorDebugger::debug_continue() { ERR_FAIL_COND(!breaked); ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("continue"); ppeer->put_var(msg); } void ScriptEditorDebugger::_scene_tree_request() { ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("request_scene_tree"); ppeer->put_var(msg); } void ScriptEditorDebugger::_video_mem_request() { ERR_FAIL_COND(connection.is_null()); ERR_FAIL_COND(!connection->is_connected()); Array msg; msg.push_back("request_video_mem"); ppeer->put_var(msg); } Size2 ScriptEditorDebugger::get_minimum_size() const { Size2 ms = Control::get_minimum_size(); ms.y = MAX(ms.y , 250 ); return ms; } void ScriptEditorDebugger::_parse_message(const String& p_msg,const Array& p_data) { if (p_msg=="debug_enter") { Array msg; msg.push_back("get_stack_dump"); ppeer->put_var(msg); ERR_FAIL_COND(p_data.size()!=2); bool can_continue=p_data[0]; String error = p_data[1]; step->set_disabled(!can_continue); next->set_disabled(!can_continue); reason->set_text(error); reason->set_tooltip(error); breaked=true; dobreak->set_disabled(true); docontinue->set_disabled(false); emit_signal("breaked",true,can_continue); OS::get_singleton()->move_window_to_foreground(); if (!profiler->is_seeking()) tabs->set_current_tab(0); profiler->set_enabled(false); EditorNode::get_singleton()->get_pause_button()->set_pressed(true); EditorNode::get_singleton()->make_bottom_panel_item_visible(this); } else if (p_msg=="debug_exit") { breaked=false; step->set_disabled(true); next->set_disabled(true); reason->set_text(""); reason->set_tooltip(""); back->set_disabled(true); forward->set_disabled(true); dobreak->set_disabled(false); docontinue->set_disabled(true); emit_signal("breaked",false,false); //tabs->set_current_tab(0); profiler->set_enabled(true); profiler->disable_seeking(); EditorNode::get_singleton()->get_pause_button()->set_pressed(false); } else if (p_msg=="message:click_ctrl") { clicked_ctrl->set_text(p_data[0]); clicked_ctrl_type->set_text(p_data[1]); } else if (p_msg=="message:scene_tree") { scene_tree->clear(); Map lv; for(int i=0;icreate_item(p); it->set_text(0,p_data[i+1]); if (has_icon(p_data[i+2],"EditorIcons")) it->set_icon(0,get_icon(p_data[i+2],"EditorIcons")); lv[level]=it; } le_clear->set_disabled(false); le_set->set_disabled(false); } else if (p_msg=="message:video_mem") { vmem_tree->clear(); TreeItem* root=vmem_tree->create_item(); int total=0; for(int i=0;icreate_item(root); String type=p_data[i+1]; int bytes=p_data[i+3].operator int(); it->set_text(0,p_data[i+0]); //path it->set_text(1,type); //type it->set_text(2,p_data[i+2]); //type it->set_text(3,String::humanize_size(bytes)); //type total+=bytes; if (has_icon(type,"EditorIcons")) it->set_icon(0,get_icon(type,"EditorIcons")); } vmem_total->set_tooltip(TTR("Bytes:")+" "+itos(total)); vmem_total->set_text(String::humanize_size(total)); } else if (p_msg=="stack_dump") { stack_dump->clear(); TreeItem *r = stack_dump->create_item(); for(int i=0;icreate_item(r); d["frame"]=i; s->set_metadata(0,d); // String line = itos(i)+" - "+String(d["file"])+":"+itos(d["line"])+" - at func: "+d["function"]; String line = itos(i)+" - "+String(d["file"])+":"+itos(d["line"]); s->set_text(0,line); if (i==0) s->select(0); } } else if (p_msg=="stack_frame_vars") { variables->clear(); int ofs =0; int mcount = p_data[ofs]; ofs++; for(int i=0;iadd_property("members/"+n,v); } ofs+=mcount*2; mcount = p_data[ofs]; ofs++; for(int i=0;iadd_property("locals/"+n,v); } variables->update(); inspector->edit(variables); } else if (p_msg=="output") { //OUT for(int i=0;iis_hidden()) { log_forced_visible=true; if (EditorNode::get_singleton()->are_bottom_panels_hidden()) { EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log()); } } EditorNode::get_log()->add_message(t); } } else if (p_msg=="performance") { Array arr = p_data[0]; Vector p; p.resize(arr.size()); for(int i=0;iset_text(1,rtos(p[i])); if (p[i]>perf_max[i]) perf_max[i]=p[i]; } } perf_history.push_front(p); perf_draw->update(); } else if (p_msg=="error") { Array err = p_data[0]; Array vals; vals.push_back(err[0]); vals.push_back(err[1]); vals.push_back(err[2]); vals.push_back(err[3]); bool warning = err[9]; bool e; String time = String("%d:%02d:%02d:%04d").sprintf(vals,&e); String txt=time+" - "+String(err[8]); String tooltip=TTR("Type:")+String(warning?TTR("Warning"):TTR("Error")); tooltip+="\n"+TTR("Description:")+" "+String(err[8]); tooltip+="\n"+TTR("Time:")+" "+time; tooltip+="\nC "+TTR("Error:")+" "+String(err[7]); tooltip+="\nC "+TTR("Source:")+" "+String(err[5])+":"+String(err[6]); tooltip+="\nC "+TTR("Function:")+" "+String(err[4]); error_list->add_item(txt,EditorNode::get_singleton()->get_gui_base()->get_icon(warning?"Warning":"Error","EditorIcons")); error_list->set_item_tooltip( error_list->get_item_count() -1,tooltip ); int scc = p_data[1]; Array stack; stack.resize(scc); for(int i=0;iset_item_metadata( error_list->get_item_count() -1,stack ); error_count++; /* int count = p_data[1]; Array cstack; OutputError oe = errors.front()->get(); packet_peer_stream->put_var(oe.hr); packet_peer_stream->put_var(oe.min); packet_peer_stream->put_var(oe.sec); packet_peer_stream->put_var(oe.msec); packet_peer_stream->put_var(oe.source_func); packet_peer_stream->put_var(oe.source_file); packet_peer_stream->put_var(oe.source_line); packet_peer_stream->put_var(oe.error); packet_peer_stream->put_var(oe.error_descr); packet_peer_stream->put_var(oe.warning); packet_peer_stream->put_var(oe.callstack); */ } else if (p_msg=="profile_sig") { //cache a signature print_line("SIG: "+String(Variant(p_data))); profiler_signature[p_data[1]]=p_data[0]; } else if (p_msg=="profile_frame" || p_msg=="profile_total") { EditorProfiler::Metric metric; metric.valid=true; metric.frame_number=p_data[0]; metric.frame_time=p_data[1]; metric.idle_time=p_data[2]; metric.fixed_time=p_data[3]; metric.fixed_frame_time=p_data[4]; int frame_data_amount = p_data[6]; int frame_function_amount = p_data[7]; if (frame_data_amount) { EditorProfiler::Metric::Category frame_time; frame_time.signature="category_frame_time"; frame_time.name="Frame Time"; frame_time.total_time=metric.frame_time; EditorProfiler::Metric::Category::Item item; item.calls=1; item.line=0; item.name="Fixed Time"; item.total=metric.fixed_time; item.self=item.total; item.signature="fixed_time"; frame_time.items.push_back(item); item.name="Idle Time"; item.total=metric.idle_time; item.self=item.total; item.signature="idle_time"; frame_time.items.push_back(item); item.name="Fixed Frame Time"; item.total=metric.fixed_frame_time; item.self=item.total; item.signature="fixed_frame_time"; frame_time.items.push_back(item); metric.categories.push_back(frame_time); } int idx=8; for(int i=0;i strings = name.split("::"); if (strings.size()==3) { item.name=strings[2]; item.script=strings[0]; item.line=strings[1].to_int(); } } else { item.name="SigErr "+itos(signature); } item.calls=calls; item.self=self; item.total=total; funcs.items[i]=item; } metric.categories.push_back(funcs); if (p_msg=="profile_frame") profiler->add_frame_metric(metric,false); else profiler->add_frame_metric(metric,true); } else if (p_msg=="kill_me") { editor->call_deferred("stop_child_process"); } } void ScriptEditorDebugger::_performance_select(Object*,int,bool) { perf_draw->update(); } void ScriptEditorDebugger::_performance_draw() { Vector which; for(int i=0;iis_selected(0)) which.push_back(i); } if(which.empty()) return; Color graph_color=get_color("font_color","TextEdit"); Ref graph_sb = get_stylebox("normal","TextEdit"); Ref graph_font = get_font("font","TextEdit"); int cols = Math::ceil(Math::sqrt(which.size())); int rows = (which.size()+1)/cols; if (which.size()==1) rows=1; int margin =3; int point_sep=5; Size2i s = Size2i(perf_draw->get_size())/Size2i(cols,rows); for(int i=0;idraw_style_box(graph_sb,r); r.pos+=graph_sb->get_offset(); r.size-=graph_sb->get_minimum_size(); int pi=which[i]; Color c = Color(0.7,0.9,0.5); c.set_hsv(Math::fmod(c.get_h()+pi*0.7654,1),c.get_s(),c.get_v()); c.a=0.8; perf_draw->draw_string(graph_font,r.pos+Point2(0,graph_font->get_ascent()),perf_items[pi]->get_text(0),c,r.size.x); c.a=0.6; perf_draw->draw_string(graph_font,r.pos+Point2(graph_font->get_char_size('X').width,graph_font->get_ascent()+graph_font->get_height()),perf_items[pi]->get_text(1),c,r.size.y); float spacing=point_sep/float(cols); float from = r.size.width; List >::Element *E=perf_history.front(); float prev=-1; while(from>=0 && E) { float m = perf_max[pi]; if (m==0) m=0.00001; float h = E->get()[pi]/m; h=(1.0-h)*r.size.y; c.a=0.7; if (E!=perf_history.front()) perf_draw->draw_line(r.pos+Point2(from,h),r.pos+Point2(from+spacing,prev),c,2.0); prev=h; E=E->next(); from-=spacing; } } } void ScriptEditorDebugger::_notification(int p_what) { switch(p_what) { case NOTIFICATION_ENTER_TREE: { inspector->edit(variables); step->set_icon( get_icon("DebugStep","EditorIcons")); next->set_icon( get_icon("DebugNext","EditorIcons")); back->set_icon( get_icon("Back","EditorIcons")); forward->set_icon( get_icon("Forward","EditorIcons")); dobreak->set_icon( get_icon("Pause","EditorIcons")); docontinue->set_icon( get_icon("DebugContinue","EditorIcons")); scene_tree_refresh->set_icon( get_icon("Reload","EditorIcons")); le_set->connect("pressed",this,"_live_edit_set"); le_clear->connect("pressed",this,"_live_edit_clear"); error_list->connect("item_selected",this,"_error_selected"); error_stack->connect("item_selected",this,"_error_stack_selected"); vmem_refresh->set_icon( get_icon("Reload","EditorIcons")); } break; case NOTIFICATION_PROCESS: { if (error_count!=last_error_count) { if (error_count==0) { error_split->set_name(TTR("Errors")); debugger_button->set_text(TTR("Debugger")); debugger_button->set_icon(Ref()); tabs->set_tab_icon(error_split->get_index(),Ref()); } else { error_split->set_name(TTR("Errors")+" ("+itos(error_count)+")"); debugger_button->set_text(TTR("Debugger")+" ("+itos(error_count)+")"); debugger_button->set_icon(get_icon("Error","EditorIcons")); tabs->set_tab_icon(error_split->get_index(),get_icon("Error","EditorIcons")); } last_error_count=error_count; } if (connection.is_null()) { if (server->is_connection_available()) { connection = server->take_connection(); if (connection.is_null()) break; EditorNode::get_log()->add_message("** Debug Process Started **"); log_forced_visible=false; ppeer->set_stream_peer(connection); //EditorNode::get_singleton()->make_bottom_panel_item_visible(this); //emit_signal("show_debugger",true); dobreak->set_disabled(false); tabs->set_current_tab(0); reason->set_text(TTR("Child Process Connected")); reason->set_tooltip(TTR("Child Process Connected")); profiler->clear(); scene_tree->clear(); le_set->set_disabled(true); le_clear->set_disabled(false); error_list->clear(); error_stack->clear(); error_count=0; profiler_signature.clear(); //live_edit_root->set_text("/root"); EditorNode::get_singleton()->get_pause_button()->set_pressed(false); EditorNode::get_singleton()->get_pause_button()->set_disabled(false); update_live_edit_root(); if (profiler->is_profiling()) { _profiler_activate(true); } } else { break; } }; if (!connection->is_connected()) { stop(); editor->notify_child_process_exited(); //somehow, exited break; }; if (ppeer->get_available_packet_count() <= 0) { break; }; while(ppeer->get_available_packet_count() > 0) { if (pending_in_queue) { int todo = MIN( ppeer->get_available_packet_count(), pending_in_queue ); for(int i=0;iget_var(cmd); if (ret!=OK) { stop(); ERR_FAIL_COND(ret!=OK); } message.push_back(cmd); pending_in_queue--; } if (pending_in_queue==0) { _parse_message(message_type,message); message.clear(); } } else { if (ppeer->get_available_packet_count()>=2) { Variant cmd; Error ret = ppeer->get_var(cmd); if (ret!=OK) { stop(); ERR_FAIL_COND(ret!=OK); } if (cmd.get_type()!=Variant::STRING) { stop(); ERR_FAIL_COND(cmd.get_type()!=Variant::STRING); } message_type=cmd; //print_line("GOT: "+message_type); ret = ppeer->get_var(cmd); if (ret!=OK) { stop(); ERR_FAIL_COND(ret!=OK); } if (cmd.get_type()!=Variant::INT) { stop(); ERR_FAIL_COND(cmd.get_type()!=Variant::INT); } pending_in_queue=cmd; if (pending_in_queue==0) { _parse_message(message_type,Array()); message.clear(); } } else { break; } } } } break; } } void ScriptEditorDebugger::start() { stop(); uint16_t port = GLOBAL_DEF("debug/remote_port",6007); perf_history.clear(); for(int i=0;ilisten(port); set_process(true); } void ScriptEditorDebugger::pause(){ } void ScriptEditorDebugger::unpause(){ } void ScriptEditorDebugger::stop(){ set_process(false); server->stop(); ppeer->set_stream_peer(Ref()); if (connection.is_valid()) { EditorNode::get_log()->add_message("** Debug Process Stopped **"); connection.unref(); } pending_in_queue=0; message.clear(); if (log_forced_visible) { //EditorNode::get_singleton()->make_bottom_panel_item_visible(this); if (EditorNode::get_log()->is_visible()) EditorNode::get_singleton()->hide_bottom_panel(); log_forced_visible=false; } node_path_cache.clear(); res_path_cache.clear(); profiler_signature.clear(); le_clear->set_disabled(false); le_set->set_disabled(true); profiler->set_enabled(true); EditorNode::get_singleton()->get_pause_button()->set_pressed(false); EditorNode::get_singleton()->get_pause_button()->set_disabled(true); if (hide_on_stop) { if (is_visible()) EditorNode::get_singleton()->hide_bottom_panel(); emit_signal("show_debugger",false); } } void ScriptEditorDebugger::_profiler_activate(bool p_enable) { if (!connection.is_valid()) return; if (p_enable) { profiler_signature.clear(); Array msg; msg.push_back("start_profiling"); int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions"); max_funcs = CLAMP(max_funcs,16,512); msg.push_back(max_funcs); ppeer->put_var(msg); print_line("BEGIN PROFILING!"); } else { Array msg; msg.push_back("stop_profiling"); ppeer->put_var(msg); print_line("END PROFILING!"); } } void ScriptEditorDebugger::_profiler_seeked() { if (!connection.is_valid() || !connection->is_connected()) return; if (breaked) return; debug_break();; } void ScriptEditorDebugger::_stack_dump_frame_selected() { TreeItem *ti = stack_dump->get_selected(); if (!ti) return; Dictionary d = ti->get_metadata(0); Ref