except.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. var
  39. _ExceptObjectStack : PExceptObject;
  40. begin
  41. _ExceptObjectStack:=ExceptObjectStack;
  42. If _ExceptObjectStack<>nil then
  43. begin
  44. Inc(_ExceptObjectStack^.refcount);
  45. AcquireExceptionObject := _ExceptObjectStack^.FObject;
  46. end
  47. else
  48. RunError(231);
  49. end;
  50. procedure ReleaseExceptionObject;
  51. var
  52. _ExceptObjectStack : PExceptObject;
  53. begin
  54. _ExceptObjectStack:=ExceptObjectStack;
  55. If _ExceptObjectStack <> nil then
  56. begin
  57. if _ExceptObjectStack^.refcount > 0 then
  58. Dec(_ExceptObjectStack^.refcount);
  59. end
  60. else
  61. RunError(231);
  62. end;
  63. Function fpc_PushExceptAddr (Ft: Longint;_buf,_newaddr : pointer): PJmp_buf ;
  64. [Public, Alias : 'FPC_PUSHEXCEPTADDR'];compilerproc;
  65. var
  66. _ExceptAddrstack : ^PExceptAddr;
  67. begin
  68. {$ifdef excdebug}
  69. writeln ('In PushExceptAddr');
  70. {$endif}
  71. _ExceptAddrstack:=@ExceptAddrstack;
  72. PExceptAddr(_newaddr)^.Next:=_ExceptAddrstack^;
  73. _ExceptAddrStack^:=PExceptAddr(_newaddr);
  74. PExceptAddr(_newaddr)^.Buf:=PJmp_Buf(_buf);
  75. PExceptAddr(_newaddr)^.FrameType:=ft;
  76. result:=PJmp_Buf(_buf);
  77. end;
  78. Procedure fpc_PushExceptObj (Obj : TObject; AnAddr,AFrame : Pointer);
  79. [Public, Alias : 'FPC_PUSHEXCEPTOBJECT']; compilerproc;
  80. var
  81. Newobj : PExceptObject;
  82. _ExceptObjectStack : ^PExceptObject;
  83. framebufsize,
  84. framecount : longint;
  85. frames : PPointer;
  86. prev_frame,
  87. curr_frame,
  88. caller_frame,
  89. caller_addr : Pointer;
  90. begin
  91. {$ifdef excdebug}
  92. writeln ('In PushExceptObject');
  93. {$endif}
  94. _ExceptObjectStack:=@ExceptObjectStack;
  95. If _ExceptObjectStack^=Nil then
  96. begin
  97. New(_ExceptObjectStack^);
  98. _ExceptObjectStack^^.Next:=Nil;
  99. end
  100. else
  101. begin
  102. New(NewObj);
  103. NewObj^.Next:=_ExceptObjectStack^;
  104. _ExceptObjectStack^:=NewObj;
  105. end;
  106. with _ExceptObjectStack^^ do
  107. begin
  108. FObject:=Obj;
  109. Addr:=AnAddr;
  110. refcount:=0;
  111. end;
  112. { Backtrace }
  113. curr_frame:=AFrame;
  114. prev_frame:=get_frame;
  115. frames:=nil;
  116. framebufsize:=0;
  117. framecount:=0;
  118. while (framecount<RaiseMaxFrameCount) and (curr_frame > prev_frame) and
  119. (curr_frame<(StackBottom + StackLength)) do
  120. Begin
  121. caller_addr := get_caller_addr(curr_frame);
  122. caller_frame := get_caller_frame(curr_frame);
  123. if (caller_addr=nil) or
  124. (caller_frame=nil) then
  125. break;
  126. if (framecount>=framebufsize) then
  127. begin
  128. inc(framebufsize,16);
  129. reallocmem(frames,framebufsize*sizeof(pointer));
  130. end;
  131. frames[framecount]:=caller_addr;
  132. inc(framecount);
  133. prev_frame:=curr_frame;
  134. curr_frame:=caller_frame;
  135. End;
  136. _ExceptObjectStack^^.framecount:=framecount;
  137. _ExceptObjectStack^^.frames:=frames;
  138. end;
  139. { make it avalable for local use }
  140. Procedure fpc_PushExceptObj (Obj : TObject; AnAddr,AFrame : Pointer); [external name 'FPC_PUSHEXCEPTOBJECT'];
  141. Procedure DoUnHandledException;
  142. var
  143. _ExceptObjectStack : PExceptObject;
  144. begin
  145. _ExceptObjectStack:=ExceptObjectStack;
  146. If (ExceptProc<>Nil) and (_ExceptObjectStack<>Nil) then
  147. with _ExceptObjectStack^ do
  148. begin
  149. TExceptProc(ExceptProc)(FObject,Addr,FrameCount,Frames);
  150. halt(217)
  151. end;
  152. if erroraddr = nil then
  153. RunError(217)
  154. else
  155. if errorcode <= maxExitCode then
  156. halt(errorcode)
  157. else
  158. halt(255)
  159. end;
  160. Function fpc_Raiseexception (Obj : TObject; AnAddr,AFrame : Pointer) : TObject;[Public, Alias : 'FPC_RAISEEXCEPTION']; compilerproc;
  161. var
  162. _ExceptObjectStack : PExceptObject;
  163. _ExceptAddrstack : PExceptAddr;
  164. begin
  165. {$ifdef excdebug}
  166. writeln ('In RaiseException');
  167. {$endif}
  168. fpc_Raiseexception:=nil;
  169. fpc_PushExceptObj(Obj,AnAddr,AFrame);
  170. _ExceptAddrstack:=ExceptAddrStack;
  171. If _ExceptAddrStack=Nil then
  172. DoUnhandledException;
  173. _ExceptObjectStack:=ExceptObjectStack;
  174. if (RaiseProc <> nil) and (_ExceptObjectStack <> nil) then
  175. with _ExceptObjectStack^ do
  176. RaiseProc(FObject,Addr,FrameCount,Frames);
  177. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  178. end;
  179. Procedure fpc_PopAddrStack;[Public, Alias : 'FPC_POPADDRSTACK']; compilerproc;
  180. var
  181. hp : ^PExceptAddr;
  182. begin
  183. {$ifdef excdebug}
  184. writeln ('In Popaddrstack');
  185. {$endif}
  186. hp:=@ExceptAddrStack;
  187. If hp^=nil then
  188. begin
  189. writeln ('At end of ExceptionAddresStack');
  190. halt (255);
  191. end
  192. else
  193. begin
  194. hp^:=hp^^.Next;
  195. end;
  196. end;
  197. function fpc_PopObjectStack : TObject;[Public, Alias : 'FPC_POPOBJECTSTACK']; compilerproc;
  198. var
  199. hp,_ExceptObjectStack : PExceptObject;
  200. begin
  201. {$ifdef excdebug}
  202. writeln ('In PopObjectstack');
  203. {$endif}
  204. _ExceptObjectStack:=ExceptObjectStack;
  205. If _ExceptObjectStack=nil then
  206. begin
  207. writeln ('At end of ExceptionObjectStack');
  208. halt (1);
  209. end
  210. else
  211. begin
  212. { we need to return the exception object to dispose it }
  213. if _ExceptObjectStack^.refcount = 0 then begin
  214. fpc_PopObjectStack:=_ExceptObjectStack^.FObject;
  215. end else begin
  216. fpc_PopObjectStack:=nil;
  217. end;
  218. hp:=_ExceptObjectStack;
  219. ExceptObjectStack:=_ExceptObjectStack^.next;
  220. if assigned(hp^.frames) then
  221. freemem(hp^.frames);
  222. dispose(hp);
  223. erroraddr:=nil;
  224. end;
  225. end;
  226. { this is for popping exception objects when a second exception is risen }
  227. { in an except/on }
  228. function fpc_PopSecondObjectStack : TObject;[Public, Alias : 'FPC_POPSECONDOBJECTSTACK']; compilerproc;
  229. var
  230. hp,_ExceptObjectStack : PExceptObject;
  231. begin
  232. {$ifdef excdebug}
  233. writeln ('In PopObjectstack');
  234. {$endif}
  235. _ExceptObjectStack:=ExceptObjectStack;
  236. If not(assigned(_ExceptObjectStack)) or
  237. not(assigned(_ExceptObjectStack^.next)) then
  238. begin
  239. writeln ('At end of ExceptionObjectStack');
  240. halt (1);
  241. end
  242. else
  243. begin
  244. if _ExceptObjectStack^.next^.refcount=0 then
  245. { we need to return the exception object to dispose it if refcount=0 }
  246. fpc_PopSecondObjectStack:=_ExceptObjectStack^.next^.FObject
  247. else
  248. fpc_PopSecondObjectStack:=nil;
  249. hp:=_ExceptObjectStack^.next;
  250. _ExceptObjectStack^.next:=hp^.next;
  251. if assigned(hp^.frames) then
  252. freemem(hp^.frames);
  253. dispose(hp);
  254. end;
  255. end;
  256. Procedure fpc_ReRaise;[Public, Alias : 'FPC_RERAISE']; compilerproc;
  257. var
  258. _ExceptAddrStack : PExceptAddr;
  259. begin
  260. {$ifdef excdebug}
  261. writeln ('In reraise');
  262. {$endif}
  263. _ExceptAddrStack:=ExceptAddrStack;
  264. If _ExceptAddrStack=Nil then
  265. DoUnHandledException;
  266. ExceptObjectStack^.refcount := 0;
  267. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  268. end;
  269. Function fpc_Catches(Objtype : TClass) : TObject;[Public, Alias : 'FPC_CATCHES']; compilerproc;
  270. var
  271. _Objtype : TExceptObjectClass;
  272. _ExceptObjectStack : PExceptObject;
  273. begin
  274. _ExceptObjectStack:=ExceptObjectStack;
  275. If _ExceptObjectStack=Nil then
  276. begin
  277. Writeln ('Internal error.');
  278. halt (255);
  279. end;
  280. _Objtype := TExceptObjectClass(Objtype);
  281. if Not ((_Objtype = TExceptObjectClass(CatchAllExceptions)) or
  282. (_ExceptObjectStack^.FObject is _ObjType)) then
  283. fpc_Catches:=Nil
  284. else
  285. begin
  286. // catch !
  287. fpc_Catches:=_ExceptObjectStack^.FObject;
  288. { this can't be done, because there could be a reraise (PFV)
  289. PopObjectStack;
  290. Also the PopAddrStack shouldn't be done, we do it now
  291. immediatly in the exception handler (FK)
  292. PopAddrStack; }
  293. end;
  294. end;
  295. Procedure fpc_DestroyException(o : TObject);[Public, Alias : 'FPC_DESTROYEXCEPTION']; compilerproc;
  296. begin
  297. { with free we're on the really save side }
  298. o.Free;
  299. end;
  300. Procedure SysInitExceptions;
  301. {
  302. Initialize exceptionsupport
  303. }
  304. begin
  305. ExceptObjectstack:=Nil;
  306. ExceptAddrStack:=Nil;
  307. end;