Why is my error handler reading garbage data? [SOLVED]

New to FreeBASIC? Post your questions here.
xX_Pokeman2003_Xx
Posts: 25
Joined: Mar 11, 2022 21:10

Re: Why is my error handler reading garbage data?

Post by xX_Pokeman2003_Xx »

That doesn't work
WHY WON'T IT WORK
AAAAAAAAAAAA
7zip, with log
C:\Users\Pokeman2003\Desktop\project>ProjMan.exe
LOADING...
Error handler primed.
DONE!
=========================================
OBVIOUSLY MY PROJECT MANAGER
Version prototype.
Internal Series - 2022\03\19-3
Compile time - 1:35:47.66
=========================================
L- Languages
P- Projects
R- pRofiles
E- Exit
[LPRE]

C:\Users\Pokeman2003\Desktop\project>echo %errorlevel%
-1073741819
AM I JUST SEVERELY INCOMPETENT!?
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why is my error handler reading garbage data?

Post by fxm »

Have you try to downright suppress the gcc option '-O 0' ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Why is my error handler reading garbage data?

Post by dodicat »

These macros work OK here.
In Main
If I make state=1 I get

Code: Select all

LOADING...
Error handler primed.
DONE!
=========================================
 OBVIOUSLY MY PROJECT MANAGER
Version prototype.
Internal Series - "%datedir%-%checknum%"
Compile time - "%time%"
=========================================
======================================================
 THIS IS SOME GARBAGE DATA TO HIDE MY TRUE INTENTION~
ERROR ENCOUNTERED!
CODE:6
FUNCTION:4222980            __FB_MAINPROC__
LINE:30
Okay technically I'm making this for a personal project manager as practice. It's a bit extreme but it's a test to all of my skills. Anyways.
SOFTWARE SER "%datedir%-%checknum%", CTI "%time%".
======================================================

 
Your switches for my ide
#cmdline "-v -exx -x ProjMan.exe -eassert -O 0 -R -edebuginfo -d DEFseries=""%datedir%-%checknum%"" -d DEFctime=""%time%"""

Your FUNCTION:4222980 is __FB_MAINPROC__, I added __function__ into the code for clarity.

Win 10, 64 bits, tested fb 32 and 64 and -gen gas64
xX_Pokeman2003_Xx
Posts: 25
Joined: Mar 11, 2022 21:10

Re: Why is my error handler reading garbage data?

Post by xX_Pokeman2003_Xx »

Okay, so, I've done some tests. It looks like the error only stops working IF it's called within the function itself. It works 100% fine if I do it outside of the function. I have zero context to why this is happening.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Why is my error handler reading garbage data?

Post by SARG »

My code below seems working fine in all case, error in main or in a function (uncomment the line in test function).

What problems I found :
- if not using -exx errfn value is null --> crash
- in case of main *erfn points to an empty string --> crash
- weird thing, for print in ehandler preferably use ';' than '+' or '&' sometimes --> crash

Code: Select all

On Error goto ehandler

goto skipover
scope
	ehandler:
		Dim  Errnum As Long = Err()
		Dim  Errfunc As String = *Erfn()
		'Dim  Errmod As String = *Ermn()
		Dim  Errlin As Integer = Erl()

		print "====================================================== 3"
		print "CODE:";str( Errnum)
		if len(errfunc)>0 then 
				print "FUNCTION:";errfunc
			else
				print "use -exx to get function name or maybe in main"
		EndIf
		
		print "LINE:";str( errlin )
		print "======================================================"
		end Errnum
end scope
skipover: 'Just a really $%#@ skip.

function test() as integer

error(9)   '<---- uncomment to get an error in the function
return 19
end function

test

error(17)

print "end"
sleep
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why is my error handler reading garbage data?

Post by fxm »

I am not sure it is safe to put the error handler in a nested scope versus 'On Error Goto ...'.
It is advisable to always put 'On Error Goto ...' and the associated error handler in the same scope.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Why is my error handler reading garbage data?

Post by SARG »

fxm wrote: Mar 21, 2022 13:46 I am not sure it is safe to put the error handler in a nested scope versus 'On Error Goto ...'.
It is advisable to always put 'On Error Goto ...' and the associated error handler in the same scope.
On Error goto ehandler is coded like that :

Code: Select all

lea rcx, .L_0005[rip+0]  <-- ehandler address 
call fb_ErrorSetHandler
When an error occurs there is a simple jump to the defined address so I don't see any problem but I could be wrong.
A case ?
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why is my error handler reading garbage data?

Post by fxm »

32-bit examples compiled with gas:
(seems to work with gcc or gas64)

Code: Select all

Declare Sub test()

On Error Goto ehandler
test()
End

ehandler:
    Dim As Integer Errnum = Err()
    Print "Error Code:";
    Print Errnum

