glog 0.1
[platform/upstream/glog.git] / src / utilities.cc
1 #include "utilities.h"
2
3 #include <sys/time.h>
4 #include <time.h>
5
6 #include "base/googleinit.h"
7 #include "stacktrace.h"
8 #include "symbolize.h"
9
10 using std::string;
11
12 _START_GOOGLE_NAMESPACE_
13
14 static const char* g_program_invocation_short_name = NULL;
15 static pthread_t g_main_thread_id;
16
17 // The following APIs are all internal.
18 #ifdef HAVE_STACKTRACE
19
20 #include "stacktrace.h"
21 #include "symbolize.h"
22 #include "base/commandlineflags.h"
23
24 DEFINE_bool(symbolize_stacktrace, true,
25             "Symbolize the stack trace in the tombstone");
26
27 typedef void DebugWriter(const char*, void*);
28
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*);
32
33 static void DebugWriteToStderr(const char* data, void *unused) {
34   // This one is signal-safe.
35   write(STDERR_FILENO, data, strlen(data));
36 }
37
38 // Print a program counter and its symbol name.
39 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
40                             const char * const prefix) {
41   char tmp[1024];
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))) {
47       symbol = tmp;
48   }
49   char buf[1024];
50   snprintf(buf, sizeof(buf), "%s@ %*p  %s\n",
51            prefix, kPrintfPointerFieldWidth, pc, symbol);
52   writerfn(buf, arg);
53 }
54
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) {
58   char buf[100];
59   if (framesize <= 0) {
60     snprintf(buf, sizeof(buf), "%s@ %*p  (unknown)\n",
61              prefix, kPrintfPointerFieldWidth, pc);
62   } else {
63     snprintf(buf, sizeof(buf), "%s@ %*p  %9d\n",
64              prefix, kPrintfPointerFieldWidth, pc, framesize);
65   }
66   writerfn(buf, arg);
67 }
68
69 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
70                    const char * const prefix) {
71   char buf[100];
72   snprintf(buf, sizeof(buf), "%s@ %*p\n",
73            prefix, kPrintfPointerFieldWidth, pc);
74   writerfn(buf, arg);
75 }
76
77 // Dump current stack trace as directed by writerfn
78 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
79   // Print stack trace
80   void* stack[32];
81   int depth = GetStackTrace(stack, sizeof(stack)/sizeof(*stack), skip_count+1);
82   for (int i = 0; i < depth; i++) {
83 #if defined(__ELF__)
84     if (FLAGS_symbolize_stacktrace) {
85       DumpPCAndSymbol(writerfn, arg, stack[i], "    ");
86     } else {
87       DumpPC(writerfn, arg, stack[i], "    ");
88     }
89 #else
90     DumpPC(writerfn, arg, stack[i], "    ");
91 #endif
92   }
93 }
94
95 static void DumpStackTraceAndExit() {
96   DumpStackTrace(1, DebugWriteToStderr, NULL);
97   exit(1);
98 }
99 #endif
100
101 namespace glog_internal_namespace_ {
102
103 const char* ProgramInvocationShortName() {
104   if (g_program_invocation_short_name != NULL) {
105     return g_program_invocation_short_name;
106   } else {
107     // TODO(hamaji): Use /proc/self/cmdline and so?
108     return "UNKNOWN";
109   }
110 }
111
112 bool IsGoogleLoggingInitialized() {
113   return g_program_invocation_short_name != NULL;
114 }
115
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
119     // thread
120     return true;
121   } else {
122     return pthread_equal(pthread_self(), g_main_thread_id);
123   }
124 }
125
126 int64 CycleClock_Now() {
127   // TODO(hamaji): temporary impementation - it might be too slow.
128   struct timeval tv;
129   gettimeofday(&tv, NULL);
130   return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
131 }
132
133 int64 UsecToCycles(int64 usec) {
134   return usec;
135 }
136
137 static int32 g_main_thread_pid = getpid();
138 int32 GetMainThreadPid() {
139   return g_main_thread_pid;
140 }
141
142 static string g_my_user_name;
143 const string& MyUserName() {
144   return g_my_user_name;
145 }
146 static void MyUserNameInitializer() {
147   // TODO(hamaji): Probably this is not portable.
148   const char* user = getenv("USER");
149   if (user != NULL) {
150     g_my_user_name = user;
151   } else {
152     g_my_user_name = "invalid-user";
153   }
154 }
155 REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());
156
157 }  // namespace glog_internal_namespace_
158
159 void InitGoogleLogging(const char* argv0) {
160   const char* slash = strrchr(argv0, '/');
161 #ifdef OS_WINDOWS
162   if (!slash)  slash = strrchr(argv0, '\\');
163 #endif
164   g_program_invocation_short_name = slash ? slash + 1 : argv0;
165   g_main_thread_id = pthread_self();
166
167 #ifdef HAVE_STACKTRACE
168   InstallFailureFunction(&DumpStackTraceAndExit);
169 #endif
170 }
171
172 _END_GOOGLE_NAMESPACE_