Skip to content

Commit

Permalink
Merge pull request #2644 from ehds/fix-can-not-recognize-absolute-pat…
Browse files Browse the repository at this point in the history
…h-from-cmdline

Fix GetProgramName not contains absolute path from cmdline
  • Loading branch information
yanglimingcn committed May 19, 2024
2 parents 66b7739 + 9f61626 commit ff140be
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/brpc/builtin/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,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];
Expand Down
3 changes: 3 additions & 0 deletions src/brpc/builtin/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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();

Expand Down
4 changes: 2 additions & 2 deletions src/brpc/builtin/hotspots_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,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,
Expand All @@ -512,7 +512,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();
Expand Down
15 changes: 15 additions & 0 deletions src/butil/process_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "butil/popen.h" // read_command_output
#include "butil/process_util.h"

#if defined(OS_MACOSX)
#include <libproc.h> // proc_pidpath
#endif
namespace butil {

ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) {
Expand Down Expand Up @@ -85,4 +88,16 @@ ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) {
}
}

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
5 changes: 5 additions & 0 deletions src/butil/process_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ff140be

Please sign in to comment.