From 8baf6d13cb50f0a000e1af825f84c3a36f06541e Mon Sep 17 00:00:00 2001 From: ehds Date: Tue, 14 May 2024 00:28:20 +0800 Subject: [PATCH 1/2] cmd line not contains absolute path --- src/brpc/builtin/common.cpp | 20 +++++++++++++++++++- src/brpc/builtin/common.h | 3 +++ src/brpc/builtin/hotspots_service.cpp | 4 ++-- src/butil/process_util.cc | 20 ++++++++++++++++++++ src/butil/process_util.h | 5 +++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/brpc/builtin/common.cpp b/src/brpc/builtin/common.cpp index 9cdc72fee6..a1eadbd11b 100644 --- a/src/brpc/builtin/common.cpp +++ b/src/brpc/builtin/common.cpp @@ -339,17 +339,35 @@ static void CreateProgramName() { s_program_name = s_cmdline; } } + const char* GetProgramName() { pthread_once(&create_program_name_once, CreateProgramName); return s_program_name; } +static pthread_once_t create_program_path_once = PTHREAD_ONCE_INIT; +static const char* s_program_path = "unknown"; +static char s_program_exec_path[PATH_MAX]; +static void CreateProgramPath() { + const ssize_t nr = butil::GetProcessAbsolutePath(s_program_exec_path, sizeof(s_program_exec_path) - 1); + if (nr > 0) { + s_program_exec_path[nr] = '\0'; + s_program_path = s_program_exec_path; + } +} + +const char* GetProgramPath() { + pthread_once(&create_program_path_once, CreateProgramPath); + return s_program_path; +} + + static pthread_once_t compute_program_checksum_once = PTHREAD_ONCE_INIT; static char s_program_checksum[33]; static const char s_alphabet[] = "0123456789abcdef"; static void ComputeProgramCHECKSUM() { unsigned char checksum[16]; - FileChecksum(GetProgramName(), checksum); + FileChecksum(GetProgramPath(), checksum); for (size_t i = 0, j = 0; i < 16; ++i, j+=2) { s_program_checksum[j] = s_alphabet[checksum[i] >> 4]; s_program_checksum[j+1] = s_alphabet[checksum[i] & 0xF]; diff --git a/src/brpc/builtin/common.h b/src/brpc/builtin/common.h index f4d6962729..d598e10a0d 100644 --- a/src/brpc/builtin/common.h +++ b/src/brpc/builtin/common.h @@ -112,6 +112,9 @@ int FileChecksum(const char* file_path, unsigned char* checksum); // Get name of current program. const char* GetProgramName(); +// Get absolute path of current program. +const char* GetProgramPath(); + // Get checksum of current program image. const char* GetProgramChecksum(); diff --git a/src/brpc/builtin/hotspots_service.cpp b/src/brpc/builtin/hotspots_service.cpp index b70266f49f..bb642c1d0a 100644 --- a/src/brpc/builtin/hotspots_service.cpp +++ b/src/brpc/builtin/hotspots_service.cpp @@ -493,7 +493,7 @@ static void DisplayResult(Controller* cntl, cmd_builder << "--base " << *base_name << ' '; } - cmd_builder << GetProgramName() << " " << prof_name; + cmd_builder << GetProgramPath() << " " << prof_name; if (display_type == DisplayType::kFlameGraph) { // For flamegraph, we don't care about pprof error msg, @@ -509,7 +509,7 @@ static void DisplayResult(Controller* cntl, if (base_name) { cmd_builder << "-base " << *base_name << ' '; } - cmd_builder << GetProgramName() << " " << prof_name << " 2>&1 "; + cmd_builder << GetProgramPath() << " " << prof_name << " 2>&1 "; #endif const std::string cmd = cmd_builder.str(); diff --git a/src/butil/process_util.cc b/src/butil/process_util.cc index fcb41b912e..7a7e4487f1 100644 --- a/src/butil/process_util.cc +++ b/src/butil/process_util.cc @@ -19,6 +19,8 @@ // Date: Wed Apr 11 14:35:56 CST 2018 +#include +#include #include // open #include // snprintf #include @@ -30,6 +32,9 @@ #include "butil/popen.h" // read_command_output #include "butil/process_util.h" +#if defined(OS_MACOSX) +#include // proc_pidpath +#endif namespace butil { ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) { @@ -85,4 +90,19 @@ ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) { } } +// Get absolute path of this program. +// Returns length of the absolute path on sucess, -1 otherwise. +// NOTE: `buf' does not end with zero. +ssize_t GetProcessAbsolutePath(char *buf, size_t len) { +#if defined(OS_LINUX) + memset(buf, 0, len); + ssize_t nr = readlink("/proc/self/exe", buf, len); + return nr; +#elif defined(OS_MACOSX) + memset(buf, 0, len); + int ret = proc_pidpath(getpid(), buf, len); + return ret; +#endif +} + } // namespace butil diff --git a/src/butil/process_util.h b/src/butil/process_util.h index 6dc9a7db5a..5493e263d6 100644 --- a/src/butil/process_util.h +++ b/src/butil/process_util.h @@ -32,6 +32,11 @@ namespace butil { // NOTE: `buf' does not end with zero. ssize_t ReadCommandLine(char* buf, size_t len, bool with_args); +// Get absolute path of this program. +// Returns length of the absolute path on sucess, -1 otherwise. +// NOTE: `buf' does not end with zero. +ssize_t GetProcessAbsolutePath(char* buf, size_t len); + } // namespace butil #endif // BUTIL_PROCESS_UTIL_H From 9f616266c585a24378967d25290829c1ca845f8a Mon Sep 17 00:00:00 2001 From: ehds Date: Wed, 15 May 2024 18:59:10 +0800 Subject: [PATCH 2/2] rm duplicate comment --- src/butil/process_util.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/butil/process_util.cc b/src/butil/process_util.cc index 7a7e4487f1..6791744e20 100644 --- a/src/butil/process_util.cc +++ b/src/butil/process_util.cc @@ -19,8 +19,6 @@ // Date: Wed Apr 11 14:35:56 CST 2018 -#include -#include #include // open #include // snprintf #include @@ -90,9 +88,6 @@ ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) { } } -// Get absolute path of this program. -// Returns length of the absolute path on sucess, -1 otherwise. -// NOTE: `buf' does not end with zero. ssize_t GetProcessAbsolutePath(char *buf, size_t len) { #if defined(OS_LINUX) memset(buf, 0, len);