39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
43 #if defined(HAVE_SYSCALL_H)
45 #elif defined(HAVE_SYS_SYSCALL_H)
46 #include <sys/syscall.h>
58 #include <android/log.h>
73 _END_GOOGLE_NAMESPACE_
76 #ifdef HAVE_STACKTRACE
83 "Symbolize the stack trace in the tombstone");
87 typedef void DebugWriter(
const char*,
void*);
91 static const int kPrintfPointerFieldWidth = 2 + 2 *
sizeof(
void*);
93 static void DebugWriteToStderr(
const char*
data,
void *) {
95 if (write(STDERR_FILENO,
data, strlen(
data)) < 0) {
98 #if defined(__ANDROID__)
100 __android_log_write(ANDROID_LOG_FATAL,
106 static void DebugWriteToString(
const char*
data,
void *arg) {
110 #ifdef HAVE_SYMBOLIZE
112 static void DumpPCAndSymbol(DebugWriter *writerfn,
void *arg,
void *pc,
113 const char *
const prefix) {
115 const char *symbol =
"(unknown)";
119 if (
Symbolize(
reinterpret_cast<char *
>(pc) - 1, tmp,
sizeof(tmp))) {
124 prefix, kPrintfPointerFieldWidth, pc, symbol);
129 static void DumpPC(DebugWriter *writerfn,
void *arg,
void *pc,
130 const char *
const prefix) {
133 prefix, kPrintfPointerFieldWidth, pc);
138 static void DumpStackTrace(
int skip_count, DebugWriter *writerfn,
void *arg) {
143 #if defined(HAVE_SYMBOLIZE)
144 if (FLAGS_symbolize_stacktrace) {
145 DumpPCAndSymbol(writerfn, arg, stack[
i],
" ");
147 DumpPC(writerfn, arg, stack[
i],
" ");
150 DumpPC(writerfn, arg, stack[
i],
" ");
155 #if defined(__GNUC__)
156 __attribute__((noreturn))
157 #elif defined(_MSC_VER)
160 static void DumpStackTraceAndExit() {
161 DumpStackTrace(1, DebugWriteToStderr,
NULL);
167 #ifdef HAVE_SIGACTION
168 struct sigaction sig_action;
169 memset(&sig_action, 0,
sizeof(sig_action));
170 sigemptyset(&sig_action.sa_mask);
171 sig_action.sa_handler = SIG_DFL;
172 sigaction(SIGABRT, &sig_action,
NULL);
173 #elif defined(GLOG_OS_WINDOWS)
174 signal(SIGABRT, SIG_DFL);
175 #endif // HAVE_SIGACTION
181 _END_GOOGLE_NAMESPACE_
183 #endif // HAVE_STACKTRACE
198 #ifdef GLOG_OS_WINDOWS
200 long tv_sec, tv_usec;
205 static int gettimeofday(
struct timeval *tv,
void* ) {
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wlong-long"
210 #define EPOCHFILETIME (116444736000000000ULL)
215 GetSystemTimeAsFileTime(&ft);
216 li.LowPart = ft.dwLowDateTime;
217 li.HighPart = ft.dwHighDateTime;
218 tt = (li.QuadPart - EPOCHFILETIME) / 10;
219 tv->tv_sec = tt / 1000000;
220 tv->tv_usec = tt % 1000000;
222 #pragma GCC diagnostic pop
232 gettimeofday(&tv,
NULL);
233 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
251 int32 pid = getpid();
261 #if defined GLOG_OS_LINUX || defined GLOG_OS_MACOSX
263 #ifdef GLOG_OS_MACOSX
264 #define __NR_gettid SYS_gettid
265 #elif ! defined __i386__
266 #error "Must define __NR_gettid for non-x86 platforms"
268 #define __NR_gettid 224
271 static bool lacks_gettid =
false;
273 #if (defined(GLOG_OS_MACOSX) && defined(HAVE_PTHREAD_THREADID_NP))
275 const int error = pthread_threadid_np(
NULL, &tid64);
276 pid_t tid =
error ? -1 :
static_cast<pid_t
>(tid64);
278 pid_t tid =
static_cast<pid_t
>(syscall(__NR_gettid));
289 #endif // GLOG_OS_LINUX || GLOG_OS_MACOSX
292 #if defined GLOG_OS_LINUX
294 #elif defined GLOG_OS_WINDOWS && !defined GLOG_OS_CYGWIN
295 return static_cast<pid_t
>(GetCurrentThreadId());
296 #elif defined(HAVE_PTHREAD)
298 return (pid_t)(uintptr_t)pthread_self();
305 const char*
base = strrchr(filepath,
'/');
306 #ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
308 base = strrchr(filepath,
'\\');
319 #if defined(GLOG_OS_WINDOWS)
320 const char* user = getenv(
"USERNAME");
322 const char* user = getenv(
"USER");
327 #if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H)
329 struct passwd* result =
NULL;
330 char buffer[1024] = {
'\0'};
331 uid_t uid = geteuid();
332 int pwuid_res = getpwuid_r(uid, &pwd,
buffer,
sizeof(
buffer), &result);
333 if (pwuid_res == 0 && result) {
348 #ifdef HAVE_STACKTRACE
350 DumpStackTrace(1, DebugWriteToString, stacktrace);
366 <<
"You called InitGoogleLogging() twice!";
367 const char* slash = strrchr(argv0,
'/');
368 #ifdef GLOG_OS_WINDOWS
369 if (!slash) slash = strrchr(argv0,
'\\');
373 #ifdef HAVE_STACKTRACE
380 <<
"You called ShutdownGoogleLogging() without calling InitGoogleLogging() first!";
389 _END_GOOGLE_NAMESPACE_
393 # include STACKTRACE_H
398 # include "stacktrace_x86_64-inl.h"