home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / except.zip / except.c
Text File  |  1994-01-24  |  5KB  |  97 lines

  1. #define INCL_DOSEXCEPTIONS
  2. #define INCL_DOSFILEMGR
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7.  
  8. /****************************************************************************/
  9. /* This is a simple example on how to register and use an OS/2 exception    */
  10. /* handler.  The main routine registers the handler and forces a protection */
  11. /* exception by attempting to store at address 0.                           */
  12. /*                                                                          */
  13. /* The exception handler gets control and checks for an addressing          */
  14. /* exception.  This is important because the exception handler will also get*/
  15. /* control for other exceptions, i.e. when a page needs to be swapped in    */
  16. /* from the swap file.  If another exception has been found, the exception  */
  17. /* handler returns with XCPT_CONTINUE_SEARCH, which tells OS/2 to pass the  */
  18. /* exception on to the next handler in the list.
  19. /*                                                                          */
  20. /* The exception handler opens a log file.  If this fails, the file pointer */
  21. /* is redirected to stderr.  The registers are then written to the log file */
  22. /* and the process is terminated with DosExit.                              */
  23. /*                                                                          */
  24. /* This is not meant to be production level code, but since several people  */
  25. /* have asked for a simple example. I came up with this.                    */
  26. /*                                                                          */
  27. /* Jerry Stuckle, JDS & Associates [70363,407]                              */
  28. /*                                                                          */
  29. /****************************************************************************/
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. ULONG _System ExceptionHandler (PEXCEPTIONREPORTRECORD pERepRec,
  38.                       PEXCEPTIONREGISTRATIONRECORD pERegRec,
  39.                       PCONTEXTRECORD pCtxRec,
  40.                       PVOID p);
  41. int main(void);
  42.  
  43. int main(void)
  44. {
  45.    EXCEPTIONREGISTRATIONRECORD er = { NULL, ExceptionHandler }; /* To register the handler */
  46.    PCHAR pc = NULL;                                   /* Used to create an addressing exception */
  47.    
  48.    DosSetExceptionHandler (&er);                      /* Add our routine to the chain */
  49.    *pc = '\0';                                        /* This should cause an error */
  50.    return 0;
  51. }
  52.  
  53. ULONG _System ExceptionHandler (PEXCEPTIONREPORTRECORD pERepRec, /* Main exception handler */
  54.                       PEXCEPTIONREGISTRATIONRECORD pERegRec,
  55.                       PCONTEXTRECORD pCtxRec, PVOID p)
  56. {
  57.    FILE * fp;
  58.  
  59.    if (pERepRec->ExceptionNum == XCPT_ACCESS_VIOLATION)     /* If this is the exception we want */
  60.    {
  61.       if ((fp = fopen ("except.log", "a+")) == 0)           /* Open the log file                */
  62.       {
  63.          fprintf (stderr, "Return code %ud from fopen for log file\n", errno);
  64.          fp = stderr;                                       /* On error, reset log to stderr */
  65.       }
  66.  
  67. /* The following fprintf statements log the error message to the file */
  68.       fprintf (fp, "Access violation occurred at location 0X%8.8X\r\nRegisters in hex:\n",
  69.                pERepRec -> ExceptionAddress);
  70.       fprintf (fp, "EAX = %8.8X, EBX = %8.8X, ECX = %8.8X, EDX = %8.8X\r\n",
  71.                pCtxRec->ctx_RegEax, pCtxRec->ctx_RegEbx,
  72.                pCtxRec->ctx_RegEcx, pCtxRec->ctx_RegEdx);
  73.       fprintf (fp, "ESI = %8.8X, EDI = %8.8X\n\n",
  74.                pCtxRec->ctx_RegEsi, pCtxRec->ctx_RegEdi);
  75.       if (pCtxRec->ContextFlags & CONTEXT_CONTROL)             /* If control registers are listed */
  76.       {
  77.          fprintf (fp, "Current instruction pointer:\nCS = %8.8X, EIP = %8.8X Flags = %8.8X\r\n\n",
  78.                   pCtxRec->ctx_SegCs, pCtxRec->ctx_RegEip, pCtxRec->ctx_EFlags);
  79.          fprintf (fp, "Current stack: SS = %8.8X, ESP = %8.8X, EBP = %8.8X\r\n",
  80.                   pCtxRec->ctx_SegSs, pCtxRec->ctx_RegEsp, pCtxRec->ctx_RegEbp);
  81.       }
  82.       else
  83.          fprintf (fp, "Control registers not available\n\n");
  84.       if (pCtxRec->ContextFlags & CONTEXT_SEGMENTS)            /* If segment registers are listed */
  85.          fprintf (fp, "DS = %8.8X, ES = %8.8X, FS = %8.8X, GS = %8.8X\r\n\n\n",
  86.                   pCtxRec->ctx_SegDs, pCtxRec->ctx_SegEs,
  87.                   pCtxRec->ctx_SegFs, pCtxRec->ctx_SegGs);
  88.       else
  89.          fprintf (fp, "Segment registers not available\n\n\n");
  90.          
  91.       if (fp != stdout)                      /* If pointing to stdout         */
  92.          fclose (fp);                        /* Otherwise close the log file  */
  93.       DosExit (EXIT_PROCESS, (ULONG) -1);    /* Terminate the process         */
  94.    }
  95.    return XCPT_CONTINUE_SEARCH;
  96. }
  97.