Skip to content

Commit

Permalink
feat: provide fallback for accessing process name (#1049)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud authored Jan 8, 2024
1 parent aaefca7 commit 7af231e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ if (WITH_FUZZING STREQUAL none)
check_cxx_symbol_exists (abi::__cxa_demangle cxxabi.h HAVE___CXA_DEMANGLE)
endif (WITH_FUZZING STREQUAL none)

check_cxx_symbol_exists (__argv cstdlib HAVE___ARGV)
check_cxx_symbol_exists (getprogname cstdlib HAVE_GETPROGNAME)
check_cxx_symbol_exists (program_invocation_short_name cerrno HAVE_PROGRAM_INVOCATION_SHORT_NAME)
check_cxx_source_compiles ([=[
#include <cstdlib>
extern char* __progname;
int main() { return __progname != nullptr ? EXIT_SUCCESS : EXIT_FAILURE; }
]=] HAVE___PROGNAME)

if (WITH_TLS)
set (GLOG_THREAD_LOCAL_STORAGE 1)
endif (WITH_TLS)
Expand Down
12 changes: 12 additions & 0 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,16 @@
/* define if abi::__cxa_demangle is available in cxxabi.h */
#cmakedefine HAVE___CXA_DEMANGLE

/* define if __argv is available in cstdlib */
#cmakedefine HAVE___ARGV

/* define if __progname is available */
#cmakedefine HAVE___PROGNAME

/* define if getprogname is available in cstdlib */
#cmakedefine HAVE_GETPROGNAME

/* define if program_invocation_short_name is available in cerrno */
#cmakedefine HAVE_PROGRAM_INVOCATION_SHORT_NAME

#endif // GLOG_CONFIG_H
43 changes: 27 additions & 16 deletions src/utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
//
// Author: Shinichiro Hamaji

#define _GNU_SOURCE 1

#include "utilities.h"

#include <atomic>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -61,6 +64,10 @@
# include <android/log.h>
#endif

#if defined(HAVE___PROGNAME)
extern char* __progname;
#endif

using std::string;

namespace google {
Expand Down Expand Up @@ -189,13 +196,29 @@ namespace google {

inline namespace glog_internal_namespace_ {

const char* const_basename(const char* filepath) {
const char* base = strrchr(filepath, '/');
#ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
if (!base) base = strrchr(filepath, '\\');
#endif
return base ? (base + 1) : filepath;
}

const char* ProgramInvocationShortName() {
if (g_program_invocation_short_name != nullptr) {
return g_program_invocation_short_name;
} else {
// TODO(hamaji): Use /proc/self/cmdline and so?
return "UNKNOWN";
}
#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
return program_invocation_short_name;
#elif defined(HAVE_GETPROGNAME)
return getprogname();
#elif defined(HAVE___PROGNAME)
return __progname;
#elif defined(HAVE___ARGV)
return const_basename(__argv[0]);
#else
return "UNKNOWN";
#endif
}

static int32 g_main_thread_pid = getpid();
Expand All @@ -210,14 +233,6 @@ bool PidHasChanged() {
return true;
}

const char* const_basename(const char* filepath) {
const char* base = strrchr(filepath, '/');
#ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
if (!base) base = strrchr(filepath, '\\');
#endif
return base ? (base + 1) : filepath;
}

static string g_my_user_name;
const string& MyUserName() { return g_my_user_name; }
static void MyUserNameInitializer() {
Expand Down Expand Up @@ -268,11 +283,7 @@ void SetCrashReason(const logging::internal::CrashReason* r) {
void InitGoogleLoggingUtilities(const char* argv0) {
CHECK(!IsGoogleLoggingInitialized())
<< "You called InitGoogleLogging() twice!";
const char* slash = strrchr(argv0, '/');
#ifdef GLOG_OS_WINDOWS
if (!slash) slash = strrchr(argv0, '\\');
#endif
g_program_invocation_short_name = slash ? slash + 1 : argv0;
g_program_invocation_short_name = const_basename(argv0);

#ifdef HAVE_STACKTRACE
InstallFailureFunction(&DumpStackTraceAndExit);
Expand Down

0 comments on commit 7af231e

Please sign in to comment.