Skip to content

Commit

Permalink
Improve process cmdline basename matching with procExe path
Browse files Browse the repository at this point in the history
The matchCmdlinePrefixWithExeSuffix() internal function (in Process.c)
can match basenames in multiple tries in case the
"cmdlineBasenameStart" input value is unreliable. Take advantage of
this and update "cmdlineBasenameStart" with the new offset detected by
the function.

Also make the matching behavior consistent regardless of
"showMergedCommand" setting.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Jan 23, 2025
1 parent 160c066 commit f83ab04
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,27 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
assert(cmdlineBasenameStart >= 0);
assert(cmdlineBasenameStart <= (int)strlen(cmdline));

int exeLen = 0;
int exeBasenameOffset = 0;
int exeBasenameLen = 0;
int matchLen = 0;
if (procExe) {
exeLen = strlen(procExe);
exeBasenameOffset = this->procExeBasenameOffset;
exeBasenameLen = exeLen - exeBasenameOffset;

assert(exeBasenameOffset >= 0);
assert(exeBasenameOffset <= (int)strlen(procExe));

if (this->cmdline) {
matchLen = matchCmdlinePrefixWithExeSuffix(this->cmdline, cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);
}
if (matchLen) {
cmdlineBasenameStart = matchLen - exeBasenameLen;
cmdlineBasenameEnd = matchLen;
}
}

if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */
if ((showMergedCommand || (Process_isUserlandThread(this) && showThreadNames)) && procComm && strlen(procComm)) { /* set column to or prefix it with comm */
if (strncmp(cmdline + cmdlineBasenameStart, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) {
Expand Down Expand Up @@ -350,13 +371,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
return;
}

int exeLen = strlen(this->procExe);
int exeBasenameOffset = this->procExeBasenameOffset;
int exeBasenameLen = exeLen - exeBasenameOffset;

assert(exeBasenameOffset >= 0);
assert(exeBasenameOffset <= (int)strlen(procExe));

bool haveCommInExe = false;
if (procExe && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0;
Expand Down Expand Up @@ -395,8 +409,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
haveCommInCmdline = (!Process_isUserlandThread(this) || showThreadNames) && findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commEnd);
}

int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);

bool haveCommField = false;

if (!haveCommInExe && !haveCommInCmdline && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
Expand Down

0 comments on commit f83ab04

Please sign in to comment.