ReactOS  0.4.12-dev-375-g61fed54
ps.c
Go to the documentation of this file.
1 /*
2  * ReactOS ps - process list console viewer
3  *
4  * ps.c
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 /*
21  Thanks to Filip Navara patch for fixing the Xp crash problem.
22 */
23 
24 #define NTOS_MODE_USER
25 #define WIN32_NO_STATUS
26 #include <windows.h>
27 #include <ndk/ntndk.h>
28 
29 typedef struct _SYSTEM_THREADS
30  {
43 
44  typedef struct _SYSTEM_PROCESSES
45  {
61 
62  /*
63  * This part corresponds to VM_COUNTERS_EX.
64  * NOTE: *NOT* THE SAME AS VM_COUNTERS!
65  */
78 
79  /* This part corresponds to IO_COUNTERS */
86 
89 
90 
91 // x00000000 00000000 000:00:00 000:00:00 ()
92 static char title[] = "P PID PPID KTime UTime NAME\n";
93 static char title1[] = "t TID KTime UTime State WaitResson\n";
94 static char title2[] = "w PID Hwnd WndStile TID WndName\n";
95 
96 
97 struct status {
99  const char desc[10];
100 } thread_stat[8 + 1] = {
101  {0, "Init "},
102  {1, "Ready "},
103  {2, "Running "},
104  {3, "Standby "},
105  {4, "Terminated"},
106  {5, "Wait "},
107  {6, "Transition"},
108  {7, "Unknown "},
109  {-1," ? "}
110 };
111 
112 struct waitres {
114  char desc[17];
115 } waitreason[35 + 1] = {
116  {0, "Executive "},
117  {1, "FreePage "},
118  {2, "PageIn "},
119  {3, "PoolAllocation "},
120  {4, "DelayExecution "},
121  {5, "Suspended "},
122  {6, "UserRequest "},
123  {7, "WrExecutive "},
124  {8, "WrFreePage "},
125  {9, "WrPageIn "},
126  {10,"WrPoolAllocation "},
127  {11,"WrDelayExecution "},
128  {12,"WrSuspended "},
129  {13,"WrUserRequest "},
130  {14,"WrEventPair "},
131  {15,"WrQueue "},
132  {16,"WrLpcReceive "},
133  {17,"WrLpcReply "},
134  {18,"WrVirtualMemory "},
135  {19,"WrPageOut "},
136  {20,"WrRendezvous "},
137  {21,"Spare2 "},
138  {22,"WrGuardedMutex "},
139  {23,"Spare4 "},
140  {24,"Spare5 "},
141  {25,"Spare6 "},
142  {26,"WrKernel "},
143  {27,"WrResource "},
144  {28,"WrPushLock "},
145  {29,"WrMutex "},
146  {30,"WrQuantumEnd "},
147  {31,"WrDispatchInt "},
148  {32,"WrPreempted "},
149  {33,"WrYieldExecution "},
150  {34,"MaximumWaitReason"},
151  {-1," ? "}
152 };
153 
154 static BOOL CALLBACK
156 {
157  DWORD r, pid, tid;
158  LONG style;
159  char buf[256];
161 
162  GetWindowText(hwnd, (LPTSTR)lp, 30);
163 
164  if(hwnd != 0)
165  {
167 
169 
170  wsprintf (buf,"w%8d %8x %08x %8d %s\n",pid, hwnd , style, tid, lp );
171  WriteFile(Stdout, buf, lstrlen(buf), &r, NULL);
172  }
173  return (TRUE);
174 }
175 
176 int main()
177 {
178  DWORD r;
179  ANSI_STRING astring;
181  PSYSTEM_PROCESSES SystemProcesses = NULL;
183  ULONG BufferSize, ReturnSize;
185  char buf[256];
186  char buf1[256];
187 
188  WriteFile(Stdout, title, lstrlen(title), &r, NULL);
189  WriteFile(Stdout, title1, lstrlen(title1), &r, NULL);
190  WriteFile(Stdout, title2, lstrlen(title2), &r, NULL);
191 
192  /* Get process information. */
193  BufferSize = 0;
194  do
195  {
196  BufferSize += 0x10000;
197  SystemProcesses = HeapAlloc(GetProcessHeap(), 0, BufferSize);
199  SystemProcesses, BufferSize,
200  &ReturnSize);
202  HeapFree(GetProcessHeap(), 0, SystemProcesses);
203  } while (Status == STATUS_INFO_LENGTH_MISMATCH);
204 
205  /* If querying system information failed, bail out. */
206  if (!NT_SUCCESS(Status))
207  return 1;
208 
209  /* For every process print the information. */
210  CurrentProcess = SystemProcesses;
211  while (CurrentProcess->NextEntryOffset != 0)
212  {
213  int hour, hour1, thour, thour1;
214  unsigned char minute, minute1, tmin, tmin1;
215  unsigned char seconds, seconds1, tsec, tsec1;
216 
217  unsigned int ti;
218  LARGE_INTEGER ptime;
219 
220  ptime.QuadPart = CurrentProcess->KernelTime.QuadPart;
221  hour = (ptime.QuadPart / (10000000LL * 3600LL));
222  minute = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
223  seconds = (ptime.QuadPart / 10000000LL) % 60LL;
224 
225  ptime.QuadPart = CurrentProcess->UserTime.QuadPart;
226  hour1 = (ptime.QuadPart / (10000000LL * 3600LL));
227  minute1 = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
228  seconds1 = (ptime.QuadPart / 10000000LL) % 60LL;
229 
230  RtlUnicodeStringToAnsiString(&astring, &CurrentProcess->ImageName, TRUE);
231 
232  wsprintf(buf,"P%8d %8d %3d:%02d:%02d %3d:%02d:%02d ProcName: %s\n",
233  CurrentProcess->UniqueProcessId, CurrentProcess->InheritedFromUniqueProcessId,
234  hour, minute, seconds, hour1, minute1, seconds1,
235  astring.Buffer);
237 
238  RtlFreeAnsiString(&astring);
239 
240  for (ti = 0; ti < CurrentProcess->NumberOfThreads; ti++)
241  {
242  struct status *statt;
243  struct waitres *waitt;
244  char szWindowName[30] = {" "};
245 
246  ptime = CurrentProcess->Threads[ti].KernelTime;
247  thour = (ptime.QuadPart / (10000000LL * 3600LL));
248  tmin = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
249  tsec = (ptime.QuadPart / 10000000LL) % 60LL;
250 
251  ptime = CurrentProcess->Threads[ti].UserTime;
252  thour1 = (ptime.QuadPart / (10000000LL * 3600LL));
253  tmin1 = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
254  tsec1 = (ptime.QuadPart / 10000000LL) % 60LL;
255 
256  statt = thread_stat;
257  while (statt->state != CurrentProcess->Threads[ti].ThreadState && statt->state >= 0)
258  statt++;
259 
260  waitt = waitreason;
261  while (waitt->state != CurrentProcess->Threads[ti].WaitReason && waitt->state >= 0)
262  waitt++;
263 
264  wsprintf (buf1,
265  "t% %8d %3d:%02d:%02d %3d:%02d:%02d %s %s\n",
266  CurrentProcess->Threads[ti].ClientId.UniqueThread,
267  thour, tmin, tsec, thour1, tmin1, tsec1,
268  statt->desc , waitt->desc);
269  WriteFile(stdout, buf1, lstrlen(buf1), &r, NULL);
270 
271  EnumThreadWindows(PtrToUlong(CurrentProcess->Threads[ti].ClientId.UniqueThread),
273  (LPARAM)(LPTSTR) szWindowName );
274  }
275 
277  (ULONG_PTR)CurrentProcess->NextEntryOffset);
278  }
279  return (0);
280 }
ULONG SessionId
Definition: ps.c:59
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
_In_ ULONG_PTR _In_ ULONG _Out_ ULONG_PTR * pid
Definition: winddi.h:3835
LARGE_INTEGER ReadOperationCount
Definition: ps.c:80
struct waitres waitreason[35+1]
#define TRUE
Definition: types.h:120
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
#define LL
Definition: tui.h:72
ULONG PrivateUsage
Definition: ps.c:77
LARGE_INTEGER WriteOperationCount
Definition: ps.c:81
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
LARGE_INTEGER UserTime
Definition: ps.c:52
ULONG PeakWorkingSetSize
Definition: ps.c:69
ULONG PageDirectoryFrame
Definition: ps.c:60
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
LONG NTSTATUS
Definition: precomp.h:26
HANDLE InheritedFromUniqueProcessId
Definition: ps.c:57
ULONG WaitTime
Definition: ps.c:34
ULONG ContextSwitches
Definition: ps.c:39
#define CALLBACK
Definition: compat.h:27
BOOL WINAPI EnumThreadWindows(_In_ DWORD, _In_ WNDENUMPROC, _In_ LPARAM)
ULONG PeakPagefileUsage
Definition: ps.c:76
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
struct _SYSTEM_THREADS SYSTEM_THREADS
static char title1[]
Definition: ps.c:93
LONG BasePriority
Definition: ps.c:38
struct _SYSTEM_THREADS * PSYSTEM_THREADS
BOOL(CALLBACK * WNDENUMPROC)(HWND, LPARAM)
Definition: winuser.h:2861
char desc[17]
Definition: ps.c:114
KPRIORITY Priority
Definition: ps.c:37
LONG KPRIORITY
Definition: compat.h:454
CHAR * LPTSTR
Definition: xmlstorage.h:192
FILE * stdout
struct _SYSTEM_PROCESSES * PSYSTEM_PROCESSES
LARGE_INTEGER OtherTransferCount
Definition: ps.c:85
#define GetWindowText
Definition: winuser.h:5664
uint32_t ULONG_PTR
Definition: typedefs.h:63
ULONG ThreadState
Definition: ps.c:40
long LONG
Definition: pedump.c:60
LONG_PTR LPARAM
Definition: windef.h:208
static BOOL CALLBACK EnumThreadProc(HWND hwnd, LPARAM lp)
Definition: ps.c:155
DWORD WINAPI GetWindowThreadProcessId(HWND, PDWORD)
LARGE_INTEGER CreateTime
Definition: ps.c:33
LARGE_INTEGER SpareLi1
Definition: ps.c:48
smooth NULL
Definition: ftsmooth.c:416
ULONG WaitReason
Definition: ps.c:41
LARGE_INTEGER WriteTransferCount
Definition: ps.c:84
ULONG VirtualSize
Definition: ps.c:67
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
ULONG QuotaPeakPagedPoolUsage
Definition: ps.c:71
#define PtrToUlong(p)
Definition: basetsd.h:83
ULONG PagefileUsage
Definition: ps.c:75
ULONG PeakVirtualSize
Definition: ps.c:66
ULONG NumberOfThreads
Definition: ps.c:47
LARGE_INTEGER OtherOperationCount
Definition: ps.c:82
ULONG CurrentProcess
Definition: shell.c:125
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
PVOID StartAddress
Definition: ps.c:35
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
DWORD state
Definition: ps.c:113
LARGE_INTEGER SpareLi3
Definition: ps.c:50
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
#define BufferSize
Definition: classpnp.h:419
unsigned long DWORD
Definition: ntddk_ex.h:95
KPRIORITY BasePriority
Definition: ps.c:55
CLIENT_ID ClientId
Definition: ps.c:36
ULONG QuotaPeakNonPagedPoolUsage
Definition: ps.c:73
ULONG PageFaultCount
Definition: ps.c:68
LARGE_INTEGER KernelTime
Definition: ps.c:53
#define GetWindowLong
Definition: winuser.h:5662
DWORD state
Definition: ps.c:98
#define STD_OUTPUT_HANDLE
Definition: winbase.h:265
LARGE_INTEGER CreateTime
Definition: ps.c:51
ULONG WorkingSetSize
Definition: ps.c:70
Definition: ps.c:112
struct status thread_stat[8+1]
LARGE_INTEGER KernelTime
Definition: ps.c:31
HANDLE UniqueProcessId
Definition: ps.c:56
Status
Definition: gdiplustypes.h:24
LARGE_INTEGER UserTime
Definition: ps.c:32
static char title2[]
Definition: ps.c:94
#define GWL_STYLE
Definition: winuser.h:846
#define lstrlen
Definition: winbase.h:3690
ULONG QuotaPagedPoolUsage
Definition: ps.c:72
UNICODE_STRING ImageName
Definition: ps.c:54
const char desc[10]
Definition: ps.c:99
ULONG HandleCount
Definition: ps.c:58
SYSTEM_THREADS Threads[1]
Definition: ps.c:87
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
struct _SYSTEM_PROCESSES SYSTEM_PROCESSES
ULONG QuotaNonPagedPoolUsage
Definition: ps.c:74
LARGE_INTEGER ReadTransferCount
Definition: ps.c:83
#define wsprintf
Definition: winuser.h:5731
Arabic default style
Definition: afstyles.h:93
ULONG NextEntryOffset
Definition: ps.c:46
static TfClientId tid
int main()
Definition: ps.c:176
static char title[]
Definition: ps.c:92
#define HeapFree(x, y, z)
Definition: compat.h:394
LARGE_INTEGER SpareLi2
Definition: ps.c:49
LONGLONG QuadPart
Definition: typedefs.h:112
Definition: ps.c:97