Trigger an "on demand" freeze detection stack trace

delphi package - automated exception handling
Post Reply
rossmcm
Posts: 74
Joined: Thu Jun 09, 2005 2:05 am
Contact:

Trigger an "on demand" freeze detection stack trace

Post by rossmcm »

Freeze detection is invaluable for finding where my program has gone when it has wandered off into the weeds. Is there any way I can trigger the freeze detection manually, i.e. carry out the same procedure that happens for a freeze detection, but arbitrarily-not waiting for the freeze detection delay to expire? I guess this is going to be more difficult if the application is not dealing with messages (i.e. I don't imaging it would be possible to have the traceback report triggered by a button or (even better) by a keystroke), but madExcept manages to do this for freeze detection.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Trigger an "on demand" freeze detection stack trace

Post by madshi »

Code: Select all

  function CreateFreezeReport : UnicodeString;
  var exc : IMEException;
  begin
    exc := NewException(etFrozen, nil, nil, true, 0, 0, 0, nil, MESettings, esManual, nil, 0, '');
    exc.ShowPleaseWaitBox := false;
    exc.ListThreads := true;
    result := exc.GetBugReport;
  end;
rossmcm
Posts: 74
Joined: Thu Jun 09, 2005 2:05 am
Contact:

Re: Trigger an "on demand" freeze detection stack trace

Post by rossmcm »

Thanks Mathias, but how would I trigger this from an application that was stuck in loop and wasn't calling Application.ProcessMessages, running timers, etc.?

How does MadExcept do it? I.e what happens when MadExcept is enabled and it detects that the freeze timeout has expired?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Trigger an "on demand" freeze detection stack trace

Post by madshi »

You would trigger this from a secondary thread, which maybe sleeps a lot of time and then checks (maybe once every 200ms or so) whether your application has frozen.
rossmcm
Posts: 74
Joined: Thu Jun 09, 2005 2:05 am
Contact:

Re: Trigger an "on demand" freeze detection stack trace

Post by rossmcm »

OK, that's presumably how you do it in MadExcept but, let be explain the scenario: say I have an application that has MadExcept enabled and freeze detection set to 1 minute. There turns out to be a place during execution where it freezes for 10 seconds every now and then - not long enough to trigger ME's freeze detection, but long enough to be annoying. So I want some way of triggereing a freeze report while it is in this state. How would I do this with a secondary thread?

Thanks for your time Mathias.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Trigger an "on demand" freeze detection stack trace

Post by madshi »

I'm really not sure what you're asking. How to detect the freeze? Or how to tell madExcept to handle a freeze which you've detected? You said earlier that you know how to detect a freeze. So I didn't say anything more about that topic. If you just want to know how to tell madExcept to handle a freeze then refer to the code in my first post in this thread.

If you don't want the freeze report as a text file but instead want madExcept to display a freeze window, then simply replace "exc.GetBugReport" with "exc.Show". Or alternatively use the following line of code:

Code: Select all

HandleException(etFrozen, nil, nil, true, 0, 0, nil, esAntiFreezeCheck);
rossmcm
Posts: 74
Joined: Thu Jun 09, 2005 2:05 am
Contact:

Re: Trigger an "on demand" freeze detection stack trace

Post by rossmcm »

I'm really not sure what you're asking. How to detect the freeze? Or how to tell madExcept to handle a freeze which you've detected?
Neither! :? Here is the scenario that I'm interested in covering.

Say I have a project that uses ME, but has freeze detection disabled. Say I have a construct in that program like:

Code: Select all

repeat
    do something
    if exit condition true
        break
    Application.ProcessMessages 
until false 
but because of a bug, exit condition is never true. If I have a key handler like:

procedure Form1.Form1OnKeydown

Code: Select all

case key of

    27 : StrToFile ('freeze.txt', CreateFreezeReport) 
hitting the ESC would trigger a report, and the resulting stack trace shows where the main program was when the key event was handled.

What can I do if the repeat..until block doesn't call the message pump? How do I trigger the call to CreateFreezeReport when my program is REALLY hung. MadExcept is obviously able to do it when it's freeze timer counts down to zero. Does it do this by running the freeze detection in a separate thread?

The reason I am interested is this: I had an application that was spending much longer than I anticipated while it was initializing - like twenty seconds when it should have only taken around one second. There were various ways I could have traced this down, but I did it without needing to touch the code by temporarily setting the MadExcept freeze timeout to 10 seconds, running it, and waiting (Of course this method doesn't work if your program is stuck in a loop that IS calling the message pump...).

That's when it occurred to me that it would be good to have the ability to trigger a freeze report on demand. I know now how to generate the report - it's just the "on demand" I'm struggling with!
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Re: Trigger an "on demand" freeze detection stack trace

Post by madshi »

> What can I do if the repeat..until block doesn't call the message pump?

Wait until madExcept's automatic freeze detection kicks in? Or run the madTraceProcess.exe tool?

> How do I trigger the call to CreateFreezeReport when my program is REALLY hung.

I already answered that in my 2nd post: By using a secondary thread, of course.

If you're asking how to detect a key press when the main thread is frozen: Key events are usually sent by the OS to top-level windows. But if your main thread is frozen, those messages won't arrive. So you may have to use a keyboard hook (e.g. SetWindowsHookEx(WH_KEYBOARD_LL)). But maybe manually starting madTraceProcess.exe is all you need?
rossmcm
Posts: 74
Joined: Thu Jun 09, 2005 2:05 am
Contact:

Re: Trigger an "on demand" freeze detection stack trace

Post by rossmcm »

Brilliant!

MadTraceProcess32.exe is all I need. Wish I had noticed that a bit sooner.

Many thanks Mathias.
Post Reply