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
48 #include "glog/logging.h"
50 // There are three different ways we can try to get the stack trace:
52 // 1) The libunwind library. This is still in development, and as a
53 // separate library adds a new dependency, but doesn't need a frame
54 // pointer. It also doesn't call malloc.
56 // 2) Our hand-coded stack-unwinder. This depends on a certain stack
57 // layout, which is used by gcc (and those systems using a
58 // gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
59 // It uses the frame pointer to do its work.
61 // 3) The gdb unwinder -- also the one used by the c++ exception code.
62 // It's obviously well-tested, but has a fatal flaw: it can call
63 // malloc() from the unwinder. This is a problem because we're
64 // trying to use the unwinder to instrument malloc().
66 // Note: if you add a new implementation here, make sure it works
67 // correctly when GetStackTrace() is called with max_depth == 0.
68 // Some code may do that.
70 #if defined(HAVE_LIB_UNWIND)
71 # define STACKTRACE_H "stacktrace_libunwind-inl.h"
72 #elif !defined(NO_FRAME_POINTER)
73 # if defined(__i386__) && __GNUC__ >= 2
74 # define STACKTRACE_H "stacktrace_x86-inl.h"
75 # elif defined(__x86_64__) && __GNUC__ >= 2
76 # define STACKTRACE_H "stacktrace_x86_64-inl.h"
77 # elif ((__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
78 # define STACKTRACE_H "stacktrace_powerpc-inl.h"
82 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
83 # define STACKTRACE_H "stacktrace_generic-inl.h"
86 #if defined(STACKTRACE_H)
87 # define HAVE_STACKTRACE
91 #if defined(__ELF__) && defined(OS_LINUX)
92 # define HAVE_SYMBOLIZE
93 #elif defined(OS_MACOSX) && defined(HAVE_DLADDR)
94 // Use dladdr to symbolize.
95 # define HAVE_SYMBOLIZE
98 // There is a better way, but this is good enough in this file.
99 #define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
101 _START_GOOGLE_NAMESPACE_
103 namespace glog_internal_namespace_ {
105 #ifdef HAVE___ATTRIBUTE__
106 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
107 # define HAVE_ATTRIBUTE_NOINLINE
109 # define ATTRIBUTE_NOINLINE
112 const char* ProgramInvocationShortName();
114 bool IsGoogleLoggingInitialized();
116 bool is_default_thread();
118 int64 CycleClock_Now();
120 int64 UsecToCycles(int64 usec);
122 int32 GetMainThreadPid();
124 const std::string& MyUserName();
126 // Get the part of filepath after the last path separator.
127 // (Doesn't modify filepath, contrary to basename() in libgen.h.)
128 const char* const_basename(const char* filepath);
130 // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
131 // defined, we try the CPU specific logics (we only support x86 and
132 // x86_64 for now) first, then use a naive implementation, which has a
135 inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
136 #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP)
137 return __sync_val_compare_and_swap(ptr, oldval, newval);
138 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
140 __asm__ __volatile__("lock; cmpxchg %1, (%2);"
142 // GCC may produces %sil or %dil for
143 // constraint "r", but some of apple's gas
144 // dosn't know the 8 bit registers.
145 // We use "q" to avoid these registers.
146 :"q"(newval), "q"(ptr), "a"(oldval)
158 } // namespace glog_internal_namespace_
160 _END_GOOGLE_NAMESPACE_
162 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
164 #endif // UTILITIES_H__