sighnd.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. member of the Free Pascal development team.
  5. Signal handler is arch dependant due to processor to language
  6. exception conversion.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. const
  14. { Bits in control register }
  15. RoundingMode = $30;
  16. RoundingPrecision = $c0;
  17. InexactDecimal = $100;
  18. InexactOperation = $200;
  19. DivideByZero = $400;
  20. UnderFlow = $800;
  21. OverFlow = $1000;
  22. OperandError = $2000;
  23. SignalingNaN = $4000;
  24. BranchOnUnordered = $800;
  25. fpucw : longint = {InexactOperation or }DivideByZero or
  26. OverFlow or OperandError or
  27. SignalingNaN or BranchOnUnordered;
  28. fpust : longint = 0;
  29. { Bits in status register }
  30. FPU_Invalid = $80;
  31. FPU_Denormal = $8;
  32. FPU_DivisionByZero = $10;
  33. FPU_Overflow = $40;
  34. FPU_Underflow = $20;
  35. { m68k is not stack based }
  36. FPU_StackUnderflow = $0;
  37. FPU_StackOverflow = $0;
  38. FPU_All = $f8;
  39. Procedure ResetFPU;
  40. begin
  41. asm
  42. fmove.l fpucw,fpcr
  43. fmove.l fpust,fpsr
  44. end;
  45. end;
  46. function GetFPUState(const SigContext : TSigContext) : longint;
  47. begin
  48. GetfpuState:=SigContext.psr;
  49. {$ifdef SYSTEM_DEBUG}
  50. Writeln(stderr,'FpuState = ',GetFpuState);
  51. {$endif SYSTEM_DEBUG}
  52. end;
  53. function signr_to_runerrornr(sig:longint;var sigcontext:Tsigcontext):word;
  54. var fpustate:word;
  55. begin
  56. signr_to_runerrornr:=0;
  57. case sig of
  58. SIGFPE :
  59. begin
  60. { this is not allways necessary but I don't know yet
  61. how to tell if it is or not PM }
  62. signr_to_runerrornr:=200;
  63. fpustate:=GetFPUState(SigContext);
  64. if (FpuState and FPU_All) <> 0 then
  65. begin
  66. { first check the more precise options }
  67. if (FpuState and FPU_DivisionByZero)<>0 then
  68. signr_to_runerrornr:=200
  69. else if (FpuState and FPU_Overflow)<>0 then
  70. signr_to_runerrornr:=205
  71. else if (FpuState and FPU_Underflow)<>0 then
  72. signr_to_runerrornr:=206
  73. else if (FpuState and FPU_Denormal)<>0 then
  74. signr_to_runerrornr:=216
  75. else if (FpuState and (FPU_StackOverflow or FPU_StackUnderflow))<>0 then
  76. signr_to_runerrornr:=207
  77. else if (FpuState and FPU_Invalid)<>0 then
  78. signr_to_runerrornr:=216
  79. else
  80. signr_to_runerrornr:=207; {'Coprocessor Error'}
  81. end;
  82. ResetFPU;
  83. end;
  84. SIGILL,
  85. SIGBUS,
  86. SIGSEGV :
  87. signr_to_runerrornr:=216;
  88. SIGINT:
  89. signr_to_runerrornr:=217;
  90. SIGQUIT:
  91. signr_to_runerrornr:=233;
  92. end;
  93. end;
  94. procedure SignalToAbort(sig : longint; SigInfo: PSigInfo;var SigContext: TSigcontext);cdecl;
  95. var
  96. s:string[5];
  97. addr:pointer;
  98. begin
  99. addr:=nil;
  100. exitcode:=signr_to_runerrornr(sig,sigcontext);
  101. reenable_signal(sig);
  102. {I had written a small stack dumper, but decided to remove it, because programs that
  103. activate the microexe mode are most likely exe size benchmarks. In the case they are not
  104. they are likely so primitive that it is unlikely that they require a stackdump to debug.
  105. dump_stack_micro(pointer(ucontext^.uc_mcontext.eip));}
  106. {Write runtime error message.}
  107. int_str(exitcode,s); {int_str instead of str pulls in less code}
  108. write_micro('Runtime error '+s+' at $'+
  109. hexstr(longint(addr),8)+ {typecast to longint to prevent pulling in int64 support}
  110. lineending);
  111. haltproc(exitcode);
  112. end;
  113. procedure SignalToRunerror(Sig: longint; Info : pointer; var SigContext: TSigContext); public name '_FPC_DEFAULTSIGHANDLER'; cdecl;
  114. var
  115. res : word;
  116. begin
  117. res:=signr_to_runerrornr(sig,SigContext);
  118. reenable_signal(sig);
  119. { give runtime error at the position where the signal was raised }
  120. if res<>0 then
  121. begin
  122. { HandleErrorAddrFrame(res,SigContext.sc_pc,SigContext.sc_fp);}
  123. { fp is not saved in context record :( }
  124. HandleError(res);
  125. HandleError(res);
  126. end;
  127. end;