views:

217

answers:

1

I have a console application written in Free Pascal, that like most larger applications sometimes crashes. As this application is live, I usually ask people to write me down the stack-trace on crash -- I distribute it with both debug and lineinfo. However, the application uses FPC video.pas output, and sometimes the text output stacktrace is not visible.

Is there a way to intercept the text that is written on unhandled exceptions WITH the stack trace, so I can write it to file? I can't just pipe standard error to a file, because FPC video somehow doesn't work with that, and also I'd like people just running the executable not a batch or shell file.

As an example, I'd like to catch this to a file or other output source:

ERangeError : Range check error∙
  $0048C0EA  TCELLS__GETCELL,  line 104 of dfmap.pas
  $004AD133  TDOOMGENERATOR__GENERATECITYDUNGEON,  line 397 of dfdungen.pas
  $004AF87D  TDOOMGENERATOR__GENERATE,  line 760 of dfdungen.pas
  $0041293B  TDOOM__RUN,  line 354 of doombase.pas
  $00401CD6  main,  line 51 of doomrl.pas

Any possibility to do that cleanly?

+1  A: 

There are two (system unit) procedure variables involved:

  • Exceptproc which handles the exception
  • and backtracestrfunc that handles address to lineinfo retrieval.

The current handling iirc is layered

  • the system unit only terminates with an error but defines above procedure variables to override this.
  • the sysutils unit converts RTEs to an language exception and overrides exceptproc with a default language exception handler (sysutils.catchunhandledexception)
  • the lineinfo (for stabs) or the linfodward (dwarf, 2.4.0+) units override the backtracestrfunc procedure variable to provide the lineinfo.

So in short:

  • copy the sysutils.catchunhandledexception routine to your own code.
  • only modify the way the copy does output in some way that you like, and make it rock solid (since exceptions during exceptions are confusing and annoying)
  • assign the functionanme of the copy to exceptproc

Note that the lineinfo unit is implicitely included when you do -gl

Marco van de Voort
Thanks a lot, somehow I'd expect that you'd be the only one to know the answer :>
Kornel Kisielewicz
Naah, I'm only one of the minor FPC devels.
Marco van de Voort