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

Debugger support. Initial import.

woollybah 11 жил өмнө
parent
commit
e547f8edd7

+ 1 - 1
appstub.mod/appstub.bmx

@@ -51,7 +51,7 @@ ModuleInfo "History: Modified float and double debug output to match compiler pr
 
 ?Debug
 'Import "debugger.stdio.bmx"
-'Import "debugger_mt.stdio.bmx"	'Let's give Otus's new MT friendly debugger a whirl!
+Import "debugger_mt.stdio.bmx"	'Let's give Otus's new MT friendly debugger a whirl!
 ?
 
 ?MacOS

+ 57 - 0
appstub.mod/debugger.stdio.glue.c

@@ -0,0 +1,57 @@
+
+#include "brl.mod/blitz.mod/blitz.h"
+
+// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+BBString * bmx_debugger_DebugScopeName(struct BBDebugScope * scope) {
+	return bbStringFromCString(scope->name);
+}
+
+int bmx_debugger_DebugScopeKind(struct BBDebugScope * scope) {
+	return scope->kind;
+}
+
+struct BBDebugDecl * bmx_debugger_DebugScopeDecl(struct BBDebugScope * scope) {
+	return &scope->decls[0];
+}
+
+// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+BBString * bmx_debugger_DebugDeclName(struct BBDebugDecl * decl) {
+	return bbStringFromCString(decl->name);
+}
+
+BBString * bmx_debugger_DebugDeclType(struct BBDebugDecl * decl) {
+	return bbStringFromCString(decl->type_tag);
+}
+
+int bmx_debugger_DebugDeclKind(struct BBDebugDecl * decl) {
+	return decl->kind;
+}
+
+struct BBDebugDecl * bmx_debugger_DebugDeclNext( struct BBDebugDecl * decl ) {
+	return ((char *)decl) + sizeof(struct BBDebugDecl);
+}
+
+void * bmx_debugger_DebugDecl_VarAddress( struct BBDebugDecl * decl ) {
+	return decl->var_address;
+}
+
+BBString * bmx_debugger_DebugDecl_StringFromAddress(BBString ** p) {
+	return *p;
+}
+
+// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+BBString * bmx_debugger_DebugStmFile(struct BBDebugStm * stmt) {
+	return bbStringFromCString(stmt->source_file);
+}
+
+int bmx_debugger_DebugStmLine(struct BBDebugStm * stmt) {
+	return stmt->line_num;
+}
+
+int bmx_debugger_DebugStmChar(struct BBDebugStm * stmt) {
+	return stmt->char_num;
+}
+

+ 100 - 51
appstub.mod/debugger_mt.stdio.bmx

@@ -1,5 +1,7 @@
 Strict
 
+Import "debugger.stdio.glue.c"
+
 NoDebug
 
 Private
@@ -31,8 +33,27 @@ Global bbEmptyString:Byte
 Global brl_blitz_NullFunctionError:Byte Ptr
 Function bbIsMainThread()="bbIsMainThread"
 Function bbGCValidate:Int( mem:Int ) = "bbGCValidate"
+
+
+
+	Function DebugScopeName:String( scope:Int Ptr )="bmx_debugger_DebugScopeName"
+	Function bmx_debugger_DebugScopeKind:Int( scope:Int Ptr )
+	Function bmx_debugger_DebugScopeDecl:Byte Ptr( scope:Int Ptr )
+
+	Function DebugDeclName:String( decl:Int Ptr )="bmx_debugger_DebugDeclName"
+	Function bmx_debugger_DebugDeclType:String( decl:Int Ptr )
+	Function bmx_debugger_DebugDeclKind:Int( decl:Int Ptr )
+	Function bmx_debugger_DebugDeclNext:Byte Ptr( decl:Int Ptr )
+	Function bmx_debugger_DebugDecl_VarAddress:Byte Ptr( decl:Int Ptr)
+	Function bmx_debugger_DebugDecl_StringFromAddress:String( addr:Byte Ptr)
+	
+	Function DebugStmFile:String( stm:Int Ptr )="bmx_debugger_DebugStmFile"
+	Function DebugStmLine:Int( stm:Int Ptr )="bmx_debugger_DebugStmLine"
+	Function DebugStmChar:Int( stm:Int Ptr )="bmx_debugger_DebugStmChar"
+
 End Extern
 
+?Not x64
 Function ToHex$( val )
 	Local buf:Short[8]
 	For Local k=7 To 0 Step -1
@@ -43,6 +64,18 @@ Function ToHex$( val )
 	Next
 	Return String.FromShorts( buf,8 ).ToLower()
 End Function
