ReactOS 0.4.15-dev-5667-ged97270
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[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," ? "}
153
154static 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
176int main()
177{
178 DWORD r;
179 ANSI_STRING astring;
181 PSYSTEM_PROCESSES SystemProcesses = NULL;
182 PSYSTEM_PROCESSES CurrentProcess;
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);
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
273 (LPARAM)(LPTSTR) szWindowName );
274 }
275
276 CurrentProcess = (PSYSTEM_PROCESSES)((ULONG_PTR)CurrentProcess +
277 (ULONG_PTR)CurrentProcess->NextEntryOffset);
278 }
279 return (0);
280}
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:32
#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 LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#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:25
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define stdout
Definition: stdio.h:99
static TfClientId tid
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 waitres waitreason[35+1]
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:155
struct _SYSTEM_THREADS * PSYSTEM_THREADS
int main()
Definition: ps.c:176
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:268
DWORD WINAPI GetWindowThreadProcessId(HWND hWnd, PDWORD lpdwProcessId)
#define lstrlen
Definition: winbase.h:3747
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
_In_ ULONG_PTR _In_ ULONG _Out_ ULONG_PTR * pid
Definition: winddi.h:3837
LONG_PTR LPARAM
Definition: windef.h:208
#define GetWindowLong
Definition: winuser.h:5786
#define wsprintf
Definition: winuser.h:5855
#define GetWindowText
Definition: winuser.h:5788
BOOL WINAPI EnumThreadWindows(_In_ DWORD, _In_ WNDENUMPROC, _In_ LPARAM)
#define GWL_STYLE
Definition: winuser.h:846
BOOL(CALLBACK * WNDENUMPROC)(HWND, LPARAM)
Definition: winuser.h:2898
CHAR * LPTSTR
Definition: xmlstorage.h:192