SetUnhandledExceptionFilter in 64 bit

General FreeBASIC programming questions.
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 17, 2018 0:27

Please be patient until i have, what you could call a beta version for FreeBASIC!

Because this IDE was originally written or PowerBASIC you must add "#compiler freebasic" as a first line to your code. The second line should be "#compile [gui|console] [exe|dll|lib] [32|64]" as stated in the help - otherwise you will get errors. And even this doesn´t work properly in all cases as deltarho[1859] reported.

I already have solved many general language specific problems, so basically most things work with my test code. But this doesn´t mean it works for all kinds of code FreeBASIC allows for. There will be an announced beta version and there will be better documentation of what´s required, how to set it up, current limitations etc. - but this will take some time still.


JK
TeeEmCee
Posts: 262
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: SetUnhandledExceptionFilter in 64 bit

Postby TeeEmCee » Mar 17, 2018 15:44

Juergen Kuehlwein wrote:@TeeEmCee

would it be possible to get your test code, which crashed the IDE in SCILEXER.DLL? Please drop me a mail at jk-ide at t minus online dot de - thanks.


It didn't occur to me that the problems I saw might be related to what file I had open, because they happen even if I don't have any file open, even with a fresh install that has never opened any files.
I'm not certain which file, if any, I had open when it crashed in scilexer, but it might have been this one:

Code: Select all

if "A" < "B" then ? 3

But the crash hasn't happened again for me; I couldn't reproduce it with that file.

Now, even after deleting the path to fbc from the ini file, JK-IDE still freezes after the splash screen. If I wait ~20 seconds then eventually it shows the window and the currently open file, but is unresponsive with 100% cpu usage.
I edited the .ini to not open any file on startup. Now it starts, but only after a lag of ~6 seconds of CPU time.
If I open any file, even a blank .txt file, quit, and then try to run the program again, it goes back to freezing on startup. Deleting the whole .ini file to restore doesn't fix this problem. Which I assume means this has nothing to do with FB, unless there's more state than just the ini file.

BTW, I'm running Win XP, and the fact that it's in a VM should make no difference except for the fact that there's a custom VirtualBox Direct3D driver, which is a bit buggy.
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 17, 2018 18:44

@TeeEmCee


Win XP - i don´t have a Win XP box anymore, only Win 7 and Win 10, so this might cause the problem. Please try Win 7 or higher!


JK
TeeEmCee
Posts: 262
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: SetUnhandledExceptionFilter in 64 bit

Postby TeeEmCee » Mar 18, 2018 2:24

Oh, OK. You do list win XP as supported. I don't have a more recent copy of Windows.
JK-IDE also works under wine, and I don't see any of those problems. However, I can't compile under wine, the program freezes. I'm not at all surprised that compiling doesn't work under wine, though; more likely to be a problem with wine than JK-IDE.

Anyway, all I wanted to know was how you generate a backtrace for FB code! I'm not going to switch away from emacs as my editor. I wrote a FB mode for emacs. But if JK-IDE had refactoring or other useful tools for FB, I might use it occasionally for specific tasks. (I haven't even looked into other FB IDEs, though)
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 18, 2018 19:15

@TeeEmCee

Oh, OK. You do list win XP as supported


Correct - i must change that. If you want to know, how i do this, then please drop me a mail at <jk-ide at t minus online dot de> (you can see the deciphered e-mail address in the help file under "Issues and Bugs" too). So we can discuss this in private.


JK
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SetUnhandledExceptionFilter in 64 bit

Postby MrSwiss » Mar 18, 2018 20:38

<off topic>
Juergen Kuehlwein wrote:So we can discuss this in private.
IMHO, a bad idea, since others (like myself) might also be interested, in this!
However: since it seems to me to be 'off topic', in this tread ... why not open a new thread for it?
</off topic>
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 19, 2018 22:53

Well, this is going to be a bit of a lengthy explanation...

First of all you must know the difference between a runtime error and a GPF (you may skip this section, if you already know all this)

A runtime error is an error condition detected by the application itself. Such an error doesn´t necessarily mean, that the application must terminate immmediately. A variable (ERR) is set indicating the type of error, so that the application can recover from or react on this error. This is a widespread mechanism implemented in various programming languages, even many Windows API return error conditions and the GetLastError API returns the latest error code.


A GPF is an error condition detected by the operating system, which results in abnormal termination of an application (aka "crash"). But just like with runtime errors there is a way of handling these error conditions. It is called SEH (Structured Exception Handling). If you register an error handler (this is like a callback function) for your application, this handler is called in case of an GPF. The handler can do "things" and return three different meaningful values:

- EXCEPTION_CONTINUE_SEARCH : couldn´t handle this kind of exception, look for another handler, which might handle it
- EXCEPTION_CONTINUE_EXECUTION : exception was fixed, continue application
- EXCEPTION_EXECUTE_HANDLER : this kind of exeception cannot be fixed, execute the system handler, JIT (just in time debugger) or Dr. Watson or similar

