Delphi:Exception输出堆栈信息
起源:
用习惯了c#之Exception的StackTrace,在程序出异常crash时候能够以其定位出问题的模块及行号,用回Delphi 2009,发现没有这东西。
显然,在编译环境日新月异的今天,是不科学的。分析Delphi的Exception,发现些线索:StackTrace。
应该有戏!
继续下去,验证输出这个StackTrace,它是空的,里面没有预想要的内容。再深究去,发现并没想象那么容易,它扯到不少蛋。
1、Exception类
Delphi 2009中,StackTrace是如此定义的,与它一起的还有几个var:
Exception = class(TObject)
private
...
protected
...
public
...
property StackTrace: string read GetStackTrace;
property StackInfo: Pointer read FStackInfo;
{$IFDEF MSWINDOWS}
class var
// Hook this function to return an opaque data structure that contains stack information
// for the given exception information record. This function will be called when the
// exception is about to be raised or if this is an external exception such as an
// Access Violation, called soon after the object is created.
GetExceptionStackInfoProc: function (P: PExceptionRecord): Pointer;
// This function is called to return a string representation of the above opaque
// data structure
GetStackInfoStringProc: function (Info: Pointer): string;
// This function is called when the destructor is called to clean up any data associated
// with the given opaque data structure.
CleanUpStackInfoProc: procedure (Info: Pointer);
// Use this function to raise an exception instance from within an exception handler and
// you want to "acquire" the active exception and chain it to the new exception and preserve
// the context. This will cause the FInnerException field to get set with the exception
// in currently in play.
// You should only call this procedure from within an except block where the this new
// exception is expected to be handled elsewhere.
class procedure RaiseOuterException(E: Exception); static;
// Provide another method that does the same thing as RaiseOuterException, but uses the
// C++ vernacular of "throw"
class procedure ThrowOuterException(E: Exception); static;
{$ENDIF}
end;
明明白白的说,我东西给你了,你要用,自己想办法。你编译环境都不能实现,让我想什么办法?去它主页上看看:

蛋疼之极,欲用StackTrace,它居然要求我们用第三方的解决方案。如此不负责任!
好吧,上JEDI。
2、JclDebug
StackOverflow是个技术的绝佳去处,曾在其上受益不少,再去找找,果然有所得。
有个家伙封装了实现方法,借鉴下来,代码如下:
unit StackTrace; interface uses
SysUtils, Classes, JclDebug; implementation function GetExceptionStackInfoProc(P: PExceptionRecord): Pointer;
var
LLines: TStringList;
LText: String;
LResult: PChar;
begin
LLines := TStringList.Create;
try
JclLastExceptStackListToStrings(LLines, True, True, True, True);
LText := LLines.Text;
LResult := StrAlloc(Length(LText));
StrCopy(LResult, PChar(LText));
Result := LResult;
finally
LLines.Free;
end;
end; function GetStackInfoStringProc(Info: Pointer): string;
begin
Result := string(PChar(Info));
end; procedure CleanUpStackInfoProc(Info: Pointer);
begin
StrDispose(PChar(Info));
end; initialization
// Start the Jcl exception tracking and register our Exception
// stack trace provider.
if JclStartExceptionTracking then
begin
Exception.GetExceptionStackInfoProc := GetExceptionStackInfoProc;
Exception.GetStackInfoStringProc := GetStackInfoStringProc;
Exception.CleanUpStackInfoProc := CleanUpStackInfoProc;
end; finalization
// Stop Jcl exception tracking and unregister our provider.
if JclExceptionTrackingActive then
begin
Exception.GetExceptionStackInfoProc := nil;
Exception.GetStackInfoStringProc := nil;
Exception.CleanUpStackInfoProc := nil;
JclStopExceptionTracking;
end; end.
很是优美。但单步去跟,发现GetExceptionStackInfoProc中,其返回值为空。
作者也发现了,所幸另个人解决这个问题,替换其函数如下:
function GetExceptionStackInfoProc(P: PExceptionRecord): Pointer;
var
LLines: TStringList;
LText: String;
LResult: PChar;
jcl_sil: TJclStackInfoList;
begin
LLines := TStringList.Create;
try
jcl_sil := TJclStackInfoList.Create(True, , p.ExceptAddr, False, nil, nil);
try
jcl_sil.AddToStrings(LLines, true, true, true, true);
finally
FreeAndNil(jcl_sil);
end;
LText := LLines.Text;
LResult := StrAlloc(Length(LText));
StrCopy(LResult, PChar(LText));
Result := LResult;
finally
LLines.Free;
end;
end;
验证OK,解决问题!


