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 GetProgramName not contains absolute path from cmdline #2644

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
20 changes: 19 additions & 1 deletion src/brpc/builtin/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
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 @@ -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();

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 @@ -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,
Expand All @@ -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();
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
Loading