6 #include "base/googleinit.h"
7 #include "stacktrace.h"
12 _START_GOOGLE_NAMESPACE_
14 static const char* g_program_invocation_short_name = NULL;
15 static pthread_t g_main_thread_id;
17 // The following APIs are all internal.
18 #ifdef HAVE_STACKTRACE
20 #include "stacktrace.h"
21 #include "symbolize.h"
22 #include "base/commandlineflags.h"
24 DEFINE_bool(symbolize_stacktrace, true,
25 "Symbolize the stack trace in the tombstone");
27 typedef void DebugWriter(const char*, void*);
29 // The %p field width for printf() functions is two characters per byte.
30 // For some environments, add two extra bytes for the leading "0x".
31 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
33 static void DebugWriteToStderr(const char* data, void *unused) {
34 // This one is signal-safe.
35 write(STDERR_FILENO, data, strlen(data));
38 // Print a program counter and its symbol name.
39 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
40 const char * const prefix) {
42 const char *symbol = "(unknown)";
43 // Symbolizes the previous address of pc because pc may be in the
44 // next function. The overrun happens when the function ends with
45 // a call to a function annotated noreturn (e.g. CHECK).
46 if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
50 snprintf(buf, sizeof(buf), "%s@ %*p %s\n",
51 prefix, kPrintfPointerFieldWidth, pc, symbol);
55 // Print a program counter and the corresponding stack frame size.
56 static void DumpPCAndFrameSize(DebugWriter *writerfn, void *arg, void *pc,
57 int framesize, const char * const prefix) {
60 snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n",
61 prefix, kPrintfPointerFieldWidth, pc);
63 snprintf(buf, sizeof(buf), "%s@ %*p %9d\n",
64 prefix, kPrintfPointerFieldWidth, pc, framesize);
69 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
70 const char * const prefix) {
72 snprintf(buf, sizeof(buf), "%s@ %*p\n",
73 prefix, kPrintfPointerFieldWidth, pc);
77 // Dump current stack trace as directed by writerfn
78 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
81 int depth = GetStackTrace(stack, sizeof(stack)/sizeof(*stack), skip_count+1);
82 for (int i = 0; i < depth; i++) {
84 if (FLAGS_symbolize_stacktrace) {
85 DumpPCAndSymbol(writerfn, arg, stack[i], " ");
87 DumpPC(writerfn, arg, stack[i], " ");
90 DumpPC(writerfn, arg, stack[i], " ");
95 static void DumpStackTraceAndExit() {
96 DumpStackTrace(1, DebugWriteToStderr, NULL);
101 namespace glog_internal_namespace_ {
103 const char* ProgramInvocationShortName() {
104 if (g_program_invocation_short_name != NULL) {
105 return g_program_invocation_short_name;
107 // TODO(hamaji): Use /proc/self/cmdline and so?
112 bool IsGoogleLoggingInitialized() {
113 return g_program_invocation_short_name != NULL;
116 bool is_default_thread() {
117 if (g_program_invocation_short_name == NULL) {
118 // InitGoogleLogging() not yet called, so unlikely to be in a different
122 return pthread_equal(pthread_self(), g_main_thread_id);
126 int64 CycleClock_Now() {
127 // TODO(hamaji): temporary impementation - it might be too slow.
129 gettimeofday(&tv, NULL);
130 return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
133 int64 UsecToCycles(int64 usec) {
137 static int32 g_main_thread_pid = getpid();
138 int32 GetMainThreadPid() {
139 return g_main_thread_pid;
142 static string g_my_user_name;
143 const string& MyUserName() {
144 return g_my_user_name;
146 static void MyUserNameInitializer() {
147 // TODO(hamaji): Probably this is not portable.
148 const char* user = getenv("USER");
150 g_my_user_name = user;
152 g_my_user_name = "invalid-user";
155 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
157 } // namespace glog_internal_namespace_
159 void InitGoogleLogging(const char* argv0) {
160 const char* slash = strrchr(argv0, '/');
162 if (!slash) slash = strrchr(argv0, '\\');
164 g_program_invocation_short_name = slash ? slash + 1 : argv0;
165 g_main_thread_id = pthread_self();
167 #ifdef HAVE_STACKTRACE
168 InstallFailureFunction(&DumpStackTraceAndExit);
172 _END_GOOGLE_NAMESPACE_