except.inc 9.5 KB

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