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.
26 /* NOTE: W32API ddk/ntapi.h header has wrong definition of SYSTEM_PROCESSES. */
27 #include <ntos/types.h>
30 // x00000000 00000000 000:00:00 000:00:00 ()
31 static char* title
= "P PID PPID KTime UTime NAME\n";
32 static char* title1
= "t TID KTime UTime State WaitResson\n";
33 static char* title2
= "w PID Hwnd WndStile TID WndName\n";
39 } thread_stat
[8 + 1] = {
54 } waitreason
[28 + 1] = {
87 EnumThreadProc(HWND hwnd
, LPARAM lp
)
91 HANDLE stdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
94 GetWindowText(hwnd
, (LPTSTR
)lp
, 30);
98 style
= GetWindowLong(hwnd
, GWL_STYLE
);
100 tid
= GetWindowThreadProcessId(hwnd
, &pid
);
102 wsprintf (buf
,"w%8d %8x %08x %8d %s\n",pid
, hwnd
, style
, tid
, lp
);
103 WriteFile(stdout
, buf
, lstrlen(buf
), &r
, NULL
);
112 HANDLE stdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
113 PSYSTEM_PROCESS_INFORMATION SystemProcesses
= NULL
;
114 PSYSTEM_PROCESS_INFORMATION CurrentProcess
;
115 ULONG BufferSize
, ReturnSize
;
120 WriteFile(stdout
, title
, lstrlen(title
), &r
, NULL
);
121 WriteFile(stdout
, title1
, lstrlen(title1
), &r
, NULL
);
122 WriteFile(stdout
, title2
, lstrlen(title2
), &r
, NULL
);
124 /* Get process information. */
128 BufferSize
+= 0x10000;
129 SystemProcesses
= HeapAlloc(GetProcessHeap(), 0, BufferSize
);
130 Status
= NtQuerySystemInformation(SystemProcessInformation
,
131 SystemProcesses
, BufferSize
,
133 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
134 HeapFree(GetProcessHeap(), 0, SystemProcesses
);
135 } while (Status
== STATUS_INFO_LENGTH_MISMATCH
);
137 /* If querying system information failed, bail out. */
138 if (!NT_SUCCESS(Status
))
141 /* For every process print the information. */
142 CurrentProcess
= SystemProcesses
;
143 while (CurrentProcess
->NextEntryOffset
!= 0)
145 int hour
, hour1
, thour
, thour1
;
146 unsigned char minute
, minute1
, tmin
, tmin1
;
147 unsigned char seconds
, seconds1
, tsec
, tsec1
;
152 ptime
.QuadPart
= CurrentProcess
->KernelTime
.QuadPart
;
153 hour
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
154 minute
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
155 seconds
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
157 ptime
.QuadPart
= CurrentProcess
->UserTime
.QuadPart
;
158 hour1
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
159 minute1
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
160 seconds1
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
162 RtlUnicodeStringToAnsiString(&astring
, &CurrentProcess
->ImageName
, TRUE
);
164 wsprintf(buf
,"P%8d %8d %3d:%02d:%02d %3d:%02d:%02d ProcName: %s\n",
165 CurrentProcess
->UniqueProcessId
, CurrentProcess
->InheritedFromUniqueProcessId
,
166 hour
, minute
, seconds
, hour1
, minute1
, seconds1
,
168 WriteFile(stdout
, buf
, lstrlen(buf
), &r
, NULL
);
170 RtlFreeAnsiString(&astring
);
172 for (ti
= 0; ti
< CurrentProcess
->NumberOfThreads
; ti
++)
174 struct status
*statt
;
175 struct waitres
*waitt
;
176 char szWindowName
[30] = {" "};
178 ptime
= CurrentProcess
->TH
[ti
].KernelTime
;
179 thour
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
180 tmin
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
181 tsec
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
183 ptime
= CurrentProcess
->TH
[ti
].UserTime
;
184 thour1
= (ptime
.QuadPart
/ (10000000LL * 3600LL));
185 tmin1
= (ptime
.QuadPart
/ (10000000LL * 60LL)) % 60LL;
186 tsec1
= (ptime
.QuadPart
/ 10000000LL) % 60LL;
189 while (statt
->state
!= CurrentProcess
->TH
[ti
].ThreadState
&& statt
->state
>= 0)
193 while (waitt
->state
!= CurrentProcess
->TH
[ti
].WaitReason
&& waitt
->state
>= 0)
197 "t% %8d %3d:%02d:%02d %3d:%02d:%02d %s %s\n",
198 CurrentProcess
->TH
[ti
].ClientId
.UniqueThread
,
199 thour
, tmin
, tsec
, thour1
, tmin1
, tsec1
,
200 statt
->desc
, waitt
->desc
);
201 WriteFile(stdout
, buf1
, lstrlen(buf1
), &r
, NULL
);
203 EnumThreadWindows((DWORD
)CurrentProcess
->TH
[ti
].ClientId
.UniqueThread
,
204 (ENUMWINDOWSPROC
) EnumThreadProc
,
205 (LPARAM
)(LPTSTR
) szWindowName
);
207 CurrentProcess
= (PSYSTEM_PROCESS_INFORMATION
)((ULONG_PTR
)CurrentProcess
+
208 CurrentProcess
->NextEntryOffset
);