这样,可以挂接Application.OnException,自个处理程序的Crash异常了。
注意事项:用JEDI求堆栈信息,须设置Delphi工程属性之Linking->Map file值为Detailed,它是基于解析map文件生成数据:

参考资料:
System.SysUtils.Exception.StackTrace
Delphi - Trying to get StackTrace for an exception
Delphi:Exception输出堆栈信息的更多相关文章
- Slf4j 打日志的问题 Exception 没有堆栈信息
Slf4j 打日志的问题 Exception 没有堆栈信息 发现线上环境有的Exception堆栈信息没打出来,只有异常信息没有堆栈信息,难以定位 一般情况下日志这么打 log.info(" ...
- C++ crash 堆栈信息获取(三篇文章)
最近在做程序异常时堆栈信息获取相关工作,上一篇文章成功的在程序creash时写下了dump文件,而有些情况写dump文件是 不可以的,比如在jni开发时,C++只做底层处理,而整个项目是android ...
- 在Linux与Windows上获取当前堆栈信息
在编写稳定可靠的软件服务时经常用到输出堆栈信息,以便用户/开发者获取准确的运行信息.常用在日志输出,错误报告,异常检测. 在Linux有比较简便的函数获取堆栈信息: #include <stdi ...
- C++ crash 堆栈信息获取
最近在做程序异常时堆栈信息获取相关工作,上一篇文章成功的在程序creash时写下了dump文件,而有些情况写dump文件是 不可以的,比如在jni开发时,C++只做底层处理,而整个项目是android ...
- C++ crash 堆栈信息获取(三篇)
最近在做程序异常时堆栈信息获取相关工作,上一篇文章成功的在程序creash时写下了dump文件,而有些情况写dump文件是 不可以的,比如在jni开发时,C++只做底层处理,而整个项目是android ...
- 使用log4j的时候如何输出printStackTrace()的堆栈信息
使用log4j的时候如何输出printStackTrace()的堆栈信息 研究了一下发现很简单,如下: log.error(e.getMessage(),e); 输出信息如下: 2009-05-11 ...
- Delphi中使用Dos窗口输出调试信息
在项目文件 *.DPR (Project->View Source) 里加上{$APPTYPE CONSOLE} 然后,在需要输出处加上 Writeln(‘your debug messa ...
- Delphi之Exception获得错误信息(简单好理解)
Delphi之Exception获得错误信息 相关资料: http://www.cnblogs.com/hackpig/archive/2010/02/15/1668547.html 实例代码: 1 ...
- Delphi RAD Berlin OutputDebugString 输出调试信息
Delphi RAD Berlin Event Log.OutputDebugString 输出调试信息,仅在win VCL下可以用.OutputDebugString(PChar('hellowor ...
随机推荐
- mysqldump备份时保持数据一致性
对MySQL数据进行备份,常见的方式如以下三种,可能有很多人对备份时数据一致性并不清楚 1.直接拷贝整个数据目录下的所有文件到新的机器.优点是简单.快速,只需要拷贝:缺点也很明显,在整个备份过程中新机 ...
- Python基本数据类型以及字符串
基本数据类型 数字 int ,所有的功能,都放在int里 a1 = 123 a1 = 456 ...
- sqoop连接SqlServer2012示例
sqoop import --connect 'jdbc:sqlserver://192.168.xx.xx:1433;username=sa;password=xxxx;database=WindE ...
- 基于官方镜像MySQL做自定义业务扩充镜像
转自:https://www.cnblogs.com/jsonhc/p/7809571.html 首先从https://hub.docker.com/_/mysql/拉取官方镜像,如果速度缓慢,建议添 ...
- delphi控制本计算机和远程计算机关机等
unit mainunit; {远程关机源码} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Con ...
- Hibernate 再接触 多对一与一对多
多对一单向关联 数据库设计: 错误做法:在多方加外键 在多这一方加外键 第一种 annotation Group.java package com.bjsxt.hibernate; import ja ...
- HTML+CSS基础课程
慕课网上的课程 为了方便以后查阅 现做笔记如下 首先 了解下面的知识 1. HTML是网页内容的载体.内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2. CSS样式是 ...
- Android高级控件(上)
Toast信息提示框 bt1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeTe ...
- 吴裕雄 32-MySQL 导入数据
1.mysql 命令导入使用 mysql 命令导入语法格式为:mysql -u用户名 -p密码 < 要导入的数据库数据(runoob.sql)实例:# mysql -uroot -p123456 ...
- 用工厂模式和策略模式优化过多的if-else
多个if-else代码: @RunWith(SpringRunner.class) @SpringBootTest public class EducationalBackgroundTest { p ...