开发者

Catching stack-trace in Free Pascal

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
  $00401C开发者_如何学CD6  main,  line 51 of doomrl.pas

Any possibility to do that cleanly?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