Style fixes for consistency
[platform/upstream/glog.git] / src / utilities.cc
index 5a0322c..98dc090 100644 (file)
 #elif defined(HAVE_SYS_SYSCALL_H)
 #include <sys/syscall.h>                 // for syscall()
 #endif
+#ifdef HAVE_SYSLOG_H
+# include <syslog.h>
+#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>  // For geteuid.
+#endif
+#ifdef HAVE_PWD_H
+# include <pwd.h>
+#endif
 
 #include "base/googleinit.h"
 
@@ -63,8 +72,8 @@ _END_GOOGLE_NAMESPACE_
 #include "symbolize.h"
 #include "base/commandlineflags.h"
 
-DEFINE_bool(symbolize_stacktrace, true,
-            "Symbolize the stack trace in the tombstone");
+GLOG_DEFINE_bool(symbolize_stacktrace, true,
+                 "Symbolize the stack trace in the tombstone");
 
 _START_GOOGLE_NAMESPACE_
 
@@ -74,15 +83,18 @@ typedef void DebugWriter(const char*, void*);
 // For some environments, add two extra bytes for the leading "0x".
 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
 
-static void DebugWriteToStderr(const char* data, void *unused) {
+static void DebugWriteToStderr(const char* data, void *) {
   // This one is signal-safe.
-  write(STDERR_FILENO, data, strlen(data));
+  if (write(STDERR_FILENO, data, strlen(data)) < 0) {
+    // Ignore errors.
+  }
 }
 
 void DebugWriteToString(const char* data, void *arg) {
   reinterpret_cast<string*>(arg)->append(data);
 }
 
+#ifdef HAVE_SYMBOLIZE
 // Print a program counter and its symbol name.
 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
                             const char * const prefix) {
@@ -99,20 +111,7 @@ static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
            prefix, kPrintfPointerFieldWidth, pc, symbol);
   writerfn(buf, arg);
 }
-
-// Print a program counter and the corresponding stack frame size.
-static void DumpPCAndFrameSize(DebugWriter *writerfn, void *arg, void *pc,
-                               int framesize, const char * const prefix) {
-  char buf[100];
-  if (framesize <= 0) {
-    snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n",
-             prefix, kPrintfPointerFieldWidth, pc);
-  } else {
-    snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n",
-             prefix, kPrintfPointerFieldWidth, pc, framesize);
-  }
-  writerfn(buf, arg);
-}
+#endif
 
 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
                    const char * const prefix) {
@@ -143,12 +142,20 @@ static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
 static void DumpStackTraceAndExit() {
   DumpStackTrace(1, DebugWriteToStderr, NULL);
 
-  // Set the default signal handler for SIGABRT, to avoid invoking our
-  // own signal handler installed by InstallFailedSignalHandler().
-  struct sigaction sig_action = {};  // Zero-clear.
-  sigemptyset(&sig_action.sa_mask);
-  sig_action.sa_handler = SIG_DFL;
-  sigaction(SIGABRT, &sig_action, NULL);
+  // TOOD(hamaji): Use signal instead of sigaction?
+  if (IsFailureSignalHandlerInstalled()) {
+    // Set the default signal handler for SIGABRT, to avoid invoking our
+    // own signal handler installed by InstallFailureSignalHandler().
+#ifdef HAVE_SIGACTION
+    struct sigaction sig_action;
+    memset(&sig_action, 0, sizeof(sig_action));
+    sigemptyset(&sig_action.sa_mask);
+    sig_action.sa_handler = SIG_DFL;
+    sigaction(SIGABRT, &sig_action, NULL);
+#elif defined(OS_WINDOWS)
+    signal(SIGABRT, SIG_DFL);
+#endif  // HAVE_SIGACTION
+  }
 
   abort();
 }
@@ -184,18 +191,35 @@ bool is_default_thread() {
   }
 }
 
