SetUnhandledExceptionFilter in 64 bit

General FreeBASIC programming questions.
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 06, 2018 22:00

Over at PowerBASIC Juergen Kuehlwein has been developing a PB IDE, called JK-IDE, for about, I think, five years and it is a very good IDE. I have been using nothing else for my PB work for some years.

Recently Juergen decided to add support for PluriBASIC and FreeBASIC. The FreeBASIC support is coming along. This is how, for example, a FreeBASIC source could be compiled.

Code: Select all

#Compiler FreeBASIC
#Compile exe console 32 /o "-gen gcc -Wc -O3"
 
Print "Print FreeBASIC"
 
Sleep

I have actually compiled more complicated stuff.

However, the support is still very much in beta which is why I have not mentioned it. I have given a little advice but Juergen has hit a snag that I cannot help with.

This is a portion of am email I got earlier.

I read posts about gcc having problems with SetUnhandledExceptionFilter in 64 bit, but as of version 5.5 it is said to have been fixed. So i updated gcc to 6.2.1 - it compiles, but SetUnhandledExceptionFilter doesn´t work either. Can you verify this in the IDE you are using for FreeBASIC?
Is this a know problem with FreeBASIC or gcc ? Can you help me, or do you know somebody/something, who/which could help ?

I could not find anything in the forum archives which would help here.

Can anyone help on this?

Once the FreeBASIC support gets to a more mature level I will let you know.

Cheers.
dodicat
Posts: 5822
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SetUnhandledExceptionFilter in 64 bit

Postby dodicat » Mar 07, 2018 0:08

fbide
(not using -exx)
My ide is set with run command:
cmd /c "<$file>" <$param> & pause
(Keep the console open)
Test code:
Null pointer if new is not used in sub testcrash.

Code: Select all


#include "windows.bi"


#define  EXCEPTION_EXECUTE_HANDLER 1
#define  EXCEPTION_CONTINUE_SEARCH 0
function CrashHandler(E as EXCEPTION_POINTERS ptr=0) as long
    dim  as long Crash = 0
    print "Gotcha"
    return iif(EXCEPTION_CONTINUE_SEARCH,Crash,EXCEPTION_EXECUTE_HANDLER)
end function



sub TestCrash()
dim f as function(as EXCEPTION_POINTERS ptr=0) as long
f=@CrashHandler
SetUnhandledExceptionFilter(f)
     dim as ubyte ptr p'=new ubyte
             *p = 5
end sub

TestCrash
print "done"
sleep
 

It is pretty basic.(I haven't tried this stuff before)
I had to :
#define EXCEPTION_EXECUTE_HANDLER 1
#define EXCEPTION_CONTINUE_SEARCH 0
I couldn't find them anywhere.
Juergen Kuehlwein
Posts: 162
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 07, 2018 14:58

Hi all,

my name is Juergen Kuehlwein, i wrote an IDE for PowerBASIC and recently decided to add FreeBASIC support to it. I´m not new to coding in BASIC and other languages. but i´m a newbie to FreeBASIC, therefore i might have one or another question...

Deltarho was kind enough to post my original question here - we know each other from PowerBASIC.


@dodicat
Thanks for your reply - your code works as expected in 32 and 64 bit and i understand what it does. It is quite similar to the code i used for testing.

Code: Select all

#include once "windows.bi"

FUNCTION gpf_handler(BYVAL ep AS Exception_Pointers PTR) AS uinteger
'***********************************************************************************************
'
'***********************************************************************************************

  print "in handler"
  sleep

  function = 0

end function


'***********************************************************************************************


SetUnhandledExceptionFilter(procptr(gpf_handler))   

 dim as ubyte ptr p'=new ubyte
'1. - null pointer
  *p = 5

'2. - read from 0
'asm xor eax, eax
'asm mov eax, [eax]

'3. - assembler breakpoint
'asm int 3


print "OK"
sleep



But my question now is: why doesn´t it work for 2. ("read from 0") and 3. ("asm breakpoint") in 64 bit ? The program crashes without ever having called the exception handler. It does work in 32 bit, the handler is called in all three cases.


JK



PS: FreeBASIC support is work in progress, but if you want to test what i already have you can download the latest version here. Be sure to read the help file in order to understand how it all works together. Please send bug reports, comments, critics, feature requests and questions to <jk-ide at t minus online dot de>
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 08, 2018 0:27

@dodicat

Ran your code but I just got a console screen open and close in a split second.
I haven't tried this stuff before

Neither have I. I had a look at the MSDN entry but it could have been written in Klingon for all the good that did. <smile>
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 08, 2018 0:40

I just Googled "SetUnhandledExceptionFilter 64 bit". It looks like this has been an issue for sometime. It is not a popular subject - the seventh entry on the Google search was my opening post. <laugh>

I found this #29 SetUnhandledExceptionFilter broken in mingw64?.

I will get back to Juergen - he should have done this before contacting me.
dodicat
Posts: 5822
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SetUnhandledExceptionFilter in 64 bit

Postby dodicat » Mar 08, 2018 10:05

If you put a sleep after print "Gotcha" then
in fb 32 and 64
1) If you have dim as ubyte ptr p=new ubyte the callback function is not called. (No error)
2) If you have dim as ubyte ptr p the callback function is called and you see Gotcha. (Error 7 null pointer)

