forked from maxkleiner/python4delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclErrorHandling.pas
More file actions
268 lines (238 loc) · 6.25 KB
/
Copy pathclErrorHandling.pas
File metadata and controls
268 lines (238 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
{
Copyright (C) 1999 - 2002 Clever Components
www.CleverComponents.com
}
unit clErrorHandling;
interface
uses
SysUtils, Forms;
type
TclLogger = class
private
FPrevOnAppException: TExceptionEvent;
procedure HandleAppException(Sender: TObject; E: Exception);
procedure HookExceptions;
procedure UnhookExceptions;
function GetModuleVersionInfo: string;
constructor CreateInstance;
class function AccessInstance(Request: Integer): TclLogger;
procedure PutMessageToFile(const AMessage: string);
public
constructor Create;
destructor Destroy; override;
class function Instance: TclLogger;
class procedure ReleaseInstance;
procedure LogMessage(const AMessage: string);
end;
implementation
uses
Windows, VerInfo{, imagehlp};
const
StoredCallStackDepth = 26;
type
TCallStack = Array[0..StoredCallStackDepth] of Pointer;
var
frame: TCallStack;
fOldExceptObjProc: Pointer;
procedure FillCallStack(var St : TCallStack; const ExcludeFirstLevel: Boolean);
var
i : integer;
_EBP : Integer;
_ESP : Integer;
begin
asm
mov _ESP, esp
mov _EBP, ebp
end;
if ExcludeFirstLevel then
begin
_ESP:= _EBP;
_EBP:= PInteger(_EBP)^;
end;
FillChar(St, SizeOf(St), 0);
if (_EBP<_ESP) or (_EBP-_ESP>30000) then Exit;
for i:= 0 to StoredCallStackDepth do
begin
_ESP:= _EBP;
_EBP:= PInteger(_EBP)^;
if (_EBP<_ESP) or (_EBP-_ESP>30000) then Exit;
St[i]:= Pointer(PInteger(_EBP+4)^-4);
end;
end;
{procedure GetCallStack(var St : TCallStack);
var
i: Integer;
stk: STACKFRAME;
Cnt: _CONTEXT;
begin
FillChar(St, SizeOf(St), 0);
Cnt.ContextFlags := CONTEXT_CONTROL;
GetThreadContext(GetCurrentThread(), Cnt);
ZeroMemory(@stk, SizeOf(STACKFRAME));
stk.AddrPC.Offset := Cnt.Eip;
stk.AddrPC.Mode := AddrModeFlat;
stk.AddrStack.Offset := Cnt.Esp;
stk.AddrStack.Mode := AddrModeFlat;
stk.AddrFrame.Offset := Cnt.Ebp;
stk.AddrFrame.Mode := AddrModeFlat;
for i := 0 to StoredCallStackDepth do
begin
if (not StackWalk(IMAGE_FILE_MACHINE_I386, GetCurrentProcess(), GetCurrentThread(),
@stk, @Cnt, nil,
SymFunctionTableAccess, SymGetModuleBase, nil)) then
begin
Break;
end;
St[i]:= Pointer(stk.AddrPC.Offset);
end;
end;}
function CallStackTextualRepresentation(const S: TCallStack; const LineHeader: string): string;
var
i: integer;
begin
i:= 0;
Result:= '';
while (i <= StoredCallStackDepth) and (S[i] <> Nil) do
begin
Result:= Result + LineHeader + 'call stack - ' + IntToStr(i) + ' : 0x' + IntToHex(Cardinal(S[i]), 8) + #13#10;
i:= i + 1;
end;
end;
procedure clUnwindStack(P: PExceptionRecord); stdcall;
var
s: string;
begin
s := Format('Exception Code: 0x%.8x', [P.ExceptionCode]);
if (P.ExceptionCode = EXCEPTION_ACCESS_VIOLATION) then
begin
if (P.ExceptionInformation[0] = 1) then
s := s + Format(#13#10 + 'AV at 0x%.8x, write at address 0x%.8x', [Integer(P.ExceptionAddress), Integer(P.ExceptionInformation[1])])
else
s := s + Format(#13#10 + 'AV at 0x%.8x, read at address 0x%.8x', [Integer(P.ExceptionAddress), Integer(P.ExceptionInformation[1])])
end;
FillCallStack(frame, True);
// GetCallStack(frame);
s := s + #13#10 + CallStackTextualRepresentation(frame, '');
TclLogger.Instance().LogMessage('Stack Trace: ' + s);
end;
function clGetExceptionObject(P: PExceptionRecord): Exception;
asm
pusha
push p
call clUnwindStack;
popa
mov edx, fOldExceptObjProc
test edx, edx
jz @@no_handler
call edx
@@no_handler:
end;
{ TclLogger }
class function TclLogger.AccessInstance(Request: Integer): TclLogger;
const
FInstance: TclLogger = nil;
begin
case Request of
0 : ;
1 : if not Assigned(FInstance) then FInstance := CreateInstance;
2 : FInstance := nil;
else
raise Exception.CreateFmt('Illegal request %d in AccessInstance',
[Request]);
end;
Result := FInstance;
end;
constructor TclLogger.Create;
begin
inherited Create;
raise Exception.CreateFmt('Access class %s through Instance only',
[ClassName]);
end;
constructor TclLogger.CreateInstance;
begin
inherited Create();
end;
destructor TclLogger.Destroy;
begin
if AccessInstance(0) = Self then AccessInstance(2);
UnhookExceptions();
inherited Destroy();
end;
function TclLogger.GetModuleVersionInfo: string;
var
Info: TVersionInfo;
begin
Info := TVersionInfo.Create(ParamStr(0));
try
Result := Info.FileVersion;
finally
Info.Free();
end;
end;
procedure TclLogger.HandleAppException(Sender: TObject; E: Exception);
begin
if (E is EAccessViolation) then Exit;
LogMessage(Format('Exception (%s): %s', [E.ClassName, E.Message]));
end;
procedure TclLogger.HookExceptions;
begin
FPrevOnAppException := Application.OnException;
Application.OnException := HandleAppException;
end;
class function TclLogger.Instance: TclLogger;
begin
Result := AccessInstance(1);
end;
procedure TclLogger.LogMessage(const AMessage: string);
var
s: string;
begin
s := GetModuleVersionInfo();
if (s <> '') then
begin
s := 'File Version: ' + s + #13#10;
end;
s := DateTimeToStr(Now()) + ': ' + s + AMessage;
PutMessageToFile(s + #13#10#13#10);
end;
procedure TclLogger.PutMessageToFile(const AMessage: string);
var
FileName: string;
hFile: THandle;
len, cnt: Cardinal;
buf: PChar;
begin
FileName := ChangeFileExt(ParamStr(0), '.log');
hFile := CreateFile(PChar(FileName), GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile = INVALID_HANDLE_VALUE) then
begin
hFile := CreateFile(PChar(FileName), GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, nil,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
end;
if (hFile <> INVALID_HANDLE_VALUE) then
begin
SetFilePointer(hFile, 0, nil, FILE_END);
len := Length(AMessage) + 1;
GetMem(buf, len);
StrCopy(buf, PCHAR(AMessage));
WriteFile(hFile, buf[0], len - 1, cnt, nil);
FreeMem(buf);
CloseHandle(hFile);
end;
end;
class procedure TclLogger.ReleaseInstance;
begin
AccessInstance(0).Free();
end;
procedure TclLogger.UnhookExceptions;
begin
Application.OnException := FPrevOnAppException;
end;
initialization
fOldExceptObjProc := ExceptObjProc;
ExceptObjProc := @clGetExceptionObject;
TclLogger.Instance().HookExceptions();
finalization
TclLogger.ReleaseInstance();
end.