Merge pull request #209 from sergiud/cmake-icc-fix
[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 #ifdef HAVE_SYSLOG_H
48 # include <syslog.h>
49 #endif
50
51 #include "base/googleinit.h"
52
53 using std::string;
54
55 _START_GOOGLE_NAMESPACE_
56
57 static const char* g_program_invocation_short_name = NULL;
58 static pthread_t g_main_thread_id;
59
60 _END_GOOGLE_NAMESPACE_
61
62 // The following APIs are all internal.
63 #ifdef HAVE_STACKTRACE
64
65 #include "stacktrace.h"
66 #include "symbolize.h"
67 #include "base/commandlineflags.h"
68
69 GLOG_DEFINE_bool(symbolize_stacktrace, true,
70                  "Symbolize the stack trace in the tombstone");
71
72 _START_GOOGLE_NAMESPACE_
73
74 typedef void DebugWriter(const char*, void*);
75
76 // The %p field width for printf() functions is two characters per byte.
77 // For some environments, add two extra bytes for the leading "0x".
78 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
79
80 static void DebugWriteToStderr(const char* data, void *) {
81   // This one is signal-safe.
82   if (write(STDERR_FILENO, data, strlen(data)) < 0) {
83     // Ignore errors.
84   }
85 }
86
87 void DebugWriteToString(const char* data, void *arg) {
88   reinterpret_cast<string*>(arg)->append(data);
89 }
90
91 #ifdef HAVE_SYMBOLIZE
92 // Print a program counter and its symbol name.
93 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
94                             const char * const prefix) {
95   char tmp[1024];
96   const char *symbol = "(unknown)";
97   // Symbolizes the previous address of pc because pc may be in the
98   // next function.  The overrun happens when the function ends with
99   // a call to a function annotated noreturn (e.g. CHECK).
100   if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
101       symbol = tmp;
102   }
103   char buf[1024];
104   snprintf(buf, sizeof(buf), "%s@ %*p  %s\n",
105            prefix, kPrintfPointerFieldWidth, pc, symbol);
106   writerfn(buf, arg);
107 }
108 #endif
109
110 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
111                    const char * const prefix) {
112   char buf[100];
113   snprintf(buf, sizeof(buf), "%s@ %*p\n",
114            prefix, kPrintfPointerFieldWidth, pc);
115   writerfn(buf, arg);
116 }
117
118 // Dump current stack trace as directed by writerfn
119 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
120   // Print stack trace
121   void* stack[32];
122   int depth = GetStackTrace(stack, ARRAYSIZE(stack), skip_count+1);
123   for (int i = 0; i < depth; i++) {
124 #if defined(HAVE_SYMBOLIZE)
125     if (FLAGS_symbolize_stacktrace) {
126       DumpPCAndSymbol(writerfn, arg, stack[i], "    ");
127     } else {
128       DumpPC(writerfn, arg, stack[i], "    ");
129     }
130 #else
131     DumpPC(writerfn, arg, stack[i], "    ");
132 #endif
133   }
134 }
135
136 static void DumpStackTraceAndExit() {
137   DumpStackTrace(1, DebugWriteToStderr, NULL);
138
139   // TOOD(hamaji): Use signal instead of sigaction?
140   if (IsFailureSignalHandlerInstalled()) {
141     // Set the default signal handler for SIGABRT, to avoid invoking our
142     // own signal handler installed by InstallFailureSignalHandler().
143 #ifdef HAVE_SIGACTION
144     struct sigaction sig_action;
145     memset(&sig_action, 0, sizeof(sig_action));
146     sigemptyset(&sig_action.sa_mask);
147     sig_action.sa_handler = SIG_DFL;
148     sigaction(SIGABRT, &sig_action, NULL);
149 #elif defined(OS_WINDOWS)
150     signal(SIGABRT, SIG_DFL);
151 #endif  // HAVE_SIGACTION
152   }
153
154   abort();
155 }
156
157 _END_GOOGLE_NAMESPACE_
158
159 #endif  // HAVE_STACKTRACE
160
161 _START_GOOGLE_NAMESPACE_
162
163 namespace glog_internal_namespace_ {
164
165 const char* ProgramInvocationShortName() {
166   if (g_program_invocation_short_name != NULL) {
167     return g_program_invocation_short_name;
168   } else {
169     // TODO(hamaji): Use /proc/self/cmdline and so?
170     return "UNKNOWN";
171   }
172 }
173
174 bool IsGoogleLoggingInitialized() {
175   return g_program_invocation_short_name != NULL;
176 }
177
178 bool is_default_thread() {
179   if (g_program_invocation_short_name == NULL) {
180     // InitGoogleLogging() not yet called, so unlikely to be in a different
181     // thread
182     return true;
183   } else {
184     return pthread_equal(pthread_self(), g_main_thread_id);
185   }
186 }
187
188 #ifdef OS_WINDOWS
189 struct timeval {
190   long tv_sec, tv_usec;
191 };
192
193 // Based on: http://www.google.com/codesearch/p?hl=en#dR3YEbitojA/os_win32.c&q=GetSystemTimeAsFileTime%20license:bsd
194 // See COPYING for copyright information.
195 static int gettimeofday(struct timeval *tv, void* tz) {
196 #define EPOCHFILETIME (116444736000000000ULL)
197   FILETIME ft;
198   LARGE_INTEGER li;
199   uint64 tt;
200
201   GetSystemTimeAsFileTime(&ft);
202   li.LowPart = ft.dwLowDateTime;
203   li.HighPart = ft.dwHighDateTime;
204   tt = (li.QuadPart - EPOCHFILETIME) / 10;
205   tv->tv_sec = tt / 1000000;
206   tv->tv_usec = tt % 1000000;
207
208   return 0;
209 }
210 #endif
211
212 int64 CycleClock_Now() {
213   // TODO(hamaji): temporary impementation - it might be too slow.
214   struct timeval tv;
215   gettimeofday(&tv, NULL);
216   return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
217 }
218
219 int64 UsecToCycles(int64 usec) {
220   return usec;
221 }
222
223 WallTime WallTime_Now() {
224   // Now, cycle clock is retuning microseconds since the epoch.
225   return CycleClock_Now() * 0.000001;
226 }
227
228 static int32 g_main_thread_pid = getpid();
229 int32 GetMainThreadPid() {
230   return g_main_thread_pid;
231 }
232
233 bool PidHasChanged() {
234   int32 pid = getpid();
235   if (g_main_thread_pid == pid) {
236     return false;
237   }
238   g_main_thread_pid = pid;
239   return true;
240 }
241
242 pid_t GetTID() {
243   // On Linux and MacOSX, we try to use gettid().
244 #if defined OS_LINUX || defined OS_MACOSX
245 #ifndef __NR_gettid
246 #ifdef OS_MACOSX
247 #define __NR_gettid SYS_gettid
248 #elif ! defined __i386__
249 #error "Must define __NR_gettid for non-x86 platforms"
250 #else
251 #define __NR_gettid 224
252 #endif
253 #endif
254   static bool lacks_gettid = false;
255   if (!lacks_gettid) {
256     pid_t tid = syscall(__NR_gettid);
257     if (tid != -1) {
258       return tid;
259     }
260     // Technically, this variable has to be volatile, but there is a small
261     // performance penalty in accessing volatile variables and there should
262     // not be any serious adverse effect if a thread does not immediately see
263     // the value change to "true".
264     lacks_gettid = true;
265   }
266 #endif  // OS_LINUX || OS_MACOSX
267
268   // If gettid() could not be used, we use one of the following.
269 #if defined OS_LINUX
270   return getpid();  // Linux:  getpid returns thread ID when gettid is absent
271 #elif defined OS_WINDOWS && !defined OS_CYGWIN
272   return GetCurrentThreadId();
273 #else
274   // If none of the techniques above worked, we use pthread_self().
275   return (pid_t)(uintptr_t)pthread_self();
276 #endif
277 }
278
279 const char* const_basename(const char* filepath) {
280   const char* base = strrchr(filepath, '/');
281 #ifdef OS_WINDOWS  // Look for either path separator in Windows
282   if (!base)
283     base = strrchr(filepath, '\\');
284 #endif
285   return base ? (base+1) : filepath;
286 }
287
288 static string g_my_user_name;
289 const string& MyUserName() {
290   return g_my_user_name;
291 }
292 static void MyUserNameInitializer() {
293   // TODO(hamaji): Probably this is not portable.
294 #if defined(OS_WINDOWS)
295   const char* user = getenv("USERNAME");
296 #else
297   const char* user = getenv("USER");
298 #endif
299   if (user != NULL) {
300     g_my_user_name = user;
301   } else {
302     g_my_user_name = "invalid-user";
303   }
304 }
305 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
306
307 #ifdef HAVE_STACKTRACE
308 void DumpStackTraceToString(string* stacktrace) {
309   DumpStackTrace(1, DebugWriteToString, stacktrace);
310 }
311 #endif
312
313 // We use an atomic operation to prevent problems with calling CrashReason
314 // from inside the Mutex implementation (potentially through RAW_CHECK).
315 static const CrashReason* g_reason = 0;
316
317 void SetCrashReason(const CrashReason* r) {
318   sync_val_compare_and_swap(&g_reason,
319                             reinterpret_cast<const CrashReason*>(0),
320                             r);
321 }
322
323 void InitGoogleLoggingUtilities(const char* argv0) {
324   CHECK(!IsGoogleLoggingInitialized())
325       << "You called InitGoogleLogging() twice!";
326   const char* slash = strrchr(argv0, '/');
327 #ifdef OS_WINDOWS
328   if (!slash)  slash = strrchr(argv0, '\\');
329 #endif
330   g_program_invocation_short_name = slash ? slash + 1 : argv0;
331   g_main_thread_id = pthread_self();
332
333 #ifdef HAVE_STACKTRACE
334   InstallFailureFunction(&DumpStackTraceAndExit);
335 #endif
336 }
337
338 void ShutdownGoogleLoggingUtilities() {
339   CHECK(IsGoogleLoggingInitialized())
340       << "You called ShutdownGoogleLogging() without calling InitGoogleLogging() first!";
341   g_program_invocation_short_name = NULL;
342 #ifdef HAVE_SYSLOG_H
343   closelog();
344 #endif
345 }
346
347 }  // namespace glog_internal_namespace_
348
349 _END_GOOGLE_NAMESPACE_
350
351 // Make an implementation of stacktrace compiled.
352 #ifdef STACKTRACE_H
353 # include STACKTRACE_H
354 # if 0
355 // For include scanners which can't handle macro expansions.
356 #  include "stacktrace_libunwind-inl.h"
357 #  include "stacktrace_x86-inl.h"
358 #  include "stacktrace_x86_64-inl.h"
359 #  include "stacktrace_powerpc-inl.h"
360 #  include "stacktrace_generic-inl.h"
361 # endif
362 #endif