Initial windows support. Now we don't have the stacktrace and several unittests.
[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 #ifdef OS_WINDOWS
44 # include "port.h"
45 #endif
46
47 #include "config.h"
48 #include "glog/logging.h"
49
50 // There are three different ways we can try to get the stack trace:
51 //
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.
55 //
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.
60 //
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().
65 //
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.
69
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"
79 # endif
80 #endif
81
82 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
83 # define STACKTRACE_H "stacktrace_generic-inl.h"
84 #endif
85
86 #if defined(STACKTRACE_H)
87 # define HAVE_STACKTRACE
88 #endif
89
90 // defined by gcc
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
96 #endif
97
98 _START_GOOGLE_NAMESPACE_
99
100 namespace glog_internal_namespace_ {
101
102 #ifdef HAVE___ATTRIBUTE__
103 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
104 # define HAVE_ATTRIBUTE_NOINLINE
105 #else
106 # define ATTRIBUTE_NOINLINE
107 #endif
108
109 const char* ProgramInvocationShortName();
110
111 bool IsGoogleLoggingInitialized();
112
113 bool is_default_thread();
114
115 int64 CycleClock_Now();
116
117 int64 UsecToCycles(int64 usec);
118
119 int32 GetMainThreadPid();
120
121 const std::string& MyUserName();
122
123 // Get the part of filepath after the last path separator.
124 // (Doesn't modify filepath, contrary to basename() in libgen.h.)
125 const char* const_basename(const char* filepath);
126
127 // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
128 // defined, we try the CPU specific logics (we only support x86 and
129 // x86_64 for now) first, then use a naive implementation, which has a
130 // race condition.
131 template<typename T>
132 inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
133 #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP)
134   return __sync_val_compare_and_swap(ptr, oldval, newval);
135 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
136   T ret;
137   __asm__ __volatile__("lock; cmpxchg %1, (%2);"
138                        :"=a"(ret)
139                         // GCC may produces %sil or %dil for
140                         // constraint "r", but some of apple's gas
141                         // dosn't know the 8 bit registers.
142                         // We use "q" to avoid these registers.
143                        :"q"(newval), "q"(ptr), "a"(oldval)
144                        :"memory", "cc");
145   return ret;
146 #else
147   T ret = *ptr;
148   if (ret == oldval) {
149     *ptr = newval;
150   }
151   return ret;
152 #endif
153 }
154
155 }  // namespace glog_internal_namespace_
156
157 _END_GOOGLE_NAMESPACE_
158
159 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
160
161 #endif  // UTILITIES_H__