27

I am pretty sure I have seen this before, but I haven't found out / remembered how to do it. I want to have a line of code that when executed from the Delphi debugger I want the debugger to pop-up like there was a break point on that line.

Something like:

FooBar := Foo(Bar);
SimulateBreakPoint; // Cause break point to occur in Delphi IDE if attached
WriteLn('Value: ' + FooBar);

Hopefully that makes sense. I know I could use an exception, but that would be a lot more overhead then I want. It is for some demonstration code.

Thanks in advance!

StanK
  • 4,750
  • 2
  • 22
  • 46
Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194

2 Answers2

39

To trigger the debugger from code (supposedly, I don't have a copy of delphi handy to try):

asm int 3 end;

See this page:

http://17slon.com/blogs/gabr/2008/03/debugging-with-lazy-breakpoints.html

Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194
Joeri Sebrechts
  • 11,012
  • 3
  • 35
  • 50
  • That was it. Worked great! Thanks! Now I need to find a way to move up the call stack for the break point. . . . – Jim McKeeth Oct 02 '08 at 05:36
  • Beware to catch EExternal else there will be an external exception while running outside of the debugger. – Toon Krijthe Oct 02 '08 at 06:40
  • 3
    Or, as I suggested: if DebugHook <> 0 then asm int 3 end; – gabr Oct 02 '08 at 07:26
  • 2
    I'd usually do "if (DebugHook <> 0) and (**Break Condition**) then asm int 3 end;" so i'd add a condition to break, and put this code right before an error/bug in some conditions. – Osama Al-Maadeed Jun 05 '09 at 17:02
14

As Andreas Hausladen stated in comments to that artice, Win32 API DebugBreak() function is less DOS-ish and works equally well.

gabr
  • 26,580
  • 9
  • 75
  • 141