-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseDebug.pas
More file actions
331 lines (283 loc) · 9.67 KB
/
BaseDebug.pas
File metadata and controls
331 lines (283 loc) · 9.67 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
(*
@Abstract(Base debug unit)
(C) 2003-2012 George "Mirage" Bakhtadze. <a href="http://www.casteng.com">www.casteng.com</a> <br/>
The source code may be used under either MPL 1.1 or LGPL 2.1 license. See included license.txt file <br/>
Created: Jan 10, 2012
The unit provides debug functionality such as stack traces.
*)
{$Include GDefines.inc}
unit BaseDebug;
interface
uses BaseTypes;
{ A special function-argument. Should be called ONLY as Assert() argument.
Allows to obtain a source file name and line number by calling Assert() procedure with this function as an argument.
Doesn't require any debug information to be included in binary module.
The only requirement is inclusion of assertions code.
Tested in Delphi 7+ and FPC 2.4.2+.
Suggested usage:
Assert(GetCodeLoc);
This call will fill SourceFilename and LineNumber fields of TCodeLocation structure
which can be obtained by calling LastCodeLoc():
Log('File name: ' + LastCodeLoc().SourceFilename + ', line: ' + IntToStr(LastCodeLoc().LineNumber));
Always returns False. }
function _CodeLoc(): Boolean;
// Returns last obtained code location
function LastCodeLoc(): TCodeLocation;
type
// Pointer to exception information
PExceptionInfo = ^TExceptionInfo;
// Exception information
TExceptionInfo = record
ExceptionObj: TObject;
Msg: string;
StackTrace: TBaseStackTrace;
NestedExceptionAddresses: array of Pointer;
end;
{ Returns TCodeLocation with additional source information for the specified by address location.
An additional debug information (TD32, map file, etc) is required for the function to work. }
function GetCodeLocation(Address: Pointer): TCodeLocation;
// Returns array of TCodeLocation structures representing current stack trace, not including the call to GetStackTrace
function GetStackTrace(LevelsIgnore: Integer = 0): TBaseStackTrace; overload;
// Returns array of TCodeLocation structures representing stack trace of last exception occured
function GetExceptionStackTrace(): TBaseStackTrace; overload;
// Returns current stack trace as string, not including the call to GetStackTraceStr
function GetStackTraceStr(const StackTrace: TBaseStackTrace): string;
// Prevents compiler optimization to remove a variable which was passed to this function
procedure Volatile(var v);
implementation
uses
{$IFDEF USE_JCLDEBUG}
JCLDebug,
{$ENDIF}
Logger,
BaseStr,
SysUtils;
const
// Max number of stack trace entries
MAX_STACK_ENTRIES = 1000;
var
LastCodeLocation: TCodeLocation;
const SRC_UNKNOWN: string = 'Unknown';
// Assert error procedure which stores source location info and restores original assert handler
{$IFDEF FPC}
procedure GetCodeLocAssert(const Message, Filename: ShortString; LineNumber: LongInt; ErrorAddr: Pointer);
{$ELSE}
procedure GetCodeLocAssert(const Message, Filename: string; LineNumber: Integer; ErrorAddr: Pointer);
{$ENDIF}
begin
LastCodeLocation.Address := ErrorAddr;
LastCodeLocation.SourceFilename := Filename;
LastCodeLocation.UnitName := '';
LastCodeLocation.ProcedureName := '';
LastCodeLocation.LineNumber := LineNumber;
AssertRestore();
end;
function _CodeLoc(): Boolean;
begin
Result := not AssertHook(GetCodeLocAssert); // Prevent assertion error if hook failed
end;
function LastCodeLoc(): TCodeLocation;
begin
Result := LastCodeLocation;
end;
{$IFDEF FPC}
{ Parse FPC stack trace line to find out code location record.
Stabs: $08048377 TMYAPPLICATION__RUN, line 43 of testcallstack.lpr
DWARF 1/2/3b: $08048377 line 43 of testcallstack.lpr }
function ParseCodeLocation(const s: ShortString; var Res: TCodeLocation): Boolean;
var
CurPos, OldPos, LineLen: Integer;
begin
Result := False;
if s = '' then Exit;
// Writeln('ParseCodeLocation: ', s);
Res := GetCodeLoc('', '', '', 0, nil);
CurPos := Pos('$', s);
if CurPos > 0 then
Res.Address := PtrOffs(nil, StrToIntDef(Copy(s, CurPos, 9), 0))
else
CurPos := 1;
// Writeln('Addr: ', Copy(s, CurPos, 9), ' => $', IntToHex(Integer(Res.Address), 8));
OldPos := CurPos+9;
CurPos := Pos(',', s);
if CurPos > 0 then
Res.ProcedureName := Trim(Copy(s, OldPos, CurPos-OldPos))
else
CurPos := 1;
CurPos := PosEx('line ', s, CurPos) + 5;
while (CurPos <= Length(s)) and (s[CurPos] = ' ') do Inc(CurPos);
LineLen := PosEx(' of ', s, CurPos) - CurPos;
// Writeln('Line: ', CurPos, ' - ', CurPos + LineLen);
Res.LineNumber := StrToIntDef(Copy(s, CurPos, LineLen), 0);
Res.SourceFilename := Copy(s, CurPos + LineLen + 4, Length(s));
Result := True;
end;
{$ENDIF}
function GetCodeLocation(Address: Pointer): TCodeLocation;
{$IFDEF USE_JCLDEBUG}
var LocInfo: TJclLocationInfo;
{$ENDIF}
begin
Result.Address := Address;
// Stub implementation
Result.SourceFilename := SRC_UNKNOWN;
Result.UnitName := SRC_UNKNOWN;
Result.ProcedureName := SRC_UNKNOWN;
Result.LineNumber := 0;
{$IFDEF USE_JCLDEBUG}
// need some kind of additional debug information such as TD32 or map file
LocInfo := GetLocationInfo(Address);
Result.SourceFilename := LocInfo.SourceName;
Result.UnitName := LocInfo.UnitName;
Result.ProcedureName := LocInfo.ProcedureName;
Result.LineNumber := LocInfo.LineNumber;
{$ENDIF}
{$IFDEF FPC}
// need debug information (Stabs, Dwarf, etc) to work
ParseCodeLocation(BackTraceStrFunc(Address), Result);
{$ENDIF}
LastCodeLocation := Result;
end;
procedure Add(var Result: TBaseStackTrace; Address: Pointer);
begin
SetLength(Result, Length(Result)+1);
Result[High(Result)] := GetCodeLocation(Address);
end;
function GetStackTrace(LevelsIgnore: Integer = 0): TBaseStackTrace;
var
i: Integer;
Address: Pointer;
{$IFDEF FPC} BasePtr, LastPtr, CallerFrame: Pointer; {$ENDIF}
begin
i := 0;
try
{$IFDEF FPC}
BasePtr := get_frame;
if BasePtr <> nil then begin
LastPtr := PtrOffs(BasePtr, -1);
while (BasePtr > LastPtr) and (i < MAX_STACK_ENTRIES) do begin
Address := get_caller_addr(BasePtr);
CallerFrame := get_caller_frame(BasePtr);
if (Address = nil) or (CallerFrame = nil) then Break;
if (i >= LevelsIgnore) then Add(Result, Address);
Inc(i);
LastPtr := BasePtr;
BasePtr := CallerFrame;
end;
end;
{$ELSE}
{$IFDEF USE_JCLDEBUG}
Address := Caller(i + LevelsIgnore + 1);
while (Address <> nil) and (i < MAX_STACK_ENTRIES) do begin
Add(Result, Address);
Inc(i);
Address := Caller(i + LevelsIgnore + 1);
end;
{$ENDIF}
{$ENDIF}
except
Log('Exception in GetStackTrace', lkError);
end;
end;
function GetExceptionStackTrace(): TBaseStackTrace;
var
i: Integer;
{$IFDEF FPC} Frames: PPointer; {$ENDIF}
{$IFDEF USE_JCLDEBUG}
StackInfo: TJclStackInfoList;
{$ENDIF}
begin
{$IFDEF FPC}
Add(Result, ExceptAddr);
Frames := ExceptFrames;
for i := 0 to ExceptFrameCount - 1 do
Add(Result, Frames[i]);
{$ELSE}
{$IFDEF USE_JCLDEBUG}
StackInfo := JclLastExceptStackList();
if StackInfo <> nil then
for i := 0 to StackInfo.Count-1 do
Add(Result, StackInfo.Items[i].CallerAddr);
{$ENDIF}
{$ENDIF}
end;
function GetStackTraceStr(const StackTrace: TBaseStackTrace): string;
var
i: Integer;
begin
Result := '';
for i := 0 to High(StackTrace) do begin
if i > 0 then Result := Result + BaseStr.NEW_LINE_SEQ;
Result := Result + ' --- ' + IntToStr(i) + '. ' + CodeLocToStr(StackTrace[i]);
end;
end;
procedure Volatile(var v);
begin
// NOP
end;
{$IFDEF DELPHI2009}{$IFDEF USE_JCLDEBUG}
function GetExceptionStackInfoProc(P: PExceptionRecord): Pointer;
var
i: Integer;
EI: PExceptionInfo;
Nested: PExceptionRecord;
begin
New(EI);
EI.ExceptionObj := P^.ExceptObject;
if EI^.ExceptionObj is Exception then
EI.Msg := Exception(EI^.ExceptionObj).Message
else
EI.Msg := '';
EI^.StackTrace := GetExceptionStackTrace();
Nested := P^.ExceptionRecord;
i := 0;
while (Nested <> nil) and (i < MAX_STACK_ENTRIES) do begin
Inc(i);
SetLength(EI^.NestedExceptionAddresses, i);
EI^.NestedExceptionAddresses[i-1] := Nested^.ExceptionAddress;
Nested := Nested^.ExceptionRecord;
end;
Result := EI;
end;
function GetStackInfoStringProc(Info: Pointer): string;
var
i: Integer;
EI: PExceptionInfo;
begin
EI := Info;
Result := 'Exception class "' + EI^.ExceptionObj.ClassName + '" with message "' + EI^.Msg + '", stack trace:' + NEW_LINE_SEQ;
Result := Result + GetStackTraceStr(EI^.StackTrace);
for i := 0 to High(EI^.NestedExceptionAddresses) do
Result := Result + NEW_LINE_SEQ + ' -- Nested: ' + CodeLocToStr(GetCodeLocation(EI^.NestedExceptionAddresses[i]));
end;
{$ENDIF}{$ENDIF}
procedure CleanUpStackInfoProc(Info: Pointer);
var EI: PExceptionInfo;
begin
EI := Info;
SetLength(EI^.StackTrace, 0);
SetLength(EI^.NestedExceptionAddresses, 0);
Dispose(EI);
end;
initialization
LastCodeLocation.Address := nil;
{$IFDEF USE_JCLDEBUG}
Include(JclStackTrackingOptions, stTraceAllExceptions);
Include(JclStackTrackingOptions, stRawMode);
JclStartExceptionTracking;
{$ENDIF}
{$IFDEF DELPHI2009}{$IFDEF USE_JCLDEBUG}
Exception.GetExceptionStackInfoProc := GetExceptionStackInfoProc;
Exception.GetStackInfoStringProc := GetStackInfoStringProc;
Exception.CleanUpStackInfoProc := CleanUpStackInfoProc;
{$ENDIF}{$ENDIF}
finalization
{$IFDEF DELPHI2009}{$IFDEF USE_JCLDEBUG}
Exception.GetExceptionStackInfoProc := nil;
Exception.GetStackInfoStringProc := nil;
Exception.CleanUpStackInfoProc := nil;
{$ENDIF}{$ENDIF}
{$IFDEF USE_JCLDEBUG}
JclStopExceptionTracking;
{$ENDIF}
end.