Skip to content

Commit 463ee6f

Browse files
committed
Improve process cmdline basename matching with procExe path
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]>
1 parent 934c526 commit 463ee6f

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

Process.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static bool findCommInCmdline(const char* comm, const char* cmdline, int cmdline
9999
return false;
100100
}
101101

102-
static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int cmdlineBaseOffset, const char* exe, int exeBaseOffset, int exeBaseLen) {
102+
static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int* cmdlineBasenameStart, const char* exe, int exeBaseOffset, int exeBaseLen) {
103103
/* cmdline prefix is an absolute path: it must match whole exe. */
104104
if (cmdline[0] == '/') {
105105
int matchLen = exeBaseLen + exeBaseOffset;
@@ -124,6 +124,7 @@ static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int cmdlineBaseO
124124
*
125125
* So if needed, we adjust cmdlineBaseOffset to the previous (if any)
126126
* component of the cmdline relative path, and retry the procedure. */
127+
int cmdlineBaseOffset = *cmdlineBasenameStart;
127128
bool delimFound = true; /* if valid basename delimiter found */
128129
do {
129130
/* match basename */
@@ -140,8 +141,10 @@ static int matchCmdlinePrefixWithExeSuffix(const char* cmdline, int cmdlineBaseO
140141
}
141142

142143
/* full match, with exe suffix being a valid relative path */
143-
if (i < 1 && j >= 1 && exe[j - 1] == '/')
144+
if (i < 1 && j >= 1 && exe[j - 1] == '/') {
145+
*cmdlineBasenameStart = cmdlineBaseOffset;
144146
return matchLen;
147+
}
145148
}
146149
}
147150

@@ -322,6 +325,26 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
322325
assert(cmdlineBasenameStart >= 0);
323326
assert(cmdlineBasenameStart <= (int)strlen(cmdline));
324327

328+
int exeLen = 0;
329+
int exeBasenameOffset = 0;
330+
int exeBasenameLen = 0;
331+
int matchLen = 0;
332+
if (procExe) {
333+
exeLen = strlen(procExe);
334+
exeBasenameOffset = this->procExeBasenameOffset;
335+
exeBasenameLen = exeLen - exeBasenameOffset;
336+
337+
assert(exeBasenameOffset >= 0);
338+
assert(exeBasenameOffset <= (int)strlen(procExe));
339+
340+
if (this->cmdline) {
341+
matchLen = matchCmdlinePrefixWithExeSuffix(this->cmdline, &cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);
342+
}
343+
if (matchLen) {
344+
cmdlineBasenameLen = exeBasenameLen;
345+
}
346+
}
347+
325348
if (!showMergedCommand || !procExe || !procComm) { /* fall back to cmdline */
326349
if ((showMergedCommand || (Process_isUserlandThread(this) && showThreadNames)) && procComm && strlen(procComm)) { /* set column to or prefix it with comm */
327350
if (strncmp(cmdline + cmdlineBasenameStart, procComm, MINIMUM(TASK_COMM_LEN - 1, strlen(procComm))) != 0) {
@@ -352,13 +375,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
352375
return;
353376
}
354377

355-
int exeLen = strlen(this->procExe);
356-
int exeBasenameOffset = this->procExeBasenameOffset;
357-
int exeBasenameLen = exeLen - exeBasenameOffset;
358-
359-
assert(exeBasenameOffset >= 0);
360-
assert(exeBasenameOffset <= (int)strlen(procExe));
361-
362378
bool haveCommInExe = false;
363379
if (procExe && procComm && (!Process_isUserlandThread(this) || showThreadNames)) {
364380
haveCommInExe = strncmp(procExe + exeBasenameOffset, procComm, TASK_COMM_LEN - 1) == 0;
@@ -397,8 +413,6 @@ void Process_makeCommandStr(Process* this, const Settings* settings) {
397413
haveCommInCmdline = (!Process_isUserlandThread(this) || showThreadNames) && findCommInCmdline(procComm, cmdline, cmdlineBasenameStart, &commStart, &commLen);
398414
}
399415

400-
int matchLen = matchCmdlinePrefixWithExeSuffix(cmdline, cmdlineBasenameStart, procExe, exeBasenameOffset, exeBasenameLen);
401-
402416
bool haveCommField = false;
403417

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

0 commit comments

Comments
 (0)