1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
11 #if defined(HAVE_SYSCALL_H)
12 #include <syscall.h> // for syscall()
13 #elif defined(HAVE_SYS_SYSCALL_H)
14 #include <sys/syscall.h> // for syscall()
17 #include "base/googleinit.h"
21 _START_GOOGLE_NAMESPACE_
23 static const char* g_program_invocation_short_name = NULL;
24 static pthread_t g_main_thread_id;
26 _END_GOOGLE_NAMESPACE_
28 // The following APIs are all internal.
29 #ifdef HAVE_STACKTRACE
31 #include "stacktrace.h"
32 #include "symbolize.h"
33 #include "base/commandlineflags.h"
35 DEFINE_bool(symbolize_stacktrace, true,
36 "Symbolize the stack trace in the tombstone");
38 _START_GOOGLE_NAMESPACE_
40 typedef void DebugWriter(const char*, void*);
42 // The %p field width for printf() functions is two characters per byte.
43 // For some environments, add two extra bytes for the leading "0x".
44 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
46 static void DebugWriteToStderr(const char* data, void *unused) {
47 // This one is signal-safe.
48 write(STDERR_FILENO, data, strlen(data));
51 void DebugWriteToString(const char* data, void *arg) {
52 reinterpret_cast<string*>(arg)->append(data);
55 // Print a program counter and its symbol name.
56 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
57 const char * const prefix) {
59 const char *symbol = "(unknown)";
60 // Symbolizes the previous address of pc because pc may be in the
61 // next function. The overrun happens when the function ends with
62 // a call to a function annotated noreturn (e.g. CHECK).
63 if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
67 snprintf(buf, sizeof(buf), "%s@ %*p %s\n",
68 prefix, kPrintfPointerFieldWidth, pc, symbol);
72 // Print a program counter and the corresponding stack frame size.
73 static void DumpPCAndFrameSize(DebugWriter *writerfn, void *arg, void *pc,
74 int framesize, const char * const prefix) {
77 snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n",
78 prefix, kPrintfPointerFieldWidth, pc);
80 snprintf(buf, sizeof(buf), "%s@ %*p %9d\n",
81 prefix, kPrintfPointerFieldWidth, pc, framesize);
86 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
87 const char * const prefix) {
89 snprintf(buf, sizeof(buf), "%s@ %*p\n",
90 prefix, kPrintfPointerFieldWidth, pc);
94 // Dump current stack trace as directed by writerfn
95 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
98 int depth = GetStackTrace(stack, ARRAYSIZE(stack), skip_count+1);
99 for (int i = 0; i < depth; i++) {
100 #if defined(HAVE_SYMBOLIZE)
101 if (FLAGS_symbolize_stacktrace) {
102 DumpPCAndSymbol(writerfn, arg, stack[i], " ");
104 DumpPC(writerfn, arg, stack[i], " ");
107 DumpPC(writerfn, arg, stack[i], " ");
112 static void DumpStackTraceAndExit() {
113 DumpStackTrace(1, DebugWriteToStderr, NULL);
115 // Set the default signal handler for SIGABRT, to avoid invoking our
116 // own signal handler installed by InstallFailedSignalHandler().
117 struct sigaction sig_action = {}; // Zero-clear.
118 sigemptyset(&sig_action.sa_mask);
119 sig_action.sa_handler = SIG_DFL;
120 sigaction(SIGABRT, &sig_action, NULL);
125 _END_GOOGLE_NAMESPACE_
127 #endif // HAVE_STACKTRACE
129 _START_GOOGLE_NAMESPACE_
131 namespace glog_internal_namespace_ {
133 const char* ProgramInvocationShortName() {
134 if (g_program_invocation_short_name != NULL) {
135 return g_program_invocation_short_name;
137 // TODO(hamaji): Use /proc/self/cmdline and so?
142 bool IsGoogleLoggingInitialized() {
143 return g_program_invocation_short_name != NULL;
146 bool is_default_thread() {
147 if (g_program_invocation_short_name == NULL) {
148 // InitGoogleLogging() not yet called, so unlikely to be in a different
152 return pthread_equal(pthread_self(), g_main_thread_id);
156 int64 CycleClock_Now() {
157 // TODO(hamaji): temporary impementation - it might be too slow.
161 return (static_cast<int64>(now.wSecond) * 1000000 +
162 static_cast<int64>(now.wMilliseconds) * 1000);
165 gettimeofday(&tv, NULL);
166 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
170 int64 UsecToCycles(int64 usec) {
174 WallTime WallTime_Now() {
175 // Now, cycle clock is retuning microseconds since the epoch.
176 return CycleClock_Now() * 0.000001;
179 static int32 g_main_thread_pid = getpid();
180 int32 GetMainThreadPid() {
181 return g_main_thread_pid;
185 // On Linux and FreeBSD, we try to use gettid().
186 #if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
189 #define __NR_gettid SYS_gettid
190 #elif ! defined __i386__
191 #error "Must define __NR_gettid for non-x86 platforms"
193 #define __NR_gettid 224
196 static bool lacks_gettid = false;
198 pid_t tid = syscall(__NR_gettid);
202 // Technically, this variable has to be volatile, but there is a small
203 // performance penalty in accessing volatile variables and there should
204 // not be any serious adverse effect if a thread does not immediately see
205 // the value change to "true".
208 #endif // OS_LINUX || OS_FREEBSD
210 // If gettid() could not be used, we use one of the following.
212 return getpid(); // Linux: getpid returns thread ID when gettid is absent
213 #elif defined OS_WINDOWS || defined OS_CYGWIN
214 return GetCurrentThreadId();
216 // If none of the techniques above worked, we use pthread_self().
217 return (pid_t)pthread_self();
221 const char* const_basename(const char* filepath) {
222 const char* base = strrchr(filepath, '/');
223 #ifdef OS_WINDOWS // Look for either path separator in Windows
225 base = strrchr(filepath, '\\');
227 return base ? (base+1) : filepath;
230 static string g_my_user_name;
231 const string& MyUserName() {
232 return g_my_user_name;
234 static void MyUserNameInitializer() {
235 // TODO(hamaji): Probably this is not portable.
236 const char* user = getenv("USER");
238 g_my_user_name = user;
240 g_my_user_name = "invalid-user";
243 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
245 #ifdef HAVE_STACKTRACE
246 void DumpStackTraceToString(string* stacktrace) {
247 DumpStackTrace(1, DebugWriteToString, stacktrace);
251 // We use an atomic operation to prevent problems with calling CrashReason
252 // from inside the Mutex implementation (potentially through RAW_CHECK).
253 static const CrashReason* g_reason = 0;
255 void SetCrashReason(const CrashReason* r) {
256 sync_val_compare_and_swap(&g_reason,
257 reinterpret_cast<const CrashReason*>(0),
261 } // namespace glog_internal_namespace_
263 void InitGoogleLogging(const char* argv0) {
264 const char* slash = strrchr(argv0, '/');
266 if (!slash) slash = strrchr(argv0, '\\');
268 g_program_invocation_short_name = slash ? slash + 1 : argv0;
269 g_main_thread_id = pthread_self();
271 #ifdef HAVE_STACKTRACE
272 InstallFailureFunction(&DumpStackTraceAndExit);
276 _END_GOOGLE_NAMESPACE_
278 // Make an implementation of stacktrace compiled.
280 # include STACKTRACE_H