[fpc-devel] BackTraceStrFunc / reset to system handler

Martin lazarus at mfriebe.de
Fri Feb 24 22:09:52 CET 2012


In light of a recent mail thread on the topic and 
http://bugs.freepascal.org/view.php?id=21370

From:  rtl\inc\lnfodwrf.pp

function DwarfBackTraceStr(addr : Pointer) : shortstring;
var
...
   Success : boolean;
begin
   { reset to prevent infinite recursion if problems inside the code }
   Success:=false;
   Store := BackTraceStrFunc;
   BackTraceStrFunc := @SysBackTraceStr;
   Success:=GetLineInfo(ptruint(addr), func, source, line);
   { create string }
.... // no access to Success
   if Success then
     BackTraceStrFunc := Store;
end;


This also unsets the procedure for the perfectly valid case, that some 
part of the code has no debug info (e.g kernel)
Of course, if there are many such frames this has a speed up effect too...

But as protection against recursion, it would be better to do something 
like:

Var DwarfBackTraceStrCount: Integer = 0; // global/ maybe thread

function DwarfBackTraceStr(addr : Pointer) : shortstring;
var
...
   Success : boolean;
begin
   { reset to prevent infinite recursion if problems inside the code }
   if DwarfBackTraceStrCount > 0 then begin
     result := SysBackTraceStr(addr);
     exit;
   end;

   inc(DwarfBackTraceStrCount);
   ....
   dec(DwarfBackTraceStrCount); // in case of crash, this will not be 
reached
end;

Of course, then an app that has its own painter to the function will no 
longer benefit from that. But it could reset the global variable...





More information about the fpc-devel mailing list