signals.pp 19 KB

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