It seems to catch run time error 7 (null pointer), but not run time error 2 (file not found)
I have tested.
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 08, 2018 12:24

@dodicat

Thank you.

This is what I got from Juergen

Running in his IDE: 32 bit captures the' int 3' but 64 bit does not (GPF).

Code: Select all

#compiler freebasic
#compile exe console 32 /o "-R"

#include once "windows.bi"

FUNCTION gpf_handler(BYVAL ep AS Exception_Pointers PTR) AS Uinteger
  Print "in handler"
  Sleep
  Function = 0
End function

SetUnhandledExceptionFilter(procptr(gpf_handler))   

Asm int 3

Print "OK"
Sleep

I am a bit slow sometimes.<smile> I removed the first two lines and run in a FreeBASIC IDE. In 32 bit it worked but I got a 'Passing different pointer types, at parameter 1 of SetUnhandledExceptionFilter.

This worked in 64 bit: SetUnhandledExceptionFilter( Cast(LPTOP_LEVEL_EXCEPTION_FILTER, Procptr(gpf_handler) ) )

The question is why did it compile with 64 bit in Juergen's IDE.

I will let him know.
counting_pine
Site Admin
Posts: 6169
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: SetUnhandledExceptionFilter in 64 bit

Postby counting_pine » Mar 08, 2018 14:46

Hi. Just to draw attention to Juergen Kuehlwein's post: viewtopic.php?f=3&t=26483&p=244203#p244203

It was posted before several other replies, but since he's a new user I had to accept his first post before anyone could see it, so people might have missed it.
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 08, 2018 16:50

In both 32 and 64 bit winbase.bi we have:

Code: Select all

type PTOP_LEVEL_EXCEPTION_FILTER as function(byval ExceptionInfo as _EXCEPTION_POINTERS ptr) as LONG
type LPTOP_LEVEL_EXCEPTION_FILTER as PTOP_LEVEL_EXCEPTION_FILTER

In 64 bit I changed that LONG to LONGINT and Juergen's 1., 2. and 3. are now working.

Mine is a Guinness, Juergen.<smile>

Oh, welcome to the FreeBASIC forums by the way. Blimey, listen to me - I have only been here for five minutes.<laugh>
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 08, 2018 19:46

The Guinness is on hold.

1., 2. and 3. are working in FreeBASIC 64 bit but only 1. is working in Juergen's IDE.

We will get there. JK-IDE has some nice features like a Window split state which Kuan introduced in PoseidenFB. However, it can be used on the same source code where two locations can be seen and we can copy and paste from one panel to the other - great for re-arranging the source code.
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 09, 2018 2:19

