Remove unnecessary #include.
[platform/upstream/glog.git] / src / utilities.cc
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
3
4 #include "utilities.h"
5
6 #include <signal.h>
7 #ifdef HAVE_SYS_TIME_H
8 # include <sys/time.h>
9 #endif
10 #include <time.h>
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()
15 #endif
16
17 #include "base/googleinit.h"
18
19 using std::string;
20
21 _START_GOOGLE_NAMESPACE_
22
23 static const char* g_program_invocation_short_name = NULL;
24 static pthread_t g_main_thread_id;
25
26 _END_GOOGLE_NAMESPACE_
27
28 // The following APIs are all internal.
29 #ifdef HAVE_STACKTRACE
30
31 #include "stacktrace.h"
32 #include "symbolize.h"
33 #include "base/commandlineflags.h"
34
35 DEFINE_bool(symbolize_stacktrace, true,
36             "Symbolize the stack trace in the tombstone");
37
38 _START_GOOGLE_NAMESPACE_
39
40 typedef void DebugWriter(const char*, void*);
41
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*);
45
46 static void DebugWriteToStderr(const char* data, void *unused) {
47   // This one is signal-safe.
48   write(STDERR_FILENO, data, strlen(data));
49 }
50
51 void DebugWriteToString(const char* data, void *arg) {
52   reinterpret_cast<string*>(arg)->append(data);
53 }
54
55 // Print a program counter and its symbol name.
56 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
57                             const char * const prefix) {
58   char tmp[1024];
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))) {
64       symbol = tmp;
65   }
66   char buf[1024];
67   snprintf(buf, sizeof(buf), "%s@ %*p  %s\n",
68            prefix, kPrintfPointerFieldWidth, pc, symbol);
69   writerfn(buf, arg);
70 }
71
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) {
75   char buf[100];
76   if (framesize <= 0) {
77     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n",
78              prefix, kPrintfPointerFieldWidth, pc);
79   } else {
80     snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n",
81              prefix, kPrintfPointerFieldWidth, pc, framesize);
82   }
83   writerfn(buf, arg);
84 }
85
86 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
87                    const char * const prefix) {
88   char buf[100];
89   snprintf(buf, sizeof(buf), "%s@ %*p\n",
90            prefix, kPrintfPointerFieldWidth, pc);
91   writerfn(buf, arg);
92 }
93
94 // Dump current stack trace as directed by writerfn
95 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
96   // Print stack trace
97   void* stack[32];
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], "    ");
103     } else {
104       DumpPC(writerfn, arg, stack[i], "    ");
105     }
106 #else
107     DumpPC(writerfn, arg, stack[i], "    ");
108 #endif
109   }
110 }
111
112 static void DumpStackTraceAndExit() {
113   DumpStackTrace(1, DebugWriteToStderr, NULL);
114
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);
121
122   abort();
123 }
124
125 _END_GOOGLE_NAMESPACE_
126
127 #endif  // HAVE_STACKTRACE
128
129 _START_GOOGLE_NAMESPACE_
130
131 namespace glog_internal_namespace_ {
132
133 const char* ProgramInvocationShortName() {
134   if (g_program_invocation_short_name != NULL) {
135     return g_program_invocation_short_name;
136   } else {
137     // TODO(hamaji): Use /proc/self/cmdline and so?
138     return "UNKNOWN";
139   }
140 }
141
142 bool IsGoogleLoggingInitialized() {
143   return g_program_invocation_short_name != NULL;
144 }
145
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
149     // thread
150     return true;
151   } else {
152     return pthread_equal(pthread_self(), g_main_thread_id);
153   }
154 }
155
156 int64 CycleClock_Now() {
157   // TODO(hamaji): temporary impementation - it might be too slow.
158 #ifdef OS_WINDOWS
159   SYSTEMTIME now;
160   GetSystemTime(&now);
161   return (static_cast<int64>(now.wSecond) * 1000000 +
162           static_cast<int64>(now.wMilliseconds) * 1000);
163 #else
164   struct timeval tv;
165   gettimeofday(&tv, NULL);
166   return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
167 #endif
168 }
169
170 int64 UsecToCycles(int64 usec) {
171   return usec;
172 }
173
174 WallTime WallTime_Now() {
175   // Now, cycle clock is retuning microseconds since the epoch.
176   return CycleClock_Now() * 0.000001;
177 }
178
179 static int32 g_main_thread_pid = getpid();
180 int32 GetMainThreadPid() {
181   return g_main_thread_pid;
182 }
183
184 pid_t GetTID() {
185   // On Linux and FreeBSD, we try to use gettid().
186 #if defined OS_LINUX || defined OS_FREEBSD || defined OS_MACOSX
187 #ifndef __NR_gettid
188 #ifdef OS_MACOSX
189 #define __NR_gettid SYS_gettid
190 #elif ! defined __i386__
191 #error "Must define __NR_gettid for non-x86 platforms"
192 #else
193 #define __NR_gettid 224
194 #endif
195 #endif
196   static bool lacks_gettid = false;
197   if (!lacks_gettid) {
198     pid_t tid = syscall(__NR_gettid);
199     if (tid != -1) {
200       return tid;
201     }
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".
206     lacks_gettid = true;
207   }
208 #endif  // OS_LINUX || OS_FREEBSD
209
210   // If gettid() could not be used, we use one of the following.
211 #if defined OS_LINUX
212   return getpid();  // Linux:  getpid returns thread ID when gettid is absent
213 #elif defined OS_WINDOWS || defined OS_CYGWIN
214   return GetCurrentThreadId();
215 #else
216   // If none of the techniques above worked, we use pthread_self().
217   return (pid_t)pthread_self();
218 #endif
219 }
220
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
224   if (!base)
225     base = strrchr(filepath, '\\');
226 #endif
227   return base ? (base+1) : filepath;
228 }
229
230 static string g_my_user_name;
231 const string& MyUserName() {
232   return g_my_user_name;
233 }
234 static void MyUserNameInitializer() {
235   // TODO(hamaji): Probably this is not portable.
236   const char* user = getenv("USER");
237   if (user != NULL) {
238     g_my_user_name = user;
239   } else {
240     g_my_user_name = "invalid-user";
241   }
242 }
243 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
244
245 #ifdef HAVE_STACKTRACE
246 void DumpStackTraceToString(string* stacktrace) {
247   DumpStackTrace(1, DebugWriteToString, stacktrace);
248 }
249 #endif
250
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;
254
255 void SetCrashReason(const CrashReason* r) {
256   sync_val_compare_and_swap(&g_reason,
257                             reinterpret_cast<const CrashReason*>(0),
258                             r);
259 }
260
261 }  // namespace glog_internal_namespace_
262
263 void InitGoogleLogging(const char* argv0) {
264   const char* slash = strrchr(argv0, '/');
265 #ifdef OS_WINDOWS
266   if (!slash)  slash = strrchr(argv0, '\\');
267 #endif
268   g_program_invocation_short_name = slash ? slash + 1 : argv0;
269   g_main_thread_id = pthread_self();
270
271 #ifdef HAVE_STACKTRACE
272   InstallFailureFunction(&DumpStackTraceAndExit);
273 #endif
274 }
275
276 _END_GOOGLE_NAMESPACE_
277
278 // Make an implementation of stacktrace compiled.
279 #ifdef STACKTRACE_H
280 # include STACKTRACE_H
281 #endif