+?x64
+Function ToHex$( val:Long )
+	Local buf:Short[16]
+	For Local k=15 To 0 Step -1
+		Local n=(val&15)+Asc("0")
+		If n>Asc("9") n=n+(Asc("A")-Asc("9")-1)
+		buf[k]=n
+		val:Shr 4
+	Next
+	Return String.FromShorts( buf,16 ).ToLower()
+End Function
+?
 
 Function IsAlpha( ch )
 	Return (ch>=Asc("a") And ch<=Asc("z")) Or (ch>=Asc("A") And ch<=Asc("Z"))
@@ -164,23 +197,23 @@ Const DEBUGSCOPEKIND_LOCAL=3
 
 Function DebugError( t$ )
 	WriteStderr "Debugger Error:"+t+"~n"
-	End
+'	End
 End Function
 
-Function DebugStmFile$( stm:Int Ptr )
-	Return String.FromCString( Byte Ptr stm[DEBUGSTM_FILE] )
-End Function
-
-Function DebugStmLine( stm:Int Ptr )
-	Return stm[DEBUGSTM_LINE]
-End Function
-
-Function DebugStmChar( stm:Int Ptr )
-	Return stm[DEBUGSTM_CHAR]
-End Function
+'Function DebugStmFile$( stm:Int Ptr )
+'	Return String.FromCString( Byte Ptr stm[DEBUGSTM_FILE] )
+'End Function
+'
+'Function DebugStmLine( stm:Int Ptr )
+'	Return stm[DEBUGSTM_LINE]
+'End Function
+'
+'Function DebugStmChar( stm:Int Ptr )
+'	Return stm[DEBUGSTM_CHAR]
+'End Function
 
 Function DebugDeclKind$( decl:Int Ptr )
-	Select decl[DEBUGDECL_KIND]
+	Select bmx_debugger_DebugDeclKind(decl)'[DEBUGDECL_KIND]
 	Case DEBUGDECLKIND_CONST Return "Const"
 	Case DEBUGDECLKIND_LOCAL Return "Local"
 	Case DEBUGDECLKIND_FIELD Return "Field"
@@ -190,12 +223,13 @@ Function DebugDeclKind$( decl:Int Ptr )
 	DebugError "Invalid decl kind"
 End Function
 
-Function DebugDeclName$( decl:Int Ptr )
-	Return String.FromCString( Byte Ptr decl[DEBUGDECL_NAME] )
-End Function
+'Function DebugDeclName$( decl:Int Ptr )
+'	Return String.FromCString( Byte Ptr decl[DEBUGDECL_NAME] )
+'End Function
 
 Function DebugDeclType$( decl:Int Ptr )
-	Local t$=String.FromCString( Byte Ptr decl[DEBUGDECL_TYPE] )
+	'Local t$=String.FromCString( Byte Ptr decl[DEBUGDECL_TYPE] )
+	Local t$=bmx_debugger_DebugDeclType(decl)
 	Local ty$=TypeName( t )
 	Return ty
 End Function
@@ -227,16 +261,19 @@ Function DebugEscapeString$( s$ )
 End Function
 
 Function DebugDeclValue$( decl:Int Ptr,inst:Byte Ptr )
-	If decl[DEBUGDECL_KIND]=DEBUGDECLKIND_CONST
+	If bmx_debugger_DebugDeclKind(decl)=DEBUGDECLKIND_CONST
 		Local p:Byte Ptr=Byte Ptr decl[DEBUGDECL_ADDR]
 		Return DebugEscapeString(String.FromShorts( Short Ptr(p+12),(Int Ptr (p+8))[0] ))
 	EndIf
 
 	Local p:Byte Ptr
-	Select decl[DEBUGDECL_KIND]
+	Select bmx_debugger_DebugDeclKind(decl)'[DEBUGDECL_KIND]
 	Case DEBUGDECLKIND_GLOBAL
 		p=Byte Ptr decl[DEBUGDECL_ADDR]
-	Case DEBUGDECLKIND_LOCAL,DEBUGDECLKIND_FIELD
+	Case DEBUGDECLKIND_LOCAL
+		p=bmx_debugger_DebugDecl_VarAddress(decl)
+		'p=Byte Ptr decl[DEBUGDECL_ADDR]
+	Case DEBUGDECLKIND_FIELD
 		p=Byte Ptr (inst+decl[DEBUGDECL_ADDR])
 	Case DEBUGDECLKIND_VARPARAM
 		p=Byte Ptr (inst+decl[DEBUGDECL_ADDR])
