From aa8552ba88e139fa1da65b1870de2839f33b8404 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sun, 18 Apr 2021 19:25:56 +0200 Subject: [PATCH] Move PROC_COMM/PROC_EXE column handling to global Process implementation --- Process.c | 41 +++++++++++++++++++++++++++++++++++++++++ Process.h | 2 ++ linux/LinuxProcess.c | 40 ---------------------------------------- linux/ProcessField.h | 2 -- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/Process.c b/Process.c index e38ea21c..8568dd23 100644 --- a/Process.c +++ b/Process.c @@ -35,6 +35,9 @@ in the source distribution for its full text. #endif +/* Used to identify kernel threads in Comm and Exe columns */ +static const char *const kthreadID = "KTHREAD"; + static uid_t Process_getuid = (uid_t)-1; int Process_pidDigits = 7; @@ -745,6 +748,34 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field Process_writeCommand(this, attr, baseattr, str); return; } + case PROC_COMM: { + const char* procComm; + if (this->procComm) { + attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM]; + procComm = this->procComm; + } else { + attr = CRT_colors[PROCESS_SHADOW]; + procComm = Process_isKernelThread(this) ? kthreadID : "N/A"; + } + + Process_printLeftAlignedField(str, attr, procComm, TASK_COMM_LEN - 1); + return; + } + case PROC_EXE: { + const char* procExe; + if (this->procExe) { + attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME]; + if (this->procExeDeleted) + attr = CRT_colors[FAILED_READ]; + procExe = this->procExe + this->procExeBasenameOffset; + } else { + attr = CRT_colors[PROCESS_SHADOW]; + procExe = Process_isKernelThread(this) ? kthreadID : "N/A"; + } + + Process_printLeftAlignedField(str, attr, procExe, TASK_COMM_LEN - 1); + return; + } case MAJFLT: Process_printCount(str, this->majflt, coloring); return; case MINFLT: Process_printCount(str, this->minflt, coloring); return; case M_RESIDENT: Process_printKBytes(str, this->m_resident, coloring); return; @@ -1031,6 +1062,16 @@ int Process_compareByKey_Base(const Process* p1, const Process* p2, ProcessField return SPACESHIP_NUMBER(p1->m_resident, p2->m_resident); case COMM: return SPACESHIP_NULLSTR(Process_getCommand(p1), Process_getCommand(p2)); + case PROC_COMM: { + const char *comm1 = p1->procComm ? p1->procComm : (Process_isKernelThread(p1) ? kthreadID : ""); + const char *comm2 = p2->procComm ? p2->procComm : (Process_isKernelThread(p2) ? kthreadID : ""); + return SPACESHIP_NULLSTR(comm1, comm2); + } + case PROC_EXE: { + const char *exe1 = p1->procExe ? (p1->procExe + p1->procExeBasenameOffset) : (Process_isKernelThread(p1) ? kthreadID : ""); + const char *exe2 = p2->procExe ? (p2->procExe + p2->procExeBasenameOffset) : (Process_isKernelThread(p2) ? kthreadID : ""); + return SPACESHIP_NULLSTR(exe1, exe2); + } case MAJFLT: return SPACESHIP_NUMBER(p1->majflt, p2->majflt); case MINFLT: diff --git a/Process.h b/Process.h index 487efcc7..9aa04b3e 100644 --- a/Process.h +++ b/Process.h @@ -46,6 +46,8 @@ typedef enum ProcessField_ { NLWP = 51, TGID = 52, PERCENT_NORM_CPU = 53, + PROC_COMM = 124, + PROC_EXE = 125, /* Platform specific fields, defined in ${platform}/ProcessField.h */ PLATFORM_PROCESS_FIELDS diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index 7d32a6d3..72127180 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -29,9 +29,6 @@ in the source distribution for its full text. int pageSize; int pageSizeKB; -/* Used to identify kernel threads in Comm and Exe columns */ -static const char *const kthreadID = "KTHREAD"; - const ProcessFieldData Process_fields[LAST_PROCESSFIELD] = { [0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, }, [PID] = { .name = "PID", .title = "PID", .description = "Process/thread ID", .flags = 0, .pidColumn = true, }, @@ -315,33 +312,6 @@ static void LinuxProcess_writeField(const Process* this, RichString* str, Proces } return; } - case PROC_COMM: { - const char* procComm; - if (this->procComm) { - attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM]; - procComm = this->procComm; - } else { - attr = CRT_colors[PROCESS_SHADOW]; - procComm = Process_isKernelThread(this) ? kthreadID : "N/A"; - } - /* 15 being (TASK_COMM_LEN - 1) */ - Process_printLeftAlignedField(str, attr, procComm, 15); - return; - } - case PROC_EXE: { - const char* procExe; - if (this->procExe) { - attr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_BASENAME : PROCESS_BASENAME]; - if (this->procExeDeleted) - attr = CRT_colors[FAILED_READ]; - procExe = this->procExe + this->procExeBasenameOffset; - } else { - attr = CRT_colors[PROCESS_SHADOW]; - procExe = Process_isKernelThread(this) ? kthreadID : "N/A"; - } - Process_printLeftAlignedField(str, attr, procExe, 15); - return; - } case CWD: { const char* cwd; if (!lp->cwd) { @@ -447,16 +417,6 @@ static int LinuxProcess_compareByKey(const Process* v1, const Process* v2, Proce return SPACESHIP_NUMBER(p1->ctxt_diff, p2->ctxt_diff); case SECATTR: return SPACESHIP_NULLSTR(p1->secattr, p2->secattr); - case PROC_COMM: { - const char *comm1 = v1->procComm ? v1->procComm : (Process_isKernelThread(v1) ? kthreadID : ""); - const char *comm2 = v2->procComm ? v2->procComm : (Process_isKernelThread(v2) ? kthreadID : ""); - return strcmp(comm1, comm2); - } - case PROC_EXE: { - const char *exe1 = v1->procExe ? (v1->procExe + v1->procExeBasenameOffset) : (Process_isKernelThread(v1) ? kthreadID : ""); - const char *exe2 = v2->procExe ? (v2->procExe + v2->procExeBasenameOffset) : (Process_isKernelThread(v2) ? kthreadID : ""); - return strcmp(exe1, exe2); - } case CWD: return SPACESHIP_NULLSTR(p1->cwd, p2->cwd); default: diff --git a/linux/ProcessField.h b/linux/ProcessField.h index 6e2eff38..a4e03009 100644 --- a/linux/ProcessField.h +++ b/linux/ProcessField.h @@ -44,8 +44,6 @@ in the source distribution for its full text. M_PSSWP = 121, \ CTXT = 122, \ SECATTR = 123, \ - PROC_COMM = 124, \ - PROC_EXE = 125, \ CWD = 126, \ // End of list