ReactOS 0.4.17-dev-357-ga8f14ff
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
29typedef 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 ()
92static char title[] = "P PID PPID KTime UTime NAME\n";
93static char title1[] = "t TID KTime UTime State WaitResson\n";
94static char title2[] = "w PID Hwnd WndStile TID WndName\n";
95
96
97struct 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," ? "}
111
112struct waitres {
114 char desc[17];
115} waitreason[44 + 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,"WrKeyedEvent "},
138 {22,"WrTerminated "},
139 {23,"WrProcessInSwap "},
140 {24,"WrCpuRateControl "},
141 {25,"WrCalloutStack "},
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,"WrFastMutex "},
151 {35,"WrGuardedMutex "},
152 {36,"WrRundown "},
153 {37,"WrAlertByThreadId"},
154 {38,"WrDeferredPreempt"},
155 {39,"WrPhysicalFault "},
156 {40,"WrIoRing "},
157 {41,"WrMdlCache "},
158 {42,"WrRcu "},
159 {43,"MaximumWaitReason"},
160 {-1," ? "}
162
163static BOOL CALLBACK
165{
166 DWORD r, pid, tid;
167 LONG style;
168 char buf[256];
170
171 GetWindowText(hwnd, (LPTSTR)lp, 30);
172
173 if(hwnd != 0)
174 {
176
178
179 wsprintf (buf,"w%8d %8x %08x %8d %s\n",pid, hwnd , style, tid, lp );
180 WriteFile(Stdout, buf, lstrlen(buf), &r, NULL);
181 }
182 return (TRUE);
183}
184
185int main()
186{
187 DWORD r;
188 ANSI_STRING astring;
190 PSYSTEM_PROCESSES SystemProcesses = NULL;
191 PSYSTEM_PROCESSES CurrentProcess;
192 ULONG BufferSize, ReturnSize;
194 char buf[256];
195 char buf1[256];
196
197 WriteFile(Stdout, title, lstrlen(title), &r, NULL);
198 WriteFile(Stdout, title1, lstrlen(title1), &r, NULL);
199 WriteFile(Stdout, title2, lstrlen(title2), &r, NULL);
200
201 /* Get process information. */
202 BufferSize = 0;
203 do
204 {
205 BufferSize += 0x10000;
206 SystemProcesses = HeapAlloc(GetProcessHeap(), 0, BufferSize);
208 SystemProcesses, BufferSize,
209 &ReturnSize);
211 HeapFree(GetProcessHeap(), 0, SystemProcesses);
213
214 /* If querying system information failed, bail out. */
215 if (!NT_SUCCESS(Status))
216 return 1;
217
218 /* For every process print the information. */
219 CurrentProcess = SystemProcesses;
220 while (CurrentProcess->NextEntryOffset != 0)
221 {
222 int hour, hour1, thour, thour1;
223 unsigned char minute, minute1, tmin, tmin1;
224 unsigned char seconds, seconds1, tsec, tsec1;
225
226 unsigned int ti;
227 LARGE_INTEGER ptime;
228
229 ptime.QuadPart = CurrentProcess->KernelTime.QuadPart;
230 hour = (ptime.QuadPart / (10000000LL * 3600LL));
231 minute = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
232 seconds = (ptime.QuadPart / 10000000LL) % 60LL;
233
234 ptime.QuadPart = CurrentProcess->UserTime.QuadPart;
235 hour1 = (ptime.QuadPart / (10000000LL * 3600LL));
236 minute1 = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
237 seconds1 = (ptime.QuadPart / 10000000LL) % 60LL;
238
239 RtlUnicodeStringToAnsiString(&astring, &CurrentProcess->ImageName, TRUE);
240
241 wsprintf(buf,"P%8d %8d %3d:%02d:%02d %3d:%02d:%02d ProcName: %s\n",
242 CurrentProcess->UniqueProcessId, CurrentProcess->InheritedFromUniqueProcessId,
243 hour, minute, seconds, hour1, minute1, seconds1,
244 astring.Buffer);
246
247 RtlFreeAnsiString(&astring);
248
249 for (ti = 0; ti < CurrentProcess->NumberOfThreads; ti++)
250 {
251 struct status *statt;
252 struct waitres *waitt;
253 char szWindowName[30] = {" "};
254
255 ptime = CurrentProcess->Threads[ti].KernelTime;
256 thour = (ptime.QuadPart / (10000000LL * 3600LL));
257 tmin = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
258 tsec = (ptime.QuadPart / 10000000LL) % 60LL;
259
260 ptime = CurrentProcess->Threads[ti].UserTime;
261 thour1 = (ptime.QuadPart / (10000000LL * 3600LL));
262 tmin1 = (ptime.QuadPart / (10000000LL * 60LL)) % 60LL;
263 tsec1 = (ptime.QuadPart / 10000000LL) % 60LL;
264
265 statt = thread_stat;
266 while (statt->state != CurrentProcess->Threads[ti].ThreadState && statt->state >= 0)
267 statt++;
268
269 waitt = waitreason;
270 while (waitt->state != CurrentProcess->Threads[ti].WaitReason && waitt->state >= 0)
271 waitt++;
272
273 wsprintf (buf1,
274 "t% %8d %3d:%02d:%02d %3d:%02d:%02d %s %s\n",
275 CurrentProcess->Threads[ti].ClientId.UniqueThread,
276 thour, tmin, tsec, thour1, tmin1, tsec1,
277 statt->desc , waitt->desc);
278 WriteFile(stdout, buf1, lstrlen(buf1), &r, NULL);
279
282 (LPARAM)(LPTSTR) szWindowName );
283 }
284
285 CurrentProcess = (PSYSTEM_PROCESSES)((ULONG_PTR)CurrentProcess +
286 (ULONG_PTR)CurrentProcess->NextEntryOffset);
287 }
288 return (0);
289}
Arabic default style
Definition: afstyles.h:94
LONG NTSTATUS
Definition: precomp.h:26
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define BufferSize
Definition: mmc.h:75
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define CALLBACK
Definition: compat.h:35
LONG KPRIORITY
Definition: compat.h:803
BOOL WINAPI WriteFile(_In_ HANDLE hFile, _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped)
Definition: rw.c:25
#define stdout
#define ULONG_PTR
Definition: config.h:101
#define PtrToUlong(u)
Definition: config.h:107
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
@ SystemProcessInformation
Definition: ntddk_ex.h:16
Status
Definition: gdiplustypes.h:24
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
static TfClientId tid
LONG_PTR LPARAM
Definition: minwindef.h:175
LPSTR LPTSTR
Definition: ms-dtyp.idl:131
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlFreeAnsiString(PANSI_STRING AnsiString)
long LONG
Definition: pedump.c:60
static char title1[]
Definition: ps.c:93
struct _SYSTEM_THREADS SYSTEM_THREADS
static char title2[]
Definition: ps.c:94
static char title[]
Definition: ps.c:92
struct status thread_stat[8+1]
struct _SYSTEM_PROCESSES * PSYSTEM_PROCESSES
static BOOL CALLBACK EnumThreadProc(HWND hwnd, LPARAM lp)
Definition: ps.c:164
struct waitres waitreason[44+1]
struct _SYSTEM_THREADS * PSYSTEM_THREADS
int main()
Definition: ps.c:185
struct _SYSTEM_PROCESSES SYSTEM_PROCESSES
NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
HANDLE UniqueThread
Definition: compat.h:826
ULONG HandleCount
Definition: ps.c:58
ULONG PagefileUsage
Definition: ps.c:75
LARGE_INTEGER OtherOperationCount
Definition: ps.c:82
LARGE_INTEGER ReadTransferCount
Definition: ps.c:83
ULONG PageDirectoryFrame
Definition: ps.c:60
ULONG WorkingSetSize
Definition: ps.c:70
ULONG QuotaPeakNonPagedPoolUsage
Definition: ps.c:73
ULONG QuotaNonPagedPoolUsage
Definition: ps.c:74
ULONG NextEntryOffset
Definition: ps.c:46
KPRIORITY BasePriority
Definition: ps.c:55
ULONG PeakPagefileUsage
Definition: ps.c:76
LARGE_INTEGER ReadOperationCount
Definition: ps.c:80
ULONG PrivateUsage
Definition: ps.c:77
LARGE_INTEGER KernelTime
Definition: ps.c:53
LARGE_INTEGER SpareLi2
Definition: ps.c:49
SYSTEM_THREADS Threads[1]
Definition: ps.c:87
ULONG SessionId
Definition: ps.c:59
ULONG QuotaPeakPagedPoolUsage
Definition: ps.c:71
ULONG NumberOfThreads
Definition: ps.c:47
LARGE_INTEGER WriteOperationCount
Definition: ps.c:81
LARGE_INTEGER CreateTime
Definition: ps.c:51
LARGE_INTEGER WriteTransferCount
Definition: ps.c:84
LARGE_INTEGER OtherTransferCount
Definition: ps.c:85
ULONG QuotaPagedPoolUsage
Definition: ps.c:72
LARGE_INTEGER SpareLi3
Definition: ps.c:50
HANDLE UniqueProcessId
Definition: ps.c:56
UNICODE_STRING ImageName
Definition: ps.c:54
ULONG PageFaultCount
Definition: ps.c:68
HANDLE InheritedFromUniqueProcessId
Definition: ps.c:57
ULONG PeakVirtualSize
Definition: ps.c:66
LARGE_INTEGER SpareLi1
Definition: ps.c:48
ULONG PeakWorkingSetSize
Definition: ps.c:69
ULONG VirtualSize
Definition: ps.c:67
LARGE_INTEGER UserTime
Definition: ps.c:52
ULONG WaitTime
Definition: ps.c:34
LARGE_INTEGER CreateTime
Definition: ps.c:33
LARGE_INTEGER UserTime
Definition: ps.c:32
LONG BasePriority
Definition: ps.c:38
KPRIORITY Priority
Definition: ps.c:37
CLIENT_ID ClientId
Definition: ps.c:36
LARGE_INTEGER KernelTime
Definition: ps.c:31
ULONG ThreadState
Definition: ps.c:40
PVOID StartAddress
Definition: ps.c:35
ULONG WaitReason
Definition: ps.c:41
ULONG ContextSwitches
Definition: ps.c:39
Definition: ps.c:97
const char desc[10]
Definition: ps.c:99
DWORD state
Definition: ps.c:98
Definition: ps.c:112
DWORD state
Definition: ps.c:113
char desc[17]
Definition: ps.c:114
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INFO_LENGTH_MISMATCH
Definition: udferr_usr.h:133
LONGLONG QuadPart
Definition: typedefs.h:114
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
#define STD_OUTPUT_HANDLE
Definition: winbase.h:293
DWORD WINAPI GetWindowThreadProcessId(HWND hWnd, PDWORD lpdwProcessId)
#define lstrlen
Definition: winbase.h:3597
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
_In_ ULONG_PTR _In_ ULONG _Out_ ULONG_PTR * pid
Definition: winddi.h:3837
#define GetWindowLong
Definition: winuser.h:5962
#define wsprintf
Definition: winuser.h:6031
#define GetWindowText
Definition: winuser.h:5964
BOOL WINAPI EnumThreadWindows(_In_ DWORD, _In_ WNDENUMPROC, _In_ LPARAM)
#define GWL_STYLE
Definition: winuser.h:863
BOOL(CALLBACK * WNDENUMPROC)(HWND, LPARAM)
Definition: winuser.h:3016