Sub test()
    Error(9)
End Sub

Code: Select all

Error Code: 9
OK

Code: Select all

Declare Sub test()

On Error Goto ehandler
test()
End

ehandler:
Scope
    Dim As Integer Errnum = Err()
    Print "Error Code:";
    Print Errnum
End Scope

Sub test()
    Error(9)
End Sub

Code: Select all

Error Code: 4236680
NOK
xX_Pokeman2003_Xx
Posts: 25
Joined: Mar 11, 2022 21:10

Re: Why is my error handler reading garbage data?

Post by xX_Pokeman2003_Xx »

SARG wrote: Mar 21, 2022 12:19 My code below seems working fine in all case, error in main or in a function (uncomment the line in test function).

What problems I found :
- if not using -exx errfn value is null --> crash
- in case of main *erfn points to an empty string --> crash
- weird thing, for print in ehandler preferably use ';' than '+' or '&' sometimes --> crash
weird thing, for print in ehandler preferably use ';' than '+' or '&' sometimes --> crash
:oops:
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Why is my error handler reading garbage data? [SOLVED]

Post by SARG »

@fxm
Try without -exx (if it was the case).
With exx same result as you, without no problem.

Edit : with or without scope and without -exx the asm code is exactly the same. So -exx induce a difference (I guess on stack) that causes the problem.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why is my error handler reading garbage data? [SOLVED]

Post by fxm »

SARG wrote: Mar 21, 2022 17:08 So -exx induce a difference (I guess on stack) that causes the problem.

Indeed, if using a static variable instead of a local variable on the stack, this seems to work:

Code: Select all

Declare Sub test()

On Error Goto ehandler
test()
End

ehandler:
Scope
    Static As Integer Errnum
    Errnum = Err()
    Print "Error Code:";
    Print Errnum
End Scope

Sub test()
    Error(9)
End Sub

Code: Select all

Error Code: 9
OK
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Why is my error handler reading garbage data? [SOLVED]

Post by dodicat »

"You can't make a silk purse out of a sow's ear", and the onerror goto seems to be a bit of a sow's ear.
Maybe when local is introduced the thing will be cleaned up.
onerror goto only catches the very obvious anyway, which you should catch yourself while coding.

This test will probably give different results on different folks' boxes.
I tested here with 32/64 bits and -gen gas64

Code: Select all


'===============================================
#macro err1_
On Error Goto ehandler
#endmacro
#macro err2_
ehandler:
    if Err() then
    Dim As Integer Errnum = Err()
    print "Error ";Errnum;"  =  ";
    select case errnum
case 0 :print "No error" 
case 1 :print  "Illegal function call" 
case 2 :print  "File not found signal" 
case 3 :print  "File I/O error" 
case 4 :print  "Out of memory" 
case 5 :print  "Illegal resume" 
case 6 :print "Out of bounds array access" 
case 7 :print  "Null Pointer Access" 
case 8 :print  "No privileges" 
case 9 :print  "interrupted signal" 
case 10 :print  "illegal instruction signal" 
case 11 :print  "floating point error signal" 
case 12 :print  "segmentation violation signal" 
case 13 :print  "Termination request signal" 
case 14 :print  "abnormal termination signal" 
case 15 :print  "quit request signal" 
case 16 :print  "return without gosub" 
case 17 :print  "end of file" 
end select
    print "Error function ";*erfn;"  "+__function__
    print "Error line ";Erl
    sleep
    end errnum
end if
#endmacro
#macro Echeck
err1_
err2_
#endmacro

'========================================================

#cmdline "-exx"

namespace mine
function dothis() as long
    Echeck
   dim as long ptr z
   return *z
    end function
end namespace

type udt extends object
    declare sub dothis
end type

sub udt.dothis
    Echeck
    error 3
    print "Hello"
    end sub

dim as udt x

Sub test()
    Echeck
    dim as any ptr i
    bload("xyz",i)
End Sub


function test2() as string
    Echeck
    open "nonesuch" for input as #1
    close #1
    return "abc"
end function

sub normal
    print "Hello world!"
end sub


print test2()
test()
normal
print mine.dothis
x.dothis

Echeck
error 7

 

Here are my results as I comment the bad subs off one by one:

Code: Select all

Error  2  =  File not found signal
Error function TEST2  TEST2
Error line  78

Error  12  =  segmentation violation signal
Error function TEST  TEST
Error line -1


Hello world!
Error  7  =  Null Pointer Access
Error function DOTHIS  MINE.DOTHIS
Error line  53

Error  3  =  File I/O error
Error function DOTHIS  UDT.DOTHIS
Error line  63

Error  7  =  Null Pointer Access
Error function   __FB_MAINPROC__
Error line  97


 
Post Reply