title: Delphi obtain stack trace after exception
body: <p>I'm trying to figure out how to obtain a stack trace after an exception is thrown in Delphi. However, when I try to read the stack in the Application.OnException event using the function below, the stack already seems to be flushed and replaced by the throwing procedures.</p>
  
  <pre><code>function GetStackReport: AnsiString;
  var
      retaddr, walker: ^pointer;
  begin
  
      // ...
  
      // History of stack, ignore esp frame
      asm
          mov walker, ebp
      end;
  
      // assume return address is present above ebp
      while Cardinal(walker^) &lt;&gt; 0 do begin
          retaddr := walker;
          Inc(retaddr);
          result := result + AddressInfo(Cardinal(retaddr^));
          walker := walker^;
      end;
  end;
  </code></pre>
  
  <p>Here's what kind of results I'm getting:</p>
  
  <pre><code>001A63E3: TApplication.HandleException (Forms)
  00129072: StdWndProc (Classes)
  001A60B0: TApplication.ProcessMessage (Forms)
  </code></pre>
  
  <p>That's obviously not what I'm looking for, although it's correct. I'd like to retrieve the stack as it was just before the exception was thrown, or in other words the contents before (after would do too) the OnException call.</p>
  
  <p><strong>Is there any way to do that?</strong></p>
  
  <p>I am aware that I'm reinventing the wheel, because the folks over at madExcept/Eurekalog/jclDebug have already done this, but I'd like to know how it's done.</p>