There may be more than one exception handler and these handlers may be chained. The "SetUnhandledExceptionFilter" API sets the "last chance" handler for an application. That is, if a GPF wasn´t handled by another handler in the SEH chain, this handler is called before the exception is passed back to the system.

SetUnhandledExceptionFilter works differently in 32 and in 64 bit Windows, for 64 bit the compiler must add a special section to the executable, which contains a function table for stack unwinding. This is necessay in 64 bit because of a different calling convention, which makes a stack unwind like in 32 bit impossible without further information. If this list isn´t present SetUnhandledExceptionFilter fails in 64 bit, meaning - it doesn´t catch exceptions as expected.

SetVectoredExceptionHandler registers an exception handler just like SetUnhandledExceptionFilter, but it works without the function table. So this is one way of doing it both worlds (32 and 64 bit).



How to get the file and line number, where the crash happens ?

Basically this very simple: i set a crash marker (a global variable) holding the file and line number before executing a regular line of code. This requires the existing code to be analyzed and processed so that before each line of code a crash marker is added where appropriate to keep it compilable - this is the difficult part.

If a crash occurs, my registered handler reads the crash marker and takes the necessary steps to make this file and line visible in the IDE. A short description of the kind of exception is shown in the IDE as well.

I made all of this work for PowerBASIC in my IDE and i will make it work for FreeBASIC too. Basically it already works - but not for all kinds of code. There are still problems preprocessing some of the sample code i use for testing. I think i understand now, why it works and why it fails in some cases. It´s only a matter of time still to get it running properly.


JK
TeeEmCee
Posts: 262
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: SetUnhandledExceptionFilter in 64 bit

Postby TeeEmCee » Mar 21, 2018 10:03

Thanks for writing this up! I certainly learnt from it. Didn't know about AddVectoredExceptionHandler, and I haven't looked into how things work in 64 bit Windows, since I'm content to avoid additional compiles. I wasn't expecting that you're instrumenting the code, but it makes perfect sense. I expect one of the most annoying parts of getting that to work was elseif statements.
TeeEmCee
Posts: 262
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: SetUnhandledExceptionFilter in 64 bit

Postby TeeEmCee » Mar 22, 2018 2:15

SEH and SetUnhandledExceptionFilter makes a lot more sense after reading this:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679353(v=vs.85).aspx

I'm jealous that Microsoft implemented something superior to standard C++ exceptions (and to the DWARF and SJLJ exception handling supported by mingw), but it's Windows-only.
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 22, 2018 21:18

I just found out that SetUnhandledExceptionFilter works in 64 bit like a charm with the following compiler switches:

Code: Select all

-gen gcc -Wc -fexceptions -Wc -funwind-tables


the default would be "fno-exceptions" and "-fno-unwind-tables", so you must set them by hand in order to get SEH working in 64 bit just like in 32 bit. This adds to my previous post here about SEH, obviously these switches make gcc generate the required tables for 64 bit.


JK
MrSwiss
Posts: 3307
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SetUnhandledExceptionFilter in 64 bit

Postby MrSwiss » Mar 23, 2018 0:08

Juergen Kuehlwein wrote:I just found out that SetUnhandledExceptionFilter works in 64 bit like a charm with the following compiler switches:

Does that now mean, that the compiler options have to be switched, between: 'debug' / 'release' mode?
TeeEmCee
Posts: 262
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: SetUnhandledExceptionFilter in 64 bit

Postby TeeEmCee » Mar 23, 2018 8:17

Juergen Kuehlwein wrote:I just found out that SetUnhandledExceptionFilter works in 64 bit like a charm with the following compiler switches:

Code: Select all

-gen gcc -Wc -fexceptions -Wc -funwind-tables


This isn't right; fbc only accepts a single -Wc argument. Specifying -Wc twice causes the second to override the first one. You have to write

Code: Select all

fbc -gen gcc -Wc -fexceptions,-funwind-tables


Same thing for -Wl and -Wa.
Juergen Kuehlwein
Posts: 271
Joined: Mar 07, 2018 13:59
Location: Germany

Re: SetUnhandledExceptionFilter in 64 bit

Postby Juergen Kuehlwein » Mar 23, 2018 14:08

Well, it definitely would have saved me and for sure many other people a lot of time, if gcc was clever enough to add those tables by itself as soon as it detects "SetUnhandledExeceptionFilter", because this is a requirement for this function to work properly in 64 bit Windows...


@MrSwiss

you don´t need a debug build, "release" is ok


@TeeEmCee

you are right! Further testing showed that "-fexception" isn´t necessary, so "-Wc -funwind-tables" is the trick, which finally does it.


JK

Return to “General”

Who is online

Users browsing this forum: Baidu [Spider], MSN [Bot] and 2 guests