开发者

How do I debug my program when it hangs?

开发者 https://www.devze.com 2023-02-02 21:54 出处:网络
I have an application which takes measurements every second (I am running it in demo mode & generating random data, so the problem is not to do with reading from devices attached to the serial por

I have an application which takes measurements every second (I am running it in demo mode & generating random data, so the problem is not to do with reading from devices attached to the serial port).

After 5 or 6 minutes it hangs.

I have added

try
  // entire body of procedure/function goes here

except
     on E: Exception do
        begin
           MessageDlg('Internal coding error in <function name>()', 
                          mtError, [mbOK], 0);
        end;
end;

to every single function (and Application.Run() in the project file), but I don't see any message dialogs. Any idea how I can test this?


Upd开发者_如何学Goate: I woudl guess sit to be a resource problem, either RAM or MySql database - but other programs are running ok, and it's only 5 floats and timestamp that get saved with each measurement, so both seem unlikely after such a short time.


Solution: there were many great answers (thanks and +1 all round), but I finally got it (as suggested) by running in the IDE and using Run/Pause to see that it was an ever increasing loop.

Thanks again, everyone.


I'd try the following:

  1. Attach and click Pause and see where it is, what threads are running, what threads are waiting (if all of them then you have a deadlock).

  2. Refactor your main method into bunch of small ones (you may have already done that) and then replace small ones with dummy/hardcoded values. This may help but not necessarily identify faulty block.

  3. Watch the system resource consumption (handles, threads, etc.) with PerfMon or something. See if you running out of memory and start using HDD.

  4. If you using sockets, check if your reading timeout is set to infinity. If yes then change to some value and watch for timeouts.

  5. In .NET it's possible to enabling handling of all exceptions, which means before the code handle it (like in catch statement), the IDE breaks at the point of the exception. Enable that in Delphi if possible and see if you are getting any.


Use the debugger to find out what your app is doing when it appears to hang.

Run your program under the debugger. Let it run until it hangs. When it hangs, switch to the debugger and select "Debug: Break All" (or equivalent) to make the debugger freeze all threads and take control of the process.

Now open the Threads view in the debugger and examine the stack traces of each thread in your program. Start with the main thread, obviously. Be sure to look back through the call stack for several calls to see if you recognize any of your code. If you find a stack trace that is in the middle of a loop, examine the local variables to see if your loop control variable has somehow slipped past the exit condition and put you into an infinite loop.

If your stack traces indicate that every thread is blocked waiting for some external event, you may have a thread deadlock - thread A taking lock A, then trying to take lock B, while thread B is holding lock B and trying to take lock A. If you're not using threads in your program, this is less likely but still keep an eye out for it.

If nothing odd jumps out at you after reviewing the stack traces of the threads, let the program run a few seconds more, then break into it again with the debugger and have another look around. See what's different in the stack traces.

This should help you narrow down at least which bodies of code are involved in your hang.


If you don't see an exception before adding the exception handlers, you won't produce any MessageDlgs to see.

If the program is hanging (rather than blowing up with an exception) you could have a looping problem, or you could have some blocking call that's never completing. Write log messages to a window or file (you can use OutputDebugStr) to isolate the problem to one section of your code — at least to the body of one function. You may see the problem right away. If not, you can use OutputDebugStr, breakpoints, and trace to learn what's happening in that section of code on a line-by-line basis.


First: try to debug it in Delphi IDE. Second: if you can't do this (on customer PC), try the "Process Stack Viewer" of my (open source) sampling profiler: http://code.google.com/p/asmprofiler/wiki/ProcessStackViewer
(you need some debug info: a .map or .jdbg file) Then look at the stack of your threads (probably main/first thread). You can post the stack trace here then (if you can't find the problem).


If you want help with this sort of thing in an app that you cannot use the IDE for, then something like madExcept can help a lot. It has an automatic freeze checker for the main thread, and you can have it give you a stack dump to show what it was doing when it was frozen. The user can choose to kill or continue, and the app can tell madExcept it is busy and not to alert if appropriate (for doing long analysis or printing or something).

0

精彩评论

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

关注公众号