home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / memsz331.zip / Source.zip / EXCEPT.CPP < prev    next >
Text File  |  1995-02-02  |  15KB  |  419 lines

  1. /**************************************************************** EXCEPT.CPP
  2.  *                                                                         *
  3.  *                            Exception Handler                            *
  4.  *                                                                         *
  5.  ***************************************************************************/
  6.  
  7. #include <builtin.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define INCL_BASE
  13. #define INCL_PM
  14. #include <os2.h>
  15.  
  16. #include "debug.h"
  17. #include "restring.h"
  18. #include "memsize.h"
  19.  
  20. #include "except.h"
  21.  
  22.  
  23. /***************************************************************************
  24.  *                                                                         *
  25.  *  Exception Handler                                                      *
  26.  *                                                                         *
  27.  ***************************************************************************/
  28.  
  29. extern ULONG APIENTRY ExceptionHandler (
  30.   PEXCEPTIONREPORTRECORD pExceptionReportRecord,
  31.   PEXCEPTIONREGISTRATIONRECORD pExceptionRegistrationRecord,
  32.   PCONTEXTRECORD pContextRecord,
  33.   PVOID pDispatcherContext
  34. ) {
  35.    static BOOL Active = FALSE ;
  36.    static char Message [500] ;
  37.    static PLONG Base ;
  38.  
  39.    static PTIB2 pTIB2 = PTIB2 ( _getTIBvalue ( offsetof(TIB,tib_ptib2) ) ) ;
  40.    static ULONG ThreadID = pTIB2->tib2_ultid ;
  41.  
  42.    if ( pExceptionReportRecord->ExceptionNum == XCPT_ACCESS_VIOLATION ) {
  43.       if ( Active ) {
  44.          Log ( "ABORT: Recursion with exception handler." ) ;
  45.          DosExit ( EXIT_PROCESS, 0 ) ;
  46.       } /* endif */
  47.       Active = TRUE ;
  48.       Log
  49.       (
  50.         "ABORT: %s%s%s%s%s%sviolation trying to access address/selector %08lX.\n"
  51.         "  Thread %i.\n"
  52.         "  The registers were as follows:\n"
  53.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  54.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  55.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  56.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  57.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_READ_ACCESS ) ? "Read " : "",
  58.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_WRITE_ACCESS ) ? "Write " : "",
  59.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_EXECUTE_ACCESS ) ? "Execute " : "",
  60.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_SPACE_ACCESS ) ? "Space " : "",
  61.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_LIMIT_ACCESS ) ? "Limit " : "",
  62.         ( pExceptionReportRecord->ExceptionInfo[0] & XCPT_UNKNOWN_ACCESS ) ? "Unknown " : "",
  63.         pExceptionReportRecord->ExceptionInfo[1],
  64.         ThreadID,
  65.         pContextRecord->ctx_RegEax,
  66.         pContextRecord->ctx_RegEbx,
  67.         pContextRecord->ctx_RegEcx,
  68.         pContextRecord->ctx_RegEdx,
  69.         pContextRecord->ctx_EFlags,
  70.         pContextRecord->ctx_RegEdi,
  71.         pContextRecord->ctx_RegEsi,
  72.         pContextRecord->ctx_RegEbp,
  73.         pContextRecord->ctx_SegCs,
  74.         pContextRecord->ctx_RegEip,
  75.         pContextRecord->ctx_SegSs,
  76.         pContextRecord->ctx_RegEsp,
  77.         pContextRecord->ctx_SegDs,
  78.         pContextRecord->ctx_SegEs,
  79.         pContextRecord->ctx_SegFs,
  80.         pContextRecord->ctx_SegGs
  81.       ) ;
  82.  
  83.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  84.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  85.  
  86.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  87.       while ( Base && ( *Base > (LONG)Base ) ) {
  88.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  89.         Base = (PLONG)*Base ;
  90.       } /* endwhile */
  91.  
  92.       DosExit ( EXIT_PROCESS, 0 ) ;
  93.    }
  94.  
  95.    if ( pExceptionReportRecord->ExceptionNum == XCPT_PRIVILEGED_INSTRUCTION ) {
  96.       if ( Active ) {
  97.          Log ( "ABORT: Recursion with exception handler." ) ;
  98.          DosExit ( EXIT_PROCESS, 0 ) ;
  99.       } /* endif */
  100.       Active = TRUE ;
  101.       Log
  102.       (
  103.         "ABORT: A privileged instruction was encountered.\n"
  104.         "  Thread %i.\n"
  105.         "  The registers were as follows:\n"
  106.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  107.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  108.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  109.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  110.         ThreadID,
  111.         pContextRecord->ctx_RegEax,
  112.         pContextRecord->ctx_RegEbx,
  113.         pContextRecord->ctx_RegEcx,
  114.         pContextRecord->ctx_RegEdx,
  115.         pContextRecord->ctx_EFlags,
  116.         pContextRecord->ctx_RegEdi,
  117.         pContextRecord->ctx_RegEsi,
  118.         pContextRecord->ctx_RegEbp,
  119.         pContextRecord->ctx_SegCs,
  120.         pContextRecord->ctx_RegEip,
  121.         pContextRecord->ctx_SegSs,
  122.         pContextRecord->ctx_RegEsp,
  123.         pContextRecord->ctx_SegDs,
  124.         pContextRecord->ctx_SegEs,
  125.         pContextRecord->ctx_SegFs,
  126.         pContextRecord->ctx_SegGs
  127.       ) ;
  128.  
  129.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  130.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  131.  
  132.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  133.       while ( Base && ( *Base > (LONG)Base ) ) {
  134.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  135.         Base = (PLONG)*Base ;
  136.       } /* endwhile */
  137.  
  138.       DosExit ( EXIT_PROCESS, 0 ) ;
  139.    }
  140.  
  141.    if ( pExceptionReportRecord->ExceptionNum == XCPT_ILLEGAL_INSTRUCTION ) {
  142.       if ( Active ) {
  143.          Log ( "ABORT: Recursion with exception handler." ) ;
  144.          DosExit ( EXIT_PROCESS, 0 ) ;
  145.       } /* endif */
  146.       Active = TRUE ;
  147.       Log
  148.       (
  149.         "ABORT: An illegal instruction was encountered.\n"
  150.         "  Thread %i.\n"
  151.         "  The registers were as follows:\n"
  152.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  153.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  154.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  155.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  156.         ThreadID,
  157.         pContextRecord->ctx_RegEax,
  158.         pContextRecord->ctx_RegEbx,
  159.         pContextRecord->ctx_RegEcx,
  160.         pContextRecord->ctx_RegEdx,
  161.         pContextRecord->ctx_EFlags,
  162.         pContextRecord->ctx_RegEdi,
  163.         pContextRecord->ctx_RegEsi,
  164.         pContextRecord->ctx_RegEbp,
  165.         pContextRecord->ctx_SegCs,
  166.         pContextRecord->ctx_RegEip,
  167.         pContextRecord->ctx_SegSs,
  168.         pContextRecord->ctx_RegEsp,
  169.         pContextRecord->ctx_SegDs,
  170.         pContextRecord->ctx_SegEs,
  171.         pContextRecord->ctx_SegFs,
  172.         pContextRecord->ctx_SegGs
  173.       ) ;
  174.  
  175.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  176.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  177.  
  178.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  179.       while ( Base && ( *Base > (LONG)Base ) ) {
  180.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  181.         Base = (PLONG)*Base ;
  182.       } /* endwhile */
  183.  
  184.       DosExit ( EXIT_PROCESS, 0 ) ;
  185.    }
  186.  
  187.    if ( pExceptionReportRecord->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO ) {
  188.       if ( Active ) {
  189.          Log ( "ABORT: Recursion with exception handler." ) ;
  190.          DosExit ( EXIT_PROCESS, 0 ) ;
  191.       } /* endif */
  192.       Active = TRUE ;
  193.       Log
  194.       (
  195.         "ABORT: An integer divide-by-zero error has occurred.\n"
  196.         "  Thread %i.\n"
  197.         "  The registers were as follows:\n"
  198.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  199.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  200.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  201.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  202.         ThreadID,
  203.         pContextRecord->ctx_RegEax,
  204.         pContextRecord->ctx_RegEbx,
  205.         pContextRecord->ctx_RegEcx,
  206.         pContextRecord->ctx_RegEdx,
  207.         pContextRecord->ctx_EFlags,
  208.         pContextRecord->ctx_RegEdi,
  209.         pContextRecord->ctx_RegEsi,
  210.         pContextRecord->ctx_RegEbp,
  211.         pContextRecord->ctx_SegCs,
  212.         pContextRecord->ctx_RegEip,
  213.         pContextRecord->ctx_SegSs,
  214.         pContextRecord->ctx_RegEsp,
  215.         pContextRecord->ctx_SegDs,
  216.         pContextRecord->ctx_SegEs,
  217.         pContextRecord->ctx_SegFs,
  218.         pContextRecord->ctx_SegGs
  219.       ) ;
  220.  
  221.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  222.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  223.  
  224.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  225.       while ( Base && ( *Base > (LONG)Base ) ) {
  226.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  227.         Base = (PLONG)*Base ;
  228.       } /* endwhile */
  229.  
  230.       DosExit ( EXIT_PROCESS, 0 ) ;
  231.    }
  232.  
  233.    if ( pExceptionReportRecord->ExceptionNum == XCPT_INTEGER_OVERFLOW ) {
  234.       if ( Active ) {
  235.          Log ( "ABORT: Recursion with exception handler." ) ;
  236.          DosExit ( EXIT_PROCESS, 0 ) ;
  237.       } /* endif */
  238.       Active = TRUE ;
  239.       Log
  240.       (
  241.         "ABORT: An integer overflow has occurred.\n"
  242.         "  Thread %i.\n"
  243.         "  The registers were as follows:\n"
  244.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  245.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  246.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  247.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  248.         ThreadID,
  249.         pContextRecord->ctx_RegEax,
  250.         pContextRecord->ctx_RegEbx,
  251.         pContextRecord->ctx_RegEcx,
  252.         pContextRecord->ctx_RegEdx,
  253.         pContextRecord->ctx_EFlags,
  254.         pContextRecord->ctx_RegEdi,
  255.         pContextRecord->ctx_RegEsi,
  256.         pContextRecord->ctx_RegEbp,
  257.         pContextRecord->ctx_SegCs,
  258.         pContextRecord->ctx_RegEip,
  259.         pContextRecord->ctx_SegSs,
  260.         pContextRecord->ctx_RegEsp,
  261.         pContextRecord->ctx_SegDs,
  262.         pContextRecord->ctx_SegEs,
  263.         pContextRecord->ctx_SegFs,
  264.         pContextRecord->ctx_SegGs
  265.       ) ;
  266.  
  267.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  268.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  269.  
  270.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  271.       while ( Base && ( *Base > (LONG)Base ) ) {
  272.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  273.         Base = (PLONG)*Base ;
  274.       } /* endwhile */
  275.  
  276.       DosExit ( EXIT_PROCESS, 0 ) ;
  277.    }
  278.  
  279.    if ( pExceptionReportRecord->ExceptionNum == XCPT_FLOAT_DIVIDE_BY_ZERO ) {
  280.       if ( Active ) {
  281.          Log ( "ABORT: Recursion with exception handler." ) ;
  282.          DosExit ( EXIT_PROCESS, 0 ) ;
  283.       } /* endif */
  284.       Active = TRUE ;
  285.       Log
  286.       (
  287.         "ABORT: A floating point divide by zero error has occurred.\n"
  288.         "  Thread %i.\n"
  289.         "  The registers were as follows:\n"
  290.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  291.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  292.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  293.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  294.         ThreadID,
  295.         pContextRecord->ctx_RegEax,
  296.         pContextRecord->ctx_RegEbx,
  297.         pContextRecord->ctx_RegEcx,
  298.         pContextRecord->ctx_RegEdx,
  299.         pContextRecord->ctx_EFlags,
  300.         pContextRecord->ctx_RegEdi,
  301.         pContextRecord->ctx_RegEsi,
  302.         pContextRecord->ctx_RegEbp,
  303.         pContextRecord->ctx_SegCs,
  304.         pContextRecord->ctx_RegEip,
  305.         pContextRecord->ctx_SegSs,
  306.         pContextRecord->ctx_RegEsp,
  307.         pContextRecord->ctx_SegDs,
  308.         pContextRecord->ctx_SegEs,
  309.         pContextRecord->ctx_SegFs,
  310.         pContextRecord->ctx_SegGs
  311.       ) ;
  312.  
  313.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  314.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  315.  
  316.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  317.       while ( Base && ( *Base > (LONG)Base ) ) {
  318.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  319.         Base = (PLONG)*Base ;
  320.       } /* endwhile */
  321.  
  322.       DosExit ( EXIT_PROCESS, 0 ) ;
  323.    }
  324.  
  325.    if ( pExceptionReportRecord->ExceptionNum == XCPT_FLOAT_OVERFLOW ) {
  326.       if ( Active ) {
  327.          Log ( "ABORT: Recursion with exception handler." ) ;
  328.          DosExit ( EXIT_PROCESS, 0 ) ;
  329.       } /* endif */
  330.       Active = TRUE ;
  331.       Log
  332.       (
  333.         "ABORT: A floating point overflow error has occurred.\n"
  334.         "  Thread %i.\n"
  335.         "  The registers were as follows:\n"
  336.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  337.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  338.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  339.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  340.         ThreadID,
  341.         pContextRecord->ctx_RegEax,
  342.         pContextRecord->ctx_RegEbx,
  343.         pContextRecord->ctx_RegEcx,
  344.         pContextRecord->ctx_RegEdx,
  345.         pContextRecord->ctx_EFlags,
  346.         pContextRecord->ctx_RegEdi,
  347.         pContextRecord->ctx_RegEsi,
  348.         pContextRecord->ctx_RegEbp,
  349.         pContextRecord->ctx_SegCs,
  350.         pContextRecord->ctx_RegEip,
  351.         pContextRecord->ctx_SegSs,
  352.         pContextRecord->ctx_RegEsp,
  353.         pContextRecord->ctx_SegDs,
  354.         pContextRecord->ctx_SegEs,
  355.         pContextRecord->ctx_SegFs,
  356.         pContextRecord->ctx_SegGs
  357.       ) ;
  358.  
  359.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  360.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  361.  
  362.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  363.       while ( Base && ( *Base > (LONG)Base ) ) {
  364.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  365.         Base = (PLONG)*Base ;
  366.       } /* endwhile */
  367.  
  368.       DosExit ( EXIT_PROCESS, 0 ) ;
  369.    }
  370.  
  371.    if ( pExceptionReportRecord->ExceptionNum == XCPT_FLOAT_UNDERFLOW ) {
  372.       if ( Active ) {
  373.          Log ( "ABORT: Recursion with exception handler." ) ;
  374.          DosExit ( EXIT_PROCESS, 0 ) ;
  375.       } /* endif */
  376.       Active = TRUE ;
  377.       Log
  378.       (
  379.         "ABORT: A floating point underflow error has occurred.\n"
  380.         "  Thread %i.\n"
  381.         "  The registers were as follows:\n"
  382.         "    AX:%08lX  BX:%08lX  CX:%08lX  DX:%08lX\n"
  383.         "    FL:%08lX  DI:%08lX  SI:%08lX  BP:%08lX\n"
  384.         "    CS:%08lX  IP:%08lX  SS:%08lX  SP:%08lX\n"
  385.         "    DS:%08lX  ES:%08lX  FS:%08lX  GS:%08lX",
  386.         ThreadID,
  387.         pContextRecord->ctx_RegEax,
  388.         pContextRecord->ctx_RegEbx,
  389.         pContextRecord->ctx_RegEcx,
  390.         pContextRecord->ctx_RegEdx,
  391.         pContextRecord->ctx_EFlags,
  392.         pContextRecord->ctx_RegEdi,
  393.         pContextRecord->ctx_RegEsi,
  394.         pContextRecord->ctx_RegEbp,
  395.         pContextRecord->ctx_SegCs,
  396.         pContextRecord->ctx_RegEip,
  397.         pContextRecord->ctx_SegSs,
  398.         pContextRecord->ctx_RegEsp,
  399.         pContextRecord->ctx_SegDs,
  400.         pContextRecord->ctx_SegEs,
  401.         pContextRecord->ctx_SegFs,
  402.         pContextRecord->ctx_SegGs
  403.       ) ;
  404.  
  405.       ResourceString Exception ( LibraryHandle, IDS_EXCEPTION ) ;
  406.       Debug ( HWND_DESKTOP, PCHAR(Exception), _fullpath(PCHAR(NULL),LOGFILE,0) ) ;
  407.  
  408.       Base = (PLONG)pContextRecord->ctx_RegEbp ;
  409.       while ( Base && ( *Base > (LONG)Base ) ) {
  410.         Log ( "  Calling function was at %08lX.", *(Base+1) ) ;
  411.         Base = (PLONG)*Base ;
  412.       } /* endwhile */
  413.  
  414.       DosExit ( EXIT_PROCESS, 0 ) ;
  415.    }
  416.  
  417.    return ( XCPT_CONTINUE_SEARCH ) ;
  418. }
  419.