Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(system_monitor): output command line (#5430) #1057

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ProcessMonitor : public rclcpp::Node
* @param [out] command output command line
* @return true if success to get command line name
*/
bool getCommandLineFromPiD(const std::string & pid, std::string * command);
bool getCommandLineFromPiD(const std::string & pid, std::string & command);

/**
* @brief get top-rated processes
Expand Down
18 changes: 13 additions & 5 deletions system/system_monitor/src/process_monitor/process_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,22 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
getTopratedProcesses(&memory_tasks_, &p2);
}

bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string * command)
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string & command)
{
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
std::ifstream commandFile(commandLineFilePath);
std::ifstream commandFile(commandLineFilePath, std::ios::in | std::ios::binary);

if (commandFile.is_open()) {
std::getline(commandFile, *command);
std::vector<uint8_t> buffer;
std::copy(
std::istream_iterator<uint8_t>(commandFile), std::istream_iterator<uint8_t>(),
std::back_inserter(buffer));
commandFile.close();
return true;
std::replace(
buffer.begin(), buffer.end(), '\0',
' '); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
command = std::string(buffer.begin(), buffer.end());
return (buffer.size() > 0) ? true : false; // cmdline is empty if it is kernel process
} else {
return false;
}
Expand Down Expand Up @@ -478,7 +486,7 @@ void ProcessMonitor::getTopratedProcesses(
std::string program_name;
std::getline(stream, program_name);

bool flag_find_command_line = getCommandLineFromPiD(info.processId, &info.commandName);
bool flag_find_command_line = getCommandLineFromPiD(info.processId, info.commandName);

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