A bug fix for Windows: Use GetSystemTimeAsFileTime instead of GetSystemTime. SYSTEMTI...
[platform/upstream/glog.git] / src / utilities.cc
1 // Copyright (c) 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Author: Shinichiro Hamaji
31
32 #include "utilities.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <signal.h>
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41 #include <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()
46 #endif
47
48 #include "base/googleinit.h"
49
50 using std::string;
51
52 _START_GOOGLE_NAMESPACE_
53
54 static const char* g_program_invocation_short_name = NULL;
55 static pthread_t g_main_thread_id;
56
57 _END_GOOGLE_NAMESPACE_
58
59 // The following APIs are all internal.
60 #ifdef HAVE_STACKTRACE
61
62 #include "stacktrace.h"
63 #include "symbolize.h"
64 #include "base/commandlineflags.h"
65
66 DEFINE_bool(symbolize_stacktrace, true,
67             "Symbolize the stack trace in the tombstone");
68
69 _START_GOOGLE_NAMESPACE_
70
71 typedef void DebugWriter(const char*, void*);
72
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*);
76
77 static void DebugWriteToStderr(const char* data, void *unused) {
78   // This one is signal-safe.
79   write(STDERR_FILENO, data, strlen(data));
80 }
81
82 void DebugWriteToString(const char* data, void *arg) {
83   reinterpret_cast<string*>(arg)->append(data);
84 }
85
86 // Print a program counter and its symbol name.
87 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
88                             const char * const prefix) {
89   char tmp[1024];
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))) {
95       symbol = tmp;
96   }
97   char buf[1024];
98   snprintf(buf, sizeof(buf), "%s@ %*p  %s\n",
99            prefix, kPrintfPointerFieldWidth, pc, symbol);
100   writerfn(buf, arg);
101 }
102
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) {
106   char buf[100];
107   if (framesize <= 0) {
108     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n",
109              prefix, kPrintfPointerFieldWidth, pc);
110   } else {
111     snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n",
112              prefix, kPrintfPointerFieldWidth, pc, framesize);
113   }
114   writerfn(buf, arg);
115 }
116
117 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
118                    const char * const prefix) {
119   char buf[100];
120   snprintf(buf, sizeof(buf), "%s@ %*p\n",
121            prefix, kPrintfPointerFieldWidth, pc);
122   writerfn(buf, arg);
123 }
124
125 // Dump current stack trace as directed by writerfn
126 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
127   // Print stack trace
128   void* stack[32];
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], "    ");
134     } else {
135       DumpPC(writerfn, arg, stack[i], "    ");
136     }
137 #else
138     DumpPC(writerfn, arg, stack[i], "    ");
139 #endif
140   }
141 }
142
143 static void DumpStackTraceAndExit() {
144   DumpStackTrace(1, DebugWriteToStderr, NULL);
145
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);
152
153   abort();
154 }
155
156 _END_GOOGLE_NAMESPACE_
157
158 #endif  // HAVE_STACKTRACE
159
160 _START_GOOGLE_NAMESPACE_
161
162 namespace glog_internal_namespace_ {
163
164 const char* ProgramInvocationShortName() {
165   if (g_program_invocation_short_name != NULL) {
166     return g_program_invocation_short_name;
167   } else {
168     // TODO(hamaji): Use /proc/self/cmdline and so?
169     return "UNKNOWN";
170   }
171 }
172
173 bool IsGoogleLoggingInitialized() {
174   return g_program_invocation_short_name != NULL;
175 }
176
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
180     // thread
181     return true;
182   } else {
183     return pthread_equal(pthread_self(), g_main_thread_id);
184   }
185 }
186
187 #ifdef OS_WINDOWS
188 struct timeval {
189   long tv_sec, tv_usec;
190 };
191
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)
196   FILETIME ft;
197   LARGE_INTEGER li;
198   uint64 tt;
199
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;
206
207   return 0;
208 }
209 #endif
210
211 int64 CycleClock_Now() {
212   // TODO(hamaji): temporary impementation - it might be too slow.
213   struct timeval tv;
214   gettimeofday(&tv, NULL);
215   return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
216 }
217
218 int64 UsecToCycles(int64 usec) {
219   return usec;
220 }
221
222 WallTime WallTime_Now() {
223   // Now, cycle clock is retuning microseconds since the epoch.
224   return CycleClock_Now() * 0.000001;
225 }
226
227 static int32 g_main_thread_pid = getpid();
228 int32 GetMainThreadPid() {
229   return g_main_thread_pid;
230 }
231
232 pid_t GetTID() {
233   // On Linux and FreeBSD, we try to use gettid().
234 #if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
235 #ifndef __NR_gettid
236 #ifdef OS_MACOSX
237 #define __NR_gettid SYS_gettid
238 #elif ! defined __i386__
239 #error "Must define __NR_gettid for non-x86 platforms"
240 #else
241 #define __NR_gettid 224
242 #endif
243 #endif
244   static bool lacks_gettid = false;
245   if (!lacks_gettid) {
246     pid_t tid = syscall(__NR_gettid);
247     if (tid != -1) {
248       return tid;
249     }
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".
254     lacks_gettid = true;
255   }
256 #endif  // OS_LINUX || OS_FREEBSD
257
258   // If gettid() could not be used, we use one of the following.
259 #if defined OS_LINUX
260   return getpid();  // Linux:  getpid returns thread ID when gettid is absent
261 #elif defined OS_WINDOWS || defined OS_CYGWIN
262   return GetCurrentThreadId();
263 #else
264   // If none of the techniques above worked, we use pthread_self().
265   return (pid_t)pthread_self();
266 #endif
267 }
268
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
272   if (!base)
273     base = strrchr(filepath, '\\');
274 #endif
275   return base ? (base+1) : filepath;
276 }
277
278 static string g_my_user_name;
279 const string& MyUserName() {
280   return g_my_user_name;
281 }
282 static void MyUserNameInitializer() {
283   // TODO(hamaji): Probably this is not portable.
284   const char* user = getenv("USER");
285   if (user != NULL) {
286     g_my_user_name = user;
287   } else {
288     g_my_user_name = "invalid-user";
289   }
290 }
291 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
292
293 #ifdef HAVE_STACKTRACE
294 void DumpStackTraceToString(string* stacktrace) {
295   DumpStackTrace(1, DebugWriteToString, stacktrace);
296 }
297 #endif
298
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;
302
303 void SetCrashReason(const CrashReason* r) {
304   sync_val_compare_and_swap(&g_reason,
305                             reinterpret_cast<const CrashReason*>(0),
306                             r);
307 }
308
309 }  // namespace glog_internal_namespace_
310
311 void InitGoogleLogging(const char* argv0) {
312   const char* slash = strrchr(argv0, '/');
313 #ifdef OS_WINDOWS
314   if (!slash)  slash = strrchr(argv0, '\\');
315 #endif
316   g_program_invocation_short_name = slash ? slash + 1 : argv0;
317   g_main_thread_id = pthread_self();
318
319 #ifdef HAVE_STACKTRACE
320   InstallFailureFunction(&DumpStackTraceAndExit);
321 #endif
322 }
323
324 _END_GOOGLE_NAMESPACE_
325
326 // Make an implementation of stacktrace compiled.
327 #ifdef STACKTRACE_H
328 # include STACKTRACE_H
329 #endif