utilities.cc
Go to the documentation of this file.
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 "config.h"
33 #include "utilities.h"
34 
35 #include <cstdio>
36 #include <cstdlib>
37 
38 #include <csignal>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
42 #include <ctime>
43 #if defined(HAVE_SYSCALL_H)
44 #include <syscall.h> // for syscall()
45 #elif defined(HAVE_SYS_SYSCALL_H)
46 #include <sys/syscall.h> // for syscall()
47 #endif
48 #ifdef HAVE_SYSLOG_H
49 # include <syslog.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 # include <unistd.h> // For geteuid.
53 #endif
54 #ifdef HAVE_PWD_H
55 # include <pwd.h>
56 #endif
57 #ifdef __ANDROID__
58 #include <android/log.h>
59 #endif
60 
61 #include "base/googleinit.h"
62 
63 using std::string;
64 
66 
68 
71 }
72 
73 _END_GOOGLE_NAMESPACE_
74 
75 // The following APIs are all internal.
76 #ifdef HAVE_STACKTRACE
77 
78 #include "stacktrace.h"
79 #include "symbolize.h"
80 #include "base/commandlineflags.h"
81 
82 GLOG_DEFINE_bool(symbolize_stacktrace, true,
83  "Symbolize the stack trace in the tombstone");
84 
86 
87 typedef void DebugWriter(const char*, void*);
88 
89 // The %p field width for printf() functions is two characters per byte.
90 // For some environments, add two extra bytes for the leading "0x".
91 static const int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
92 
93 static void DebugWriteToStderr(const char* data, void *) {
94  // This one is signal-safe.
95  if (write(STDERR_FILENO, data, strlen(data)) < 0) {
96  // Ignore errors.
97  }
98 #if defined(__ANDROID__)
99  // ANDROID_LOG_FATAL as fatal error occurred and now is dumping call stack.
100  __android_log_write(ANDROID_LOG_FATAL,
102  data);
103 #endif
104 }
105 
106 static void DebugWriteToString(const char* data, void *arg) {
107  reinterpret_cast<string*>(arg)->append(data);
108 }
109 
110 #ifdef HAVE_SYMBOLIZE
111 // Print a program counter and its symbol name.
112 static void DumpPCAndSymbol(DebugWriter *writerfn, void *arg, void *pc,
113  const char * const prefix) {
114  char tmp[1024];
115  const char *symbol = "(unknown)";
116  // Symbolizes the previous address of pc because pc may be in the
117  // next function. The overrun happens when the function ends with
118  // a call to a function annotated noreturn (e.g. CHECK).
119  if (Symbolize(reinterpret_cast<char *>(pc) - 1, tmp, sizeof(tmp))) {
120  symbol = tmp;
121  }
122  char buf[1024];
123  snprintf(buf, sizeof(buf), "%s@ %*p %s\n",
124  prefix, kPrintfPointerFieldWidth, pc, symbol);
125  writerfn(buf, arg);
126 }
127 #endif
128 
129 static void DumpPC(DebugWriter *writerfn, void *arg, void *pc,
130  const char * const prefix) {
131  char buf[100];
132  snprintf(buf, sizeof(buf), "%s@ %*p\n",
133  prefix, kPrintfPointerFieldWidth, pc);
134  writerfn(buf, arg);
135 }
136 
137 // Dump current stack trace as directed by writerfn
138 static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) {
139  // Print stack trace
140  void* stack[32];
141  int depth = GetStackTrace(stack, ARRAYSIZE(stack), skip_count+1);
142  for (int i = 0; i < depth; i++) {
143 #if defined(HAVE_SYMBOLIZE)
144  if (FLAGS_symbolize_stacktrace) {
145  DumpPCAndSymbol(writerfn, arg, stack[i], " ");
146  } else {
147  DumpPC(writerfn, arg, stack[i], " ");
148  }
149 #else
150  DumpPC(writerfn, arg, stack[i], " ");
151 #endif
152  }
153 }
154 
155 #if defined(__GNUC__)
156 __attribute__((noreturn))
157 #elif defined(_MSC_VER)
158 __declspec(noreturn)
159 #endif
160 static void DumpStackTraceAndExit() {
161  DumpStackTrace(1, DebugWriteToStderr, NULL);
162 
163  // TODO(hamaji): Use signal instead of sigaction?
165  // Set the default signal handler for SIGABRT, to avoid invoking our
166  // own signal handler installed by InstallFailureSignalHandler().
167 #ifdef HAVE_SIGACTION
168  struct sigaction sig_action;
169  memset(&sig_action, 0, sizeof(sig_action));
170  sigemptyset(&sig_action.sa_mask);
171  sig_action.sa_handler = SIG_DFL;
172  sigaction(SIGABRT, &sig_action, NULL);
173 #elif defined(GLOG_OS_WINDOWS)
174  signal(SIGABRT, SIG_DFL);
175 #endif // HAVE_SIGACTION
176  }
177 
178  abort();
179 }
180 
181 _END_GOOGLE_NAMESPACE_
182 
183 #endif // HAVE_STACKTRACE
184 
186 
187 namespace glog_internal_namespace_ {
188 
192  } else {
193  // TODO(hamaji): Use /proc/self/cmdline and so?
194  return "UNKNOWN";
195  }
196 }
197 
198 #ifdef GLOG_OS_WINDOWS
199 struct timeval {
200  long tv_sec, tv_usec;
201 };
202 
203 // Based on: http://www.google.com/codesearch/p?hl=en#dR3YEbitojA/os_win32.c&q=GetSystemTimeAsFileTime%20license:bsd
204 // See COPYING for copyright information.
205 static int gettimeofday(struct timeval *tv, void* /*tz*/) {
206 #ifdef __GNUC__
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wlong-long"
209 #endif
210 #define EPOCHFILETIME (116444736000000000ULL)
211  FILETIME ft;
212  ULARGE_INTEGER li;
213  uint64 tt;
214 
215  GetSystemTimeAsFileTime(&ft);
216  li.LowPart = ft.dwLowDateTime;
217  li.HighPart = ft.dwHighDateTime;
218  tt = (li.QuadPart - EPOCHFILETIME) / 10;
219  tv->tv_sec = tt / 1000000;
220  tv->tv_usec = tt % 1000000;
221 #ifdef __GNUC__
222 #pragma GCC diagnostic pop
223 #endif
224 
225  return 0;
226 }
227 #endif
228 
230  // TODO(hamaji): temporary impementation - it might be too slow.
231  struct timeval tv;
232  gettimeofday(&tv, NULL);
233  return static_cast<int64>(tv.tv_sec) * 1000000 + tv.tv_usec;
234 }
235 
237  return usec;
238 }
239 
240 WallTime WallTime_Now() {
241  // Now, cycle clock is retuning microseconds since the epoch.
242  return CycleClock_Now() * 0.000001;
243 }
244 
245 static int32 g_main_thread_pid = getpid();
247  return g_main_thread_pid;
248 }
249 
251  int32 pid = getpid();
252  if (g_main_thread_pid == pid) {
253  return false;
254  }
255  g_main_thread_pid = pid;
256  return true;
257 }
258 
259 pid_t GetTID() {
260  // On Linux and MacOSX, we try to use gettid().
261 #if defined GLOG_OS_LINUX || defined GLOG_OS_MACOSX
262 #ifndef __NR_gettid
263 #ifdef GLOG_OS_MACOSX
264 #define __NR_gettid SYS_gettid
265 #elif ! defined __i386__
266 #error "Must define __NR_gettid for non-x86 platforms"
267 #else
268 #define __NR_gettid 224
269 #endif
270 #endif
271  static bool lacks_gettid = false;
272  if (!lacks_gettid) {
273 #if (defined(GLOG_OS_MACOSX) && defined(HAVE_PTHREAD_THREADID_NP))
274  uint64_t tid64;
275  const int error = pthread_threadid_np(NULL, &tid64);
276  pid_t tid = error ? -1 : static_cast<pid_t>(tid64);
277 #else
278  pid_t tid = static_cast<pid_t>(syscall(__NR_gettid));
279 #endif
280  if (tid != -1) {
281  return tid;
282  }
283  // Technically, this variable has to be volatile, but there is a small
284  // performance penalty in accessing volatile variables and there should
285  // not be any serious adverse effect if a thread does not immediately see
286  // the value change to "true".
287  lacks_gettid = true;
288  }
289 #endif // GLOG_OS_LINUX || GLOG_OS_MACOSX
290 
291  // If gettid() could not be used, we use one of the following.
292 #if defined GLOG_OS_LINUX
293  return getpid(); // Linux: getpid returns thread ID when gettid is absent
294 #elif defined GLOG_OS_WINDOWS && !defined GLOG_OS_CYGWIN
295  return static_cast<pid_t>(GetCurrentThreadId());
296 #elif defined(HAVE_PTHREAD)
297  // If none of the techniques above worked, we use pthread_self().
298  return (pid_t)(uintptr_t)pthread_self();
299 #else
300  return -1;
301 #endif
302 }
303 
304 const char* const_basename(const char* filepath) {
305  const char* base = strrchr(filepath, '/');
306 #ifdef GLOG_OS_WINDOWS // Look for either path separator in Windows
307  if (!base)
308  base = strrchr(filepath, '\\');
309 #endif
310  return base ? (base+1) : filepath;
311 }
312 
313 static string g_my_user_name;
314 const string& MyUserName() {
315  return g_my_user_name;
316 }
317 static void MyUserNameInitializer() {
318  // TODO(hamaji): Probably this is not portable.
319 #if defined(GLOG_OS_WINDOWS)
320  const char* user = getenv("USERNAME");
321 #else
322  const char* user = getenv("USER");
323 #endif
324  if (user != NULL) {
325  g_my_user_name = user;
326  } else {
327 #if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H)
328  struct passwd pwd;
329  struct passwd* result = NULL;
330  char buffer[1024] = {'\0'};
331  uid_t uid = geteuid();
332  int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof(buffer), &result);
333  if (pwuid_res == 0 && result) {
334  g_my_user_name = pwd.pw_name;
335  } else {
336  snprintf(buffer, sizeof(buffer), "uid%d", uid);
338  }
339 #endif
340  if (g_my_user_name.empty()) {
341  g_my_user_name = "invalid-user";
342  }
343  }
344 
345 }
347 
348 #ifdef HAVE_STACKTRACE
349 void DumpStackTraceToString(string* stacktrace) {
350  DumpStackTrace(1, DebugWriteToString, stacktrace);
351 }
352 #endif
353 
354 // We use an atomic operation to prevent problems with calling CrashReason
355 // from inside the Mutex implementation (potentially through RAW_CHECK).
356 static const CrashReason* g_reason = 0;
357 
360  reinterpret_cast<const CrashReason*>(0),
361  r);
362 }
363 
364 void InitGoogleLoggingUtilities(const char* argv0) {
366  << "You called InitGoogleLogging() twice!";
367  const char* slash = strrchr(argv0, '/');
368 #ifdef GLOG_OS_WINDOWS
369  if (!slash) slash = strrchr(argv0, '\\');
370 #endif
371  g_program_invocation_short_name = slash ? slash + 1 : argv0;
372 
373 #ifdef HAVE_STACKTRACE
374  InstallFailureFunction(&DumpStackTraceAndExit);
375 #endif
376 }
377 
380  << "You called ShutdownGoogleLogging() without calling InitGoogleLogging() first!";
382 #ifdef HAVE_SYSLOG_H
383  closelog();
384 #endif
385 }
386 
387 } // namespace glog_internal_namespace_
388 
389 _END_GOOGLE_NAMESPACE_
390 
391 // Make an implementation of stacktrace compiled.
392 #ifdef STACKTRACE_H
393 # include STACKTRACE_H
394 # if 0
395 // For include scanners which can't handle macro expansions.
396 # include "stacktrace_libunwind-inl.h"
397 # include "stacktrace_x86-inl.h"
398 # include "stacktrace_x86_64-inl.h"
399 # include "stacktrace_powerpc-inl.h"
400 # include "stacktrace_generic-inl.h"
401 # endif
402 #endif
glog_internal_namespace_::CrashReason
Definition: utilities.h:193
glog_internal_namespace_::g_main_thread_pid
static int32 g_main_thread_pid
Definition: utilities.cc:245
glog_internal_namespace_::PidHasChanged
bool PidHasChanged()
Definition: utilities.cc:250
stacktrace_libunwind-inl.h
symbolize.h
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
glog_internal_namespace_::ProgramInvocationShortName
const char * ProgramInvocationShortName()
Definition: utilities.cc:189
glog_internal_namespace_::ShutdownGoogleLoggingUtilities
void ShutdownGoogleLoggingUtilities()
Definition: utilities.cc:378
googleinit.h
base
Definition: logging.cc:2162
glog_internal_namespace_::WallTime_Now
WallTime WallTime_Now()
Definition: utilities.cc:240
Symbolize
_START_GOOGLE_NAMESPACE_ bool Symbolize(void *, char *, size_t)
Definition: symbolize.cc:948
ARRAYSIZE
#define ARRAYSIZE(a)
Definition: utilities.h:128
glog_internal_namespace_
Definition: logging_custom_prefix_unittest.cc:1003
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
GetStackTrace
_START_GOOGLE_NAMESPACE_ GLOG_EXPORT int GetStackTrace(void **result, int max_depth, int skip_count)
Definition: stacktrace_generic-inl.h:41
error
Definition: cJSON.c:88
glog_internal_namespace_::IsFailureSignalHandlerInstalled
bool IsFailureSignalHandlerInstalled()
Definition: signalhandler.cc:363
glog_internal_namespace_::GetTID
pid_t GetTID()
Definition: utilities.cc:259
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
glog_internal_namespace_::SetCrashReason
void SetCrashReason(const CrashReason *r)
Definition: utilities.cc:358
stacktrace_generic-inl.h
stacktrace_powerpc-inl.h
stacktrace_x86-inl.h
prefix
static const char prefix[]
Definition: test_pair_ipc.cpp:26
buffer
GLuint buffer
Definition: glcorearb.h:2939
g_program_invocation_short_name
static const _START_GOOGLE_NAMESPACE_ char * g_program_invocation_short_name
Definition: utilities.cc:67
glog_internal_namespace_::CycleClock_Now
int64 CycleClock_Now()
Definition: utilities.cc:229
google::protobuf::uint64
uint64_t uint64
Definition: protobuf/src/google/protobuf/stubs/port.h:156
CHECK
#define CHECK(x)
Definition: php/ext/google/protobuf/upb.c:8393
buffer
Definition: buffer_processor.h:43
glog_internal_namespace_::MyUserNameInitializer
static void MyUserNameInitializer()
Definition: utilities.cc:317
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
depth
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:2859
glog_internal_namespace_::InitGoogleLoggingUtilities
void InitGoogleLoggingUtilities(const char *argv0)
Definition: utilities.cc:364
commandlineflags.h
utilities.h
i
int i
Definition: gmock-matchers_test.cc:764
_START_GOOGLE_NAMESPACE_
Definition: signalhandler.cc:51
InstallFailureFunction
void InstallFailureFunction(logging_fail_func_t fail_func)
Definition: logging.cc:1941
append
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
glog_internal_namespace_::DumpStackTraceToString
void DumpStackTraceToString(std::string *stacktrace)
IsGoogleLoggingInitialized
bool IsGoogleLoggingInitialized()
Definition: utilities.cc:69
r
GLboolean r
Definition: glcorearb.h:3228
stacktrace.h
glog_internal_namespace_::GetMainThreadPid
int32 GetMainThreadPid()
Definition: utilities.cc:246
glog_internal_namespace_::g_my_user_name
static string g_my_user_name
Definition: utilities.cc:313
GLOG_DEFINE_bool
#define GLOG_DEFINE_bool(name, value, meaning)
Definition: glog/src/base/commandlineflags.h:120
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
glog_internal_namespace_::const_basename
const char * const_basename(const char *filepath)
Definition: utilities.cc:304
glog_internal_namespace_::UsecToCycles
int64 UsecToCycles(int64 usec)
Definition: utilities.cc:236
glog_internal_namespace_::g_reason
static const CrashReason * g_reason
Definition: utilities.cc:356
glog_internal_namespace_::sync_val_compare_and_swap
T sync_val_compare_and_swap(T *ptr, T oldval, T newval)
Definition: utilities.h:168
glog_internal_namespace_::MyUserName
const string & MyUserName()
Definition: utilities.cc:314
REGISTER_MODULE_INITIALIZER
#define REGISTER_MODULE_INITIALIZER(name, body)
Definition: googleinit.h:44


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01