@@ -261,10 +298,10 @@ Function DebugDeclValue$( decl:Int Ptr,inst:Byte Ptr )
 	Case Asc("d")
 		Return String.FromDouble( (Double Ptr p)[0] )
 	Case Asc("$")
-		p=(Byte Ptr Ptr p)[0]
-		Local sz=Int Ptr(p+8)[0]
-		Local s$=String.FromShorts( Short Ptr(p+12),sz )
-		Return DebugEscapeString( s )
+		'p=(Byte Ptr Ptr p)[0]
+		'Local sz=Int Ptr(p+8)[0]
+		'Local s$=String.FromShorts( Short Ptr(p+12),sz )
+		Return DebugEscapeString( bmx_debugger_DebugDecl_StringFromAddress(p) )
 	Case Asc("z")
 		p=(Byte Ptr Ptr p)[0]
 		If Not p Return "Null"
@@ -298,7 +335,7 @@ Function DebugDeclValue$( decl:Int Ptr,inst:Byte Ptr )
 End Function
 
 Function DebugScopeKind$( scope:Int Ptr )
-	Select scope[DEBUGSCOPE_KIND]
+	Select bmx_debugger_DebugScopeKind(scope)
 	Case DEBUGSCOPEKIND_FUNCTION Return "Function"
 	Case DEBUGSCOPEKIND_TYPE Return "Type"
 	Case DEBUGSCOPEKIND_LOCAL Return "Local"
@@ -306,9 +343,9 @@ Function DebugScopeKind$( scope:Int Ptr )
 	DebugError "Invalid scope kind"
 End Function
 
-Function DebugScopeName$( scope:Int Ptr )
-	Return String.FromCString( Byte Ptr scope[DEBUGSCOPE_NAME] )
-End Function
+'Function DebugScopeName$( scope:Int Ptr )
+	'Return String.FromCString( Byte Ptr scope[DEBUGSCOPE_NAME] )
+'End Function
 
 Function DebugScopeDecls:Int Ptr[]( scope:Int Ptr )
 	Local n,p:Int Ptr=scope+DEBUGSCOPE_DECLS
@@ -331,7 +368,7 @@ Extern
 Global bbOnDebugStop()
 Global bbOnDebugLog( message$ )
 Global bbOnDebugEnterStm( stm:Int Ptr )
-Global bbOnDebugEnterScope( scope:Int Ptr,inst:Byte Ptr )
+Global bbOnDebugEnterScope( scope:Int Ptr)',inst:Byte Ptr )
 Global bbOnDebugLeaveScope()
 Global bbOnDebugPushExState()
 Global bbOnDebugPopExState()
@@ -368,7 +405,7 @@ Type TExState
 End Type
 
 Type TDbgState
-	Field mode,debugLevel,funcLevel
+	Field Mode,debugLevel,funcLevel
 	Field currentScope:TScope=New TScope
 	Field scopeStack:TScope[],scopeStackTop
 	Field exStateStack:TExState[],exStateStackTop
@@ -406,44 +443,46 @@ Function WriteDebug( t$ )
 	WriteStderr "~~>"+t
 End Function
 
-Function DumpScope( scope:Int Ptr,inst:Byte Ptr )
+Function DumpScope( scope:Int Ptr)',inst:Byte Ptr )
 
-	Local decl:Int Ptr=scope+DEBUGSCOPE_DECLS
+	Local decl:Int Ptr=bmx_debugger_DebugScopeDecl(scope)'+DEBUGSCOPE_DECLS
 	
 	Local kind$=DebugScopeKind( scope ),name$=DebugScopeName( scope )
+'DebugError "DumpScope : " + name
 	
 	If Not name name="<local>"
 	
 	WriteDebug kind+" "+name+"~n"
 	
-	While decl[DEBUGDECL_KIND]<>DEBUGDECLKIND_END
+	While bmx_debugger_DebugDeclKind(decl)<>DEBUGDECLKIND_END
 	
-		Select decl[DEBUGDECL_KIND]
+		Select bmx_debugger_DebugDeclKind(decl)
 		Case DEBUGDECLKIND_TYPEMETHOD,DEBUGDECLKIND_TYPEFUNCTION
-			decl:+4
+			decl = bmx_debugger_DebugDeclNext(decl)
 			Continue
 		End Select
 
 		Local kind$=DebugDeclKind( decl )
 		Local name$=DebugDeclname( decl )
 		Local tipe$=DebugDeclType( decl )
