except.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. Exception support
  13. ****************************************************************************}
  14. Const
  15. { Type of exception. Currently only one. }
  16. FPC_EXCEPTION = 1;
  17. { types of frames for the exception address stack }
  18. cExceptionFrame = 1;
  19. cFinalizeFrame = 2;
  20. Type
  21. PExceptAddr = ^TExceptAddr;
  22. TExceptAddr = record
  23. buf : pjmp_buf;
  24. next : PExceptAddr;
  25. frametype : Longint;
  26. end;
  27. TExceptObjectClass = Class of TObject;
  28. Const
  29. CatchAllExceptions : PtrInt = -1;
  30. ThreadVar
  31. ExceptAddrStack : PExceptAddr;
  32. ExceptObjectStack : PExceptObject;
  33. Function RaiseList : PExceptObject;
  34. begin
  35. RaiseList:=ExceptObjectStack;
  36. end;
  37. function AcquireExceptionObject: Pointer;
  38. begin
  39. If ExceptObjectStack<>nil then
  40. begin
  41. Inc(ExceptObjectStack^.refcount);
  42. AcquireExceptionObject := ExceptObjectStack^.FObject;
  43. end
  44. else
  45. RunError(231);
  46. end;
  47. procedure ReleaseExceptionObject;
  48. begin
  49. If ExceptObjectStack <> nil then
  50. begin
  51. if ExceptObjectStack^.refcount > 0 then
  52. Dec(ExceptObjectStack^.refcount);
  53. end
  54. else
  55. RunError(231);
  56. end;
  57. Function fpc_PushExceptAddr (Ft: Longint;_buf,_newaddr : pointer): PJmp_buf ;
  58. [Public, Alias : 'FPC_PUSHEXCEPTADDR'];compilerproc;
  59. begin
  60. {$ifdef excdebug}
  61. writeln ('In PushExceptAddr');
  62. {$endif}
  63. PExceptAddr(_newaddr)^.Next:=ExceptAddrstack;
  64. ExceptAddrStack:=PExceptAddr(_newaddr);
  65. PExceptAddr(_newaddr)^.Buf:=PJmp_Buf(_buf);
  66. PExceptAddr(_newaddr)^.FrameType:=ft;
  67. result:=PJmp_Buf(_buf);
  68. end;
  69. Procedure fpc_PushExceptObj (Obj : TObject; AnAddr,AFrame : Pointer);
  70. [Public, Alias : 'FPC_PUSHEXCEPTOBJECT']; compilerproc;
  71. var
  72. Newobj : PExceptObject;
  73. framebufsize,
  74. framecount : longint;
  75. frames : PPointer;
  76. prev_frame,
  77. curr_frame,
  78. caller_frame,
  79. caller_addr : Pointer;
  80. begin
  81. {$ifdef excdebug}
  82. writeln ('In PushExceptObject');
  83. {$endif}
  84. If ExceptObjectStack=Nil then
  85. begin
  86. New(ExceptObjectStack);
  87. ExceptObjectStack^.Next:=Nil;
  88. end
  89. else
  90. begin
  91. New(NewObj);
  92. NewObj^.Next:=ExceptObjectStack;
  93. ExceptObjectStack:=NewObj;
  94. end;
  95. ExceptObjectStack^.FObject:=Obj;
  96. ExceptObjectStack^.Addr:=AnAddr;
  97. ExceptObjectStack^.refcount:=0;
  98. { Backtrace }
  99. curr_frame:=AFrame;
  100. prev_frame:=get_frame;
  101. frames:=nil;
  102. framebufsize:=0;
  103. framecount:=0;
  104. while (framecount<RaiseMaxFrameCount) and (curr_frame > prev_frame) and
  105. (curr_frame<(StackBottom + StackLength)) do
  106. Begin
  107. caller_addr := get_caller_addr(curr_frame);
  108. caller_frame := get_caller_frame(curr_frame);
  109. if (caller_addr=nil) or
  110. (caller_frame=nil) then
  111. break;
  112. if (framecount>=framebufsize) then
  113. begin
  114. inc(framebufsize,16);
  115. reallocmem(frames,framebufsize*sizeof(pointer));
  116. end;
  117. frames[framecount]:=caller_addr;
  118. inc(framecount);
  119. prev_frame:=curr_frame;
  120. curr_frame:=caller_frame;
  121. End;
  122. ExceptObjectStack^.framecount:=framecount;
  123. ExceptObjectStack^.frames:=frames;
  124. end;
  125. { make it avalable for local use }
  126. Procedure fpc_PushExceptObj (Obj : TObject; AnAddr,AFrame : Pointer); [external name 'FPC_PUSHEXCEPTOBJECT'];
  127. Procedure DoUnHandledException;
  128. begin
  129. If (ExceptProc<>Nil) and (ExceptObjectStack<>Nil) then
  130. with ExceptObjectStack^ do
  131. TExceptProc(ExceptProc)(FObject,Addr,FrameCount,Frames);
  132. if erroraddr = nil then
  133. RunError(217)
  134. else
  135. if errorcode <= maxExitCode then
  136. halt(errorcode)
  137. else
  138. halt(255)
  139. end;
  140. Function fpc_Raiseexception (Obj : TObject; AnAddr,AFrame : Pointer) : TObject;[Public, Alias : 'FPC_RAISEEXCEPTION']; compilerproc;
  141. begin
  142. {$ifdef excdebug}
  143. writeln ('In RaiseException');
  144. {$endif}
  145. fpc_Raiseexception:=nil;
  146. fpc_PushExceptObj(Obj,AnAddr,AFrame);
  147. If ExceptAddrStack=Nil then
  148. DoUnhandledException;
  149. if (RaiseProc <> nil) and (ExceptObjectStack <> nil) then
  150. with ExceptObjectStack^ do
  151. RaiseProc(FObject,Addr,FrameCount,Frames);
  152. longjmp(ExceptAddrStack^.Buf^,FPC_Exception);
  153. end;
  154. Procedure fpc_PopAddrStack;[Public, Alias : 'FPC_POPADDRSTACK']; compilerproc;
  155. var
  156. hp : ^PExceptAddr;
  157. begin
  158. {$ifdef excdebug}
  159. writeln ('In Popaddrstack');
  160. {$endif}
  161. hp:=@ExceptAddrStack;
  162. If hp^=nil then
  163. begin
  164. writeln ('At end of ExceptionAddresStack');
  165. halt (255);
  166. end
  167. else
  168. begin
  169. hp^:=hp^^.Next;
  170. end;
  171. end;
  172. function fpc_PopObjectStack : TObject;[Public, Alias : 'FPC_POPOBJECTSTACK']; compilerproc;
  173. var
  174. hp : PExceptObject;
  175. begin
  176. {$ifdef excdebug}
  177. writeln ('In PopObjectstack');
  178. {$endif}
  179. If ExceptObjectStack=nil then
  180. begin
  181. writeln ('At end of ExceptionObjectStack');
  182. halt (1);
  183. end
  184. else
  185. begin
  186. { we need to return the exception object to dispose it }
  187. if ExceptObjectStack^.refcount = 0 then begin
  188. fpc_PopObjectStack:=ExceptObjectStack^.FObject;
  189. end else begin
  190. fpc_PopObjectStack:=nil;
  191. end;
  192. hp:=ExceptObjectStack;
  193. ExceptObjectStack:=ExceptObjectStack^.next;
  194. if assigned(hp^.frames) then
  195. freemem(hp^.frames);
  196. dispose(hp);
  197. erroraddr:=nil;
  198. end;
  199. end;
  200. { this is for popping exception objects when a second exception is risen }
  201. { in an except/on }
  202. function fpc_PopSecondObjectStack : TObject;[Public, Alias : 'FPC_POPSECONDOBJECTSTACK']; compilerproc;
  203. var
  204. hp : PExceptObject;
  205. begin
  206. {$ifdef excdebug}
  207. writeln ('In PopObjectstack');
  208. {$endif}
  209. If not(assigned(ExceptObjectStack)) or
  210. not(assigned(ExceptObjectStack^.next)) then
  211. begin
  212. writeln ('At end of ExceptionObjectStack');
  213. halt (1);
  214. end
  215. else
  216. begin
  217. if ExceptObjectStack^.next^.refcount=0 then
  218. { we need to return the exception object to dispose it if refcount=0 }
  219. fpc_PopSecondObjectStack:=ExceptObjectStack^.next^.FObject
  220. else
  221. fpc_PopSecondObjectStack:=nil;
  222. hp:=ExceptObjectStack^.next;
  223. ExceptObjectStack^.next:=hp^.next;
  224. if assigned(hp^.frames) then
  225. freemem(hp^.frames);
  226. dispose(hp);
  227. end;
  228. end;
  229. Procedure fpc_ReRaise;[Public, Alias : 'FPC_RERAISE']; compilerproc;
  230. begin
  231. {$ifdef excdebug}
  232. writeln ('In reraise');
  233. {$endif}
  234. If ExceptAddrStack=Nil then
  235. DoUnHandledException;
  236. ExceptObjectStack^.refcount := 0;
  237. longjmp(ExceptAddrStack^.Buf^,FPC_Exception);
  238. end;
  239. Function fpc_Catches(Objtype : TClass) : TObject;[Public, Alias : 'FPC_CATCHES']; compilerproc;
  240. var
  241. _Objtype : TExceptObjectClass;
  242. begin
  243. If ExceptObjectStack=Nil then
  244. begin
  245. Writeln ('Internal error.');
  246. halt (255);
  247. end;
  248. _Objtype := TExceptObjectClass(Objtype);
  249. if Not ((_Objtype = TExceptObjectClass(CatchAllExceptions)) or
  250. (ExceptObjectStack^.FObject is _ObjType)) then
  251. fpc_Catches:=Nil
  252. else
  253. begin
  254. // catch !
  255. fpc_Catches:=ExceptObjectStack^.FObject;
  256. { this can't be done, because there could be a reraise (PFV)
  257. PopObjectStack;
  258. Also the PopAddrStack shouldn't be done, we do it now
  259. immediatly in the exception handler (FK)
  260. PopAddrStack; }
  261. end;
  262. end;
  263. Procedure fpc_DestroyException(o : TObject);[Public, Alias : 'FPC_DESTROYEXCEPTION']; compilerproc;
  264. begin
  265. { with free we're on the really save side }
  266. o.Free;
  267. end;
  268. Procedure SysInitExceptions;
  269. {
  270. Initialize exceptionsupport
  271. }
  272. begin
  273. ExceptObjectstack:=Nil;
  274. ExceptAddrStack:=Nil;
  275. end;