2
0
Эх сурвалжийг харах

error debugger

shows the list of errors that happened during running the game, traces
can be analyzed
Juan Linietsky 10 жил өмнө
parent
commit
d1da2c2995

+ 121 - 2
core/script_debugger_remote.cpp

@@ -289,6 +289,37 @@ void ScriptDebuggerRemote::_get_output() {
 		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();
 }
 
@@ -303,6 +334,61 @@ void ScriptDebuggerRemote::line_poll() {
 }
 
 
+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<ScriptLanguage::StackInfo> 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()) {
+
+		sdr->errors.push_back(oe);
+	}
+
+	sdr->mutex->unlock();
+}
+
+
 bool ScriptDebuggerRemote::_parse_live_edit(const Array& cmd) {
 
 	String cmdstr = cmd[0];
@@ -497,10 +583,35 @@ void ScriptDebuggerRemote::_print_handler(void *p_this,const String& p_string) {
 
 	ScriptDebuggerRemote *sdr = (ScriptDebuggerRemote*)p_this;
 
+	uint64_t ticks = OS::get_singleton()->get_ticks_usec()/1000;
+	sdr->msec_count+=ticks-sdr->last_msec;
+	sdr->last_msec=ticks;
+
+	if (sdr->msec_count>1000) {
+		sdr->char_count=0;
+		sdr->msec_count=0;
+	}
+
+	String s = p_string;
+	int allowed_chars = MIN(MAX(sdr->max_cps - sdr->char_count,0), s.length());
+
+	if (allowed_chars==0)
+		return;
+
+	if (allowed_chars<s.length()) {
+		s=s.substr(0,allowed_chars);
+	}
+
+	sdr->char_count+=allowed_chars;
+
+	if (sdr->char_count>=sdr->max_cps) {
+		s+="\n[output overflow, print less text!]\n";
+	}
+
 	sdr->mutex->lock();
 	if (!sdr->locking && sdr->tcp_client->is_connected()) {
 
-		sdr->output_strings .push_back(p_string);
+		sdr->output_strings.push_back(s);
 	}
 	sdr->mutex->unlock();
 }
@@ -538,13 +649,21 @@ ScriptDebuggerRemote::ScriptDebuggerRemote() {
 	poll_every=0;
 	request_scene_tree=NULL;
 	live_edit_funcs=NULL;
+	max_cps = GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048);
+	char_count=0;
+	msec_count=0;
+	last_msec=0;
+
+	eh.errfunc=_err_handler;
+	eh.userdata=this;
+	add_error_handler(&eh);
 
 }
 
 ScriptDebuggerRemote::~ScriptDebuggerRemote() {
 
 	remove_print_handler(&phl);
+	remove_error_handler(&eh);
 	memdelete(mutex);
 
-
 }

+ 27 - 0
core/script_debugger_remote.h

@@ -49,8 +49,31 @@ class ScriptDebuggerRemote : public ScriptDebugger {
 	Object *performance;
 	bool requested_quit;
 	Mutex *mutex;
+
+	struct OutputError {
+
+		int hr;
+		int min;
+		int sec;
+		int msec;
+		String source_file;
+		String source_func;
+		int source_line;
+		String error;
+		String error_descr;
+		bool warning;
+		Array callstack;
+
+	};
+
 	List<String> output_strings;
 	List<Message> messages;
+	List<OutputError> errors;
+
+	int max_cps;
+	int char_count;
+	uint64_t last_msec;
+	uint64_t msec_count;
 
 	bool locking; //hack to avoid a deadloop
 	static void _print_handler(void *p_this,const String& p_string);
@@ -69,6 +92,10 @@ class ScriptDebuggerRemote : public ScriptDebugger {
 
 	LiveEditFuncs *live_edit_funcs;
 
+	ErrorHandlerList eh;
+	static void _err_handler(void*,const char*,const char*,int p_line,const char *, const char *,ErrorHandlerType p_type);
+
+
 public:
 
 	Error connect_to_host(const String& p_host,uint16_t p_port);

+ 7 - 0
core/script_language.h

@@ -176,6 +176,13 @@ public:
 	virtual void debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems=-1,int p_max_depth=-1)=0;
 	virtual String debug_parse_stack_level_expression(int p_level,const String& p_expression,int p_max_subitems=-1,int p_max_depth=-1)=0;
 
+	struct StackInfo {
+		Ref<Script> script;
+		int line;
+	};
+
+	virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); }
+
 	/* LOADER FUNCTIONS */
 
 	virtual void get_recognized_extensions(List<String> *p_extensions) const=0;

