Skip to content

Commit 29d34df

Browse files
fix(system_monitor): output command line (#5430)
* fix(system_monitor): output command line Signed-off-by: takeshi.iwanari <takeshi.iwanari@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: takeshi.iwanari <takeshi.iwanari@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b97ea6e commit 29d34df

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

system/system_monitor/include/system_monitor/process_monitor/process_monitor.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ProcessMonitor : public rclcpp::Node
9494
* @param [out] command output command line
9595
* @return true if success to get command line name
9696
*/
97-
bool getCommandLineFromPiD(const std::string & pid, std::string * command);
97+
bool getCommandLineFromPiD(const std::string & pid, std::string & command);
9898

9999
/**
100100
* @brief get top-rated processes

system/system_monitor/src/process_monitor/process_monitor.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,22 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
414414
getTopratedProcesses(&memory_tasks_, &p2);
415415
}
416416

417-
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string * command)
417+
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string & command)
418418
{
419419
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
420-
std::ifstream commandFile(commandLineFilePath);
420+
std::ifstream commandFile(commandLineFilePath, std::ios::in | std::ios::binary);
421+
421422
if (commandFile.is_open()) {
422-
std::getline(commandFile, *command);
423+
std::vector<uint8_t> buffer;
424+
std::copy(
425+
std::istream_iterator<uint8_t>(commandFile), std::istream_iterator<uint8_t>(),
426+
std::back_inserter(buffer));
423427
commandFile.close();
424-
return true;
428+
std::replace(
429+
buffer.begin(), buffer.end(), '\0',
430+
' '); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
431+
command = std::string(buffer.begin(), buffer.end());
432+
return (buffer.size() > 0) ? true : false; // cmdline is empty if it is kernel process
425433
} else {
426434
return false;
427435
}
@@ -478,7 +486,7 @@ void ProcessMonitor::getTopratedProcesses(
478486
std::string program_name;
479487
std::getline(stream, program_name);
480488

481-
bool flag_find_command_line = getCommandLineFromPiD(info.processId, &info.commandName);
489+
bool flag_find_command_line = getCommandLineFromPiD(info.processId, info.commandName);
482490

483491
if (!flag_find_command_line) {
484492
info.commandName = program_name; // if command line is not found, use program name instead

0 commit comments

Comments
 (0)