1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
4 // Define utilties for glog internal usage.
9 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
11 #elif defined(linux) || defined(__linux) || defined(__linux__)
13 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
15 #elif defined(__FreeBSD__)
18 // TODO(hamaji): Add other platforms.
21 // printf macros for size_t, in the style of inttypes.h
23 #define __PRIS_PREFIX "z"
28 // Use these macros after a % in a printf format string
29 // to get correct 32/64 bit behavior, like this:
30 // size_t size = records.size();
31 // printf("%"PRIuS"\n", size);
33 #define PRIdS __PRIS_PREFIX "d"
34 #define PRIxS __PRIS_PREFIX "x"
35 #define PRIuS __PRIS_PREFIX "u"
36 #define PRIXS __PRIS_PREFIX "X"
37 #define PRIoS __PRIS_PREFIX "o"
39 #include "base/mutex.h" // This must go first so we get _XOPEN_SOURCE
44 #include "glog/logging.h"
46 // There are three different ways we can try to get the stack trace:
48 // 1) The libunwind library. This is still in development, and as a
49 // separate library adds a new dependency, but doesn't need a frame
50 // pointer. It also doesn't call malloc.
52 // 2) Our hand-coded stack-unwinder. This depends on a certain stack
53 // layout, which is used by gcc (and those systems using a
54 // gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
55 // It uses the frame pointer to do its work.
57 // 3) The gdb unwinder -- also the one used by the c++ exception code.
58 // It's obviously well-tested, but has a fatal flaw: it can call
59 // malloc() from the unwinder. This is a problem because we're
60 // trying to use the unwinder to instrument malloc().
62 // Note: if you add a new implementation here, make sure it works
63 // correctly when GetStackTrace() is called with max_depth == 0.
64 // Some code may do that.
66 #if defined(HAVE_LIB_UNWIND)
67 # define STACKTRACE_H "stacktrace_libunwind-inl.h"
68 #elif !defined(NO_FRAME_POINTER)
69 # if defined(__i386__) && __GNUC__ >= 2
70 # define STACKTRACE_H "stacktrace_x86-inl.h"
71 # elif defined(__x86_64__) && __GNUC__ >= 2
72 # define STACKTRACE_H "stacktrace_x86_64-inl.h"
73 # elif ((__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
74 # define STACKTRACE_H "stacktrace_powerpc-inl.h"
78 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
79 # define STACKTRACE_H "stacktrace_generic-inl.h"
82 #if defined(STACKTRACE_H)
83 # define HAVE_STACKTRACE
87 #if defined(__ELF__) && defined(OS_LINUX)
88 # define HAVE_SYMBOLIZE
89 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
90 // Use dladdr to symbolize.
91 # define HAVE_SYMBOLIZE
94 _START_GOOGLE_NAMESPACE_
96 namespace glog_internal_namespace_ {
98 #ifdef HAVE___ATTRIBUTE__
99 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
100 # define HAVE_ATTRIBUTE_NOINLINE
102 # define ATTRIBUTE_NOINLINE
105 const char* ProgramInvocationShortName();
107 bool IsGoogleLoggingInitialized();
109 bool is_default_thread();
111 int64 CycleClock_Now();
113 int64 UsecToCycles(int64 usec);
115 int32 GetMainThreadPid();
117 const std::string& MyUserName();
119 // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
120 // defined, we try the CPU specific logics (we only support x86 and
121 // x86_64 for now) first, then use a naive implementation, which has a
124 inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
125 #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP)
126 return __sync_val_compare_and_swap(ptr, oldval, newval);
127 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
129 __asm__ __volatile__("lock; cmpxchg %1, (%2);"
131 // GCC may produces %sil or %dil for
132 // constraint "r", but some of apple's gas
133 // dosn't know the 8 bit registers.
134 // We use "q" to avoid these registers.
135 :"q"(newval), "q"(ptr), "a"(oldval)
147 } // namespace glog_internal_namespace_
149 _END_GOOGLE_NAMESPACE_
151 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
153 #endif // UTILITIES_H__