+ 1 - 1
main/main.cpp

@@ -518,7 +518,7 @@ Error Main::setup(const char *execpath,int argc, char *argv[],bool p_second_phas
 	}
 
 
-
+	GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048);
 	if (debug_mode == "remote") {
 
 		ScriptDebuggerRemote *sdr = memnew( ScriptDebuggerRemote );

+ 13 - 0
modules/gdscript/gd_script.h

@@ -475,6 +475,19 @@ public:
     }
 
 
+	virtual Vector<StackInfo> debug_get_current_stack_info() {
+	    if (Thread::get_main_ID()!=Thread::get_caller_ID())
+		return Vector<StackInfo>();
+
+		Vector<StackInfo> csi;
+		csi.resize(_debug_call_stack_pos);
+		for(int i=0;i<_debug_call_stack_pos;i++) {
+			csi[_debug_call_stack_pos-i-1].line=_call_stack[i].line?*_call_stack[i].line:0;
+			csi[_debug_call_stack_pos-i-1].script=Ref<GDScript>(_call_stack[i].function->get_script());
+		}
+		return csi;
+	}
+
 	struct {
 
 		StringName _init;

+ 126 - 0
tools/editor/script_editor_debugger.cpp

@@ -344,6 +344,63 @@ void ScriptEditorDebugger::_parse_message(const String& p_msg,const Array& p_dat
 		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="Type:"+String(warning?"Warning":"Error");
+		tooltip+="\nDescription: "+String(err[8]);
+		tooltip+="\nTime: "+time;
+		tooltip+="\nC Error: "+String(err[7]);
+		tooltip+="\nC Source: "+String(err[5])+":"+String(err[6]);
+		tooltip+="\nC 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;i<scc;i++) {
+			stack[i]=p_data[2+i];
+		}
+
+		error_list->set_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=="kill_me") {
 
 		editor->call_deferred("stop_child_process");
@@ -447,10 +504,21 @@ void ScriptEditorDebugger::_notification(int p_what) {
 			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");
 
 		} break;
 		case NOTIFICATION_PROCESS: {
 
+			if (error_count!=last_error_count) {
+
+				if (error_count==0) {
+					error_split->set_name("Errors");
+				} else {
+					error_split->set_name("Errors ("+itos(error_count)+")");
+				}
+				last_error_count=error_count;
+			}
 			if (connection.is_null()) {
 
 				if (server->is_connection_available()) {
@@ -475,6 +543,9 @@ void ScriptEditorDebugger::_notification(int p_what) {
 					scene_tree->clear();
 					le_set->set_disabled(true);
 					le_clear->set_disabled(false);
+					error_list->clear();
+					error_stack->clear();
+					error_count=0;
 					//live_edit_root->set_text("/root");
 
 					update_live_edit_root();
@@ -1007,6 +1078,38 @@ void ScriptEditorDebugger::live_debug_reparent_node(const NodePath& p_at, const
 
 }
 
+void ScriptEditorDebugger::_error_selected(int p_idx) {
+
+	error_stack->clear();
+	Array st=error_list->get_item_metadata(p_idx);
+	for(int i=0;i<st.size();i+=2) {
+
+		String script=st[i];
+		int line=st[i+1];
+		Array md;
+		md.push_back(st[i]);
+		md.push_back(st[i+1]);
+
+		String str = script.get_file()+":"+itos(line);
+
+		error_stack->add_item(str);
+		error_stack->set_item_metadata(error_stack->get_item_count()-1,md);
+		error_stack->set_item_tooltip(error_stack->get_item_count()-1,"File: "+String(st[i])+"\nLine: "+itos(line));
+	}
+}
+
+void ScriptEditorDebugger:: _error_stack_selected(int p_idx){
+
+	Array arr = error_stack->get_item_metadata(p_idx);
+	if (arr.size()!=2)
+		return;
+
+
+	Ref<Script> s = ResourceLoader::load(arr[0]);
+	emit_signal("goto_script_line",s,int(arr[1])-1);
+
+}
+
 
 void ScriptEditorDebugger::_bind_methods() {
 
@@ -1023,6 +1126,9 @@ void ScriptEditorDebugger::_bind_methods() {
 	ObjectTypeDB::bind_method(_MD("_live_edit_set"),&ScriptEditorDebugger::_live_edit_set);
 	ObjectTypeDB::bind_method(_MD("_live_edit_clear"),&ScriptEditorDebugger::_live_edit_clear);
 
+	ObjectTypeDB::bind_method(_MD("_error_selected"),&ScriptEditorDebugger::_error_selected);
+	ObjectTypeDB::bind_method(_MD("_error_stack_selected"),&ScriptEditorDebugger::_error_stack_selected);
+
 	ObjectTypeDB::bind_method(_MD("live_debug_create_node"),&ScriptEditorDebugger::live_debug_create_node);
 	ObjectTypeDB::bind_method(_MD("live_debug_instance_node"),&ScriptEditorDebugger::live_debug_instance_node);
 	ObjectTypeDB::bind_method(_MD("live_debug_remove_node"),&ScriptEditorDebugger::live_debug_remove_node);
@@ -1143,6 +1249,23 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor){
 	vbc->add_child(hbc);
 
 
+	error_split = memnew( HSplitContainer );
+	VBoxContainer *errvb = memnew( VBoxContainer );
+	errvb->set_h_size_flags(SIZE_EXPAND_FILL);
+	error_list = memnew( ItemList );
+	errvb->add_margin_child("Errors:",error_list,true);
+	error_split->add_child(errvb);
+
+	errvb = memnew( VBoxContainer );
+	errvb->set_h_size_flags(SIZE_EXPAND_FILL);
+	error_stack = memnew( ItemList );
+	errvb->add_margin_child("Stack Trace (if applies):",error_stack,true);
+	error_split->add_child(errvb);
+
+	error_split->set_name("Errors");
+	tabs->add_child(error_split);
+
+
 	HSplitContainer *hsp = memnew( HSplitContainer );
 
 	perf_monitors = memnew(Tree);
@@ -1246,6 +1369,9 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor){
 	p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds,this);
 	live_debug=false;
 	last_path_id=false;
+	error_count=0;
+	last_error_count=0;
+
 
 }
 

+ 12 - 0
tools/editor/script_editor_debugger.h

@@ -45,6 +45,7 @@ class TextureButton;
 class AcceptDialog;
 class TreeItem;
 class HSplitContainer;
+class ItemList;
 
 class ScriptEditorDebugger : public Control {
 
@@ -63,6 +64,14 @@ class ScriptEditorDebugger : public Control {
 	Button *le_set;
 	Button *le_clear;
 
+	HSplitContainer *error_split;
+	ItemList *error_list;
+	ItemList *error_stack;
+
+	int error_count;
+	int last_error_count;
+
+
 
 	TextureButton *tb;
 
@@ -132,6 +141,9 @@ class ScriptEditorDebugger : public Control {
 	static void _method_changeds(void *p_ud,Object*p_base,const StringName& p_name,VARIANT_ARG_DECLARE);
 	static void _property_changeds(void *p_ud,Object*p_base,const StringName& p_property,const Variant& p_value);
 
+	void _error_selected(int p_idx);
+	void _error_stack_selected(int p_idx);
+
 protected:
 
 	void _notification(int p_what);