1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: Shinichiro Hamaji
32 #include "utilities.h"
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #if defined(HAVE_SYSCALL_H)
43 #include <syscall.h> // for syscall()
44 #elif defined(HAVE_SYS_SYSCALL_H)
45 #include <sys/syscall.h> // for syscall()
48 #include "base/googleinit.h"
52 _START_GOOGLE_NAMESPACE_
54 static const char* g_program_invocation_short_name = NULL;
55 static pthread_t g_main_thread_id;
57 _END_GOOGLE_NAMESPACE_
59 // The following APIs are all internal.
60 #ifdef HAVE_STACKTRACE
62 #include "stacktrace.h"
63 #include "symbolize.h"
64 #include "base/commandlineflags.h"
66 DEFINE_bool(symbolize_stacktrace, true,
67 "Symbolize the stack trace in the tombstone");
69 _START_GOOGLE_NAMESPACE_
71 typedef void DebugWriter(const char*, void*);
73 // The %p field width for printf() functions is two characters per byte.
74 // For some environments, add two extra bytes for the leading "0x".
75 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
77 static void DebugWriteToStderr(const char* data, void *unused) {
78 // This one is signal-safe.
79 write(STDERR_FILENO, data, strlen(data));
82 void DebugWriteToString(const char* data, void *arg) {
83 reinterpret_cast<string*>(arg)->append(data);
86 // Print a program counter and its symbol name.
87 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
88 const char * const prefix) {
90 const char *symbol = "(unknown)";
91 // Symbolizes the previous address of pc because pc may be in the
92 // next function. The overrun happens when the function ends with
93 // a call to a function annotated noreturn (e.g. CHECK).
94 if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
98 snprintf(buf, sizeof(buf), "%s@ %*p %s\n",
99 prefix, kPrintfPointerFieldWidth, pc, symbol);
103 // Print a program counter and the corresponding stack frame size.
104 static void DumpPCAndFrameSize(DebugWriter *writerfn, void *arg, void *pc,
105 int framesize, const char * const prefix) {
107 if (framesize <= 0) {
108 snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n",
109 prefix, kPrintfPointerFieldWidth, pc);
111 snprintf(buf, sizeof(buf), "%s@ %*p %9d\n",
112 prefix, kPrintfPointerFieldWidth, pc, framesize);
117 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
118 const char * const prefix) {
120 snprintf(buf, sizeof(buf), "%s@ %*p\n",
121 prefix, kPrintfPointerFieldWidth, pc);
125 // Dump current stack trace as directed by writerfn
126 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
129 int depth = GetStackTrace(stack, ARRAYSIZE(stack), skip_count+1);
130 for (int i = 0; i < depth; i++) {
131 #if defined(HAVE_SYMBOLIZE)
132 if (FLAGS_symbolize_stacktrace) {
133 DumpPCAndSymbol(writerfn, arg, stack[i], " ");
135 DumpPC(writerfn, arg, stack[i], " ");
138 DumpPC(writerfn, arg, stack[i], " ");
143 static void DumpStackTraceAndExit() {
144 DumpStackTrace(1, DebugWriteToStderr, NULL);
146 // Set the default signal handler for SIGABRT, to avoid invoking our
147 // own signal handler installed by InstallFailedSignalHandler().
148 struct sigaction sig_action = {}; // Zero-clear.
149 sigemptyset(&sig_action.sa_mask);
150 sig_action.sa_handler = SIG_DFL;
151 sigaction(SIGABRT, &sig_action, NULL);
156 _END_GOOGLE_NAMESPACE_
158 #endif // HAVE_STACKTRACE
160 _START_GOOGLE_NAMESPACE_
162 namespace glog_internal_namespace_ {
164 const char* ProgramInvocationShortName() {
165 if (g_program_invocation_short_name != NULL) {
166 return g_program_invocation_short_name;
168 // TODO(hamaji): Use /proc/self/cmdline and so?
173 bool IsGoogleLoggingInitialized() {
174 return g_program_invocation_short_name != NULL;
177 bool is_default_thread() {
178 if (g_program_invocation_short_name == NULL) {
179 // InitGoogleLogging() not yet called, so unlikely to be in a different
183 return pthread_equal(pthread_self(), g_main_thread_id);
189 long tv_sec, tv_usec;
192 // Based on: http://www.google.com/codesearch/p?hl=en#dR3YEbitojA/os_win32.c&q=GetSystemTimeAsFileTime%20license:bsd
193 // See COPYING for copyright information.
194 static int gettimeofday(struct timeval *tv, void* tz) {
195 #define EPOCHFILETIME (116444736000000000ULL)
200 GetSystemTimeAsFileTime(&ft);
201 li.LowPart = ft.dwLowDateTime;
202 li.HighPart = ft.dwHighDateTime;
203 tt = (li.QuadPart - EPOCHFILETIME) / 10;
204 tv->tv_sec = tt / 1000000;
205 tv->tv_usec = tt % 1000000;
211 int64 CycleClock_Now() {
212 // TODO(hamaji): temporary impementation - it might be too slow.
214 gettimeofday(&tv, NULL);
215 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
218 int64 UsecToCycles(int64 usec) {
222 WallTime WallTime_Now() {
223 // Now, cycle clock is retuning microseconds since the epoch.
224 return CycleClock_Now() * 0.000001;
227 static int32 g_main_thread_pid = getpid();
228 int32 GetMainThreadPid() {
229 return g_main_thread_pid;
233 // On Linux and FreeBSD, we try to use gettid().
234 #if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
237 #define __NR_gettid SYS_gettid
238 #elif ! defined __i386__
239 #error "Must define __NR_gettid for non-x86 platforms"
241 #define __NR_gettid 224
244 static bool lacks_gettid = false;
246 pid_t tid = syscall(__NR_gettid);
250 // Technically, this variable has to be volatile, but there is a small
251 // performance penalty in accessing volatile variables and there should
252 // not be any serious adverse effect if a thread does not immediately see
253 // the value change to "true".
256 #endif // OS_LINUX || OS_FREEBSD
258 // If gettid() could not be used, we use one of the following.
260 return getpid(); // Linux: getpid returns thread ID when gettid is absent
261 #elif defined OS_WINDOWS || defined OS_CYGWIN
262 return GetCurrentThreadId();
264 // If none of the techniques above worked, we use pthread_self().
265 return (pid_t)pthread_self();
269 const char* const_basename(const char* filepath) {
270 const char* base = strrchr(filepath, '/');
271 #ifdef OS_WINDOWS // Look for either path separator in Windows
273 base = strrchr(filepath, '\\');
275 return base ? (base+1) : filepath;
278 static string g_my_user_name;
279 const string& MyUserName() {
280 return g_my_user_name;
282 static void MyUserNameInitializer() {
283 // TODO(hamaji): Probably this is not portable.
284 const char* user = getenv("USER");
286 g_my_user_name = user;
288 g_my_user_name = "invalid-user";
291 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
293 #ifdef HAVE_STACKTRACE
294 void DumpStackTraceToString(string* stacktrace) {
295 DumpStackTrace(1, DebugWriteToString, stacktrace);
299 // We use an atomic operation to prevent problems with calling CrashReason
300 // from inside the Mutex implementation (potentially through RAW_CHECK).
301 static const CrashReason* g_reason = 0;
303 void SetCrashReason(const CrashReason* r) {
304 sync_val_compare_and_swap(&g_reason,
305 reinterpret_cast<const CrashReason*>(0),
309 } // namespace glog_internal_namespace_
311 void InitGoogleLogging(const char* argv0) {
312 CHECK(!IsGoogleLoggingInitialized())
313 << "You called InitGoogleLogging() twice!";
314 const char* slash = strrchr(argv0, '/');
316 if (!slash) slash = strrchr(argv0, '\\');
318 g_program_invocation_short_name = slash ? slash + 1 : argv0;
319 g_main_thread_id = pthread_self();
321 #ifdef HAVE_STACKTRACE
322 InstallFailureFunction(&DumpStackTraceAndExit);
326 _END_GOOGLE_NAMESPACE_
328 // Make an implementation of stacktrace compiled.
330 # include STACKTRACE_H