Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
5.1 KiB

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