signals.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. {$asmmode att}
  293. const
  294. EXCEPTION_ACCESS_VIOLATION = $c0000005;
  295. EXCEPTION_BREAKPOINT = $80000003;
  296. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  297. EXCEPTION_SINGLE_STEP = $80000004;
  298. EXCEPTION_ARRAY_BOUNDS_EXCEEDED = $c000008c;
  299. EXCEPTION_FLT_DENORMAL_OPERAND = $c000008d;
  300. EXCEPTION_FLT_DIVIDE_BY_ZERO = $c000008e;
  301. EXCEPTION_FLT_INEXACT_RESULT = $c000008f;
  302. EXCEPTION_FLT_INVALID_OPERATION = $c0000090;
  303. EXCEPTION_FLT_OVERFLOW = $c0000091;
  304. EXCEPTION_FLT_STACK_CHECK = $c0000092;
  305. EXCEPTION_FLT_UNDERFLOW = $c0000093;
  306. EXCEPTION_INT_DIVIDE_BY_ZERO = $c0000094;
  307. EXCEPTION_INT_OVERFLOW = $c0000095;
  308. EXCEPTION_INVALID_HANDLE = $c0000008;
  309. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  310. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  311. EXCEPTION_NONCONTINUABLE = $1;
  312. EXCEPTION_STACK_OVERFLOW = $c00000fd;
  313. EXCEPTION_INVALID_DISPOSITION = $c0000026;
  314. EXCEPTION_ILLEGAL_INSTRUCTION = $C000001D;
  315. EXCEPTION_IN_PAGE_ERROR = $C0000006;
  316. EXCEPTION_EXECUTE_HANDLER = 1;
  317. EXCEPTION_CONTINUE_EXECUTION = -(1);
  318. EXCEPTION_CONTINUE_SEARCH = 0;
  319. type
  320. { type of functions that should be used for exception handling }
  321. LPTOP_LEVEL_EXCEPTION_FILTER = function(excep :PEXCEPTION_POINTERS) : longint;stdcall;
  322. function SetUnhandledExceptionFilter(lpTopLevelExceptionFilter : LPTOP_LEVEL_EXCEPTION_FILTER)
  323. : LPTOP_LEVEL_EXCEPTION_FILTER;
  324. stdcall; external 'kernel32' name 'SetUnhandledExceptionFilter';
  325. var
  326. signal_list : Array[SIGABRT..SIGMAX] of SignalHandler;
  327. { var
  328. value of the stack segment
  329. to check if the call stack can be written on exceptions
  330. this is without any object on win64
  331. _SS : cardinal; }
  332. const
  333. Exception_handler_installed : boolean = false;
  334. MAX_Level = 16;
  335. except_level : byte = 0;
  336. var
  337. except_rip : array[0..Max_level-1] of dword64;
  338. except_signal : array[0..Max_level-1] of dword64;
  339. reset_fpu : array[0..max_level-1] of boolean;
  340. procedure JumpToHandleSignal;
  341. var
  342. res, rip, _rbp, sigtype : dword64;
  343. begin
  344. asm
  345. movq (%rbp),%rax
  346. movq %rax,_rbp
  347. end;
  348. {$ifdef SIGNALS_DEBUG}
  349. if IsConsole then
  350. Writeln(stderr,'In start of JumpToHandleSignal');
  351. {$endif SIGNALS_DEBUG}
  352. if except_level>0 then
  353. dec(except_level)
  354. else
  355. RunError(216);
  356. rip:=except_rip[except_level];
  357. sigtype:=except_signal[except_level];
  358. if reset_fpu[except_level] then
  359. SysResetFPU;
  360. if assigned(System_exception_frame) then
  361. { get the handler in front again }
  362. asm
  363. movq System_exception_frame,%rax
  364. movq %rax,%gs:(0)
  365. end;
  366. if (sigtype>=SIGABRT) and (sigtype<=SIGMAX) and
  367. (signal_list[sigtype]<>@SIG_DFL) then
  368. begin
  369. res:=signal_list[sigtype](sigtype);
  370. end
  371. else
  372. res:=0;
  373. if res=0 then
  374. Begin
  375. {$ifdef SIGNALS_DEBUG}
  376. if IsConsole then
  377. Writeln(stderr,'In JumpToHandleSignal');
  378. {$endif SIGNALS_DEBUG}
  379. RunError(sigtype);
  380. end
  381. else
  382. { jump back to old code }
  383. asm
  384. movq rip,%rax
  385. push %rax
  386. movq _rbp,%rax
  387. push %rax
  388. leave
  389. ret
  390. end;
  391. end;
  392. function Signals_exception_handler
  393. (excep_exceptionrecord :PEXCEPTION_RECORD;
  394. excep_frame : PEXCEPTION_FRAME;
  395. excep_contextrecord : PCONTEXT;
  396. dispatch : pointer) : longint;stdcall;
  397. var frame,res : longint;
  398. function CallSignal(sigtype,frame : longint;must_reset_fpu : boolean) : longint;
  399. begin
  400. {$ifdef SIGNALS_DEBUG}
  401. if IsConsole then
  402. begin
  403. writeln(stderr,'CallSignal called for signal ',sigtype);
  404. dump_stack(stderr,pointer(frame));
  405. end;
  406. {$endif SIGNALS_DEBUG}
  407. {if frame=0 then
  408. begin
  409. CallSignal:=1;
  410. writeln(stderr,'CallSignal frame is zero');
  411. end
  412. else }
  413. begin
  414. if except_level >= Max_level then
  415. exit;
  416. except_rip[except_level]:=excep_ContextRecord^.Rip;
  417. except_signal[except_level]:=sigtype;
  418. reset_fpu[except_level]:=must_reset_fpu;
  419. inc(except_level);
  420. {dec(excep^.ContextRecord^.Esp,4);
  421. plongint (excep^.ContextRecord^.Esp)^ := longint(excep^.ContextRecord^.rip);}
  422. excep_ContextRecord^.rip:=ptruint(@JumpToHandleSignal);
  423. excep_ExceptionRecord^.ExceptionCode:=0;
  424. CallSignal:=0;
  425. {$ifdef SIGNALS_DEBUG}
  426. if IsConsole then
  427. writeln(stderr,'Exception_Continue_Execution set');
  428. {$endif SIGNALS_DEBUG}
  429. end;
  430. end;
  431. begin
  432. frame:=excep_ContextRecord^.rbp;
  433. { default : unhandled !}
  434. res:=1;
  435. {$ifdef SIGNALS_DEBUG}
  436. if IsConsole then
  437. writeln(stderr,'Signals exception ',
  438. hexstr(excep_ExceptionRecord^.ExceptionCode,8));
  439. {$endif SIGNALS_DEBUG}
  440. case excep_ExceptionRecord^.ExceptionCode of
  441. EXCEPTION_ACCESS_VIOLATION :
  442. res:=CallSignal(SIGSEGV,frame,false);
  443. { EXCEPTION_BREAKPOINT = $80000003;
  444. EXCEPTION_DATATYPE_MISALIGNMENT = $80000002;
  445. EXCEPTION_SINGLE_STEP = $80000004; }
  446. EXCEPTION_ARRAY_BOUNDS_EXCEEDED :
  447. res:=CallSignal(SIGSEGV,frame,false);
  448. EXCEPTION_FLT_DENORMAL_OPERAND :
  449. begin
  450. res:=CallSignal(SIGFPE,frame,true);
  451. end;
  452. EXCEPTION_FLT_DIVIDE_BY_ZERO :
  453. begin
  454. res:=CallSignal(SIGFPE,frame,true);
  455. {excep^.ContextRecord^.FloatSave.StatusWord:=excep^.ContextRecord^.FloatSave.StatusWord and $ffffff00;}
  456. end;
  457. {EXCEPTION_FLT_INEXACT_RESULT = $c000008f; }
  458. EXCEPTION_FLT_INVALID_OPERATION :
  459. begin
  460. res:=CallSignal(SIGFPE,frame,true);
  461. end;
  462. EXCEPTION_FLT_OVERFLOW :
  463. begin
  464. res:=CallSignal(SIGFPE,frame,true);
  465. end;
  466. EXCEPTION_FLT_STACK_CHECK :
  467. begin
  468. res:=CallSignal(SIGFPE,frame,true);
  469. end;
  470. EXCEPTION_FLT_UNDERFLOW :
  471. begin
  472. res:=CallSignal(SIGFPE,frame,true); { should be accepted as zero !! }
  473. end;
  474. EXCEPTION_INT_DIVIDE_BY_ZERO :
  475. res:=CallSignal(SIGFPE,frame,false);
  476. EXCEPTION_INT_OVERFLOW :
  477. res:=CallSignal(SIGFPE,frame,false);
  478. {EXCEPTION_INVALID_HANDLE = $c0000008;
  479. EXCEPTION_PRIV_INSTRUCTION = $c0000096;
  480. EXCEPTION_NONCONTINUABLE_EXCEPTION = $c0000025;
  481. EXCEPTION_NONCONTINUABLE = $1;}
  482. EXCEPTION_STACK_OVERFLOW :
  483. res:=CallSignal(SIGSEGV,frame,false);
  484. {EXCEPTION_INVALID_DISPOSITION = $c0000026;}
  485. EXCEPTION_ILLEGAL_INSTRUCTION,
  486. EXCEPTION_PRIV_INSTRUCTION,
  487. EXCEPTION_IN_PAGE_ERROR,
  488. EXCEPTION_SINGLE_STEP : res:=CallSignal(SIGSEGV,frame,false);
  489. { Ignore EXCEPTION_INVALID_HANDLE exceptions }
  490. EXCEPTION_INVALID_HANDLE : res:=0;
  491. end;
  492. Signals_exception_handler:=res;
  493. end;
  494. function API_signals_exception_handler(exceptptrs : PEXCEPTION_POINTERS) : longint; stdcall;
  495. begin
  496. API_signals_exception_handler:=Signals_exception_handler(
  497. @exceptptrs^.ExceptionRecord,
  498. nil,
  499. @exceptptrs^.ContextRecord,
  500. nil);
  501. end;
  502. const
  503. PreviousHandler : LPTOP_LEVEL_EXCEPTION_FILTER = nil;
  504. Prev_Handler : pointer = nil;
  505. Prev_fpc_handler : pointer = nil;
  506. procedure install_exception_handler;
  507. {$ifdef SIGNALS_DEBUG}
  508. var
  509. oldexceptaddr,newexceptaddr : longint;
  510. {$endif SIGNALS_DEBUG}
  511. begin
  512. if Exception_handler_installed then
  513. exit;
  514. if assigned(System_exception_frame) then
  515. begin
  516. prev_fpc_handler:=System_exception_frame^.handler;
  517. System_exception_frame^.handler:=@Signals_exception_handler;
  518. { get the handler in front again }
  519. asm
  520. movq %gs:(0),%rax
  521. movq %rax,prev_handler
  522. movq System_exception_frame,%rax
  523. movq %rax,%gs:(0)
  524. end;
  525. Exception_handler_installed:=true;
  526. exit;
  527. end;
  528. {$ifdef SIGNALS_DEBUG}
  529. asm
  530. movq $0,%rax
  531. movq %gs:(%rax),%rax
  532. movq %rax,oldexceptaddr
  533. end;
  534. {$endif SIGNALS_DEBUG}
  535. PreviousHandler:=SetUnhandledExceptionFilter(@API_signals_exception_handler);
  536. {$ifdef SIGNALS_DEBUG}
  537. asm
  538. movq $0,%rax
  539. movq %gs:(%rax),%rax
  540. movq %rax,newexceptaddr
  541. end;
  542. if IsConsole then
  543. begin
  544. writeln(stderr,'Old exception ',hexstr(oldexceptaddr,8),
  545. ' new exception ',hexstr(newexceptaddr,8));
  546. writeln('SetUnhandledExceptionFilter returned ',hexstr(longint(PreviousHandler),8));
  547. end;
  548. {$endif SIGNALS_DEBUG}
  549. Exception_handler_installed := true;
  550. end;
  551. procedure remove_exception_handler;
  552. begin
  553. if not Exception_handler_installed then
  554. exit;
  555. if assigned(System_exception_frame) then
  556. begin
  557. if assigned(prev_fpc_handler) then
  558. System_exception_frame^.handler:=prev_fpc_handler;
  559. prev_fpc_handler:=nil;
  560. { restore old handler order again }
  561. if assigned(prev_handler) then
  562. asm
  563. movq prev_handler,%rax
  564. movq %rax,%gs:(0)
  565. end;
  566. prev_handler:=nil;
  567. Exception_handler_installed:=false;
  568. exit;
  569. end;
  570. SetUnhandledExceptionFilter(PreviousHandler);
  571. PreviousHandler:=nil;
  572. Exception_handler_installed:=false;
  573. end;
  574. function SIG_ERR(x:longint):longint; cdecl;
  575. begin
  576. SIG_ERR:=-1;
  577. end;
  578. function SIG_IGN(x:longint):longint; cdecl;
  579. begin
  580. SIG_IGN:=-1;
  581. end;
  582. function SIG_DFL(x:longint):longint; cdecl;
  583. begin
  584. SIG_DFL:=0;
  585. end;
  586. function signal(sig : longint;func : SignalHandler) : SignalHandler;
  587. var
  588. temp : SignalHandler;
  589. begin
  590. if ((sig < SIGABRT) or (sig > SIGMAX) or (sig = SIGKILL)) then
  591. begin
  592. signal:=@SIG_ERR;
  593. runerror(201);
  594. end;
  595. if not Exception_handler_installed then
  596. install_exception_handler;
  597. temp := signal_list[sig];
  598. signal_list[sig] := func;
  599. signal:=temp;
  600. end;
  601. var
  602. i : longint;
  603. initialization
  604. for i:=SIGABRT to SIGMAX do
  605. signal_list[i]:=@SIG_DFL;
  606. {install_exception_handler;
  607. delay this to first use
  608. as other units also might install their handlers PM }
  609. finalization
  610. remove_exception_handler;
  611. end.