3 * ReactOS ps - process list console viewer
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Thanks to Filip Navara patch for fixing the Xp crash problem.
25 #define NTOS_MODE_USER
26 #define WIN32_NO_STATUS
28 #include <ndk/ntndk.h>
30 typedef struct _SYSTEM_THREADS
32 LARGE_INTEGER KernelTime
;
33 LARGE_INTEGER UserTime
;
34 LARGE_INTEGER CreateTime
;
40 ULONG ContextSwitches
;
43 } SYSTEM_THREADS
, *PSYSTEM_THREADS
;
45 typedef struct _SYSTEM_PROCESSES
47 ULONG NextEntryOffset
;
48 ULONG NumberOfThreads
;
49 LARGE_INTEGER SpareLi1
;
50 LARGE_INTEGER SpareLi2
;
51 LARGE_INTEGER SpareLi3
;
52 LARGE_INTEGER CreateTime
;
53 LARGE_INTEGER UserTime
;
54 LARGE_INTEGER KernelTime
;
55 UNICODE_STRING ImageName
;
56 KPRIORITY BasePriority
;
57 HANDLE UniqueProcessId
;
58 HANDLE InheritedFromUniqueProcessId
;
61 ULONG PageDirectoryFrame
;
64 * This part corresponds to VM_COUNTERS_EX.
65 * NOTE: *NOT* THE SAME AS VM_COUNTERS!
67 ULONG PeakVirtualSize
;
70 ULONG PeakWorkingSetSize
;
72 ULONG QuotaPeakPagedPoolUsage
;
73 ULONG QuotaPagedPoolUsage
;
74 ULONG QuotaPeakNonPagedPoolUsage
;
75 ULONG QuotaNonPagedPoolUsage
;
77 ULONG PeakPagefileUsage
;
80 /* This part corresponds to IO_COUNTERS */
81 LARGE_INTEGER ReadOperationCount
;
82 LARGE_INTEGER WriteOperationCount
;
83 LARGE_INTEGER OtherOperationCount
;
84 LARGE_INTEGER ReadTransferCount
;
85 LARGE_INTEGER WriteTransferCount
;
86 LARGE_INTEGER OtherTransferCount
;
88 SYSTEM_THREADS Threads
[1];
89 } SYSTEM_PROCESSES
, *PSYSTEM_PROCESSES
;
92 // x00000000 00000000 000:00:00 000:00:00 ()
93 static char title
[] = "P PID PPID KTime UTime NAME\n";
94 static char title1
[] = "t TID KTime UTime State WaitResson\n";
95 static char title2
[] = "w PID Hwnd WndStile TID WndName\n";
101 } thread_stat
[8 + 1] = {
116 } waitreason
[35 + 1] = {
120 {3, "PoolAllocation "},
121 {4, "DelayExecution "},
127 {10,"WrPoolAllocation "},
128 {11,"WrDelayExecution "},
130 {13,"WrUserRequest "},
133 {16,"WrLpcReceive "},
135 {18,"WrVirtualMemory "},
137 {20,"WrRendezvous "},
139 {22,"WrGuardedMutex "},
147 {30,"WrQuantumEnd "},
148 {31,"WrDispatchInt "},
150 {33,"WrYieldExecution "},
151 {34,"MaximumWaitReason"},
156 EnumThreadProc(HWND hwnd
, LPARAM lp
)
161 HANDLE Stdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
163 GetWindowText(hwnd
, (LPTSTR
)lp
, 30);
167 style
= GetWindowLong(hwnd
, GWL_STYLE
);
169 tid
= GetWindowThreadProcessId(hwnd
, &pid
);
171 wsprintf (buf
,"w%8d %8x %08x %8d %s\n",pid
, hwnd
, style
, tid
, lp
);
172 WriteFile(Stdout
, buf
, lstrlen(buf
), &r
, NULL
);
181 HANDLE Stdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
182 PSYSTEM_PROCESSES SystemProcesses
= NULL
;
183 PSYSTEM_PROCESSES CurrentProcess
;
184 ULONG BufferSize
, ReturnSize
;
189 WriteFile(Stdout
, title
, lstrlen(title
), &r
, NULL
);
190 WriteFile(Stdout
, title1
, lstrlen(title1
), &r
, NULL
);
191 WriteFile(Stdout
, title2
, lstrlen(title2
), &r
, NULL
);
193 /* Get process information. */
197 BufferSize
+= 0x10000;
198 SystemProcesses
= HeapAlloc(GetProcessHeap(), 0, BufferSize
);
199 Status
= NtQuerySystemInformation(SystemProcessInformation
,
200 SystemProcesses
, BufferSize
,
202 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
203 HeapFree(GetProcessHeap(), 0, SystemProcesses
);
204 } while (Status
== STATUS_INFO_LENGTH_MISMATCH
);
206 /* If querying system information failed, bail out. */
207 if (!NT_SUCCESS(Status
))
210 /* For every process print the information. */
211 CurrentProcess
= SystemProcesses
;
212 while (CurrentProcess
->NextEntryOffset
!= 0)
214 int hour
, hour1
, thour
, thour1
;
215 unsigned char minute
, minute1
, tmin
, tmin1
;
216 unsigned char seconds
, seconds1
, tsec
, tsec1
;
221 ptime
.QuadPart
= CurrentProcess
->KernelTime
.QuadPart
;
222 hour
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
223 minute
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
224 seconds
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
226 ptime
.QuadPart
= CurrentProcess
->UserTime
.QuadPart
;
227 hour1
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
228 minute1
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
229 seconds1
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
231 RtlUnicodeStringToAnsiString(&astring
, &CurrentProcess
->ImageName
, TRUE
);
233 wsprintf(buf
,"P%8d %8d %3d:%02d:%02d %3d:%02d:%02d ProcName: %s\n",
234 CurrentProcess
->UniqueProcessId
, CurrentProcess
->InheritedFromUniqueProcessId
,
235 hour
, minute
, seconds
, hour1
, minute1
, seconds1
,
237 WriteFile(stdout
, buf
, lstrlen(buf
), &r
, NULL
);
239 RtlFreeAnsiString(&astring
);
241 for (ti
= 0; ti
< CurrentProcess
->NumberOfThreads
; ti
++)
243 struct status
*statt
;
244 struct waitres
*waitt
;
245 char szWindowName
[30] = {" "};
247 ptime
= CurrentProcess
->Threads
[ti
].KernelTime
;
248 thour
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
249 tmin
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
250 tsec
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
252 ptime
= CurrentProcess
->Threads
[ti
].UserTime
;
253 thour1
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
254 tmin1
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
255 tsec1
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
258 while (statt
->state
!= CurrentProcess
->Threads
[ti
].ThreadState
&& statt
->state
>= 0)
262 while (waitt
->state
!= CurrentProcess
->Threads
[ti
].WaitReason
&& waitt
->state
>= 0)
266 "t% %8d %3d:%02d:%02d %3d:%02d:%02d %s %s\n",
267 CurrentProcess
->Threads
[ti
].ClientId
.UniqueThread
,
268 thour
, tmin
, tsec
, thour1
, tmin1
, tsec1
,
269 statt
->desc
, waitt
->desc
);
270 WriteFile(stdout
, buf1
, lstrlen(buf1
), &r
, NULL
);
272 EnumThreadWindows(PtrToUlong(CurrentProcess
->Threads
[ti
].ClientId
.UniqueThread
),
273 (WNDENUMPROC
) EnumThreadProc
,
274 (LPARAM
)(LPTSTR
) szWindowName
);
277 CurrentProcess
= (PSYSTEM_PROCESSES
)((ULONG_PTR
)CurrentProcess
+
278 (ULONG_PTR
)CurrentProcess
->NextEntryOffset
);