home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / XSRVXCPT.C < prev    next >
Text File  |  1995-08-16  |  17KB  |  334 lines

  1. #include "all.h"
  2.  
  3. ULONG exception_nos[MAXEXCEPTIONS] =
  4. {
  5.  XCPT_GUARD_PAGE_VIOLATION,
  6.  XCPT_UNABLE_TO_GROW_STACK,
  7.  XCPT_DATATYPE_MISALIGNMENT,
  8.  XCPT_ACCESS_VIOLATION,
  9.  XCPT_ILLEGAL_INSTRUCTION,
  10.  XCPT_FLOAT_DENORMAL_OPERAND,
  11.  XCPT_FLOAT_DIVIDE_BY_ZERO,
  12.  XCPT_FLOAT_INEXACT_RESULT,
  13.  XCPT_FLOAT_INVALID_OPERATION,
  14.  XCPT_FLOAT_OVERFLOW,
  15.  XCPT_FLOAT_STACK_CHECK,
  16.  XCPT_FLOAT_UNDERFLOW,
  17.  XCPT_INTEGER_DIVIDE_BY_ZERO,
  18.  XCPT_INTEGER_OVERFLOW,
  19.  XCPT_PRIVILEGED_INSTRUCTION,
  20.  XCPT_IN_PAGE_ERROR,
  21.  XCPT_PROCESS_TERMINATE,
  22.  XCPT_ASYNC_PROCESS_TERMINATE,
  23.  XCPT_NONCONTINUABLE_EXCEPTION,
  24.  XCPT_INVALID_DISPOSITION,
  25.  XCPT_INVALID_LOCK_SEQUENCE,
  26.  XCPT_ARRAY_BOUNDS_EXCEEDED,
  27.  XCPT_UNWIND,
  28.  XCPT_BAD_STACK,
  29.  XCPT_INVALID_UNWIND_TARGET,
  30.  XCPT_SIGNAL,
  31.  XCPT_PROGRAM
  32. };
  33.  
  34. UCHAR ExceptionMap[MAXEXCEPTIONS] =
  35. {
  36.  NO_NOTIFY,     /* 0  XCPT_GUARD_PAGE_VIOLATION      */
  37.  NOTIFY,        /* 1  XCPT_UNABLE_TO_GROW_STACK      */
  38.  NOTIFY,        /* 2  XCPT_DATATYPE_MISALIGNMENT     */
  39.  NOTIFY,        /* 3  XCPT_ACCESS_VIOLATION          */
  40.  NOTIFY,        /* 4  XCPT_ILLEGAL_INSTRUCTION       */
  41.  NOTIFY,        /* 5  XCPT_FLOAT_DENORMAL_OPERAND    */
  42.  NOTIFY,        /* 6  XCPT_FLOAT_DIVIDE_BY_ZERO      */
  43.  NOTIFY,        /* 7  XCPT_FLOAT_INEXACT_RESULT      */
  44.  NOTIFY,        /* 8  XCPT_FLOAT_INVALID_OPERATION   */
  45.  NOTIFY,        /* 9  XCPT_FLOAT_OVERFLOW            */
  46.  NOTIFY,        /*10  XCPT_FLOAT_STACK_CHECK         */
  47.  NOTIFY,        /*11  XCPT_FLOAT_UNDERFLOW           */
  48.  NOTIFY,        /*12  XCPT_INTEGER_DIVIDE_BY_ZERO    */
  49.  NOTIFY,        /*13  XCPT_INTEGER_OVERFLOW          */
  50.  NOTIFY,        /*14  XCPT_PRIVILEGED_INSTRUCTION    */
  51.  NOTIFY,        /*15  XCPT_IN_PAGE_ERROR             */
  52.  NO_NOTIFY,     /*16  XCPT_PROCESS_TERMINATE         */
  53.  NO_NOTIFY,     /*17  XCPT_ASYNC_PROCESS_TERMINATE   */
  54.  NOTIFY,        /*18  XCPT_NONCONTINUABLE_EXCEPTION  */
  55.  NOTIFY,        /*19  XCPT_INVALID_DISPOSITION       */
  56.  NOTIFY,        /*20  XCPT_INVALID_LOCK_SEQUENCE     */
  57.  NOTIFY,        /*21  XCPT_ARRAY_BOUNDS_EXCEEDED     */
  58.  NO_NOTIFY,     /*22  XCPT_UNWIND                    */
  59.  NOTIFY,        /*23  XCPT_BAD_STACK                 */
  60.  NOTIFY,        /*24  XCPT_INVALID_UNWIND_TARGET     */
  61.  NOTIFY,        /*25  XCPT_SIGNAL                    */
  62.  NO_NOTIFY      /*26  XCPT_PROGRAM                   */
  63. };
  64.  
  65. char *ExcepTypes[MAXEXCEPTIONS] =       /*                                   */
  66. {                                       /*                                   */
  67.  "GuardPageViolation",                  /* 0                                 */
  68.  "UnableToGrowStack",                   /* 1                                 */
  69.  "DataTypeMisAlignment",                /* 2                                 */
  70.  "AccessViolation",                     /* 3                                 */
  71.  "IllegalInstruction",                  /* 4                                 */
  72.  "FloatingDenormalOperand",             /* 5                                 */
  73.  "FloatingDivideByZero",                /* 6                                 */
  74.  "FloatingInExactResult",               /* 7                                 */
  75.  "FloatingInvalidOperation",            /* 8                                 */
  76.  "FloatingOverflow",                    /* 9                                 */
  77.  "FloatingStackCheck",                  /* 10                                */
  78.  "FloatingUnderflow",                   /* 11                                */
  79.  "IntegerDivideByZero",                 /* 12                                */
  80.  "IntegerOverFlow",                     /* 13                                */
  81.  "PrivilegedInstruction",               /* 14                                */
  82.  "PageError",                           /* 15                                */
  83.  "ProcessTerminate",                    /* 16                                */
  84.  "AsyncProcessTerminate",               /* 17                                */
  85.  "NonContinuableException",             /* 18                                */
  86.  "InvalidDisposition",                  /* 19                                */
  87.  "InvalidLockSequence",                 /* 20                                */
  88.  "ArrayBoundsExceeded",                 /* 21                                */
  89.  "UnwindException",                     /* 22                                */
  90.  "BadStack",                            /* 23                                */
  91.  "InvalidUnwindTarget",                 /* 24                                */
  92.  "Signal",                              /* 25                                */
  93.  "DosRaiseException/c++/throw"          /* 26                                */
  94. };
  95.  
  96. char *ExcepSel[2]    = {
  97.                         "NoNotify",
  98.                         "Notify"
  99.                        };
  100.  
  101. void XSrvSetExceptions( UCHAR *pExceptionMap, int length )
  102. {
  103.  memcpy( ExceptionMap,pExceptionMap,length );
  104. }
  105.  
  106. /*****************************************************************************/
  107. /* GetExceptionMap()                                                         */
  108. /*                                                                           */
  109. /* Description:                                                              */
  110. /*                                                                           */
  111. /*   Get a notify/nonotify specification from the exception map.             */
  112. /*                                                                           */
  113. /* Parameters:                                                               */
  114. /*                                                                           */
  115. /*   ExceptionIndex  an index into the exception map.                        */
  116. /*                                                                           */
  117. /* Return:                                                                   */
  118. /*                                                                           */
  119. /*   NOTIFY/NONOTIFY                                                         */
  120. /*                                                                           */
  121. /* Assumptions:                                                              */
  122. /*                                                                           */
  123. /*   ExceptionIndex is within the bounds of the ExceptionMap array.          */
  124. /*                                                                           */
  125. /*****************************************************************************/
  126. int  GetExceptionMap( int ExceptionIndex )
  127. {
  128.  return( ExceptionMap[ExceptionIndex] );
  129. }
  130.  
  131. /*****************************************************************************/
  132. /* GetExceptionIndex()                                                       */
  133. /*                                                                           */
  134. /* Description:                                                              */
  135. /*                                                                           */
  136. /*   Get the index in the exception number map of an exception.              */
  137. /*                                                                           */
  138. /* Parameters:                                                               */
  139. /*                                                                           */
  140. /*   exception_no    the exception number.                                   */
  141. /*                                                                           */
  142. /* Return:                                                                   */
  143. /*                                                                           */
  144. /*   n               index in the exception_nos map.                         */
  145. /*                                                                           */
  146. /* Assumptions:                                                              */
  147. /*                                                                           */
  148. /*                                                                           */
  149. /*****************************************************************************/
  150. int  GetExceptionIndex( int exception_no )
  151. {
  152.  int n;
  153.  
  154.  n = lindex(exception_nos, MAXEXCEPTIONS, exception_no);
  155.  
  156.  if( n == MAXEXCEPTIONS )
  157.   n = lindex(exception_nos, MAXEXCEPTIONS, XCPT_PROGRAM);
  158.  
  159.  return( n );
  160. }
  161.  
  162. /*****************************************************************************/
  163. /* GetExceptionNumber()                                                      */
  164. /*                                                                           */
  165. /* Description:                                                              */
  166. /*                                                                           */
  167. /*   Get the number of the exception for an exception index.                 */
  168. /*                                                                           */
  169. /* Parameters:                                                               */
  170. /*                                                                           */
  171. /*   ExceptionIndex  the index of the exception.    .                        */
  172. /*                                                                           */
  173. /* Return:                                                                   */
  174. /*                                                                           */
  175. /*   The exception number.                                                   */
  176. /*                                                                           */
  177. /* Assumptions:                                                              */
  178. /*                                                                           */
  179. /*   ExceptionIndex is within the bounds of the ExceptionMap array.          */
  180. /*                                                                           */
  181. /*****************************************************************************/
  182. int  GetExceptionNumber( int ExceptionIndex )
  183. {
  184.  return( exception_nos[ExceptionIndex] );
  185. }
  186.  
  187. /*****************************************************************************/
  188. /* GetExceptionType()                                                        */
  189. /*                                                                           */
  190. /* Description:                                                              */
  191. /*                                                                           */
  192. /*   Get the string defining the exception.                                  */
  193. /*                                                                           */
  194. /* Parameters:                                                               */
  195. /*                                                                           */
  196. /*   ExceptionIndex  the index of the exception.    .                        */
  197. /*                                                                           */
  198. /* Return:                                                                   */
  199. /*                                                                           */
  200. /*   Pointer to the defining string.                                         */
  201. /*                                                                           */
  202. /* Assumptions:                                                              */
  203. /*                                                                           */
  204. /*   ExceptionIndex is within the bounds of the ExceptionTypes array.        */
  205. /*                                                                           */
  206. /*****************************************************************************/
  207. char *GetExceptionType( int ExceptionIndex )
  208. {
  209.  return( ExcepTypes[ExceptionIndex] );
  210. }
  211.  
  212. /*****************************************************************************/
  213. /* ResolveException()                                                        */
  214. /*                                                                           */
  215. /* Description:                                                              */
  216. /*                                                                           */
  217. /*   Handle exception notifications other than INT3 and Single-Step          */
  218. /*   from DosDebug.                                                          */
  219. /*                                                                           */
  220. /* Parameters:                                                               */
  221. /*                                                                           */
  222. /*   pptb          -> to the ptrace buffer.                                  */
  223. /*                                                                           */
  224. /* Return:                                                                   */
  225. /*                                                                           */
  226. /*   exception_no  the exception number from the the exception               */
  227. /*                 registration record.                                      */
  228. /*                                                                           */
  229. /* Assumptions:                                                              */
  230. /*                                                                           */
  231. /*   none                                                                    */
  232. /*                                                                           */
  233. /*****************************************************************************/
  234. ULONG ResolveException(PtraceBuffer *pptb)
  235. {
  236.  UINT                  exception_no;
  237.  UINT                  rc;
  238.  PtraceBuffer          LocalPtb;
  239.  EXCEPTIONREPORTRECORD ExceptionReportRecord;
  240.  UINT                  nbytes;
  241.  
  242.  /****************************************************************************/
  243.  /* First, get the exception number.                                         */
  244.  /****************************************************************************/
  245.  exception_no = 0;
  246.  nbytes = sizeof(EXCEPTIONREPORTRECORD);
  247.  memset(&LocalPtb,0,sizeof(LocalPtb));
  248.  
  249.  LocalPtb.Pid    = pptb->Pid;
  250.  LocalPtb.Cmd    = DBG_C_ReadMemBuf ;
  251.  LocalPtb.Addr   =  (ULONG)pptb->Buffer;
  252.  LocalPtb.Buffer = (ULONG)&ExceptionReportRecord;
  253.  LocalPtb.Len    = (ULONG)nbytes ;
  254.  rc              = DosDebug( &LocalPtb );
  255.  
  256.  if(rc || LocalPtb.Cmd!=DBG_N_Success)
  257.    return(TRAP_EXP);                    /* return exception anomaly code     */
  258.  
  259.  exception_no = ExceptionReportRecord.ExceptionNum;
  260.  
  261.  /****************************************************************************/
  262.  /* Now, read the "real" register values into AppPTB. DosDebug gives us      */
  263.  /* a bogus set of registers so we have to patch them up.                    */
  264.  /****************************************************************************/
  265.  {
  266.   #define LDTBIT 4
  267.   CONTEXTRECORD XcptContextRecord;
  268.  
  269.   nbytes = sizeof(XcptContextRecord);
  270.   memset(&LocalPtb,0,sizeof(LocalPtb));
  271.   LocalPtb.Pid = pptb->Pid;
  272.   LocalPtb.Cmd = DBG_C_ReadMemBuf ;
  273.   LocalPtb.Addr =  (ULONG)pptb->Len;
  274.   LocalPtb.Buffer = (ULONG)&XcptContextRecord;
  275.   LocalPtb.Len = (ULONG)nbytes ;
  276.   rc = DosDebug( &LocalPtb );
  277.   if(rc || LocalPtb.Cmd!=DBG_N_Success)
  278.     return(TRAP_EXP);
  279.  
  280.   if( XcptContextRecord.ContextFlags & CONTEXT_CONTROL )
  281.   {
  282.    pptb->EBP    = XcptContextRecord.ctx_RegEbp;
  283.    pptb->EIP    = XcptContextRecord.ctx_RegEip;
  284.    pptb->CS     = XcptContextRecord.ctx_SegCs;
  285.    pptb->EFlags = XcptContextRecord.ctx_EFlags;
  286.    pptb->ESP    = XcptContextRecord.ctx_RegEsp;
  287.    pptb->SS     = XcptContextRecord.ctx_SegSs;
  288.  
  289.    pptb->CSAtr = 0xd0;
  290.    if( pptb->CS & LDTBIT )
  291.     pptb->CSAtr = 0;
  292.  
  293.    pptb->SSAtr = 0xd0;
  294.    if( pptb->SS & LDTBIT )
  295.     pptb->SSAtr = 0;
  296.   }
  297.  
  298.   if( XcptContextRecord.ContextFlags & CONTEXT_INTEGER )
  299.   {
  300.    pptb->EDI = XcptContextRecord.ctx_RegEdi;
  301.    pptb->ESI = XcptContextRecord.ctx_RegEsi;
  302.    pptb->EAX = XcptContextRecord.ctx_RegEax;
  303.    pptb->EBX = XcptContextRecord.ctx_RegEbx;
  304.    pptb->ECX = XcptContextRecord.ctx_RegEcx;
  305.    pptb->EDX = XcptContextRecord.ctx_RegEdx;
  306.   }
  307.  
  308.   if( XcptContextRecord.ContextFlags & CONTEXT_SEGMENTS )
  309.   {
  310.    pptb->GS = XcptContextRecord.ctx_SegGs;
  311.    pptb->FS = XcptContextRecord.ctx_SegFs;
  312.    pptb->ES = XcptContextRecord.ctx_SegEs;
  313.    pptb->DS = XcptContextRecord.ctx_SegDs;
  314.  
  315.    pptb->GSAtr = 0xd0;
  316.    if( pptb->GS & LDTBIT )
  317.     pptb->GSAtr = 0;
  318.  
  319.    pptb->FSAtr = 0xd0;
  320.    if( pptb->FS & LDTBIT )
  321.     pptb->FSAtr = 0;
  322.  
  323.    pptb->ESAtr = 0xd0;
  324.    if( pptb->ES & LDTBIT )
  325.     pptb->ESAtr = 0;
  326.  
  327.    pptb->DSAtr = 0xd0;
  328.    if( pptb->DS & LDTBIT )
  329.     pptb->DSAtr = 0;
  330.   }
  331.  }
  332.  return(exception_no);
  333. }
  334.