Skip to content

Commit a753894

Browse files
committed
Improve "comm" string highlighting in Process_makeCommandStr()
Add a special case when the process "comm" string is found in "cmdline", but at the starting basename position that would be stripped by "stripExeFromCmdline" setting. This special case will now highlight the basename of "exe" string as well as the first token of "cmdline" (as the two strings are concatenated together when displaying). Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 463ee6f commit a753894

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

Process.c

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,48 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
375375
return;
376376
}
377377

378+
int commLen = 0;
379+
378380
bool haveCommInExe = false;
379381
if (procExe && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
380382
haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0;
381383
}
384+
if (haveCommInExe) {
385+
commLen = exeBasenameLen;
386+
}
387+
388+
bool haveCommInCmdline = false;
389+
int commStart = 0;
390+
391+
if (!haveCommInExe && this->cmdline && procComm && searchCommInCmdline && (!Process_isUserlandThread(this) || showThreadNames)) {
392+
haveCommInCmdline = findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commLen);
393+
}
394+
395+
if (!stripExeFromCmdline) {
396+
matchLen = 0;
397+
}
398+
if (matchLen) {
399+
/* strip the matched exe prefix */
400+
cmdline += matchLen;
401+
402+
if (haveCommInCmdline) {
403+
if (commStart == cmdlineBasenameStart) {
404+
haveCommInExe = true;
405+
haveCommInCmdline = false;
406+
commStart = 0;
407+
} else {
408+
assert(commStart >= matchLen);
409+
commStart -= matchLen;
410+
}
411+
}
412+
}
382413

383414
/* Start with copying exe */
384415
if (showProgramPath) {
385416
if (shadowDistPathPrefix)
386417
CHECK_AND_MARK_DIST_PATH_PREFIXES(procExe);
387418
if (haveCommInExe)
388-
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
419+
WRITE_HIGHLIGHT(exeBasenameOffset, commLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
389420
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
390421
if (this->procExeDeleted)
391422
WRITE_HIGHLIGHT(exeBasenameOffset, exeBasenameLen, delExeAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
@@ -394,7 +425,7 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
394425
str = stpcpy(str, procExe);
395426
} else {
396427
if (haveCommInExe)
397-
WRITE_HIGHLIGHT(0, exeBasenameLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
428+
WRITE_HIGHLIGHT(0, commLen, commAttr, CMDLINE_HIGHLIGHT_FLAG_COMM);
398429
WRITE_HIGHLIGHT(0, exeBasenameLen, baseAttr, CMDLINE_HIGHLIGHT_FLAG_BASENAME);
399430
if (this->procExeDeleted)
400431
WRITE_HIGHLIGHT(0, exeBasenameLen, delExeAttr, CMDLINE_HIGHLIGHT_FLAG_DELETED);
@@ -403,16 +434,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
403434
str = stpcpy(str, procExe + exeBasenameOffset);
404435
}
405436

406-
bool haveCommInCmdline = false;
407-
int commStart = 0;
408-
int commLen = 0;
409-
410-
/* Try to match procComm with procExe's basename: This is reliable (predictable) */
411-
if (searchCommInCmdline) {
412-
/* commStart/commLen will be adjusted later along with cmdline */
413-
haveCommInCmdline = (!Process_isUserlandThread(this) || showThreadNames) && findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commLen);
414-
}
415-
416437
bool haveCommField = false;
417438

418439
if (!haveCommInExe && !haveCommInCmdline && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
@@ -422,17 +443,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
422443
haveCommField = true;
423444
}
424445

425-
if (matchLen) {
426-
if (stripExeFromCmdline) {
427-
/* strip the matched exe prefix */
428-
cmdline += matchLen;
429-
430-
commStart -= matchLen;
431-
} else {
432-
matchLen = 0;
433-
}
434-
}
435-
436446
if (!matchLen || (haveCommField && *cmdline)) {
437447
/* cmdline will be a separate field */
438448
WRITE_SEPARATOR;

0 commit comments

Comments
 (0)