signals.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. {
  2. This file is part of the Free Pascal run time library.
  3. This unit implements unix like signal handling for win32
  4. Copyright (c) 1999-2006 by 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. unit signals;
  12. interface
  13. {$PACKRECORDS C}
  14. { Signals }
  15. const
  16. SIGABRT = 288;
  17. SIGFPE = 289;
  18. SIGILL = 290;
  19. SIGSEGV = 291;
  20. SIGTERM = 292;
  21. SIGALRM = 293;
  22. SIGHUP = 294;
  23. SIGINT = 295;
  24. SIGKILL = 296;
  25. SIGPIPE = 297;
  26. SIGQUIT = 298;
  27. SIGUSR1 = 299;
  28. SIGUSR2 = 300;
  29. SIGNOFP = 301;
  30. SIGTRAP = 302;
  31. SIGTIMR = 303; { Internal for setitimer (SIGALRM, SIGPROF) }
  32. SIGPROF = 304;
  33. SIGMAX = 320;
  34. SIG_BLOCK = 1;
  35. SIG_SETMASK = 2;
  36. SIG_UNBLOCK = 3;
  37. function SIG_DFL( x: longint) : longint; cdecl;
  38. function SIG_ERR( x: longint) : longint; cdecl;
  39. function SIG_IGN( x: longint) : longint; cdecl;
  40. type
  41. SignalHandler = function (v : longint) : longint;cdecl;
  42. PSignalHandler = ^SignalHandler; { to be compatible with linux.pp }
  43. function signal(sig : longint;func : SignalHandler) : SignalHandler;
  44. const
  45. EXCEPTION_MAXIMUM_PARAMETERS = 15;
  46. {$define __HASCONTEXT__}
  47. //
  48. // Define 128-bit 16-byte aligned xmm register type.
  49. //
  50. //typedef struct DECLSPEC_ALIGN(16) _M128A {
  51. {$note todo, fix alignment }
  52. type
  53. DWORD64 = QWORD;
  54. ULONGLONG = QWORD;
  55. LONGLONG = int64;
  56. M128A = record
  57. Low: ULONGLONG;
  58. High: LONGLONG;
  59. end;
  60. _M128A = M128A;
  61. TM128A = M128A;
  62. PM128A = TM128A;
  63. //
  64. // Format of data for 32-bit fxsave/fxrstor instructions.
  65. //
  66. //typedef struct _XMM_SAVE_AREA32 {
  67. type
  68. XMM_SAVE_AREA32 = record
  69. ControlWord: WORD;
  70. StatusWord: WORD;
  71. TagWord: BYTE;
  72. Reserved1: BYTE;
  73. ErrorOpcode: WORD;
  74. ErrorOffset: DWORD;
  75. ErrorSelector: WORD;
  76. Reserved2: WORD;
  77. DataOffset: DWORD;
  78. DataSelector: WORD;
  79. Reserved3: WORD;
  80. MxCsr: DWORD;
  81. MxCsr_Mask: DWORD;
  82. FloatRegisters: array[0..7] of M128A;
  83. XmmRegisters: array[0..16] of M128A;
  84. Reserved4: array[0..95] of BYTE;
  85. end;
  86. _XMM_SAVE_AREA32 = XMM_SAVE_AREA32;
  87. TXmmSaveArea = XMM_SAVE_AREA32;
  88. PXmmSaveArea = ^TXmmSaveArea;
  89. const
  90. LEGACY_SAVE_AREA_LENGTH = sizeof(XMM_SAVE_AREA32);
  91. //
  92. // Context Frame
  93. //
  94. // This frame has a several purposes: 1) it is used as an argument to
  95. // NtContinue, 2) is is used to constuct a call frame for APC delivery,
  96. // and 3) it is used in the user level thread creation routines.
  97. //
  98. //
  99. // The flags field within this record controls the contents of a CONTEXT
  100. // record.
  101. //
  102. // If the context record is used as an input parameter, then for each
  103. // portion of the context record controlled by a flag whose value is
  104. // set, it is assumed that that portion of the context record contains
  105. // valid context. If the context record is being used to modify a threads
  106. // context, then only that portion of the threads context is modified.
  107. //
  108. // If the context record is used as an output parameter to capture the
  109. // context of a thread, then only those portions of the thread's context
  110. // corresponding to set flags will be returned.
  111. //
  112. // CONTEXT_CONTROL specifies SegSs, Rsp, SegCs, Rip, and EFlags.
  113. //
  114. // CONTEXT_INTEGER specifies Rax, Rcx, Rdx, Rbx, Rbp, Rsi, Rdi, and R8-R15.
  115. //
  116. // CONTEXT_SEGMENTS specifies SegDs, SegEs, SegFs, and SegGs.
  117. //
  118. // CONTEXT_DEBUG_REGISTERS specifies Dr0-Dr3 and Dr6-Dr7.
  119. //
  120. // CONTEXT_MMX_REGISTERS specifies the floating point and extended registers
  121. // Mm0/St0-Mm7/St7 and Xmm0-Xmm15).
  122. //
  123. //typedef struct DECLSPEC_ALIGN(16) _CONTEXT {
  124. {$packrecords C}
  125. type
  126. CONTEXT = record
  127. //
  128. // Register parameter home addresses.
  129. //
  130. // N.B. These fields are for convience - they could be used to extend the
  131. // context record in the future.
  132. //
  133. P1Home: DWORD64;
  134. P2Home: DWORD64;
  135. P3Home: DWORD64;
  136. P4Home: DWORD64;
  137. P5Home: DWORD64;
  138. P6Home: DWORD64;
  139. //
  140. // Control flags.
  141. //
  142. ContextFlags: DWORD;
  143. MxCsr: DWORD;
  144. //
  145. // Segment Registers and processor flags.
  146. //
  147. SegCs: WORD;
  148. SegDs: WORD;
  149. SegEs: WORD;
  150. SegFs: WORD;
  151. SegGs: WORD;
  152. SegSs: WORD;
  153. EFlags: DWORD;
  154. //
  155. // Debug registers
  156. //
  157. Dr0: DWORD64;
  158. Dr1: DWORD64;
  159. Dr2: DWORD64;
  160. Dr3: DWORD64;
  161. Dr6: DWORD64;
  162. Dr7: DWORD64;
  163. //
  164. // Integer registers.
  165. //
  166. Rax: DWORD64;
  167. Rcx: DWORD64;
  168. Rdx: DWORD64;
  169. Rbx: DWORD64;
  170. Rsp: DWORD64;
  171. Rbp: DWORD64;
  172. Rsi: DWORD64;
  173. Rdi: DWORD64;
  174. R8: DWORD64;
  175. R9: DWORD64;
  176. R10: DWORD64;
  177. R11: DWORD64;
  178. R12: DWORD64;
  179. R13: DWORD64;
  180. R14: DWORD64;
  181. R15: DWORD64;
  182. //
  183. // Program counter.
  184. //
  185. Rip: DWORD64;
  186. //
  187. // Floating point state.
  188. //
  189. FltSave: XMM_SAVE_AREA32; // MWE: only translated the FltSave part of the union
  190. (*
  191. union {
  192. XMM_SAVE_AREA32 FltSave;
  193. struct {
  194. M128A Header[2];
  195. M128A Legacy[8];
  196. M128A Xmm0;
  197. M128A Xmm1;
  198. M128A Xmm2;
  199. M128A Xmm3;
  200. M128A Xmm4;
  201. M128A Xmm5;
  202. M128A Xmm6;
  203. M128A Xmm7;
  204. M128A Xmm8;
  205. M128A Xmm9;
  206. M128A Xmm10;
  207. M128A Xmm11;
  208. M128A Xmm12;
  209. M128A Xmm13;
  210. M128A Xmm14;
  211. M128A Xmm15;
  212. };
  213. };
  214. *)
  215. //
  216. // Vector registers.
  217. //
  218. VectorRegister: array[0..25] of M128A;
  219. VectorControl: DWORD64;
  220. //
  221. // Special debug control registers.
  222. //
  223. DebugControl: DWORD64;
  224. LastBranchToRip: DWORD64;
  225. LastBranchFromRip: DWORD64;
  226. LastExceptionToRip: DWORD64;
  227. LastExceptionFromRip: DWORD64;
  228. end;
  229. (*
  230. type
  231. FLOATING_SAVE_AREA = record
  232. ControlWord : DWORD;
  233. StatusWord : DWORD;
  234. TagWord : DWORD;
  235. ErrorOffset : DWORD;
  236. ErrorSelector : DWORD;
  237. DataOffset : DWORD;
  238. DataSelector : DWORD;
  239. RegisterArea : array[0..79] of BYTE;
  240. Cr0NpxState : DWORD;
  241. end;
  242. _FLOATING_SAVE_AREA = FLOATING_SAVE_AREA;
  243. TFLOATINGSAVEAREA = FLOATING_SAVE_AREA;
  244. PFLOATINGSAVEAREA = ^FLOATING_SAVE_AREA;
  245. CONTEXT = record
  246. ContextFlags : DWORD;
  247. Dr0 : DWORD;
  248. Dr1 : DWORD;
  249. Dr2 : DWORD;
  250. Dr3 : DWORD;
  251. Dr6 : DWORD;
  252. Dr7 : DWORD;
  253. FloatSave : FLOATING_SAVE_AREA;
  254. SegGs : DWORD;
  255. SegFs : DWORD;
  256. SegEs : DWORD;
  257. SegDs : DWORD;
  258. Edi : DWORD;
  259. Esi : DWORD;
  260. Ebx : DWORD;
  261. Edx : DWORD;
  262. Ecx : DWORD;
  263. Eax : DWORD;
  264. Ebp : DWORD;
  265. Eip : DWORD;
  266. SegCs : DWORD;
  267. EFlags : DWORD;
  268. Esp : DWORD;
  269. SegSs : DWORD;
  270. end;
  271. *)
  272. LPCONTEXT = ^CONTEXT;
  273. _CONTEXT = CONTEXT;
  274. TCONTEXT = CONTEXT;
  275. PCONTEXT = ^CONTEXT;
  276. type
  277. pexception_record = ^exception_record;
  278. EXCEPTION_RECORD = record
  279. ExceptionCode : cardinal;
  280. ExceptionFlags : longint;
  281. ExceptionRecord : pexception_record;
  282. ExceptionAddress : pointer;
  283. NumberParameters : longint;
  284. ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of pointer;
  285. end;
  286. PEXCEPTION_POINTERS = ^EXCEPTION_POINTERS;
  287. EXCEPTION_POINTERS = record
  288. ExceptionRecord : PEXCEPTION_RECORD ;
  289. ContextRecord : PCONTEXT ;
  290. end;
  291. implementation
  292. const
  293. EXCEPTION_ACCESS_VIOLATION = $c0000005;
  294. EXCEPTION_BREAKPOINT = $80000003;
  295. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  296. EXCEPTION_SINGLE_STEP = $80000004;
  297. EXCEPTION_ARRAY_BOUNDS_EXCEEDED = $c000008c;
  298. EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d;
  299. EXCEPTION_FLT_DIVIDE_BY_ZERO = $c000008e;
  300. EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  301. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;
  302. EXCEPTION_FLT_OVERFLOW = $c0000091;
  303. EXCEPTION_FLT_STACK_CHECK = $c0000092;
  304. EXCEPTION_FLT_UNDERFLOW = $c0000093;
  305. EXCEPTION_INT_DIVIDE_BY_ZERO = $c0000094;
  306. EXCEPTION_INT_OVERFLOW = $c0000095;
  307. EXCEPTION_INVALID_HANDLE = $c0000008;
  308. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  309. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  310. EXCEPTION_NONCONTINUABLE = $1;
  311. EXCEPTION_STACK_OVERFLOW = $c00000fd;
  312. EXCEPTION_INVALID_DISPOSITION = $c0000026;
  313. EXCEPTION_ILLEGAL_INSTRUCTION = $C000001D;
  314. EXCEPTION_IN_PAGE_ERROR = $C0000006;
  315. EXCEPTION_EXECUTE_HANDLER = 1;
  316. EXCEPTION_CONTINUE_EXECUTION = -(1);
  317. EXCEPTION_CONTINUE_SEARCH = 0;
  318. type
  319. { type of functions that should be used for exception handling }
  320. LPTOP_LEVEL_EXCEPTION_FILTER = function(excep :PEXCEPTION_POINTERS) : longint;stdcall;
  321. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER)
  322. : LPTOP_LEVEL_EXCEPTION_FILTER;
  323. stdcall; external 'kernel32' name 'SetUnhandledExceptionFilter';
  324. var
  325. signal_list : Array[SIGABRT..SIGMAX] of SignalHandler;
  326. { var
  327. value of the stack segment
  328. to check if the call stack can be written on exceptions
  329. this is without any object on win64
  330. _SS : cardinal; }
  331. const
  332. Exception_handler_installed : boolean = false;
  333. MAX_Level = 16;
  334. except_level : byte = 0;
  335. var
  336. except_rip : array[0..Max_level-1] of dword64;
  337. except_signal : array[0..Max_level-1] of dword64;
  338. reset_fpu : array[0..max_level-1] of boolean;
  339. procedure JumpToHandleSignal;
  340. var
  341. res, rip, _rbp, sigtype : dword64;
  342. begin
  343. asm
  344. movq (%rbp),%rax
  345. movq %rax,_rbp
  346. end;
  347. {$ifdef SIGNALS_DEBUG}
  348. if IsConsole then
  349. Writeln(stderr,'In start of JumpToHandleSignal');
  350. {$endif SIGNALS_DEBUG}
  351. if except_level>0 then
  352. dec(except_level)
  353. else
  354. RunError(216);
  355. rip:=except_rip[except_level];
  356. sigtype:=except_signal[except_level];
  357. if reset_fpu[except_level] then
  358. SysResetFPU;
  359. if assigned(System_exception_frame) then
  360. { get the handler in front again }
  361. asm
  362. movq System_exception_frame,%rax
  363. movq %rax,%gs:(0)
  364. end;
  365. if (sigtype>=SIGABRT) and (sigtype<=SIGMAX) and
  366. (signal_list[sigtype]<>@SIG_DFL) then
  367. begin
  368. res:=signal_list[sigtype](sigtype);
  369. end
  370. else
  371. res:=0;
  372. if res=0 then
  373. Begin
  374. {$ifdef SIGNALS_DEBUG}
  375. if IsConsole then
  376. Writeln(stderr,'In JumpToHandleSignal');
  377. {$endif SIGNALS_DEBUG}
  378. RunError(sigtype);
  379. end
  380. else
  381. { jump back to old code }
  382. asm
  383. movq rip,%rax
  384. push %rax
  385. movq _rbp,%rax
  386. push %rax
  387. leave
  388. ret
  389. end;
  390. end;
  391. function Signals_exception_handler
  392. (excep_exceptionrecord :PEXCEPTION_RECORD;
  393. excep_frame : PEXCEPTION_FRAME;
  394. excep_contextrecord : PCONTEXT;
  395. dispatch : pointer) : longint;stdcall;
  396. var frame,res : longint;
  397. function CallSignal(sigtype,frame : longint;must_reset_fpu : boolean) : longint;
  398. begin
  399. {$ifdef SIGNALS_DEBUG}
  400. if IsConsole then
  401. begin
  402. writeln(stderr,'CallSignal called for signal ',sigtype);
  403. dump_stack(stderr,pointer(frame));
  404. end;
  405. {$endif SIGNALS_DEBUG}
  406. {if frame=0 then
  407. begin
  408. CallSignal:=1;
  409. writeln(stderr,'CallSignal frame is zero');
  410. end
  411. else }
  412. begin
  413. if except_level >= Max_level then
  414. exit;
  415. except_rip[except_level]:=excep_ContextRecord^.Rip;
  416. except_signal[except_level]:=sigtype;
  417. reset_fpu[except_level]:=must_reset_fpu;
  418. inc(except_level);
  419. {dec(excep^.ContextRecord^.Esp,4);
  420. plongint (excep^.ContextRecord^.Esp)^ := longint(excep^.ContextRecord^.rip);}
  421. excep_ContextRecord^.rip:=longint(@JumpToHandleSignal);
  422. excep_ExceptionRecord^.ExceptionCode:=0;
  423. CallSignal:=0;
  424. {$ifdef SIGNALS_DEBUG}
  425. if IsConsole then
  426. writeln(stderr,'Exception_Continue_Execution set');
  427. {$endif SIGNALS_DEBUG}
  428. end;
  429. end;
  430. begin
  431. frame:=excep_ContextRecord^.rbp;
  432. { default : unhandled !}
  433. res:=1;
  434. {$ifdef SIGNALS_DEBUG}
  435. if IsConsole then
  436. writeln(stderr,'Signals exception ',
  437. hexstr(excep_ExceptionRecord^.ExceptionCode,8));
  438. {$endif SIGNALS_DEBUG}
  439. case excep_ExceptionRecord^.ExceptionCode of
  440. EXCEPTION_ACCESS_VIOLATION :
  441. res:=CallSignal(SIGSEGV,frame,false);
  442. { EXCEPTION_BREAKPOINT = $80000003;
  443. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  444. EXCEPTION_SINGLE_STEP = $80000004; }
  445. EXCEPTION_ARRAY_BOUNDS_EXCEEDED :
  446. res:=CallSignal(SIGSEGV,frame,false);
  447. EXCEPTION_FLT_DENORMAL_OPERAND :
  448. begin
  449. res:=CallSignal(SIGFPE,frame,true);
  450. end;
  451. EXCEPTION_FLT_DIVIDE_BY_ZERO :
  452. begin
  453. res:=CallSignal(SIGFPE,frame,true);
  454. {excep^.ContextRecord^.FloatSave.StatusWord:=excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  455. end;
  456. {EXCEPTION_FLT_INEXACT_RESULT = $c000008f; }
  457. EXCEPTION_FLT_INVALID_OPERATION :
  458. begin
  459. res:=CallSignal(SIGFPE,frame,true);
  460. end;
  461. EXCEPTION_FLT_OVERFLOW :
  462. begin
  463. res:=CallSignal(SIGFPE,frame,true);
  464. end;
  465. EXCEPTION_FLT_STACK_CHECK :
  466. begin
  467. res:=CallSignal(SIGFPE,frame,true);
  468. end;
  469. EXCEPTION_FLT_UNDERFLOW :
  470. begin
  471. res:=CallSignal(SIGFPE,frame,true); { should be accepted as zero !! }
  472. end;
  473. EXCEPTION_INT_DIVIDE_BY_ZERO :
  474. res:=CallSignal(SIGFPE,frame,false);
  475. EXCEPTION_INT_OVERFLOW :
  476. res:=CallSignal(SIGFPE,frame,false);
  477. {EXCEPTION_INVALID_HANDLE = $c0000008;
  478. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  479. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  480. EXCEPTION_NONCONTINUABLE = $1;}
  481. EXCEPTION_STACK_OVERFLOW :
  482. res:=CallSignal(SIGSEGV,frame,false);
  483. {EXCEPTION_INVALID_DISPOSITION = $c0000026;}
  484. EXCEPTION_ILLEGAL_INSTRUCTION,
  485. EXCEPTION_PRIV_INSTRUCTION,
  486. EXCEPTION_IN_PAGE_ERROR,
  487. EXCEPTION_SINGLE_STEP : res:=CallSignal(SIGSEGV,frame,false);
  488. { Ignore EXCEPTION_INVALID_HANDLE exceptions }
  489. EXCEPTION_INVALID_HANDLE : res:=0;
  490. end;
  491. Signals_exception_handler:=res;
  492. end;
  493. function API_signals_exception_handler(exceptptrs : PEXCEPTION_POINTERS) : longint; stdcall;
  494. begin
  495. API_signals_exception_handler:=Signals_exception_handler(
  496. @exceptptrs^.ExceptionRecord,
  497. nil,
  498. @exceptptrs^.ContextRecord,
  499. nil);
  500. end;
  501. const
  502. PreviousHandler : LPTOP_LEVEL_EXCEPTION_FILTER = nil;
  503. Prev_Handler : pointer = nil;
  504. Prev_fpc_handler : pointer = nil;
  505. procedure install_exception_handler;
  506. {$ifdef SIGNALS_DEBUG}
  507. var
  508. oldexceptaddr,newexceptaddr : longint;
  509. {$endif SIGNALS_DEBUG}
  510. begin
  511. if Exception_handler_installed then
  512. exit;
  513. if assigned(System_exception_frame) then
  514. begin
  515. prev_fpc_handler:=System_exception_frame^.handler;
  516. System_exception_frame^.handler:=@Signals_exception_handler;
  517. { get the handler in front again }
  518. asm
  519. movq %gs:(0),%rax
  520. movq %rax,prev_handler
  521. movq System_exception_frame,%rax
  522. movq %rax,%gs:(0)
  523. end;
  524. Exception_handler_installed:=true;
  525. exit;
  526. end;
  527. {$ifdef SIGNALS_DEBUG}
  528. asm
  529. movq $0,%rax
  530. movq %gs:(%rax),%rax
  531. movq %rax,oldexceptaddr
  532. end;
  533. {$endif SIGNALS_DEBUG}
  534. PreviousHandler:=SetUnhandledExceptionFilter(@API_signals_exception_handler);
  535. {$ifdef SIGNALS_DEBUG}
  536. asm
  537. movq $0,%rax
  538. movq %gs:(%rax),%rax
  539. movq %rax,newexceptaddr
  540. end;
  541. if IsConsole then
  542. begin
  543. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  544. ' new exception ',hexstr(newexceptaddr,8));
  545. writeln('SetUnhandledExceptionFilter returned ',hexstr(longint(PreviousHandler),8));
  546. end;
  547. {$endif SIGNALS_DEBUG}
  548. Exception_handler_installed := true;
  549. end;
  550. procedure remove_exception_handler;
  551. begin
  552. if not Exception_handler_installed then
  553. exit;
  554. if assigned(System_exception_frame) then
  555. begin
  556. if assigned(prev_fpc_handler) then
  557. System_exception_frame^.handler:=prev_fpc_handler;
  558. prev_fpc_handler:=nil;
  559. { restore old handler order again }
  560. if assigned(prev_handler) then
  561. asm
  562. movq prev_handler,%rax
  563. movq %rax,%gs:(0)
  564. end;
  565. prev_handler:=nil;
  566. Exception_handler_installed:=false;
  567. exit;
  568. end;
  569. SetUnhandledExceptionFilter(PreviousHandler);
  570. PreviousHandler:=nil;
  571. Exception_handler_installed:=false;
  572. end;
  573. function SIG_ERR(x:longint):longint; cdecl;
  574. begin
  575. SIG_ERR:=-1;
  576. end;
  577. function SIG_IGN(x:longint):longint; cdecl;
  578. begin
  579. SIG_IGN:=-1;
  580. end;
  581. function SIG_DFL(x:longint):longint; cdecl;
  582. begin
  583. SIG_DFL:=0;
  584. end;
  585. function signal(sig : longint;func : SignalHandler) : SignalHandler;
  586. var
  587. temp : SignalHandler;
  588. begin
  589. if ((sig < SIGABRT) or (sig > SIGMAX) or (sig = SIGKILL)) then
  590. begin
  591. signal:=@SIG_ERR;
  592. runerror(201);
  593. end;
  594. if not Exception_handler_installed then
  595. install_exception_handler;
  596. temp := signal_list[sig];
  597. signal_list[sig] := func;
  598. signal:=temp;
  599. end;
  600. var
  601. i : longint;
  602. initialization
  603. for i:=SIGABRT to SIGMAX do
  604. signal_list[i]:=@SIG_DFL;
  605. {install_exception_handler;
  606. delay this to first use
  607. as other units also might install their handlers PM }
  608. finalization
  609. remove_exception_handler;
  610. end.