1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
12 #include "base/googleinit.h"
13 #include "stacktrace.h"
14 #include "symbolize.h"
18 _START_GOOGLE_NAMESPACE_
20 static const char* g_program_invocation_short_name = NULL;
21 static pthread_t g_main_thread_id;
23 // The following APIs are all internal.
24 #ifdef HAVE_STACKTRACE
26 #include "stacktrace.h"
27 #include "symbolize.h"
28 #include "base/commandlineflags.h"
30 DEFINE_bool(symbolize_stacktrace, true,
31 "Symbolize the stack trace in the tombstone");
33 typedef void DebugWriter(const char*, void*);
35 // The %p field width for printf() functions is two characters per byte.
36 // For some environments, add two extra bytes for the leading "0x".
37 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
39 static void DebugWriteToStderr(const char* data, void *unused) {
40 // This one is signal-safe.
41 write(STDERR_FILENO, data, strlen(data));
44 // Print a program counter and its symbol name.
45 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
46 const char * const prefix) {
48 const char *symbol = "(unknown)";
49 // Symbolizes the previous address of pc because pc may be in the
50 // next function. The overrun happens when the function ends with
51 // a call to a function annotated noreturn (e.g. CHECK).
52 if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
56 snprintf(buf, sizeof(buf), "%s@ %*p %s\n",
57 prefix, kPrintfPointerFieldWidth, pc, symbol);
61 // Print a program counter and the corresponding stack frame size.
62 static void DumpPCAndFrameSize(DebugWriter *writerfn, void *arg, void *pc,
63 int framesize, const char * const prefix) {
66 snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n",
67 prefix, kPrintfPointerFieldWidth, pc);
69 snprintf(buf, sizeof(buf), "%s@ %*p %9d\n",
70 prefix, kPrintfPointerFieldWidth, pc, framesize);
75 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
76 const char * const prefix) {
78 snprintf(buf, sizeof(buf), "%s@ %*p\n",
79 prefix, kPrintfPointerFieldWidth, pc);
83 // Dump current stack trace as directed by writerfn
84 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
87 int depth = GetStackTrace(stack, ARRAYSIZE(stack), skip_count+1);
88 for (int i = 0; i < depth; i++) {
89 #if defined(HAVE_SYMBOLIZE)
90 if (FLAGS_symbolize_stacktrace) {
91 DumpPCAndSymbol(writerfn, arg, stack[i], " ");
93 DumpPC(writerfn, arg, stack[i], " ");
96 DumpPC(writerfn, arg, stack[i], " ");
101 static void DumpStackTraceAndExit() {
102 DumpStackTrace(1, DebugWriteToStderr, NULL);
104 // Set the default signal handler for SIGABRT, to avoid invoking our
105 // own signal handler installed by InstallFailedSignalHandler().
106 struct sigaction sig_action = {}; // Zero-clear.
107 sigemptyset(&sig_action.sa_mask);
108 sig_action.sa_handler = SIG_DFL;
109 sigaction(SIGABRT, &sig_action, NULL);
114 #endif // HAVE_STACKTRACE
116 namespace glog_internal_namespace_ {
118 const char* ProgramInvocationShortName() {
119 if (g_program_invocation_short_name != NULL) {
120 return g_program_invocation_short_name;
122 // TODO(hamaji): Use /proc/self/cmdline and so?
127 bool IsGoogleLoggingInitialized() {
128 return g_program_invocation_short_name != NULL;
131 bool is_default_thread() {
132 if (g_program_invocation_short_name == NULL) {
133 // InitGoogleLogging() not yet called, so unlikely to be in a different
137 return pthread_equal(pthread_self(), g_main_thread_id);
141 int64 CycleClock_Now() {
142 // TODO(hamaji): temporary impementation - it might be too slow.
146 return (static_cast<int64>(now.wSecond) * 1000000 +
147 static_cast<int64>(now.wMilliseconds) * 1000);
149 // TODO(hamaji): temporary impementation - it might be too slow.
151 gettimeofday(&tv, NULL);
152 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
156 int64 UsecToCycles(int64 usec) {
160 static int32 g_main_thread_pid = getpid();
161 int32 GetMainThreadPid() {
162 return g_main_thread_pid;
165 const char* const_basename(const char* filepath) {
166 const char* base = strrchr(filepath, '/');
167 #ifdef OS_WINDOWS // Look for either path separator in Windows
169 base = strrchr(filepath, '\\');
171 return base ? (base+1) : filepath;
174 static string g_my_user_name;
175 const string& MyUserName() {
176 return g_my_user_name;
178 static void MyUserNameInitializer() {
179 // TODO(hamaji): Probably this is not portable.
180 const char* user = getenv("USER");
182 g_my_user_name = user;
184 g_my_user_name = "invalid-user";
187 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
189 } // namespace glog_internal_namespace_
191 void InitGoogleLogging(const char* argv0) {
192 const char* slash = strrchr(argv0, '/');
194 if (!slash) slash = strrchr(argv0, '\\');
196 g_program_invocation_short_name = slash ? slash + 1 : argv0;
197 g_main_thread_id = pthread_self();
199 #ifdef HAVE_STACKTRACE
200 InstallFailureFunction(&DumpStackTraceAndExit);
204 _END_GOOGLE_NAMESPACE_
206 // Make an implementation of stacktrace compiled.
208 # include STACKTRACE_H