1236fbeaf1263f2a14cc0a78894124d79dbb958a
[platform/upstream/glog.git] / src / utilities.h
1 // Copyright 2008 Google Inc. All Rights Reserved.
2 // Author: hamaji@google.com (Shinichiro Hamaji)
3 //
4 // Define utilties for glog internal usage.
5
6 #ifndef UTILITIES_H__
7 #define UTILITIES_H__
8
9 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
10 # define OS_WINDOWS
11 #elif defined(linux) || defined(__linux) || defined(__linux__)
12 # define OS_LINUX
13 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
14 # define OS_MACOSX
15 #elif defined(__FreeBSD__)
16 # define OS_FREEBSD
17 #else
18 // TODO(hamaji): Add other platforms.
19 #endif
20
21 // printf macros for size_t, in the style of inttypes.h
22 #ifdef _LP64
23 #define __PRIS_PREFIX "z"
24 #else
25 #define __PRIS_PREFIX
26 #endif
27
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);
32
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"
38
39 #include "base/mutex.h"  // This must go first so we get _XOPEN_SOURCE
40
41 #include <string>
42
43 #include "config.h"
44 #include "glog/logging.h"
45
46 // There are three different ways we can try to get the stack trace:
47 //
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.
51 //
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.
56 //
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().
61 //
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.
65
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"
75 # endif
76 #endif
77
78 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
79 # define STACKTRACE_H "stacktrace_generic-inl.h"
80 #endif
81
82 #if defined(STACKTRACE_H)
83 # define HAVE_STACKTRACE
84 #endif
85
86 // defined by gcc
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
92 #endif
93
94 _START_GOOGLE_NAMESPACE_
95
96 namespace glog_internal_namespace_ {
97
98 #ifdef HAVE___ATTRIBUTE__
99 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
100 # define HAVE_ATTRIBUTE_NOINLINE
101 #else
102 # define ATTRIBUTE_NOINLINE
103 #endif
104
105 const char* ProgramInvocationShortName();
106
107 bool IsGoogleLoggingInitialized();
108
109 bool is_default_thread();
110
111 int64 CycleClock_Now();
112
113 int64 UsecToCycles(int64 usec);
114
115 int32 GetMainThreadPid();
116
117 const std::string& MyUserName();
118
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
122 // race condition.
123 template<typename T>
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__))
128   T ret;
129   __asm__ __volatile__("lock; cmpxchg %1, (%2);"
130                        :"=a"(ret)
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)
136                        :"memory", "cc");
137   return ret;
138 #else
139   T ret = *ptr;
140   if (ret == oldval) {
141     *ptr = newval;
142   }
143   return ret;
144 #endif
145 }
146
147 }  // namespace glog_internal_namespace_
148
149 _END_GOOGLE_NAMESPACE_
150
151 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
152
153 #endif  // UTILITIES_H__