-		Local value$=DebugDeclValue( decl,inst )
+		Local value$=DebugDeclValue( decl,0 )
 		
 		WriteDebug kind+" "+name+":"+tipe+"="+value+"~n"
 
-		decl:+4	
+		'decl:+4
+		decl = bmx_debugger_DebugDeclNext(decl)
 	Wend
 End Function
 
 Function DumpClassScope( clas:Int Ptr,inst:Byte Ptr )
-
+'WriteDebug("DumpClassScope")
 	Local supa:Int Ptr=Int Ptr clas[0]
 	
 	If Not supa Return
 	
 	DumpClassScope supa,inst
 	
-	DumpScope Int Ptr clas[2],inst
+	DumpScope Int Ptr clas[2]',inst
 
 End Function
 
@@ -505,13 +544,14 @@ Function DumpObject( inst:Byte Ptr,index )
 End Function
 
 Function DumpScopeStack()
+'DebugError("DumpScopeStack")
 	Local dbgState:TDbgState = GetDbgState()
 	For Local i=Max(dbgState.scopeStackTop-100,0) Until dbgState.scopeStackTop
 		Local t:TScope=dbgState.scopeStack[i]
 		Local stm:Int Ptr=t.stm
 		If Not stm Continue
 		WriteDebug "@"+DebugStmFile(stm)+"<"+DebugStmLine(stm)+","+DebugStmChar(stm)+">~n"
-		DumpScope t.scope,t.inst
+		DumpScope t.scope',t.inst
 	Next
 End Function
 
@@ -540,17 +580,17 @@ Function UpdateDebug( msg$ )
 
 		Select line[..1].ToLower()
 		Case "r"
-			dbgState.mode=MODE_RUN
+			dbgState.Mode=MODE_RUN
 			Exit
 		Case "s"
-			dbgState.mode=MODE_STEP
+			dbgState.Mode=MODE_STEP
 			dbgState.debugLevel=dbgState.funcLevel
 			Exit
 		Case "e"
-			dbgState.mode=MODE_STEPIN
+			dbgState.Mode=MODE_STEPIN
 			Exit
 		Case "l"
-			dbgState.mode=MODE_STEPOUT
+			dbgState.Mode=MODE_STEPOUT
 			dbgState.debugLevel=dbgState.scopeStackTop-1
 			Exit
 		Case "t"
@@ -567,11 +607,19 @@ Function UpdateDebug( msg$ )
 			EndIf
 			If t[..1]="$" t=t[1..].Trim()
 			If t[..2].ToLower()="0x" t=t[2..].Trim()
+?Not x64
 			Local pointer = Int( "$"+t )
+?x64
+			Local pointer:Long = Long( "$"+t )
+?
 			If Not (pointer And bbGCValidate(pointer)) Then Continue
+?Not x64
 			Local inst:Int Ptr=Int Ptr pointer
-			
 			Local cmd$="ObjectDump@"+ToHex( Int inst )
+?x64
+			Local inst:Long Ptr=Long Ptr pointer
+			Local cmd$="ObjectDump@"+ToHex( Long inst )
+?			
 			If i<>-1 cmd:+":"+index
 			WriteDebug cmd$+"{~n"
 
@@ -616,7 +664,7 @@ Function OnDebugEnterStm( stm:Int Ptr )
 	Local dbgState:TDbgState = GetDbgState()
 	dbgState.currentScope.stm=stm
 	
-	Select dbgState.mode
+	Select dbgState.Mode
 	Case MODE_RUN
 		Return
 	Case MODE_STEP
@@ -632,8 +680,8 @@ Function OnDebugEnterStm( stm:Int Ptr )
 	UpdateDebug "Debug:~n"
 End Function
 
-Function OnDebugEnterScope( scope:Int Ptr,inst:Byte Ptr )
-
+Function OnDebugEnterScope( scope:Int Ptr)',inst:Byte Ptr )
+'DebugError ">>> OnDebugEnterScope : " + DebugScopeName(scope)
 	Local dbgState:TDbgState = GetDbgState()
 	GCSuspend
 
@@ -647,23 +695,24 @@ Function OnDebugEnterScope( scope:Int Ptr,inst:Byte Ptr )
 	dbgState.currentScope=dbgState.scopeStack[dbgState.scopeStackTop]
 
 	dbgState.currentScope.scope=scope
-	dbgState.currentScope.inst=inst
+	dbgState.currentScope.inst=0
 
 	dbgState.scopeStackTop:+1
 