+#ifdef OS_WINDOWS
+struct timeval {
+  long tv_sec, tv_usec;
+};
+
+// Based on: http://www.google.com/codesearch/p?hl=en#dR3YEbitojA/os_win32.c&q=GetSystemTimeAsFileTime%20license:bsd
+// See COPYING for copyright information.
+static int gettimeofday(struct timeval *tv, void* tz) {
+#define EPOCHFILETIME (116444736000000000ULL)
+  FILETIME ft;
+  LARGE_INTEGER li;
+  uint64 tt;
+
+  GetSystemTimeAsFileTime(&ft);
+  li.LowPart = ft.dwLowDateTime;
+  li.HighPart = ft.dwHighDateTime;
+  tt = (li.QuadPart - EPOCHFILETIME) / 10;
+  tv->tv_sec = tt / 1000000;
+  tv->tv_usec = tt % 1000000;
+
+  return 0;
+}
+#endif
+
 int64 CycleClock_Now() {
   // TODO(hamaji): temporary impementation - it might be too slow.
-#ifdef OS_WINDOWS
-  SYSTEMTIME now;
-  GetSystemTime(&now);
-  return (static_cast<int64>(now.wSecond) * 1000000 +
-          static_cast<int64>(now.wMilliseconds) * 1000);
-#else
   struct timeval tv;
   gettimeofday(&tv, NULL);
   return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
-#endif
 }
 
 int64 UsecToCycles(int64 usec) {
@@ -212,9 +236,18 @@ int32 GetMainThreadPid() {
   return g_main_thread_pid;
 }
 
+bool PidHasChanged() {
+  int32 pid = getpid();
+  if (g_main_thread_pid == pid) {
+    return false;
+  }
+  g_main_thread_pid = pid;
+  return true;
+}
+
 pid_t GetTID() {
-  // On Linux and FreeBSD, we try to use gettid().
-#if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
+  // On Linux and MacOSX, we try to use gettid().
+#if defined OS_LINUX || defined OS_MACOSX
 #ifndef __NR_gettid
 #ifdef OS_MACOSX
 #define __NR_gettid SYS_gettid
@@ -236,16 +269,16 @@ pid_t GetTID() {
     // the value change to "true".
     lacks_gettid = true;
   }
-#endif  // OS_LINUX || OS_FREEBSD
+#endif  // OS_LINUX || OS_MACOSX
 
   // If gettid() could not be used, we use one of the following.
 #if defined OS_LINUX
   return getpid();  // Linux:  getpid returns thread ID when gettid is absent
-#elif defined OS_WINDOWS || defined OS_CYGWIN
+#elif defined OS_WINDOWS && !defined OS_CYGWIN
   return GetCurrentThreadId();
 #else
   // If none of the techniques above worked, we use pthread_self().
-  return (pid_t)pthread_self();
+  return (pid_t)(uintptr_t)pthread_self();
 #endif
 }
 
@@ -264,12 +297,32 @@ const string& MyUserName() {
 }
 static void MyUserNameInitializer() {
   // TODO(hamaji): Probably this is not portable.
+#if defined(OS_WINDOWS)
+  const char* user = getenv("USERNAME");
+#else
   const char* user = getenv("USER");
+#endif
   if (user != NULL) {
     g_my_user_name = user;
   } else {
-    g_my_user_name = "invalid-user";
+#if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H)
+    struct passwd pwd;
+    struct passwd* result = NULL;
+    char buffer[1024] = {'\0'};
+    uid_t uid = geteuid();
+    int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof(buffer), &result);
+    if (pwuid_res == 0) {
+      g_my_user_name = pwd.pw_name;
+    } else {
+      snprintf(buffer, sizeof(buffer), "uid%d", uid);
+      g_my_user_name = buffer;
+    }
+#endif
+    if (g_my_user_name.empty()) {
+      g_my_user_name = "invalid-user";
+    }
   }
+
 }
 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
 
@@ -289,9 +342,9 @@ void SetCrashReason(const CrashReason* r) {
                             r);
 }
 
-}  // namespace glog_internal_namespace_
-
-void InitGoogleLogging(const char* argv0) {
+void InitGoogleLoggingUtilities(const char* argv0) {
+  CHECK(!IsGoogleLoggingInitialized())
+      << "You called InitGoogleLogging() twice!";
   const char* slash = strrchr(argv0, '/');
 #ifdef OS_WINDOWS
   if (!slash)  slash = strrchr(argv0, '\\');
@@ -304,9 +357,28 @@ void InitGoogleLogging(const char* argv0) {
 #endif
 }
 
+void ShutdownGoogleLoggingUtilities() {
+  CHECK(IsGoogleLoggingInitialized())
+      << "You called ShutdownGoogleLogging() without calling InitGoogleLogging() first!";
+  g_program_invocation_short_name = NULL;
+#ifdef HAVE_SYSLOG_H
+  closelog();
+#endif
+}
+
+}  // namespace glog_internal_namespace_
+
 _END_GOOGLE_NAMESPACE_
 
 // Make an implementation of stacktrace compiled.
 #ifdef STACKTRACE_H
 # include STACKTRACE_H
+# if 0
+// For include scanners which can't handle macro expansions.
+#  include "stacktrace_libunwind-inl.h"
+#  include "stacktrace_x86-inl.h"
+#  include "stacktrace_x86_64-inl.h"
+#  include "stacktrace_powerpc-inl.h"
+#  include "stacktrace_generic-inl.h"
+# endif
 #endif