我在 Delphi 2007 中编写了应用程序,有时会挂起(甚至不是每周,应用程序都在 24/7 运行)。看起来主线程卡住了。有哪些选项可以查明此问题的原因?
应用程序是用 Delphi 2007 编写的,它使用 RemObjects,DBExpress 和 Firebird,使用 COM 的 OPC 通信。
我在 Delphi 2007 中编写了应用程序,有时会挂起(甚至不是每周,应用程序都在 24/7 运行)。看起来主线程卡住了。有哪些选项可以查明此问题的原因?
应用程序是用 Delphi 2007 编写的,它使用 RemObjects,DBExpress 和 Firebird,使用 COM 的 OPC 通信。
使用 MadExcept,您可以指定定期检查主线程以仍然处理消息(使用可变超时,您可以将其设置为高于最长操作所需的值)。如果主线程挂起,您可以获得堆栈跟踪。
请参阅主线程冻结检查...
我已经成功地使用了它,它仅在挂起在驱动程序中时才失败(这可能是意料之中的)。在开始怀疑驱动程序(对于 A/D 转换卡)之后,我在每次 API 调用之前和之后添加了跟踪消息,并且能够证明驱动程序是罪魁祸首。请注意,立即将消息写入文件并刷新缓冲区以获得可靠的日志数据非常重要。
我还成功地使用WinDbg在未安装 Delphi 的系统上附加到挂起的可执行文件。事实证明这是一个僵局,因为并不总是以相同的顺序获取关键部分。WinDbg 通过检查线程堆栈和临界区的状态来帮助分析这种情况。
I used a "watchdog" thread for this, which checks if the mainform is responding, and make a minidump (you can load this dump with WinDbg, use map2dbg.exe to convert a Delphi .map to a .dbg).
FMainformHandle := Application.MainForm.Handle;
Result := SendMessageTimeOut( FMainformHandle, WM_NULL, 0, 0,
SMTO_NORMAL or SMTO_ABORTIFHUNG,
C_TIME_OUT_SECONDS * 1000, //wait 1minute
iRes) <> 0;
if not Result then
begin
hFile := CreateFile(PChar(Result), GENERIC_WRITE, FILE_SHARE_WRITE, nil,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
try
MiniDumpWriteDump(GetCurrentProcess, GetCurrentProcessId, hFile,
aDumpType, nil, nil ,nil);
finally
FileClose(hfile);
end;
end;
But you can also use
jclDebug.pas:
JclCreateThreadStackTraceFromID(MainthreadId)
for this (no need for WinDbg etc, only the JCL + Delphi .map)
3rd option is to use my new sampling profiler, which has a "process stack viewer", so you can watch the stack of any thread of a running process (I used SysInternals Process Explorer for this before, but it needs .dbg files). It uses .map, TD32, JDBG etc (any Delphi debug info) for stack tracing.
You can use this when you app hangs, to investigate the stack.
Windows API (for MiniDumpWriteDump):
http://sourceforge.net/projects/jedi-apilib/files/JEDI%20Windows%20API/
WinDbg:
http://www.microsoft.com/whdc/devtools/debugging/installx86.Mspx
Map2Dbg:
http://code.google.com/p/map2dbg/
JEDI JCL:
http://jcl.delphi-jedi.org/
AsmProfiler, samling mode: (still under development!)
http://asmprofiler.googlecode.com/files/AsmSamplingProfiler0.4.zip