-	If dbgState.currentScope.scope[DEBUGSCOPE_KIND]=DEBUGSCOPEKIND_FUNCTION dbgState.funcLevel:+1
+	If bmx_debugger_DebugScopeKind(dbgState.currentScope.scope)=DEBUGSCOPEKIND_FUNCTION dbgState.funcLevel:+1
 
 	GCResume	
 End Function
 
 Function OnDebugLeaveScope()
+'DebugError "<<< OnDebugLeaveScope"
 
 	Local dbgState:TDbgState = GetDbgState()
 	GCSuspend
 
 	If Not dbgState.scopeStackTop DebugError "scope stack underflow"
 
-	If dbgState.currentScope.scope[DEBUGSCOPE_KIND]=DEBUGSCOPEKIND_FUNCTION dbgState.funcLevel:-1
+	If bmx_debugger_DebugScopeKind(dbgState.currentScope.scope)=DEBUGSCOPEKIND_FUNCTION dbgState.funcLevel:-1
 	
 	dbgState.scopeStackTop:-1
 

+ 9 - 0
appstub.mod/debugger_mt.stdio.x

@@ -0,0 +1,9 @@
+void bbOnDebugStop()!
+void bbOnDebugLog( BBString * )!
+void bbOnDebugEnterStm( BBDebugStm * )!
+void bbOnDebugEnterScope( BBDebugScope * )!
+void bbOnDebugLeaveScope()!
+void bbOnDebugPushExState()!
+void bbOnDebugPopExState()!
+void bbOnDebugUnhandledEx( BBObject * )!
+int   bbGCValidate( void * )!

+ 25 - 25
blitz.mod/blitz_debug.c

@@ -1,25 +1,25 @@
-
-#include "blitz.h"
-
-void bbCAssertEx(){
-	bbExThrowCString( "C Assert failed" );
-}
-
-static void debugNop(){
-}
-
-static void debugUnhandledEx( BBObject *ex ){
-	bbWriteStderr( ex->clas->ToString( ex ) );
-	exit(-1);
-}
-
-void (*bbOnDebugStop)()=debugNop;
-void (*bbOnDebugLog)( BBString *str )=debugNop;
-void (*bbOnDebugEnterStm)( BBDebugStm *stm )=debugNop;
-void (*bbOnDebugEnterScope)( BBDebugScope *scope,void *inst )=debugNop;
-void (*bbOnDebugLeaveScope)()=debugNop;
-void (*bbOnDebugPushExState)()=debugNop;
-void (*bbOnDebugPopExState)()=debugNop;
-
-void (*bbOnDebugUnhandledEx)( BBObject *ex )=debugUnhandledEx;
-
+
+#include "blitz.h"
+
+void bbCAssertEx(){
+	bbExThrowCString( "C Assert failed" );
+}
+
+static void debugNop(){
+}
+
+static void debugUnhandledEx( BBObject *ex ){
+	bbWriteStderr( ex->clas->ToString( ex ) );
+	exit(-1);
+}
+
+void (*bbOnDebugStop)()=debugNop;
+void (*bbOnDebugLog)( BBString *str )=debugNop;
+void (*bbOnDebugEnterStm)( BBDebugStm *stm )=debugNop;
+void (*bbOnDebugEnterScope)( BBDebugScope *scope )=debugNop;
+void (*bbOnDebugLeaveScope)()=debugNop;
+void (*bbOnDebugPushExState)()=debugNop;
+void (*bbOnDebugPopExState)()=debugNop;
+
+void (*bbOnDebugUnhandledEx)( BBObject *ex )=debugUnhandledEx;
+

+ 4 - 4
blitz.mod/blitz_debug.h

@@ -36,13 +36,13 @@ struct BBDebugDecl{
 	union{
 		BBString*	const_value;
 #if ( __WORDSIZE == 64 )
-		BBInt64		local_offset;
+//		BBInt64		local_offset;
 		BBInt64		field_offset;
 #else
-		int			local_offset;
+//		int			local_offset;
 		int			field_offset;
 #endif
-		void		*global_address;
+		void		*var_address;
 	};
 };
 
@@ -68,7 +68,7 @@ extern void bbCAssertEx();
 extern void (*bbOnDebugStop)();
 extern void (*bbOnDebugLog)( BBString *msg );
 extern void (*bbOnDebugEnterStm)( BBDebugStm *stm );
-extern void (*bbOnDebugEnterScope)( BBDebugScope *scope,void *inst );
+extern void (*bbOnDebugEnterScope)( BBDebugScope *scope );//,void *inst );
 extern void (*bbOnDebugLeaveScope)();
 extern void (*bbOnDebugPushExState)();
 extern void (*bbOnDebugPopExState)();