fpcatch.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {$ifdef FVISION}
  47. FVConsts,
  48. {$else}
  49. Commands,
  50. {$endif}
  51. dos,app,msgbox,
  52. FPString,FPCompil,FPIDE;
  53. Const
  54. LastCtrlC : longint = 0;
  55. {$ifdef HasSignal}
  56. {$ifdef Unix}
  57. Procedure CatchSignal(Sig : Integer);cdecl;
  58. {$else}
  59. Function CatchSignal(Sig : longint):longint;
  60. {$endif}
  61. var MustQuit: boolean;
  62. begin
  63. case Sig of
  64. SIGSEGV : begin
  65. if StopJmpValid then
  66. LongJmp(StopJmp,SIGSEGV);
  67. if Assigned(Application) then IDEApp.Done;
  68. Writeln('Internal SIGSEGV Error caught');
  69. {$ifndef DEBUG}
  70. Halt;
  71. {$else DEBUG}
  72. RunError(216);
  73. {$endif DEBUG}
  74. end;
  75. SIGFPE : begin
  76. if StopJmpValid then
  77. LongJmp(StopJmp,SIGFPE);
  78. if Assigned(Application) then IDEApp.Done;
  79. Writeln('Internal SIGFPE Error caught');
  80. {$ifndef DEBUG}
  81. Halt;
  82. {$else DEBUG}
  83. RunError(207);
  84. {$endif DEBUG}
  85. end;
  86. SIGILL : begin
  87. if StopJmpValid then
  88. LongJmp(StopJmp,SIGILL);
  89. if Assigned(Application) then IDEApp.Done;
  90. Writeln('Internal SIGILL Error caught');
  91. {$ifndef DEBUG}
  92. Halt;
  93. {$else DEBUG}
  94. RunError(216);
  95. {$endif DEBUG}
  96. end;
  97. SIGINT : begin
  98. if StopJmpValid then
  99. LongJmp(StopJmp,SIGINT);
  100. IF NOT CtrlCPressed and Assigned(Application) then
  101. begin
  102. MustQuit:=false;
  103. {$ifdef FPC}
  104. if GetDosTicks>LastCtrlC+10 then
  105. begin
  106. CtrlCPressed:=true;
  107. Keyboard.PutKeyEvent((kbCtrl shl 16) or kbCtrlC);
  108. LastCtrlC:=GetDosTicks;
  109. end;
  110. {$endif FPC}
  111. end
  112. else
  113. begin
  114. if Assigned(Application) then
  115. MustQuit:=MessageBox(#3+msg_QuitConfirm,nil,mferror+mfyesbutton+mfnobutton)=cmYes
  116. else
  117. MustQuit:=true;
  118. end;
  119. if MustQuit then
  120. begin
  121. if Assigned(Application) then IDEApp.Done;
  122. {$ifndef DEBUG}
  123. Halt;
  124. {$else DEBUG}
  125. RunError(216);
  126. {$endif DEBUG}
  127. end;
  128. end;
  129. end;
  130. {$ifndef Unix}
  131. CatchSignal:=0;
  132. {$endif}
  133. end;
  134. {$endif def HasSignal}
  135. Const
  136. CatchSignalsEnabled : boolean = false;
  137. Procedure EnableCatchSignals;
  138. {$ifdef win32}
  139. var Mode: DWORD;
  140. {$endif win32}
  141. begin
  142. if CatchSignalsEnabled then
  143. exit;
  144. {$ifdef win32}
  145. if GetConsoleMode(GetStdHandle(Std_Input_Handle), @Mode) then
  146. SetConsoleMode(GetStdHandle(Std_Input_Handle), Mode and not ENABLE_PROCESSED_INPUT);
  147. {$endif win32}
  148. {$ifdef go32v2}
  149. djgpp_set_ctrl_c(false);
  150. {$endif go32v2}
  151. {$ifdef HasSignal}
  152. {$ifndef TP}
  153. NewSignal:=SignalHandler(@CatchSignal);
  154. {$else TP}
  155. NewSignal:=SignalHandler(CatchSignal);
  156. {$endif TP}
  157. OldSigSegm:=Signal (SIGSEGV,NewSignal);
  158. OldSigInt:=Signal (SIGINT,NewSignal);
  159. OldSigFPE:=Signal (SIGFPE,NewSignal);
  160. OldSigILL:=Signal (SIGILL,NewSignal);
  161. CatchSignalsEnabled:=true;
  162. {$endif}
  163. end;
  164. Procedure DisableCatchSignals;
  165. begin
  166. {$ifdef HasSignal}
  167. if not CatchSignalsEnabled then
  168. exit;
  169. Signal (SIGSEGV,OldSigSegm);
  170. Signal (SIGINT,OldSigInt);
  171. Signal (SIGFPE,OldSigFPE);
  172. Signal (SIGILL,OldSigILL);
  173. CatchSignalsEnabled:=false;
  174. {$endif}
  175. end;
  176. end.
  177. {
  178. $Log$
  179. Revision 1.2 2001-08-05 02:01:47 peter
  180. * FVISION define to compile with fvision units
  181. Revision 1.1 2001/08/04 11:30:22 peter
  182. * ide works now with both compiler versions
  183. Revision 1.1.2.4 2000/11/30 13:04:01 pierre
  184. * fix for bug 1205
  185. Revision 1.1.2.3 2000/11/29 00:54:44 pierre
  186. + preserve window number and save special windows
  187. Revision 1.1.2.2 2000/11/14 09:23:55 marco
  188. * Second batch
  189. Revision 1.1.2.1 2000/10/31 07:52:55 pierre
  190. * recover gracefully if compiler generates a signal
  191. Revision 1.1 2000/07/13 09:48:34 michael
  192. + Initial import
  193. Revision 1.6 2000/06/22 09:07:11 pierre
  194. * Gabor changes: see fixes.txt
  195. Revision 1.5 2000/05/02 08:42:26 pierre
  196. * new set of Gabor changes: see fixes.txt
  197. Revision 1.4 2000/03/07 21:09:20 pierre
  198. * Use globdir.inc HasSignal conditional
  199. + Uses PutKeyEvent for CtrlC
  200. Revision 1.3 1999/12/20 14:23:16 pierre
  201. * MyApp renamed IDEApp
  202. * TDebugController.ResetDebuggerRows added to
  203. get resetting of debugger rows
  204. Revision 1.2 1999/04/07 21:55:42 peter
  205. + object support for browser
  206. * html help fixes
  207. * more desktop saving things
  208. * NODEBUG directive to exclude debugger
  209. Revision 1.1 1999/02/20 15:18:28 peter
  210. + ctrl-c capture with confirm dialog
  211. + ascii table in the tools menu
  212. + heapviewer
  213. * empty file fixed
  214. * fixed callback routines in fpdebug to have far for tp7
  215. }