home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / WIN_NT / MAPCON.ZIP / MAPDEBUG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-03  |  5.8 KB  |  214 lines

  1. // ---------------------------------------------------------------------
  2.  
  3. // MODULE:      MAPDEBUG
  4.  
  5. // PURPOSE:     Contains the debug portions of MAPCON
  6.  
  7.  
  8. // ---------------------------------------------------------------------
  9.  
  10. #include <Windows.H>
  11. #include <Malloc.H>
  12. #include <StdIO.H>
  13. #include <StdLib.H>
  14. #include <WinCon.H>
  15. #include <String.H>
  16. #include "MAPDEBUG.H"
  17. #include "MAPCON.H"
  18.  
  19.     PDEBUGMAP        pDebugMap;
  20.     UINT            iNumMaps;
  21.  
  22.  
  23. // ---------------------------------------------------------------------
  24.  
  25. // FUNCTION:    OpenDebug
  26.  
  27. // PURPOSE:     Load the debug map file
  28.  
  29. // COMMENTS:    Return FALSE if error
  30.  
  31. // ---------------------------------------------------------------------
  32.  
  33.  
  34. BOOL OpenDebug( VOID )
  35. {
  36. HANDLE        hMap;
  37. DWORD        dwRead;
  38. LPVOID        lpMap;
  39. DWORD        MapSize;
  40.  
  41.     hMap = CreateFile( "MAPCON.COD", GENERIC_READ, FILE_SHARE_READ, 0L,
  42.                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  43.     if ( hMap == (HANDLE) -1 )
  44.         return FALSE;
  45.     ReadFile( hMap, &iNumMaps, sizeof( UINT ), &dwRead, NULL );
  46.     if ( !iNumMaps )
  47.         {
  48.          CloseHandle( hMap );
  49.          return FALSE;
  50.         }
  51.     MapSize = iNumMaps * sizeof( DEBUGMAP );
  52.     lpMap = VirtualAlloc( NULL, MapSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
  53.     if ( !lpMap )
  54.         {
  55.          iNumMaps = 0;
  56.          CloseHandle( hMap );
  57.          return FALSE;
  58.         }
  59.  
  60.     ReadFile( hMap, lpMap, MapSize, &dwRead, NULL );
  61.  CloseHandle( hMap );
  62.     if ( dwRead != MapSize )
  63.          iNumMaps = 0;
  64.     pDebugMap = (PDEBUGMAP)lpMap;
  65.  
  66.     return TRUE;
  67. }
  68.  
  69.  
  70. // ---------------------------------------------------------------------
  71.  
  72. // FUNCTION:    HandleException
  73.  
  74. // FUNCTION:        An exception has occurred
  75.  
  76.  
  77. // PURPOSE:     This function is an exception filter that handles exceptions
  78. //              that occurr while executing.
  79.  
  80. // ARGUMENTS:
  81. //              ExceptionCode - Reason for the exception.
  82. //              ExceptionInfo - Information about the exception and the
  83. //                              context that it occurred in.
  84.  
  85. // RETURNS:     Exception disposition code that tells the exception dispatcher
  86. //              what to do with this exception. One of three values is returned:
  87.  
  88. //              EXCEPTION_EXECUTE_HANDLER - execute the exception handler
  89. //              associated with the exception clause that called this filter
  90. //              procedure.
  91.  
  92. //              EXCEPTION_CONTINUE_SEARCH - Continue searching for an exception
  93. //              handler to handle this exception.
  94.  
  95. //              EXCEPTION_CONTINUE_EXECUTION - Dismiss this exception and
  96. //              return control to the instruction that caused the exception.
  97.  
  98. // COMMENTS:
  99. //              We always go to the EXECUTE_HANDLER clause, returning
  100. //              to clean up and exit.
  101.  
  102. // ---------------------------------------------------------------------
  103.  
  104. int HandleException( DWORD ExceptionCode, PEXCEPTION_RECORD ExceptionInfo )
  105. {
  106.     UINT            FaultAddress[EXCEPTION_MAXIMUM_PARAMETERS+1];
  107.     DWORD            dwIx;
  108.     char            szMsg[256];
  109.     char            szWhere[128];
  110.     PDEBUGMAP        pDebugCurr;
  111.     UINT            iDebugIdx;
  112.  
  113.     FaultAddress[0] = (UINT)ExceptionInfo->ExceptionAddress;
  114.     for ( dwIx = 0;    dwIx < ExceptionInfo->NumberParameters;    dwIx++ )
  115.         FaultAddress[dwIx+1] = (UINT)(ExceptionInfo->ExceptionInformation[dwIx]);
  116.  
  117.     switch ( ExceptionCode )
  118.         {
  119.          case EXCEPTION_ACCESS_VIOLATION:
  120.                 sprintf( szMsg, "Access error. Ref to locn 0x%08X",    FaultAddress[2]);
  121.                 break;
  122.          case EXCEPTION_DATATYPE_MISALIGNMENT:
  123.                 strcpy( szMsg, "Data type misalignment" );
  124.                 break;
  125.          case EXCEPTION_BREAKPOINT:
  126.                 strcpy( szMsg, "Breakpoint instruction" );
  127.                 break;
  128.          case EXCEPTION_SINGLE_STEP:
  129.                 strcpy( szMsg, "Single step breakpoint" );
  130.                 break;
  131.          case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  132.                 strcpy( szMsg, "Array bounds exceeded" );
  133.                 break;
  134.          case EXCEPTION_FLT_DENORMAL_OPERAND:
  135.                 strcpy( szMsg, "FLOAT: Denormalized operand" );
  136.                 break;
  137.          case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  138.                 strcpy( szMsg, "FLOAT: Divide by zero" );
  139.                 break;
  140.          case EXCEPTION_FLT_INEXACT_RESULT:
  141.                 strcpy( szMsg, "FLOAT: Inexact result" );
  142.                 break;
  143.          case EXCEPTION_FLT_INVALID_OPERATION:
  144.                 strcpy( szMsg, "FLOAT: Invalid operation" );
  145.                 break;
  146.          case EXCEPTION_FLT_OVERFLOW:
  147.                 strcpy( szMsg, "FLOAT: Result overflow" );
  148.                 break;
  149.          case EXCEPTION_FLT_STACK_CHECK:
  150.                 strcpy( szMsg, "FLOAT: FP Stack check" );
  151.                 break;
  152.          case EXCEPTION_FLT_UNDERFLOW:
  153.                 strcpy( szMsg, "FLOAT: Result underflow" );
  154.                 break;
  155.          case EXCEPTION_INT_DIVIDE_BY_ZERO:
  156.                 strcpy( szMsg, "Integer divide by zero" );
  157.                 break;
  158.          case EXCEPTION_INT_OVERFLOW:
  159.                 strcpy( szMsg, "Integer result overflow" );
  160.                 break;
  161.          case EXCEPTION_PRIV_INSTRUCTION:
  162.                 strcpy( szMsg, "Privileged instruction" );
  163.                 break;
  164.          case CONTROL_C_EXIT:
  165.                 strcpy( szMsg, "Control-C interrupt" );
  166.                 break;
  167.          default:
  168.                 sprintf(szMsg,"Unknown code : 0x%08X", ExceptionCode );
  169.         }
  170.     pDebugCurr = pDebugMap;
  171.     for ( iDebugIdx = 0; iDebugIdx < iNumMaps; iDebugIdx++ )
  172.         {
  173.          if ( FaultAddress[0] < pDebugCurr->SymbAddr )
  174.             break;
  175.          pDebugCurr++;
  176.         }
  177.     if ( ( iDebugIdx < iNumMaps ) && ( iDebugIdx > 0 ) )
  178.         {
  179.          pDebugCurr--;
  180.          sprintf( szWhere, "%s(%s)+0x%X", pDebugCurr->Routine, pDebugCurr->Module,
  181.                                                 FaultAddress[0] - pDebugCurr->SymbAddr );
  182.         }
  183.     else
  184.         sprintf( szWhere, "0x%08X", FaultAddress[0] );
  185.     printf( "EXCEPTION : %s at %s.", szMsg, szWhere );
  186.  
  187.     return EXCEPTION_EXECUTE_HANDLER;
  188.  
  189. }
  190.  
  191.  
  192. // ---------------------------------------------------------------------
  193.  
  194. // FUNCTION:    CloseDebug
  195.  
  196. // PURPOSE:     Free the debug map entries
  197.  
  198. // COMMENTS:    Return FALSE if error
  199.  
  200. // ---------------------------------------------------------------------
  201.  
  202.  
  203. BOOL CloseDebug( VOID )
  204. {
  205.     if ( !pDebugMap )
  206.         return FALSE;
  207.  
  208.     VirtualFree( pDebugMap, 0, MEM_DECOMMIT );
  209.     VirtualFree( pDebugMap, 0, MEM_RELEASE );
  210.  
  211.     return TRUE;
  212.  
  213. }
  214.