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 / process.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  5KB  |  229 lines

  1. /*++
  2.  
  3. Copyright (c) 1995-1997  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     process.c
  8.  
  9. Abstract:
  10.  
  11.     This module maintains state about each process/thread created by the application
  12.     pfmon program.
  13.  
  14. Author:
  15.  
  16.     Mark Lucovsky (markl) 26-Jan-1995
  17.  
  18. Revision History:
  19.  
  20. --*/
  21.  
  22. #include "pfmonp.h"
  23.  
  24. BOOL
  25. AddProcess(
  26.     LPDEBUG_EVENT DebugEvent,
  27.     PPROCESS_INFO *ReturnedProcess
  28.     )
  29. {
  30.     PPROCESS_INFO Process;
  31.  
  32.     Process = LocalAlloc(LMEM_ZEROINIT, sizeof( *Process ) );
  33.     if (Process == NULL) {
  34.         return FALSE;
  35.         }
  36.  
  37.     Process->Id = DebugEvent->dwProcessId;
  38.     Process->Handle = DebugEvent->u.CreateProcessInfo.hProcess;
  39.     InitializeListHead( &Process->ThreadListHead );
  40.     InsertTailList( &ProcessListHead, &Process->Entry );
  41.     *ReturnedProcess = Process;
  42.  
  43.     return TRUE;
  44. }
  45.  
  46. BOOL
  47. DeleteProcess(
  48.     PPROCESS_INFO Process
  49.     )
  50. {
  51.     PLIST_ENTRY Next, Head;
  52.     PTHREAD_INFO Thread;
  53.     PMODULE_INFO Module;
  54.     CHAR Line[256];
  55.  
  56.     RemoveEntryList( &Process->Entry );
  57.  
  58.     Head = &Process->ThreadListHead;
  59.     Next = Head->Flink;
  60.     while (Next != Head) {
  61.         Thread = CONTAINING_RECORD( Next, THREAD_INFO, Entry );
  62.         Next = Next->Flink;
  63.         DeleteThread( Process, Thread );
  64.         }
  65.  
  66.     LocalFree( Process );
  67.     fprintf(stdout,"\n");
  68.  
  69.     Next = ModuleListHead.Flink;
  70.     while ( Next != &ModuleListHead ) {
  71.         Module = CONTAINING_RECORD(Next,MODULE_INFO,Entry);
  72.  
  73.  
  74.         sprintf(Line,"%16s Caused %6d faults had %6d Soft %6d Hard faulted VA's\n",
  75.             Module->DebugInfo->ImageFileName,
  76.             Module->NumberCausedFaults,
  77.             Module->NumberFaultedSoftVas,
  78.             Module->NumberFaultedHardVas
  79.             );
  80.         if ( !fLogOnly ) {
  81.             fprintf(stdout,"%s",Line);
  82.             }
  83.         if ( LogFile ) {
  84.             fprintf(LogFile,"%s",Line);
  85.             }
  86.  
  87.         Next = Next->Flink;
  88.         }
  89.  
  90.  
  91.     return TRUE;
  92. }
  93.  
  94.  
  95. BOOL
  96. AddThread(
  97.     LPDEBUG_EVENT DebugEvent,
  98.     PPROCESS_INFO Process,
  99.     PTHREAD_INFO *ReturnedThread
  100.     )
  101. {
  102.     PTHREAD_INFO Thread;
  103.  
  104.     Thread = LocalAlloc(LMEM_ZEROINIT, sizeof( *Thread ) );
  105.     if (Thread == NULL) {
  106.         return FALSE;
  107.         }
  108.  
  109.     Thread->Id = DebugEvent->dwThreadId;
  110.     if (DebugEvent->dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
  111.         Thread->Handle = DebugEvent->u.CreateProcessInfo.hThread;
  112.         Thread->StartAddress = DebugEvent->u.CreateProcessInfo.lpStartAddress;
  113.         }
  114.     else {
  115.         Thread->Handle = DebugEvent->u.CreateThread.hThread;
  116.         Thread->StartAddress = DebugEvent->u.CreateThread.lpStartAddress;
  117.         }
  118.     InsertTailList( &Process->ThreadListHead, &Thread->Entry );
  119.     *ReturnedThread = Thread;
  120.     return TRUE;
  121. }
  122.  
  123. BOOL
  124. DeleteThread(
  125.     PPROCESS_INFO Process,
  126.     PTHREAD_INFO Thread
  127.     )
  128. {
  129.  
  130.     RemoveEntryList( &Thread->Entry );
  131.  
  132.     LocalFree( Thread );
  133.     return TRUE;
  134. }
  135.  
  136.  
  137. PPROCESS_INFO
  138. FindProcessById(
  139.     ULONG Id
  140.     )
  141. {
  142.     PLIST_ENTRY Next, Head;
  143.     PPROCESS_INFO Process;
  144.  
  145.     Head = &ProcessListHead;
  146.     Next = Head->Flink;
  147.     while (Next != Head) {
  148.         Process = CONTAINING_RECORD( Next, PROCESS_INFO, Entry );
  149.         if (Process->Id == Id) {
  150.             return Process;
  151.             }
  152.  
  153.         Next = Next->Flink;
  154.         }
  155.  
  156.     return NULL;
  157. }
  158.  
  159. BOOL
  160. FindProcessAndThreadForEvent(
  161.     LPDEBUG_EVENT DebugEvent,
  162.     PPROCESS_INFO *ReturnedProcess,
  163.     PTHREAD_INFO *ReturnedThread
  164.     )
  165. {
  166.     PLIST_ENTRY Next, Head;
  167.     PPROCESS_INFO Process;
  168.     PTHREAD_INFO Thread;
  169.  
  170.     Head = &ProcessListHead;
  171.     Next = Head->Flink;
  172.     Process = NULL;
  173.     Thread = NULL;
  174.     while (Next != Head) {
  175.         Process = CONTAINING_RECORD( Next, PROCESS_INFO, Entry );
  176.         if (Process->Id == DebugEvent->dwProcessId) {
  177.             Head = &Process->ThreadListHead;
  178.             Next = Head->Flink;
  179.             while (Next != Head) {
  180.                 Thread = CONTAINING_RECORD( Next, THREAD_INFO, Entry );
  181.                 if (Thread->Id == DebugEvent->dwThreadId) {
  182.                     break;
  183.                     }
  184.  
  185.                 Thread = NULL;
  186.                 Next = Next->Flink;
  187.                 }
  188.  
  189.             break;
  190.             }
  191.  
  192.         Process = NULL;
  193.         Next = Next->Flink;
  194.         }
  195.  
  196.     *ReturnedProcess = Process;
  197.     *ReturnedThread = Thread;
  198.  
  199.     if (DebugEvent->dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) {
  200.         if (Process != NULL) {
  201.             DeclareError( PFMON_DUPLICATE_PROCESS_ID, 0, DebugEvent->dwProcessId );
  202.             return FALSE;
  203.             }
  204.         }
  205.     else
  206.     if (DebugEvent->dwDebugEventCode == CREATE_THREAD_DEBUG_EVENT) {
  207.         if (Thread != NULL) {
  208.             DeclareError( PFMON_DUPLICATE_THREAD_ID, 0, DebugEvent->dwThreadId, DebugEvent->dwProcessId );
  209.             return FALSE;
  210.             }
  211.         if (Process == NULL) {
  212.             DeclareError( PFMON_MISSING_PROCESS_ID, 0, DebugEvent->dwProcessId );
  213.             return FALSE;
  214.             }
  215.         }
  216.     else
  217.     if (Process == NULL) {
  218.         DeclareError( PFMON_MISSING_PROCESS_ID, 0, DebugEvent->dwProcessId );
  219.         return FALSE;
  220.         }
  221.     else
  222.     if (Thread == NULL) {
  223.         DeclareError( PFMON_MISSING_THREAD_ID, 0, DebugEvent->dwThreadId, DebugEvent->dwProcessId );
  224.         return FALSE;
  225.         }
  226.  
  227.     return TRUE;
  228. }
  229.