except.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. {$ifdef excdebug}
  190. writeln ('At end of ExceptionAddresStack');
  191. {$endif}
  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. {$ifdef excdebug}
  210. writeln ('At end of ExceptionObjectStack');
  211. {$endif}
  212. halt (1);
  213. end
  214. else
  215. begin
  216. { we need to return the exception object to dispose it }
  217. if _ExceptObjectStack^.refcount = 0 then begin
  218. fpc_PopObjectStack:=_ExceptObjectStack^.FObject;
  219. end else begin
  220. fpc_PopObjectStack:=nil;
  221. end;
  222. hp:=_ExceptObjectStack;
  223. ExceptObjectStack:=_ExceptObjectStack^.next;
  224. if assigned(hp^.frames) then
  225. freemem(hp^.frames);
  226. dispose(hp);
  227. erroraddr:=nil;
  228. end;
  229. end;
  230. { this is for popping exception objects when a second exception is risen }
  231. { in an except/on }
  232. function fpc_PopSecondObjectStack : TObject;[Public, Alias : 'FPC_POPSECONDOBJECTSTACK']; compilerproc;
  233. var
  234. hp,_ExceptObjectStack : PExceptObject;
  235. begin
  236. {$ifdef excdebug}
  237. writeln ('In PopObjectstack');
  238. {$endif}
  239. _ExceptObjectStack:=ExceptObjectStack;
  240. If not(assigned(_ExceptObjectStack)) or
  241. not(assigned(_ExceptObjectStack^.next)) then
  242. begin
  243. {$ifdef excdebug}
  244. writeln ('At end of ExceptionObjectStack');
  245. {$endif}
  246. halt (1);
  247. end
  248. else
  249. begin
  250. if _ExceptObjectStack^.next^.refcount=0 then
  251. { we need to return the exception object to dispose it if refcount=0 }
  252. fpc_PopSecondObjectStack:=_ExceptObjectStack^.next^.FObject
  253. else
  254. fpc_PopSecondObjectStack:=nil;
  255. hp:=_ExceptObjectStack^.next;
  256. _ExceptObjectStack^.next:=hp^.next;
  257. if assigned(hp^.frames) then
  258. freemem(hp^.frames);
  259. dispose(hp);
  260. end;
  261. end;
  262. Procedure fpc_ReRaise;[Public, Alias : 'FPC_RERAISE']; compilerproc;
  263. var
  264. _ExceptAddrStack : PExceptAddr;
  265. begin
  266. {$ifdef excdebug}
  267. writeln ('In reraise');
  268. {$endif}
  269. _ExceptAddrStack:=ExceptAddrStack;
  270. If _ExceptAddrStack=Nil then
  271. DoUnHandledException;
  272. ExceptObjectStack^.refcount := 0;
  273. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  274. end;
  275. Function fpc_Catches(Objtype : TClass) : TObject;[Public, Alias : 'FPC_CATCHES']; compilerproc;
  276. var
  277. _Objtype : TExceptObjectClass;
  278. _ExceptObjectStack : PExceptObject;
  279. begin
  280. _ExceptObjectStack:=ExceptObjectStack;
  281. If _ExceptObjectStack=Nil then
  282. begin
  283. {$ifdef excdebug}
  284. Writeln ('Internal error.');
  285. {$endif}
  286. halt (255);
  287. end;
  288. _Objtype := TExceptObjectClass(Objtype);
  289. if Not ((_Objtype = TExceptObjectClass(CatchAllExceptions)) or
  290. (_ExceptObjectStack^.FObject is _ObjType)) then
  291. fpc_Catches:=Nil
  292. else
  293. begin
  294. // catch !
  295. fpc_Catches:=_ExceptObjectStack^.FObject;
  296. { this can't be done, because there could be a reraise (PFV)
  297. PopObjectStack;
  298. Also the PopAddrStack shouldn't be done, we do it now
  299. immediatly in the exception handler (FK)
  300. PopAddrStack; }
  301. end;
  302. end;
  303. Procedure fpc_DestroyException(o : TObject);[Public, Alias : 'FPC_DESTROYEXCEPTION']; compilerproc;
  304. begin
  305. { with free we're on the really save side }
  306. o.Free;
  307. end;
  308. Procedure SysInitExceptions;
  309. {
  310. Initialize exceptionsupport
  311. }
  312. begin
  313. ExceptObjectstack:=Nil;
  314. ExceptAddrStack:=Nil;
  315. end;