Refactor makeCommandStr

This commit is contained in:
Benny Baumann 2020-12-12 12:21:22 +01:00 committed by BenBE
parent 07a4657a47
commit fbec3e4005
3 changed files with 144 additions and 151 deletions

View File

@ -241,6 +241,11 @@ static inline bool Process_isChildOf(const Process* this, pid_t pid) {
return pid == Process_getParentPid(this); return pid == Process_getParentPid(this);
} }
#define CMDLINE_HIGHLIGHT_FLAG_SEPARATOR 0x00000001
#define CMDLINE_HIGHLIGHT_FLAG_BASENAME 0x00000002
#define CMDLINE_HIGHLIGHT_FLAG_COMM 0x00000004
#define CMDLINE_HIGHLIGHT_FLAG_DELETED 0x00000008
#define ONE_K 1024UL #define ONE_K 1024UL
#define ONE_M (ONE_K * ONE_K) #define ONE_M (ONE_K * ONE_K)
#define ONE_G (ONE_M * ONE_K) #define ONE_G (ONE_M * ONE_K)

View File

@ -223,7 +223,7 @@ static bool findCommInCmdline(const char *comm, const char *cmdline, int cmdline
if (*token) { if (*token) {
do { do {
++token; ++token;
} while ('\n' == *token); } while (*token && '\n' == *token);
} }
} }
return false; return false;
@ -268,14 +268,15 @@ static int matchCmdlinePrefixWithExeSuffix(const char *cmdline, int cmdlineBaseO
int i, j; int i, j;
/* reverse match the cmdline prefix and exe suffix */ /* reverse match the cmdline prefix and exe suffix */
for (i = cmdlineBaseOffset - 1, j = exeBaseOffset - 1; for (i = cmdlineBaseOffset - 1, j = exeBaseOffset - 1;
i >= 0 && cmdline[i] == exe[j]; --i, --j) i >= 0 && j >= 0 && cmdline[i] == exe[j]; --i, --j)
; ;
/* full match, with exe suffix being a valid relative path */ /* full match, with exe suffix being a valid relative path */
if (i < 0 && exe[j] == '/') { if (i < 0 && j >= 0 && exe[j] == '/') {
return matchLen; return matchLen;
} }
} }
} }
/* Try to find the previous potential cmdlineBaseOffset - it would be /* Try to find the previous potential cmdlineBaseOffset - it would be
* preceded by '/' or nothing, and delimited by ' ' or '\n' */ * preceded by '/' or nothing, and delimited by ' ' or '\n' */
for (delimFound = false, cmdlineBaseOffset -= 2; cmdlineBaseOffset > 0; --cmdlineBaseOffset) { for (delimFound = false, cmdlineBaseOffset -= 2; cmdlineBaseOffset > 0; --cmdlineBaseOffset) {
@ -310,11 +311,12 @@ returned by LinuxProcess_getCommandStr() for searching, sorting and filtering.
void LinuxProcess_makeCommandStr(Process* this) { void LinuxProcess_makeCommandStr(Process* this) {
LinuxProcess *lp = (LinuxProcess *)this; LinuxProcess *lp = (LinuxProcess *)this;
LinuxProcessMergedCommand *mc = &lp->mergedCommand; LinuxProcessMergedCommand *mc = &lp->mergedCommand;
const Settings* settings = this->settings;
bool showMergedCommand = this->settings->showMergedCommand; bool showMergedCommand = settings->showMergedCommand;
bool showProgramPath = this->settings->showProgramPath; bool showProgramPath = settings->showProgramPath;
bool searchCommInCmdline = this->settings->findCommInCmdline; bool searchCommInCmdline = settings->findCommInCmdline;
bool stripExeFromCmdline = this->settings->stripExeFromCmdline; bool stripExeFromCmdline = settings->stripExeFromCmdline;
/* lp->mergedCommand.str needs updating only if its state or contents changed. /* lp->mergedCommand.str needs updating only if its state or contents changed.
* Its content is based on the fields cmdline, comm, and exe. */ * Its content is based on the fields cmdline, comm, and exe. */
@ -353,16 +355,37 @@ void LinuxProcess_makeCommandStr(Process* this) {
mc->commChanged = false; mc->commChanged = false;
mc->exeChanged = false; mc->exeChanged = false;
/* Clear any separators */ /* Reset all locations that need extra handling when actually displaying */
mc->sep1 = 0; mc->highlightCount = 0;
mc->sep2 = 0; memset(mc->highlights, 0, sizeof(mc->highlights));
/* Clear any highlighting locations */ size_t mbMismatch = 0;
mc->baseStart = 0; #define WRITE_HIGHLIGHT(_offset, _length, _attr, _flags) \
mc->baseEnd = 0; do { \
mc->commStart = 0; /* Check if we still have capacity */ \
mc->commEnd = 0; assert(mc->highlightCount < ARRAYSIZE(mc->highlights)); \
if (mc->highlightCount >= ARRAYSIZE(mc->highlights)) \
continue; \
\
mc->highlights[mc->highlightCount].offset = str - strStart + _offset - mbMismatch; \
mc->highlights[mc->highlightCount].length = _length; \
mc->highlights[mc->highlightCount].attr = _attr; \
mc->highlights[mc->highlightCount].flags = _flags; \
mc->highlightCount++; \
} while (0)
#define WRITE_SEPARATOR \
do { \
WRITE_HIGHLIGHT(0, 1, CRT_colors[FAILED_READ], CMDLINE_HIGHLIGHT_FLAG_SEPARATOR); \
mbMismatch += SEPARATOR_LEN - 1; \
str = stpcpy(str, SEPARATOR); \
} while (0)
const int baseAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_BASENAME] : CRT_colors[PROCESS_BASENAME];
const int commAttr = Process_isThread(this) ? CRT_colors[PROCESS_THREAD_COMM] : CRT_colors[PROCESS_COMM];
const int delAttr = CRT_colors[FAILED_READ];
/* Establish some shortcuts to data we need */
const char *cmdline = this->comm; const char *cmdline = this->comm;
const char *procExe = lp->procExe; const char *procExe = lp->procExe;
const char *procComm = lp->procComm; const char *procComm = lp->procComm;
@ -385,29 +408,19 @@ void LinuxProcess_makeCommandStr(Process* this) {
if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */ if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */
if (showMergedCommand && !procExe && procComm && strlen(procComm)) { /* Prefix column with comm */ if (showMergedCommand && !procExe && procComm && strlen(procComm)) { /* Prefix column with comm */
if (strncmp(cmdline + cmdlineBasenameOffset, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) { if (strncmp(cmdline + cmdlineBasenameOffset, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) {
mc->commStart = 0; WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
mc->commEnd = strlen(procComm);
str = stpcpy(str, procComm); str = stpcpy(str, procComm);
mc->sep1 = str - strStart; WRITE_SEPARATOR;
str = stpcpy(str, SEPARATOR);
} }
} }
if (showProgramPath) { if (showProgramPath) {
WRITE_HIGHLIGHT(cmdlineBasenameOffset, cmdlineBasenameEnd - cmdlineBasenameOffset, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
(void)stpcpyWithNewlineConversion(str, cmdline); (void)stpcpyWithNewlineConversion(str, cmdline);
mc->baseStart = cmdlineBasenameOffset;
mc->baseEnd = cmdlineBasenameEnd;
} else { } else {
WRITE_HIGHLIGHT(0, cmdlineBasenameEnd - cmdlineBasenameOffset, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
(void)stpcpyWithNewlineConversion(str, cmdline + cmdlineBasenameOffset); (void)stpcpyWithNewlineConversion(str, cmdline + cmdlineBasenameOffset);
mc->baseStart = 0;
mc->baseEnd = cmdlineBasenameEnd - cmdlineBasenameOffset;
}
if (mc->sep1) {
mc->baseStart += str - strStart - SEPARATOR_LEN + 1;
mc->baseEnd += str - strStart - SEPARATOR_LEN + 1;
} }
return; return;
@ -420,142 +433,111 @@ void LinuxProcess_makeCommandStr(Process* this) {
assert(exeBasenameOffset >= 0); assert(exeBasenameOffset >= 0);
assert(exeBasenameOffset <= (int)strlen(procExe)); assert(exeBasenameOffset <= (int)strlen(procExe));
/* Start with copying exe */ bool haveCommInExe = false;
if (showProgramPath) { if (procExe && procComm) {
str = stpcpy(str, procExe); haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0;
mc->baseStart = exeBasenameOffset;
mc->baseEnd = exeLen;
} else {
str = stpcpy(str, procExe + exeBasenameOffset);
mc->baseStart = 0;
mc->baseEnd = exeBasenameLen;
} }
mc->sep1 = 0; /* Start with copying exe */
mc->sep2 = 0; if (showProgramPath) {
if (haveCommInExe)
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
if (lp->procExeDeleted)
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, delAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
str = stpcpy(str, procExe);
} else {
if (haveCommInExe)
WRITE_HIGHLIGHT(0, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
WRITE_HIGHLIGHT(0, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
if (lp->procExeDeleted)
WRITE_HIGHLIGHT(0, exeBasenameLen, delAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
str = stpcpy(str, procExe + exeBasenameOffset);
}
bool haveCommInCmdline = false;
int commStart = 0; int commStart = 0;
int commEnd = 0; int commEnd = 0;
bool commInCmdline = false;
/* Try to match procComm with procExe's basename: This is reliable (predictable) */ /* Try to match procComm with procExe's basename: This is reliable (predictable) */
if (strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0) { if (searchCommInCmdline) {
commStart = mc->baseStart;
commEnd = mc->baseEnd;
} else if (searchCommInCmdline) {
/* commStart/commEnd will be adjusted later along with cmdline */ /* commStart/commEnd will be adjusted later along with cmdline */
commInCmdline = findCommInCmdline(procComm, cmdline, cmdlineBasenameOffset, &commStart, &commEnd); haveCommInCmdline = findCommInCmdline(procComm, cmdline, cmdlineBasenameOffset, &commStart, &commEnd);
} }
int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameOffset, procExe, exeBasenameOffset, exeBasenameLen); int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameOffset, procExe, exeBasenameOffset, exeBasenameLen);
/* Note: commStart, commEnd are offsets into RichString. But the multibyte bool haveCommField = false;
* separator (with size SEPARATOR_LEN) has size 1 in RichString. The offset
* adjustments below reflect this. */ if (!haveCommInExe && !haveCommInCmdline && procComm) {
if (commEnd) { WRITE_SEPARATOR;
mc->unmatchedExe = !matchLen; WRITE_HIGHLIGHT(0, strlen(procComm), commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
str = stpcpy(str, procComm);
haveCommField = true;
}
if (matchLen) { if (matchLen) {
/* strip the matched exe prefix */ /* strip the matched exe prefix */
cmdline += matchLen; cmdline += matchLen;
if (commInCmdline) { commStart -= matchLen;
commStart += str - strStart - matchLen; commEnd -= matchLen;
commEnd += str - strStart - matchLen;
} }
} else {
if (!matchLen || (haveCommField && *cmdline)) {
/* cmdline will be a separate field */ /* cmdline will be a separate field */
mc->sep1 = str - strStart; WRITE_SEPARATOR;
str = stpcpy(str, SEPARATOR);
if (commInCmdline) {
commStart += str - strStart - SEPARATOR_LEN + 1;
commEnd += str - strStart - SEPARATOR_LEN + 1;
}
} }
mc->separateComm = false; /* procComm merged */ if (!haveCommInExe && haveCommInCmdline && !haveCommField)
} else { WRITE_HIGHLIGHT(commStart, commEnd - commStart, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
mc->sep1 = str - strStart;
str = stpcpy(str, SEPARATOR);
commStart = str - strStart - SEPARATOR_LEN + 1;
str = stpcpy(str, procComm);
commEnd = str - strStart - SEPARATOR_LEN + 1; /* or commStart + strlen(procComm) */
mc->unmatchedExe = !matchLen;
if (matchLen) {
if (stripExeFromCmdline) {
cmdline += matchLen;
}
}
if (*cmdline) {
mc->sep2 = str - strStart - SEPARATOR_LEN + 1;
str = stpcpy(str, SEPARATOR);
}
mc->separateComm = true; /* procComm a separate field */
}
/* Display cmdline if it hasn't been consumed by procExe */ /* Display cmdline if it hasn't been consumed by procExe */
if (*cmdline) { if (*cmdline) {
(void) stpcpyWithNewlineConversion(str, cmdline); (void) stpcpyWithNewlineConversion(str, cmdline);
} }
mc->commStart = commStart; #undef WRITE_SEPARATOR
mc->commEnd = commEnd; #undef WRITE_HIGHLIGHT
} }
static void LinuxProcess_writeCommand(const Process* this, int attr, int baseAttr, RichString* str) { static void LinuxProcess_writeCommand(const Process* this, int attr, int baseAttr, RichString* str) {
(void)baseAttr;
const LinuxProcess *lp = (const LinuxProcess *)this; const LinuxProcess *lp = (const LinuxProcess *)this;
const LinuxProcessMergedCommand *mc = &lp->mergedCommand; const LinuxProcessMergedCommand *mc = &lp->mergedCommand;
int strStart = RichString_size(str); int strStart = RichString_size(str);
int baseStart = strStart + lp->mergedCommand.baseStart; // int commAttr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM];
int baseEnd = strStart + lp->mergedCommand.baseEnd;
int commStart = strStart + lp->mergedCommand.commStart;
int commEnd = strStart + lp->mergedCommand.commEnd;
int commAttr = CRT_colors[Process_isUserlandThread(this) ? PROCESS_THREAD_COMM : PROCESS_COMM]; const bool highlightBaseName = this->settings->highlightBaseName;
const bool highlightSeparator = true;
bool highlightBaseName = this->settings->highlightBaseName; const bool highlightDeleted = true;
if(lp->procExeDeleted)
baseAttr = CRT_colors[FAILED_READ];
RichString_appendWide(str, attr, lp->mergedCommand.str); RichString_appendWide(str, attr, lp->mergedCommand.str);
if (lp->mergedCommand.commEnd) { for (size_t i = 0, hlCount = CLAMP(mc->highlightCount, 0, ARRAYSIZE(mc->highlights)); i < hlCount; i++)
if (!lp->mergedCommand.separateComm && commStart == baseStart && highlightBaseName) { {
/* If it was matched with procExe's basename, make it bold if needed */ const LinuxProcessCmdlineHighlight* hl = &mc->highlights[i];
if (commEnd > baseEnd) {
RichString_setAttrn(str, A_BOLD | baseAttr, baseStart, baseEnd - baseStart);
RichString_setAttrn(str, A_BOLD | commAttr, baseEnd, commEnd - baseEnd);
} else if (commEnd < baseEnd) {
RichString_setAttrn(str, A_BOLD | commAttr, commStart, commEnd - commStart);
RichString_setAttrn(str, A_BOLD | baseAttr, commEnd, baseEnd - commEnd);
} else {
// Actually should be highlighted commAttr, but marked baseAttr to reduce visual noise
RichString_setAttrn(str, A_BOLD | baseAttr, commStart, commEnd - commStart);
}
baseStart = baseEnd; if (!hl->length)
} else { continue;
RichString_setAttrn(str, commAttr, commStart, commEnd - commStart);
}
}
if (baseStart < baseEnd && highlightBaseName) { if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_SEPARATOR)
RichString_setAttrn(str, baseAttr, baseStart, baseEnd - baseStart); if (!highlightSeparator)
} continue;
if (mc->sep1) if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_BASENAME)
RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep1, 1); if (!highlightBaseName)
if (mc->sep2) continue;
RichString_setAttrn(str, CRT_colors[FAILED_READ], strStart + mc->sep2, 1);
if (hl->flags & CMDLINE_HIGHLIGHT_FLAG_DELETED)
if (!highlightDeleted)
continue;
RichString_setAttrn(str, hl->attr, strStart + hl->offset, hl->length);
}
} }
static void LinuxProcess_writeCommandField(const Process *this, RichString *str, char *buffer, int n, int attr) { static void LinuxProcess_writeCommandField(const Process *this, RichString *str, char *buffer, int n, int attr) {

View File

@ -31,28 +31,34 @@ in the source distribution for its full text.
#define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000 #define PROCESS_FLAG_LINUX_DELAYACCT 0x00040000
/* Holds information about regions of the cmdline that should be
* highlighted (e.g. program basename, delimiter, comm). */
typedef struct LinuxProcessCmdlineHighlight_
{
size_t offset; /* first character to highlight */
size_t length; /* How many characters to highlight, zero if unused */
int attr; /* The attributes used to highlight */
int flags; /* Special flags used for selective highlighting, zero for always */
} LinuxProcessCmdlineHighlight;
/* LinuxProcessMergedCommand is populated by LinuxProcess_makeCommandStr: It /* LinuxProcessMergedCommand is populated by LinuxProcess_makeCommandStr: It
* contains the merged Command string, and the information needed by * contains the merged Command string, and the information needed by
* LinuxProcess_writeCommand to color the string. str will be NULL for kernel * LinuxProcess_writeCommand to color the string. str will be NULL for kernel
* threads and zombies */ * threads and zombies */
typedef struct LinuxProcessMergedCommand_ { typedef struct LinuxProcessMergedCommand_ {
char *str; /* merged Command string */ char *str; /* merged Command string */
int maxLen; /* maximum expected length of Command string */ size_t maxLen; /* maximum expected length of Command string */
int baseStart; /* basename's start offset */ size_t highlightCount; /* how many portions of cmdline to highlight */
int baseEnd; /* basename's end offset */ LinuxProcessCmdlineHighlight highlights[8]; /* which portions of cmdline to highlight */
int commStart; /* comm's start offset */ bool separateComm : 1; /* whether comm is a separate field */
int commEnd; /* comm's end offset */ bool unmatchedExe : 1; /* whether exe matched with cmdline */
int sep1; /* first field separator, used if non-zero */ bool cmdlineChanged : 1; /* whether cmdline changed */
int sep2; /* second field separator, used if non-zero */ bool exeChanged : 1; /* whether exe changed */
bool separateComm; /* whether comm is a separate field */ bool commChanged : 1; /* whether comm changed */
bool unmatchedExe; /* whether exe matched with cmdline */ bool prevMergeSet : 1; /* whether showMergedCommand was set */
bool cmdlineChanged; /* whether cmdline changed */ bool prevPathSet : 1; /* whether showProgramPath was set */
bool exeChanged; /* whether exe changed */ bool prevCommSet : 1; /* whether findCommInCmdline was set */
bool commChanged; /* whether comm changed */ bool prevCmdlineSet : 1; /* whether stripExeFromCmdline was set */
bool prevMergeSet; /* whether showMergedCommand was set */
bool prevPathSet; /* whether showProgramPath was set */
bool prevCommSet; /* whether findCommInCmdline was set */
bool prevCmdlineSet; /* whether findCommInCmdline was set */
} LinuxProcessMergedCommand; } LinuxProcessMergedCommand;
typedef struct LinuxProcess_ { typedef struct LinuxProcess_ {