feat: provide fallback for accessing process name (#1049)

This commit is contained in:
Sergiu Deitsch 2024-01-08 11:18:59 +01:00 committed by GitHub
parent aaefca72b3
commit 7af231e6bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 16 deletions

View File

@ -164,6 +164,15 @@ if (WITH_FUZZING STREQUAL none)
check_cxx_symbol_exists (abi::__cxa_demangle cxxabi.h HAVE___CXA_DEMANGLE) check_cxx_symbol_exists (abi::__cxa_demangle cxxabi.h HAVE___CXA_DEMANGLE)
endif (WITH_FUZZING STREQUAL none) 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) if (WITH_TLS)
set (GLOG_THREAD_LOCAL_STORAGE 1) set (GLOG_THREAD_LOCAL_STORAGE 1)
endif (WITH_TLS) endif (WITH_TLS)

View File

@ -115,4 +115,16 @@
/* define if abi::__cxa_demangle is available in cxxabi.h */ /* define if abi::__cxa_demangle is available in cxxabi.h */
#cmakedefine HAVE___CXA_DEMANGLE #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 #endif // GLOG_CONFIG_H

View File

@ -29,9 +29,12 @@
// //
// Author: Shinichiro Hamaji // Author: Shinichiro Hamaji
#define _GNU_SOURCE 1
#include "utilities.h" #include "utilities.h"
#include <atomic> #include <atomic>
#include <cerrno>
#include <csignal> #include <csignal>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -61,6 +64,10 @@
# include <android/log.h> # include <android/log.h>
#endif #endif
#if defined(HAVE___PROGNAME)
extern char* __progname;
#endif
using std::string; using std::string;
namespace google { namespace google {
@ -189,13 +196,29 @@ namespace google {
inline namespace glog_internal_namespace_ { 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() { const char* ProgramInvocationShortName() {
if (g_program_invocation_short_name != nullptr) { if (g_program_invocation_short_name != nullptr) {
return g_program_invocation_short_name; 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(); static int32 g_main_thread_pid = getpid();
@ -210,14 +233,6 @@ bool PidHasChanged() {
return true; 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; static string g_my_user_name;
const string& MyUserName() { return g_my_user_name; } const string& MyUserName() { return g_my_user_name; }
static void MyUserNameInitializer() { static void MyUserNameInitializer() {
@ -268,11 +283,7 @@ void SetCrashReason(const logging::internal::CrashReason* r) {
void InitGoogleLoggingUtilities(const char* argv0) { void InitGoogleLoggingUtilities(const char* argv0) {
CHECK(!IsGoogleLoggingInitialized()) CHECK(!IsGoogleLoggingInitialized())
<< "You called InitGoogleLogging() twice!"; << "You called InitGoogleLogging() twice!";
const char* slash = strrchr(argv0, '/'); g_program_invocation_short_name = const_basename(argv0);
#ifdef GLOG_OS_WINDOWS
if (!slash) slash = strrchr(argv0, '\\');
#endif
g_program_invocation_short_name = slash ? slash + 1 : argv0;
#ifdef HAVE_STACKTRACE #ifdef HAVE_STACKTRACE
InstallFailureFunction(&DumpStackTraceAndExit); InstallFailureFunction(&DumpStackTraceAndExit);