home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / image / winnt / pfmon / debug.c next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  209 lines

  1. /*++
  2.  
  3. Copyright 1996 - 1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     debug.c
  8.  
  9. Abstract:
  10.  
  11.     Main debug loop for pfmon
  12.  
  13. Author:
  14.  
  15.     Mark Lucovsky (markl) 26-Jan-1995
  16.  
  17. Revision History:
  18.  
  19. --*/
  20.  
  21. #include "pfmonp.h"
  22.  
  23. DWORD
  24. DebugEventHandler(
  25.     LPDEBUG_EVENT DebugEvent
  26.     );
  27.  
  28. VOID
  29. DebugEventLoop( VOID )
  30. {
  31.     DEBUG_EVENT DebugEvent;
  32.     DWORD ContinueStatus;
  33.     DWORD OldPriority;
  34.  
  35.     //
  36.     // We want to process debug events quickly
  37.     //
  38.  
  39.     OldPriority = GetPriorityClass( GetCurrentProcess() );
  40.     SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS );
  41.  
  42.     do {
  43. retry_debug_wait:
  44.         ProcessPfMonData();
  45.         if (!WaitForDebugEvent( &DebugEvent, 500 )) {
  46.             if ( GetLastError() == ERROR_SEM_TIMEOUT ) {
  47.                 goto retry_debug_wait;
  48.                 }
  49.             DeclareError( PFMON_WAITDEBUGEVENT_FAILED, GetLastError() );
  50.             ExitProcess( 1 );
  51.             }
  52.         ProcessPfMonData();
  53.         if ( fVerbose ) {
  54.             if (DebugEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) {
  55.                 fprintf(stderr,"Debug exception event - Code: %x  Address: %x  Info: [%u] %x %x %x %x\n",
  56.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionCode,
  57.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionAddress,
  58.                         DebugEvent.u.Exception.ExceptionRecord.NumberParameters,
  59.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[ 0 ],
  60.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[ 1 ],
  61.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[ 2 ],
  62.                         DebugEvent.u.Exception.ExceptionRecord.ExceptionInformation[ 3 ]
  63.                         );
  64.                 }
  65.             else {
  66.                 fprintf(stderr,"Debug %x event\n", DebugEvent.dwDebugEventCode);
  67.                 }
  68.             }
  69.  
  70.         ContinueStatus = DebugEventHandler( &DebugEvent );
  71.  
  72.         if ( fVerbose ) {
  73.             fprintf(stderr,"Continue Status %x\n", ContinueStatus);
  74.             }
  75.  
  76.         if (!ContinueDebugEvent( DebugEvent.dwProcessId,
  77.                                  DebugEvent.dwThreadId,
  78.                                  ContinueStatus
  79.                                )
  80.            ) {
  81.             DeclareError( PFMON_CONTDEBUGEVENT_FAILED, GetLastError() );
  82.             ExitProcess( 1 );
  83.             }
  84.         }
  85.     while (!IsListEmpty( &ProcessListHead ));
  86.  
  87.  
  88.     //
  89.     // Drop back to old priority to interact with user.
  90.     //
  91.  
  92.     SetPriorityClass( GetCurrentProcess(), OldPriority );
  93. }
  94.  
  95. DWORD
  96. DebugEventHandler(
  97.     LPDEBUG_EVENT DebugEvent
  98.     )
  99. {
  100.     DWORD ContinueStatus;
  101.     PPROCESS_INFO Process;
  102.     PTHREAD_INFO Thread;
  103.     CONTEXT Context;
  104.     PCONTEXT pContext;
  105.  
  106.  
  107.     ContinueStatus = (DWORD)DBG_CONTINUE;
  108.     if (FindProcessAndThreadForEvent( DebugEvent, &Process, &Thread )) {
  109.         switch (DebugEvent->dwDebugEventCode) {
  110.             case CREATE_PROCESS_DEBUG_EVENT:
  111.                 //
  112.                 // Create process event includes first thread of process
  113.                 // as well.  Remember process and thread in our process tree
  114.                 //
  115.  
  116.                 if (AddProcess( DebugEvent, &Process )) {
  117.                     AddModule( DebugEvent );
  118.                     AddThread( DebugEvent, Process, &Thread );
  119.                     }
  120.                 break;
  121.  
  122.             case EXIT_PROCESS_DEBUG_EVENT:
  123.                 //
  124.                 // Exit process event includes last thread of process
  125.                 // as well.  Remove process and thread from our process tree
  126.                 //
  127.  
  128.                 if (DeleteThread( Process, Thread )) {
  129.                     DeleteProcess( Process );
  130.                     }
  131.                 break;
  132.  
  133.             case CREATE_THREAD_DEBUG_EVENT:
  134.                 //
  135.                 // Create thread.  Remember thread in our process tree.
  136.                 //
  137.  
  138.                 AddThread( DebugEvent, Process, &Thread );
  139.                 break;
  140.  
  141.             case EXIT_THREAD_DEBUG_EVENT:
  142.                 //
  143.                 // Exit thread.  Remove thread from our process tree.
  144.                 //
  145.  
  146.                 DeleteThread( Process, Thread );
  147.                 break;
  148.  
  149.             case LOAD_DLL_DEBUG_EVENT:
  150.                 AddModule( DebugEvent );
  151.                 break;
  152.  
  153.             case UNLOAD_DLL_DEBUG_EVENT:
  154.                 break;
  155.  
  156.             case OUTPUT_DEBUG_STRING_EVENT:
  157.             case RIP_EVENT:
  158.                 //
  159.                 // Ignore these
  160.                 //
  161.                 break;
  162.  
  163.             case EXCEPTION_DEBUG_EVENT:
  164.                 //
  165.                 // Assume we wont handle this exception
  166.                 //
  167.  
  168.                 ContinueStatus = (DWORD)DBG_CONTINUE;
  169.                 switch (DebugEvent->u.Exception.ExceptionRecord.ExceptionCode) {
  170.                     //
  171.                     // Breakpoint exception.
  172.                     //
  173.  
  174.                     case STATUS_BREAKPOINT:
  175.                             Context.ContextFlags = CONTEXT_FULL;
  176.  
  177.                             if (!GetThreadContext( Thread->Handle, &Context )) {
  178.                                 fprintf(stderr,"Failed to get context for thread %x (%x) - %u\n", Thread->Id, Thread->Handle, GetLastError());
  179.                                 ExitProcess(1);
  180.                                 }
  181.                             pContext = &Context;
  182.                             CONTEXT_TO_PROGRAM_COUNTER(pContext) = (ULONG)((PCHAR)DebugEvent->u.Exception.ExceptionRecord.ExceptionAddress + BPSKIP);
  183.  
  184.                             if (!SetThreadContext( Thread->Handle, &Context )) {
  185.                                 fprintf(stderr,"Failed to set context for thread %x (%x) - %u\n", Thread->Id, Thread->Handle, GetLastError());
  186.                                 ExitProcess(1);
  187.                                 }
  188.  
  189.                         break;
  190.  
  191.                     default:
  192.                         ContinueStatus = (DWORD) DBG_EXCEPTION_NOT_HANDLED;
  193.                         if ( fVerbose ) {
  194.                             fprintf(stderr,"Unknown exception: %08x at %08x\n",
  195.                                     DebugEvent->u.Exception.ExceptionRecord.ExceptionCode,
  196.                                     DebugEvent->u.Exception.ExceptionRecord.ExceptionAddress
  197.                                     );
  198.                             }
  199.                         break;
  200.                     }
  201.                 break;
  202.  
  203.             default:
  204.                 break;
  205.             }
  206.         }
  207.     return( ContinueStatus );
  208. }
  209.