fpcatch.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Michael Van Canneyt
  4. Unit to catch segmentation faults and Ctrl-C and exit gracefully
  5. under linux and go32v2
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. Unit fpcatch;
  13. interface
  14. {$i globdir.inc}
  15. {$ifdef Unix}
  16. uses
  17. {$ifdef VER1_0}
  18. linux;
  19. {$else}
  20. unix;
  21. {$endif}
  22. {$endif}
  23. {$ifdef go32v2}
  24. uses
  25. dpmiexcp;
  26. {$endif}
  27. {$ifdef win32}
  28. uses
  29. windows, signals;
  30. {$endif}
  31. {$ifdef HasSignal}
  32. Var
  33. NewSignal,OldSigSegm,OldSigILL,
  34. OldSigInt,OldSigFPE : SignalHandler;
  35. {$endif}
  36. Const
  37. CtrlCPressed : Boolean = false;
  38. Procedure EnableCatchSignals;
  39. Procedure DisableCatchSignals;
  40. Implementation
  41. uses
  42. {$ifdef FPC}
  43. keyboard,
  44. drivers,
  45. {$endif FPC}
  46. dos,app,commands,msgbox,
  47. FPString,FPCompil,FPIDE;
  48. Const
  49. LastCtrlC : longint = 0;
  50. {$ifdef HasSignal}
  51. {$ifdef Unix}
  52. Procedure CatchSignal(Sig : Integer);cdecl;
  53. {$else}
  54. Function CatchSignal(Sig : longint):longint;
  55. {$endif}
  56. var MustQuit: boolean;
  57. begin
  58. case Sig of
  59. SIGSEGV : begin
  60. if StopJmpValid then
  61. LongJmp(StopJmp,SIGSEGV);
  62. if Assigned(Application) then IDEApp.Done;
  63. Writeln('Internal SIGSEGV Error caught');
  64. {$ifndef DEBUG}
  65. Halt;
  66. {$else DEBUG}
  67. RunError(216);
  68. {$endif DEBUG}
  69. end;
  70. SIGFPE : begin
  71. if StopJmpValid then
  72. LongJmp(StopJmp,SIGFPE);
  73. if Assigned(Application) then IDEApp.Done;
  74. Writeln('Internal SIGFPE Error caught');
  75. {$ifndef DEBUG}
  76. Halt;
  77. {$else DEBUG}
  78. RunError(207);
  79. {$endif DEBUG}
  80. end;
  81. SIGILL : begin
  82. if StopJmpValid then
  83. LongJmp(StopJmp,SIGILL);
  84. if Assigned(Application) then IDEApp.Done;
  85. Writeln('Internal SIGILL Error caught');
  86. {$ifndef DEBUG}
  87. Halt;
  88. {$else DEBUG}
  89. RunError(216);
  90. {$endif DEBUG}
  91. end;
  92. SIGINT : begin
  93. if StopJmpValid then
  94. LongJmp(StopJmp,SIGINT);
  95. IF NOT CtrlCPressed and Assigned(Application) then
  96. begin
  97. MustQuit:=false;
  98. {$ifdef FPC}
  99. if GetDosTicks>LastCtrlC+10 then
  100. begin
  101. CtrlCPressed:=true;
  102. Keyboard.PutKeyEvent((kbCtrl shl 16) or kbCtrlC);
  103. LastCtrlC:=GetDosTicks;
  104. end;
  105. {$endif FPC}
  106. end
  107. else
  108. begin
  109. if Assigned(Application) then
  110. MustQuit:=MessageBox(#3+msg_QuitConfirm,nil,mferror+mfyesbutton+mfnobutton)=cmYes
  111. else
  112. MustQuit:=true;
  113. end;
  114. if MustQuit then
  115. begin
  116. if Assigned(Application) then IDEApp.Done;
  117. {$ifndef DEBUG}
  118. Halt;
  119. {$else DEBUG}
  120. RunError(216);
  121. {$endif DEBUG}
  122. end;
  123. end;
  124. end;
  125. {$ifndef Unix}
  126. CatchSignal:=0;
  127. {$endif}
  128. end;
  129. {$endif def HasSignal}
  130. Const
  131. CatchSignalsEnabled : boolean = false;
  132. Procedure EnableCatchSignals;
  133. {$ifdef win32}
  134. var Mode: DWORD;
  135. {$endif win32}
  136. begin
  137. if CatchSignalsEnabled then
  138. exit;
  139. {$ifdef win32}
  140. if GetConsoleMode(GetStdHandle(Std_Input_Handle), @Mode) then
  141. SetConsoleMode(GetStdHandle(Std_Input_Handle), Mode and not ENABLE_PROCESSED_INPUT);
  142. {$endif win32}
  143. {$ifdef go32v2}
  144. djgpp_set_ctrl_c(false);
  145. {$endif go32v2}
  146. {$ifdef HasSignal}
  147. {$ifndef TP}
  148. NewSignal:=SignalHandler(@CatchSignal);
  149. {$else TP}
  150. NewSignal:=SignalHandler(CatchSignal);
  151. {$endif TP}
  152. OldSigSegm:=Signal (SIGSEGV,NewSignal);
  153. OldSigInt:=Signal (SIGINT,NewSignal);
  154. OldSigFPE:=Signal (SIGFPE,NewSignal);
  155. OldSigILL:=Signal (SIGILL,NewSignal);
  156. CatchSignalsEnabled:=true;
  157. {$endif}
  158. end;
  159. Procedure DisableCatchSignals;
  160. begin
  161. {$ifdef HasSignal}
  162. if not CatchSignalsEnabled then
  163. exit;
  164. Signal (SIGSEGV,OldSigSegm);
  165. Signal (SIGINT,OldSigInt);
  166. Signal (SIGFPE,OldSigFPE);
  167. Signal (SIGILL,OldSigILL);
  168. CatchSignalsEnabled:=false;
  169. {$endif}
  170. end;
  171. end.
  172. {
  173. $Log$
  174. Revision 1.1 2001-08-04 11:30:22 peter
  175. * ide works now with both compiler versions
  176. Revision 1.1.2.4 2000/11/30 13:04:01 pierre
  177. * fix for bug 1205
  178. Revision 1.1.2.3 2000/11/29 00:54:44 pierre
  179. + preserve window number and save special windows
  180. Revision 1.1.2.2 2000/11/14 09:23:55 marco
  181. * Second batch
  182. Revision 1.1.2.1 2000/10/31 07:52:55 pierre
  183. * recover gracefully if compiler generates a signal
  184. Revision 1.1 2000/07/13 09:48:34 michael
  185. + Initial import
  186. Revision 1.6 2000/06/22 09:07:11 pierre
  187. * Gabor changes: see fixes.txt
  188. Revision 1.5 2000/05/02 08:42:26 pierre
  189. * new set of Gabor changes: see fixes.txt
  190. Revision 1.4 2000/03/07 21:09:20 pierre
  191. * Use globdir.inc HasSignal conditional
  192. + Uses PutKeyEvent for CtrlC
  193. Revision 1.3 1999/12/20 14:23:16 pierre
  194. * MyApp renamed IDEApp
  195. * TDebugController.ResetDebuggerRows added to
  196. get resetting of debugger rows
  197. Revision 1.2 1999/04/07 21:55:42 peter
  198. + object support for browser
  199. * html help fixes
  200. * more desktop saving things
  201. * NODEBUG directive to exclude debugger
  202. Revision 1.1 1999/02/20 15:18:28 peter
  203. + ctrl-c capture with confirm dialog
  204. + ascii table in the tools menu
  205. + heapviewer
  206. * empty file fixed
  207. * fixed callback routines in fpdebug to have far for tp7
  208. }