-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathsysinfo.c
352 lines (307 loc) · 12.7 KB
/
sysinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* PROJECT: Native Shell
* COPYRIGHT: LGPL; See LICENSE in the top level directory
* FILE: sysinfo.c
* DESCRIPTION: This module implements commands for displaying system information.
* DEVELOPERS: See CONTRIBUTORS.md in the top level directory
*/
#include "precomp.h"
NTSTATUS
RtlCliShutdown(VOID)
{
BOOLEAN Old;
// Get the shutdown privilege and shutdown the system
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownNoReboot);
}
NTSTATUS
RtlCliReboot(VOID)
{
BOOLEAN Old;
// Get the shutdown privilege and shutdown the system
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownReboot);
}
NTSTATUS
RtlCliPowerOff(VOID)
{
BOOLEAN Old;
// Get the shutdown privilege and shutdown the system
RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &Old);
return ZwShutdownSystem(ShutdownPowerOff);
}
NTSTATUS
RtlCliListDrivers(VOID)
{
PRTL_PROCESS_MODULES ModuleInfo;
PRTL_PROCESS_MODULE_INFORMATION ModuleEntry;
NTSTATUS Status;
ULONG Size = 1024*1024;
ULONG i;
// Allocate it
ModuleInfo = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, Size);
// Query the buffer
Status = NtQuerySystemInformation(SystemModuleInformation,
ModuleInfo,
Size,
NULL);
// Display Header
RtlCliDisplayString("*** ACTIVE MODULE LIST - DUMPING %d MODULES\n",
ModuleInfo->NumberOfModules);
// Now walk every module in it
for (i = 0; i < ModuleInfo->NumberOfModules; i++)
{
// Check if we've displayed 20
// BUGBUG: Should be natively handled by our display routines
if (i && !(i % 20))
{
// Hold for more input
RtlCliDisplayString("--- PRESS SPACE TO CONTINUE ---\n");
while (RtlCliGetChar(hKeyboard) != ' ')
;
}
// Get this entry
ModuleEntry = &ModuleInfo->Modules[i];
// Display basic data
RtlCliDisplayString("%s - Base: %p Size: 0x%lx\n",
ModuleEntry->FullPathName,
ModuleEntry->ImageBase,
ModuleEntry->ImageSize);
}
RtlFreeHeap(RtlGetProcessHeap(), 0, ModuleInfo);
// Return error code
return Status;
}
/*++
* @name RtlCliListProcesses
*
* The RtlCliListProcesses routine provides a way to list the current
* processes.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliListProcesses(VOID)
{
PSYSTEM_PROCESS_INFORMATION ModuleInfo;
NTSTATUS Status;
ULONG Size = 0x10000;
// Allocate a static buffer that should be large enough
ModuleInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
if (!ModuleInfo)
return STATUS_INSUFFICIENT_RESOURCES;
// Query the buffer
Status = NtQuerySystemInformation(SystemProcessInformation,
ModuleInfo,
Size,
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Display Header
RtlCliDisplayString("*** ACTIVE PROCESS LIST\n");
// Now walk every module in it
while (TRUE)
{
// Display basic data
RtlCliDisplayString("[%lx] %S - WS/PF/V:[%dK/%dK/%dK] Threads: %d\n",
ModuleInfo->UniqueProcessId,
ModuleInfo->ImageName.Buffer,
ModuleInfo->WorkingSetSize / 1024,
ModuleInfo->PagefileUsage / 1024,
ModuleInfo->VirtualSize / 1024,
ModuleInfo->NumberOfThreads);
// Break out if we're done
if (!ModuleInfo->NextEntryOffset)
break;
// Get next entry
ModuleInfo = (PSYSTEM_PROCESS_INFORMATION)((ULONG_PTR)ModuleInfo +
ModuleInfo->NextEntryOffset);
}
// Return error code
return Status;
}
/*++
* @name RtlCliDumpSysInfo
*
* The RtlCliDumpSysInfo routine queries a large amount of system information
* and displays it on screen.
*
* @param None.
*
* @return NTSTATUS
*
* @remarks Documentation for this routine needs to be completed.
*
*--*/
NTSTATUS
RtlCliDumpSysInfo(VOID)
{
NTSTATUS Status;
SYSTEM_BASIC_INFORMATION BasicInfo;
SYSTEM_PROCESSOR_INFORMATION ProcInfo;
SYSTEM_PERFORMANCE_INFORMATION PerfInfo;
SYSTEM_TIMEOFDAY_INFORMATION TimeInfo;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION ProcPerfInfo[2];
SYSTEM_FILECACHE_INFORMATION CacheInfo;
PKUSER_SHARED_DATA SharedData = (PKUSER_SHARED_DATA)USER_SHARED_DATA;
TIME_FIELDS BootTime, IdleTime, KernelTime, UserTime, DpcTime;
// Query basic system information
Status = NtQuerySystemInformation(SystemBasicInformation,
&BasicInfo,
sizeof(BasicInfo),
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Query basic processor information
Status = NtQuerySystemInformation(SystemProcessorInformation,
&ProcInfo,
sizeof(ProcInfo),
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Query basic system information
Status = NtQuerySystemInformation(SystemPerformanceInformation,
&PerfInfo,
sizeof(PerfInfo),
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Query basic system information
Status = NtQuerySystemInformation(SystemTimeOfDayInformation,
&TimeInfo,
sizeof(TimeInfo),
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Query basic system information
Status = NtQuerySystemInformation(SystemProcessorPerformanceInformation,
&ProcPerfInfo,
sizeof(ProcPerfInfo),
NULL);
if (!NT_SUCCESS(Status))
return Status;
// Display Header
// FIXME: Center it
RtlTimeToTimeFields(&TimeInfo.BootTime, &BootTime);
RtlCliDisplayString("Native shell running in %S booted on %02d-%02d-%02d "
"at %02d:%02d. CPUs: %d\n",
SharedData->NtSystemRoot,
BootTime.Day,
BootTime.Month,
BootTime.Year,
BootTime.Hour,
BootTime.Minute,
BasicInfo.NumberOfProcessors);
// Display System Flags
RtlCliDisplayString("Version: %x.%x. Debug Mode: %x. Safe Mode: %x "
"Product Type: %x. Suite Mask: %x\n",
SharedData->NtMajorVersion,
SharedData->NtMinorVersion,
SharedData->KdDebuggerEnabled,
SharedData->SafeBootMode,
SharedData->NtProductType,
SharedData->SuiteMask);
RtlCliDisplayString("-------------------------------------"
"-------------------------------------\n");
// Display CPU Information
RtlCliDisplayString("[CPU] %s Family %d Model %x Stepping %x. "
"Feature Bits: 0x%X NX: 0x%x\n",
(ProcInfo.ProcessorArchitecture ==
PROCESSOR_ARCHITECTURE_INTEL)
? "x86"
: "Unknown",
ProcInfo.ProcessorLevel,
ProcInfo.ProcessorRevision >> 8,
ProcInfo.ProcessorRevision & 0xFF,
ProcInfo.ProcessorFeatureBits,
SharedData->NXSupportPolicy);
// Display RAM Information
RtlCliDisplayString("[RAM] Page Size: %dKB. Physical Pages: 0x%X. "
"Total Physical RAM: %dKB\n",
BasicInfo.PageSize / 1024,
BasicInfo.NumberOfPhysicalPages,
BasicInfo.NumberOfPhysicalPages * PAGE_SIZE / 1024);
// Display User-Mode Virtual Memory Information
RtlCliDisplayString("[USR] User-Mode Range: 0x%08X-0x%X. "
"Allocation Granularity: %dKB\n",
BasicInfo.MinimumUserModeAddress,
BasicInfo.MaximumUserModeAddress,
BasicInfo.AllocationGranularity / 1024);
// Display System Virtual Memory Information
RtlCliDisplayString("[VRAM] Free: %dKB. Committed: %dKB. "
"Total: %dKB. Peak: %dKB\n",
PerfInfo.AvailablePages * PAGE_SIZE / 1024,
PerfInfo.CommittedPages * PAGE_SIZE / 1024,
PerfInfo.CommitLimit * PAGE_SIZE / 1024,
PerfInfo.PeakCommitment * PAGE_SIZE / 1024);
// Display Kernel Memory/Pool Information
RtlCliDisplayString("[KRNL] Paged: %dKB. Non-Paged: %dKB. "
"Drivers: %dKB Code: %dKB\n",
PerfInfo.PagedPoolPages * PAGE_SIZE / 1024,
PerfInfo.NonPagedPoolPages * PAGE_SIZE / 1024,
PerfInfo.TotalSystemDriverPages * PAGE_SIZE / 1024,
PerfInfo.TotalSystemCodePages * PAGE_SIZE / 1024);
// Check if we have two CPUs
if (BasicInfo.NumberOfProcessors > 1)
{
// Handle two CPU case by adding all of CPU 2's times into CPU 1's
// FIXME: This should be improved to support 2+ CPUs later
ProcPerfInfo[0].IdleTime.QuadPart +=
ProcPerfInfo[1].IdleTime.QuadPart;
ProcPerfInfo[0].KernelTime.QuadPart +=
ProcPerfInfo[1].KernelTime.QuadPart;
ProcPerfInfo[0].UserTime.QuadPart +=
ProcPerfInfo[1].UserTime.QuadPart;
ProcPerfInfo[0].DpcTime.QuadPart +=
ProcPerfInfo[1].DpcTime.QuadPart;
ProcPerfInfo[0].InterruptCount += ProcPerfInfo[1].InterruptCount;
}
// Convert all 64-bit times into a readable format
RtlTimeToTimeFields(&ProcPerfInfo[0].IdleTime, &IdleTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].KernelTime, &KernelTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].UserTime, &UserTime);
RtlTimeToTimeFields(&ProcPerfInfo[0].DpcTime, &DpcTime);
// Display System Times
RtlCliDisplayString("[TIME] Kernel: %02d:%02d:%02d. User: %02d:%02d:%02d. "
"DPC: %02d:%02d:%02d. Idle: %02d:%02d:%02d.\n",
KernelTime.Hour, KernelTime.Minute, KernelTime.Second,
UserTime.Hour, UserTime.Minute, UserTime.Second,
DpcTime.Hour, DpcTime.Minute, DpcTime.Second,
IdleTime.Hour, IdleTime.Minute, IdleTime.Second);
// Display Core Performance Information
RtlCliDisplayString("[PERF] INTs: %d. SysCalls: %d. PFs: %d. "
"Ctx Switches: %d\n",
ProcPerfInfo[0].InterruptCount,
PerfInfo.SystemCalls,
PerfInfo.PageFaultCount,
PerfInfo.ContextSwitches);
// Display I/O Information
RtlCliDisplayString("[I/O] Reads: %d/%I64dKB. Writes: %d/%I64dKB. "
"Others: %d/%I64dKB\n",
PerfInfo.IoReadOperationCount,
PerfInfo.IoReadTransferCount.QuadPart / 1024,
PerfInfo.IoWriteOperationCount,
PerfInfo.IoWriteTransferCount.QuadPart / 1024,
PerfInfo.IoOtherOperationCount,
PerfInfo.IoOtherTransferCount.QuadPart / 1024);
// Display FileSystem Cache Information
Status = NtQuerySystemInformation(SystemFileCacheInformation,
&CacheInfo,
sizeof(CacheInfo),
NULL);
if (NT_SUCCESS(Status))
{
RtlCliDisplayString("[CACHE] Size: %dKB. Peak: %dKB. "
"Min WS: %dKB. Max WS: %dKB\n",
CacheInfo.CurrentSize / 1024,
CacheInfo.PeakSize / 1024,
CacheInfo.MinimumWorkingSet,
CacheInfo.MaximumWorkingSet);
}
return STATUS_SUCCESS;
}