When I removed the first two lines from Juergen's code I didn't spot the '-R'.

Putting '-R' with my usual options also saw 2. & 3. crashing in FreeBASIC IDEs. 2. & 3. are using ASM.

Any thoughts on this because I have none.
dodicat
Posts: 5822
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SetUnhandledExceptionFilter in 64 bit

Postby dodicat » Mar 09, 2018 10:49

Trying to debug:
trial 1)
I eliminate loading windows.bi.


I make up a type (structure) EXCEPTIONPOINTERS, which consists of two pointer fields.
So 8 bytes in fb 32 and 16 bytes in fb 64.
I get the function SetUnhandledExceptionFilter directly from kernel32.dll
I get the same results here (Win 10)

Code: Select all

'#include once "windows.bi"

type EXCEPTIONPOINTERS
  as any ptr ExceptionRecord
  as any ptr ContextRecord
  end type

'var L=loadlibraryex("kernel32.dll",0,1) 'tried 0,0 and 0,1
var L=dylibload("kernel32.dll")
dim  shared SetUnhandledExceptionFilter_ as function(as function(byval as  ExceptionPointers ptr) as long) as long
SetUnhandledExceptionFilter_=dylibsymbol(L,"SetUnhandledExceptionFilter")
if SetUnhandledExceptionFilter_ =0 then print "load error":sleep:end



FUNCTION gpf_handler(BYval ep AS ExceptionPointers PTR) AS long
'***********************************************************************************************
'
'***********************************************************************************************

  print "in handler"
  sleep
end(1)
  function = 0

end function


'***********************************************************************************************


 SetUnhandledExceptionFilter_(procptr(gpf_handler))   

 dim as ubyte ptr p'=new ubyte
'1. - null pointer
  *p = 5

'2. - read from 0
'asm xor eax, eax
'asm mov eax, [eax]

'3. - assembler breakpoint
'asm int 3


print "OK"
sleep 
deltarho[1859]
Posts: 1789
Joined: Jan 02, 2017 0:34
Location: UK

Re: SetUnhandledExceptionFilter in 64 bit

Postby deltarho[1859] » Mar 09, 2018 11:33

dodicat wrote:I get the same results here (Win 10)

Which I assume you mean crashing with 2. & 3. when using -R.

Well, I have no idea what is going on now because I am definitely in 64 bit and using -R and I am not crashing this morning. I did some work in the registry first thing on a different subject and had to do a Restart. With the GPFs I was getting the exes were still running, according to Task Manager. I found three last night before retiring. They may have screwed up subsequent executions and there removal may explain why I am no longer crashing. In the old days I always did a Restart after a GPF.

I think that I will take a wee rest from this - I am starting to feel like a dog chasing its own tail.

One thing is for sure, just tried it, -R is an issue with JK-IDE.
dodicat
Posts: 5822
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: SetUnhandledExceptionFilter in 64 bit

Postby dodicat » Mar 09, 2018 13:37

I found an old fb/pb editor from years ago.
http://www.rowalt.de/pc/programming/toolse.htm
I wonder if it was popular with pb fans
Juergen Kuehlwein
Posts: 162
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 09, 2018 14:13

@deltarho,

i definitely owe you some beer!


@all

we are making progress - but maybe i should explain more what´s it all about:

For PowerBASIC i added a feature "Compile (GPF)", which creates a special build, trapping GPFs and showing the offending line in the code editor with some information, why this happend. Obviously this is something you wouldn´t need all the time, but when you need it, it can be invaluable, because it will save you tons of time finding the line, where the GPF occurred.

You don´t have to change or prepare your code in any way - just click and see (this is how it already works in PowerBASIC).

My goal is to implement this (and some other debugging) features for FreeBASIC too. So the first step would be to get "SetUnhandledExceptionFilter" running for 32 and 64 bit FreeBASIC compilations.


JK

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests