-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexceptions.pas
196 lines (172 loc) · 5.43 KB
/
exceptions.pas
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit exceptions;
interface
uses
sysutils, baseunix;
type
EKernelError = class(Exception)
constructor Create(AErrorCode: cint);
end;
ESocketError = class(Exception)
constructor Create(AErrorCode: cint);
end;
ESyntaxError = class(Exception)
end;
ECaughtException = class end;
TReportExceptionEvent = procedure(E: Exception) of object;
procedure ReportCurrentException();
procedure ReportException(E: Exception);
procedure ReportExceptionAndFree(E: Exception); { for unraised exceptions }
function SetReportExceptionMethod(AReportExceptionMethod: TReportExceptionEvent): TReportExceptionEvent;
type
TXXX = record end unimplemented;
function XXX: Variant; unimplemented;
{$IFDEF DEBUG} function GetStackTrace(): AnsiString; {$ENDIF}
{$IFDEF DEBUG} procedure DumpRaiseList(); {$ENDIF}
implementation
uses
errors;
const
KernelErrorMsg: String = 'kernel error %d: %s';
SocketErrorMsg: String = 'socket error %d: %s';
var
ReportExceptionMethod: TReportExceptionEvent = nil;
constructor EKernelError.Create(AErrorCode: cint);
begin
inherited Create(Format(KernelErrorMsg, [AErrorCode, StrError(AErrorCode)]));
end;
constructor ESocketError.Create(AErrorCode: cint);
begin
inherited Create(Format(SocketErrorMsg, [AErrorCode, StrError(AErrorCode)]));
end;
procedure WriteBacktrace(Address: Pointer; Frames: PPointer; FrameCount: Integer);
var
FrameNumber: Cardinal;
begin
Writeln('Backtrace:');
if (Address = nil) then
Writeln(' dereferenced nil pointer')
else
Writeln(BackTraceStrFunc(Address));
if (FrameCount > 0) then
for FrameNumber := 0 to FrameCount-1 do {BOGUS Warning: Type size mismatch, possible loss of data / range check error}
Writeln(BackTraceStrFunc(Frames[FrameNumber]));
end;
procedure ReportCurrentException();
begin
Assert(Assigned(RaiseList));
Assert(Assigned(RaiseList^.FObject));
if (RaiseList^.FObject is Exception) then
Writeln(RaiseList^.FObject.ClassName, ' exception: ', (RaiseList^.FObject as Exception).Message)
else
Writeln(RaiseList^.FObject.ClassName, ' exception');
WriteBacktrace(RaiseList^.Addr, RaiseList^.Frames, RaiseList^.FrameCount);
end;
procedure ReportException(E: Exception);
begin
Assert((not Assigned(RaiseList)) or (not Assigned(RaiseList^.FObject)) or (RaiseList^.FObject <> E), 'Inside an exception handler, use ReportCurrentException() to get the right stack trace');
if (Assigned(ReportExceptionMethod)) then
ReportExceptionMethod(E)
else
begin
Writeln(E.Message);
Dump_Stack(Output, Get_Frame);
end;
end;
procedure ReportExceptionAndFree(E: Exception);
begin
ReportException(E);
E.Free();
end;
function SetReportExceptionMethod(AReportExceptionMethod: TReportExceptionEvent): TReportExceptionEvent;
begin
Result := ReportExceptionMethod;
ReportExceptionMethod := AReportExceptionMethod;
end;
{$WARNINGS OFF}
function XXX: Variant;
begin
//Assert(False, 'Not Implemented');
raise Exception.Create('Not Implemented') at Get_Caller_Addr(Get_Frame), Get_Caller_Frame(Get_Frame);
end;
{$WARNINGS ON}
procedure AssertionHandler(const Message, FileName: ShortString; LineNo: Longint; ErrorAddr: Pointer);
var
CompleteMessage: AnsiString;
begin
if (Message <> '') then
CompleteMessage := 'Assertion "' + Message + '" failed on line ' + IntToStr(LineNo) + ' of ' + FileName
else
CompleteMessage := 'Assertion failed on line ' + IntToStr(LineNo) + ' of ' + FileName;
{$IFDEF DEBUG}
Writeln('Raising assertion: ', CompleteMessage);
{$ENDIF}
raise EAssertionFailed.Create(CompleteMessage) at Get_Caller_Addr(ErrorAddr), Get_Caller_Frame(ErrorAddr);
end;
{$IFDEF DEBUG}
function GetStackTrace(): AnsiString;
// the following is a verbatim copy from http://wiki.freepascal.org/Logging_exceptions
var
I: Longint;
prevbp: Pointer;
CallerFrame,
CallerAddress,
bp: Pointer;
Report: string;
const
MaxDepth = maxint;
begin
Report := '';
bp := get_frame;
// This trick skip SendCallstack item
// bp:= get_caller_frame(get_frame);
try
prevbp := bp - 1;
I := 0;
while bp > prevbp do begin
CallerAddress := get_caller_addr(bp);
CallerFrame := get_caller_frame(bp);
if (CallerAddress = nil) then
Break;
Report := Report + BackTraceStrFunc(CallerAddress) + LineEnding;
Inc(I);
if (I >= MaxDepth) or (CallerFrame = nil) then
Break;
prevbp := bp;
bp := CallerFrame;
end;
except
{ prevent endless dump if an exception occured }
end;
// end of copy from http://wiki.freepascal.org/Logging_exceptions
Result := Report;
end;
{$ENDIF}
{$IFDEF DEBUG}
procedure DumpRaiseList();
var
Current: PExceptObject;
Index, Count: Cardinal;
begin
Current := RaiseList;
Count := 1;
while Assigned(Current) do
begin
Writeln('#', Count, ': ', Current^.FObject.ClassName);
Writeln(' Addr: ', HexStr(Current^.Addr));
Writeln(' RefCount: ', Current^.RefCount);
Writeln(' FrameCount: ', Current^.FrameCount);
if (Current^.FrameCount > 0) then
for Index := 0 to Current^.FrameCount - 1 do // $R-
begin
Writeln(' ', HexStr((Current^.Frames + Index * SizeOf(CodePointer))^));
end;
Current := Current^.Next;
Inc(Count);
end;
end;
{$ENDIF}
initialization
{$IFDEF DEBUG} AssertErrorProc := @AssertionHandler; {$ENDIF}
end.