except.inc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. {$ifdef FPC_LIMITED_EXITCODE}
  156. if errorcode > maxExitCode then
  157. halt(255)
  158. else
  159. {$endif FPC_LIMITED_EXITCODE}
  160. halt(errorcode);
  161. end;
  162. Function fpc_Raiseexception (Obj : TObject; AnAddr,AFrame : Pointer) : TObject;[Public, Alias : 'FPC_RAISEEXCEPTION']; compilerproc;
  163. var
  164. _ExceptObjectStack : PExceptObject;
  165. _ExceptAddrstack : PExceptAddr;
  166. begin
  167. {$ifdef excdebug}
  168. writeln ('In RaiseException');
  169. {$endif}
  170. fpc_Raiseexception:=nil;
  171. fpc_PushExceptObj(Obj,AnAddr,AFrame);
  172. _ExceptAddrstack:=ExceptAddrStack;
  173. If _ExceptAddrStack=Nil then
  174. DoUnhandledException;
  175. _ExceptObjectStack:=ExceptObjectStack;
  176. if (RaiseProc <> nil) and (_ExceptObjectStack <> nil) then
  177. with _ExceptObjectStack^ do
  178. RaiseProc(FObject,Addr,FrameCount,Frames);
  179. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  180. end;
  181. Procedure fpc_PopAddrStack;[Public, Alias : 'FPC_POPADDRSTACK']; compilerproc;
  182. var
  183. hp : ^PExceptAddr;
  184. begin
  185. {$ifdef excdebug}
  186. writeln ('In Popaddrstack');
  187. {$endif}
  188. hp:=@ExceptAddrStack;
  189. If hp^=nil then
  190. begin
  191. writeln ('At end of ExceptionAddresStack');
  192. halt (255);
  193. end
  194. else
  195. begin
  196. hp^:=hp^^.Next;
  197. end;
  198. end;
  199. function fpc_PopObjectStack : TObject;[Public, Alias : 'FPC_POPOBJECTSTACK']; compilerproc;
  200. var
  201. hp,_ExceptObjectStack : PExceptObject;
  202. begin
  203. {$ifdef excdebug}
  204. writeln ('In PopObjectstack');
  205. {$endif}
  206. _ExceptObjectStack:=ExceptObjectStack;
  207. If _ExceptObjectStack=nil then
  208. begin
  209. writeln ('At end of ExceptionObjectStack');
  210. halt (1);
  211. end
  212. else
  213. begin
  214. { we need to return the exception object to dispose it }
  215. if _ExceptObjectStack^.refcount = 0 then begin
  216. fpc_PopObjectStack:=_ExceptObjectStack^.FObject;
  217. end else begin
  218. fpc_PopObjectStack:=nil;
  219. end;
  220. hp:=_ExceptObjectStack;
  221. ExceptObjectStack:=_ExceptObjectStack^.next;
  222. if assigned(hp^.frames) then
  223. freemem(hp^.frames);
  224. dispose(hp);
  225. erroraddr:=nil;
  226. end;
  227. end;
  228. { this is for popping exception objects when a second exception is risen }
  229. { in an except/on }
  230. function fpc_PopSecondObjectStack : TObject;[Public, Alias : 'FPC_POPSECONDOBJECTSTACK']; compilerproc;
  231. var
  232. hp,_ExceptObjectStack : PExceptObject;
  233. begin
  234. {$ifdef excdebug}
  235. writeln ('In PopObjectstack');
  236. {$endif}
  237. _ExceptObjectStack:=ExceptObjectStack;
  238. If not(assigned(_ExceptObjectStack)) or
  239. not(assigned(_ExceptObjectStack^.next)) then
  240. begin
  241. writeln ('At end of ExceptionObjectStack');
  242. halt (1);
  243. end
  244. else
  245. begin
  246. if _ExceptObjectStack^.next^.refcount=0 then
  247. { we need to return the exception object to dispose it if refcount=0 }
  248. fpc_PopSecondObjectStack:=_ExceptObjectStack^.next^.FObject
  249. else
  250. fpc_PopSecondObjectStack:=nil;
  251. hp:=_ExceptObjectStack^.next;
  252. _ExceptObjectStack^.next:=hp^.next;
  253. if assigned(hp^.frames) then
  254. freemem(hp^.frames);
  255. dispose(hp);
  256. end;
  257. end;
  258. Procedure fpc_ReRaise;[Public, Alias : 'FPC_RERAISE']; compilerproc;
  259. var
  260. _ExceptAddrStack : PExceptAddr;
  261. begin
  262. {$ifdef excdebug}
  263. writeln ('In reraise');
  264. {$endif}
  265. _ExceptAddrStack:=ExceptAddrStack;
  266. If _ExceptAddrStack=Nil then
  267. DoUnHandledException;
  268. ExceptObjectStack^.refcount := 0;
  269. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  270. end;
  271. Function fpc_Catches(Objtype : TClass) : TObject;[Public, Alias : 'FPC_CATCHES']; compilerproc;
  272. var
  273. _Objtype : TExceptObjectClass;
  274. _ExceptObjectStack : PExceptObject;
  275. begin
  276. _ExceptObjectStack:=ExceptObjectStack;
  277. If _ExceptObjectStack=Nil then
  278. begin
  279. Writeln ('Internal error.');
  280. halt (255);
  281. end;
  282. _Objtype := TExceptObjectClass(Objtype);
  283. if Not ((_Objtype = TExceptObjectClass(CatchAllExceptions)) or
  284. (_ExceptObjectStack^.FObject is _ObjType)) then
  285. fpc_Catches:=Nil
  286. else
  287. begin
  288. // catch !
  289. fpc_Catches:=_ExceptObjectStack^.FObject;
  290. { this can't be done, because there could be a reraise (PFV)
  291. PopObjectStack;
  292. Also the PopAddrStack shouldn't be done, we do it now
  293. immediatly in the exception handler (FK)
  294. PopAddrStack; }
  295. end;
  296. end;
  297. Procedure fpc_DestroyException(o : TObject);[Public, Alias : 'FPC_DESTROYEXCEPTION']; compilerproc;
  298. begin
  299. { with free we're on the really save side }
  300. o.Free;
  301. end;
  302. Procedure SysInitExceptions;
  303. {
  304. Initialize exceptionsupport
  305. }
  306. begin
  307. ExceptObjectstack:=Nil;
  308. ExceptAddrStack:=Nil;
  309. end;