better ps
This commit is contained in:
1 parent
325149006c
commit
c1a291c4c9
8 files changed
+228
-26
No files matched your search
@@ -5,10 +5,7 @@
|
||||
#include <ntlite/syscall.h>
|
||||
|
||||
#define MAX_CMD 256
|
||||
#define MAX_PIDS 64 // Maximum number of PIDs we will fetch at once
|
||||
|
||||
// External Syscall Prototypes
|
||||
uint32_t SysGetProcessList(uint32_t* pidsOut, size_t maxCount, size_t* countOut);
|
||||
#define MAX_PROCS 64 // Maximum number of processes we will fetch at once
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Output helpers
|
||||
@@ -64,6 +61,34 @@ static void printUInt64(uint64_t n) {
|
||||
while (i > 0) { char c[2] = { buf[--i], '\0' }; print(c); }
|
||||
}
|
||||
|
||||
static uint32_t u64Digits(uint64_t n) {
|
||||
uint32_t digits = 1;
|
||||
while (n >= 10u) { n /= 10u; digits++; }
|
||||
return digits;
|
||||
}
|
||||
|
||||
static void printSpaces(size_t count) {
|
||||
while (count--) print(" ");
|
||||
}
|
||||
|
||||
static void printUIntWidth(uint32_t n, size_t width) {
|
||||
uint32_t digits = u64Digits(n);
|
||||
if (width > digits) printSpaces(width - digits);
|
||||
printUInt(n);
|
||||
}
|
||||
|
||||
static void printUInt64Width(uint64_t n, size_t width) {
|
||||
uint32_t digits = u64Digits(n);
|
||||
if (width > digits) printSpaces(width - digits);
|
||||
printUInt64(n);
|
||||
}
|
||||
|
||||
static void printStringWidth(const char* s, size_t width) {
|
||||
size_t len = s ? strlen(s) : 0;
|
||||
if (s) print(s);
|
||||
if (width > len) printSpaces(width - len);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Line editor
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -96,7 +121,7 @@ static void cmd_help() {
|
||||
" help - Show this list\n"
|
||||
" mem - Show memory usage\n"
|
||||
" stats - Show scheduler/CPU/PMM telemetry\n"
|
||||
" ps - List running process IDs\n"
|
||||
" ps - List processes\n"
|
||||
" dir [path] - List directory contents\n"
|
||||
" cd <path> - Change directory\n"
|
||||
" type <file> - Display file contents\n"
|
||||
@@ -218,30 +243,71 @@ static void cmd_mem(void) {
|
||||
print("\n");
|
||||
}
|
||||
|
||||
static const char* processStateName(uint32_t state) {
|
||||
switch (state) {
|
||||
case SYS_PROCESS_STATE_RUNNING: return "run";
|
||||
case SYS_PROCESS_STATE_TERMINATING: return "term";
|
||||
case SYS_PROCESS_STATE_EXITED: return "exit";
|
||||
default: return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void cmd_ps(void) {
|
||||
uint32_t pids[MAX_PIDS];
|
||||
SYS_PROCESS_INFO procs[MAX_PROCS];
|
||||
size_t count = 0;
|
||||
|
||||
uint32_t status = SysGetProcessList(pids, MAX_PIDS, &count);
|
||||
|
||||
uint32_t status = SysGetProcessList(procs, MAX_PROCS, &count);
|
||||
if (status != 0) {
|
||||
print("ps: failed to retrieve process list\n");
|
||||
return;
|
||||
}
|
||||
|
||||
print("Running Processes (Count: ");
|
||||
print("Processes (Count: ");
|
||||
printUInt((uint32_t)count);
|
||||
print("):\n");
|
||||
|
||||
size_t displayCount = count < MAX_PIDS ? count : MAX_PIDS;
|
||||
print(" PID State Thr Hnd Mod UserKiB ImageKiB Flags Name\n");
|
||||
|
||||
size_t displayCount = count < MAX_PROCS ? count : MAX_PROCS;
|
||||
for (size_t i = 0; i < displayCount; i++) {
|
||||
print(" PID: ");
|
||||
printUInt(pids[i]);
|
||||
SYS_PROCESS_INFO* p = &procs[i];
|
||||
char flags[5];
|
||||
size_t flagLen = 0;
|
||||
|
||||
if (p->Flags & SYS_PROCESS_FLAG_SYSTEM) flags[flagLen++] = 'S';
|
||||
if (p->Flags & SYS_PROCESS_FLAG_CURRENT) flags[flagLen++] = '*';
|
||||
if (p->Flags & SYS_PROCESS_FLAG_TERMINATING) flags[flagLen++] = 'T';
|
||||
if (p->Flags & SYS_PROCESS_FLAG_EXITED) flags[flagLen++] = 'X';
|
||||
if (flagLen == 0) flags[flagLen++] = '-';
|
||||
flags[flagLen] = '\0';
|
||||
|
||||
print(" ");
|
||||
printUIntWidth(p->ProcessId, 3);
|
||||
print(" ");
|
||||
printStringWidth(processStateName(p->State), 5);
|
||||
print(" ");
|
||||
printUIntWidth(p->ThreadCount, 3);
|
||||
print(" ");
|
||||
printUIntWidth(p->HandleCount, 3);
|
||||
print(" ");
|
||||
printUIntWidth(p->ModuleCount, 3);
|
||||
print(" ");
|
||||
printUInt64Width((uint64_t)p->UserCommittedBytes / 1024u, 7);
|
||||
print(" ");
|
||||
printUInt64Width((uint64_t)p->ImageSize / 1024u, 8);
|
||||
print(" ");
|
||||
printStringWidth(flags, 5);
|
||||
print(" ");
|
||||
print(p->Name);
|
||||
if (p->CurrentDirectory[0]) {
|
||||
print(" cwd=");
|
||||
print(p->CurrentDirectory);
|
||||
}
|
||||
print("\n");
|
||||
}
|
||||
|
||||
if (count > MAX_PIDS) {
|
||||
if (count > MAX_PROCS) {
|
||||
print(" ... and ");
|
||||
printUInt((uint32_t)(count - MAX_PIDS));
|
||||
printUInt((uint32_t)(count - MAX_PROCS));
|
||||
print(" more processes.\n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user