2
0

except.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. {$ifdef FPC_LIMITED_EXITCODE}
  141. if errorcode > maxExitCode then
  142. halt(255)
  143. else
  144. {$endif FPC_LIMITED_EXITCODE}
  145. halt(errorcode);
  146. end;
  147. Function fpc_RaiseException (Obj : TObject; AnAddr,AFrame : Pointer) : TObject;[Public, Alias : 'FPC_RAISEEXCEPTION']; compilerproc;
  148. var
  149. _ExceptObjectStack : PExceptObject;
  150. _ExceptAddrstack : PExceptAddr;
  151. begin
  152. {$ifdef excdebug}
  153. writeln ('In RaiseException');
  154. {$endif}
  155. fpc_Raiseexception:=nil;
  156. PushExceptObject(Obj,AnAddr,AFrame);
  157. _ExceptAddrstack:=ExceptAddrStack;
  158. If _ExceptAddrStack=Nil then
  159. DoUnhandledException;
  160. _ExceptObjectStack:=ExceptObjectStack;
  161. if (RaiseProc <> nil) and (_ExceptObjectStack <> nil) then
  162. with _ExceptObjectStack^ do
  163. RaiseProc(FObject,Addr,FrameCount,Frames);
  164. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  165. end;
  166. Procedure fpc_PopAddrStack;[Public, Alias : 'FPC_POPADDRSTACK']; compilerproc;
  167. var
  168. hp : ^PExceptAddr;
  169. begin
  170. {$ifdef excdebug}
  171. writeln ('In Popaddrstack');
  172. {$endif}
  173. hp:=@ExceptAddrStack;
  174. If hp^=nil then
  175. begin
  176. {$ifdef excdebug}
  177. writeln ('At end of ExceptionAddresStack');
  178. {$endif}
  179. halt (255);
  180. end
  181. else
  182. begin
  183. hp^:=hp^^.Next;
  184. end;
  185. end;
  186. function fpc_PopObjectStack : TObject;[Public, Alias : 'FPC_POPOBJECTSTACK']; compilerproc;
  187. var
  188. hp,_ExceptObjectStack : PExceptObject;
  189. begin
  190. {$ifdef excdebug}
  191. writeln ('In PopObjectstack');
  192. {$endif}
  193. _ExceptObjectStack:=ExceptObjectStack;
  194. If _ExceptObjectStack=nil then
  195. begin
  196. {$ifdef excdebug}
  197. writeln ('At end of ExceptionObjectStack');
  198. {$endif}
  199. halt (1);
  200. end
  201. else
  202. begin
  203. { we need to return the exception object to dispose it }
  204. if _ExceptObjectStack^.refcount = 0 then begin
  205. fpc_PopObjectStack:=_ExceptObjectStack^.FObject;
  206. end else begin
  207. fpc_PopObjectStack:=nil;
  208. end;
  209. hp:=_ExceptObjectStack;
  210. ExceptObjectStack:=_ExceptObjectStack^.next;
  211. if assigned(hp^.frames) then
  212. freemem(hp^.frames);
  213. dispose(hp);
  214. erroraddr:=nil;
  215. end;
  216. end;
  217. { this is for popping exception objects when a second exception is risen }
  218. { in an except/on }
  219. function fpc_PopSecondObjectStack : TObject;[Public, Alias : 'FPC_POPSECONDOBJECTSTACK']; compilerproc;
  220. var
  221. hp,_ExceptObjectStack : PExceptObject;
  222. begin
  223. {$ifdef excdebug}
  224. writeln ('In PopObjectstack');
  225. {$endif}
  226. _ExceptObjectStack:=ExceptObjectStack;
  227. If not(assigned(_ExceptObjectStack)) or
  228. not(assigned(_ExceptObjectStack^.next)) then
  229. begin
  230. {$ifdef excdebug}
  231. writeln ('At end of ExceptionObjectStack');
  232. {$endif}
  233. halt (1);
  234. end
  235. else
  236. begin
  237. if _ExceptObjectStack^.next^.refcount=0 then
  238. { we need to return the exception object to dispose it if refcount=0 }
  239. fpc_PopSecondObjectStack:=_ExceptObjectStack^.next^.FObject
  240. else
  241. fpc_PopSecondObjectStack:=nil;
  242. hp:=_ExceptObjectStack^.next;
  243. _ExceptObjectStack^.next:=hp^.next;
  244. if assigned(hp^.frames) then
  245. freemem(hp^.frames);
  246. dispose(hp);
  247. end;
  248. end;
  249. Procedure fpc_ReRaise;[Public, Alias : 'FPC_RERAISE']; compilerproc;
  250. var
  251. _ExceptAddrStack : PExceptAddr;
  252. begin
  253. {$ifdef excdebug}
  254. writeln ('In reraise');
  255. {$endif}
  256. _ExceptAddrStack:=ExceptAddrStack;
  257. If _ExceptAddrStack=Nil then
  258. DoUnHandledException;
  259. ExceptObjectStack^.refcount := 0;
  260. longjmp(_ExceptAddrStack^.Buf^,FPC_Exception);
  261. end;
  262. function Internal_PopSecondObjectStack : TObject; external name 'FPC_POPSECONDOBJECTSTACK';
  263. function Internal_PopObjectStack: TObject; external name 'FPC_POPOBJECTSTACK';
  264. procedure Internal_Reraise; external name 'FPC_RERAISE';
  265. Function fpc_Catches(Objtype : TClass) : TObject;[Public, Alias : 'FPC_CATCHES']; compilerproc;
  266. var
  267. _ExceptObjectStack : PExceptObject;
  268. begin
  269. _ExceptObjectStack:=ExceptObjectStack;
  270. If _ExceptObjectStack=Nil then
  271. begin
  272. {$ifdef excdebug}
  273. Writeln ('Internal error.');
  274. {$endif}
  275. halt (255);
  276. end;
  277. if Not ((Objtype = TClass(CatchAllExceptions)) or
  278. (_ExceptObjectStack^.FObject is ObjType)) then
  279. fpc_Catches:=Nil
  280. else
  281. begin
  282. // catch !
  283. fpc_Catches:=_ExceptObjectStack^.FObject;
  284. { this can't be done, because there could be a reraise (PFV)
  285. PopObjectStack;
  286. Also the PopAddrStack shouldn't be done, we do it now
  287. immediatly in the exception handler (FK)
  288. PopAddrStack; }
  289. end;
  290. end;
  291. Procedure fpc_DestroyException(o : TObject);[Public, Alias : 'FPC_DESTROYEXCEPTION']; compilerproc;
  292. begin
  293. { with free we're on the really safe side }
  294. o.Free;
  295. end;
  296. { TODO: no longer used, clean up }
  297. function fpc_GetExceptionAddr : Pointer;[Public, Alias : 'FPC_GETEXCEPTIONADDR']; compilerproc;
  298. var
  299. _ExceptObjectStack : PExceptObject;
  300. begin
  301. _ExceptObjectStack:=ExceptObjectStack;
  302. if _ExceptObjectStack=nil then
  303. fpc_GetExceptionAddr:=nil
  304. else
  305. fpc_GetExceptionAddr:=_ExceptObjectStack^.Addr;
  306. end;
  307. Procedure SysInitExceptions;
  308. {
  309. Initialize exceptionsupport
  310. }
  311. begin
  312. ExceptObjectstack:=Nil;
  313. ExceptAddrStack:=Nil;
  314. end;
  315. procedure fpc_doneexception;[public,alias:'FPC_DONEEXCEPTION'] compilerproc;
  316. begin
  317. Internal_PopObjectStack.Free;
  318. end;
  319. procedure fpc_raise_nested;[public,alias:'FPC_RAISE_NESTED']compilerproc;
  320. begin
  321. Internal_PopSecondObjectStack.Free;
  322. Internal_Reraise;
  323. end;
  324. function fpc_safecallhandler(obj: TObject): HResult; [public,alias:'FPC_SAFECALLHANDLER']; compilerproc;
  325. var
  326. raiselist: PExceptObject;
  327. adr: Pointer;
  328. exc: TObject;
  329. begin
  330. raiselist:=ExceptObjectStack;
  331. if Assigned(raiseList) then
  332. adr:=raiseList^.Addr
  333. else
  334. adr:=nil;
  335. exc:=Internal_PopObjectStack;
  336. if Assigned(obj) then
  337. result:=obj.SafeCallException(exc,adr)
  338. else
  339. result:=E_UNEXPECTED;
  340. exc.Free;
  341. end;