utilities.h
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 // Define utilties for glog internal usage.
33 
34 #ifndef UTILITIES_H__
35 #define UTILITIES_H__
36 
37 // printf macros for size_t, in the style of inttypes.h
38 #ifdef _LP64
39 #define __PRIS_PREFIX "z"
40 #else
41 #define __PRIS_PREFIX
42 #endif
43 
44 // Use these macros after a % in a printf format string
45 // to get correct 32/64 bit behavior, like this:
46 // size_t size = records.size();
47 // printf("%"PRIuS"\n", size);
48 
49 #define PRIdS __PRIS_PREFIX "d"
50 #define PRIxS __PRIS_PREFIX "x"
51 #define PRIuS __PRIS_PREFIX "u"
52 #define PRIXS __PRIS_PREFIX "X"
53 #define PRIoS __PRIS_PREFIX "o"
54 
55 #include "base/mutex.h" // This must go first so we get _XOPEN_SOURCE
56 
57 #include <string>
58 
59 #include <glog/logging.h>
60 
61 #if defined(GLOG_OS_WINDOWS)
62 # include "port.h"
63 #endif
64 
65 #include "config.h"
66 
67 // There are three different ways we can try to get the stack trace:
68 //
69 // 1) The libunwind library. This is still in development, and as a
70 // separate library adds a new dependency, but doesn't need a frame
71 // pointer. It also doesn't call malloc.
72 //
73 // 2) Our hand-coded stack-unwinder. This depends on a certain stack
74 // layout, which is used by gcc (and those systems using a
75 // gcc-compatible ABI) on x86 systems, at least since gcc 2.95.
76 // It uses the frame pointer to do its work.
77 //
78 // 3) The gdb unwinder -- also the one used by the c++ exception code.
79 // It's obviously well-tested, but has a fatal flaw: it can call
80 // malloc() from the unwinder. This is a problem because we're
81 // trying to use the unwinder to instrument malloc().
82 //
83 // 4) The Windows API CaptureStackTrace.
84 //
85 // Note: if you add a new implementation here, make sure it works
86 // correctly when GetStackTrace() is called with max_depth == 0.
87 // Some code may do that.
88 
89 #if defined(HAVE_LIB_UNWIND)
90 # define STACKTRACE_H "stacktrace_libunwind-inl.h"
91 #elif defined(HAVE__UNWIND_BACKTRACE)
92 # define STACKTRACE_H "stacktrace_unwind-inl.h"
93 #elif !defined(NO_FRAME_POINTER)
94 # if defined(__i386__) && __GNUC__ >= 2
95 # define STACKTRACE_H "stacktrace_x86-inl.h"
96 # elif (defined(__ppc__) || defined(__PPC__)) && __GNUC__ >= 2
97 # define STACKTRACE_H "stacktrace_powerpc-inl.h"
98 # elif defined(GLOG_OS_WINDOWS)
99 # define STACKTRACE_H "stacktrace_windows-inl.h"
100 # endif
101 #endif
102 
103 #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_H)
104 # define STACKTRACE_H "stacktrace_generic-inl.h"
105 #endif
106 
107 #if defined(STACKTRACE_H)
108 # define HAVE_STACKTRACE
109 #endif
110 
111 #ifndef GLOG_NO_SYMBOLIZE_DETECTION
112 #ifndef HAVE_SYMBOLIZE
113 // defined by gcc
114 #if defined(__ELF__) && defined(GLOG_OS_LINUX)
115 # define HAVE_SYMBOLIZE
116 #elif defined(GLOG_OS_MACOSX) && defined(HAVE_DLADDR)
117 // Use dladdr to symbolize.
118 # define HAVE_SYMBOLIZE
119 #elif defined(GLOG_OS_WINDOWS)
120 // Use DbgHelp to symbolize
121 # define HAVE_SYMBOLIZE
122 #endif
123 #endif // !defined(HAVE_SYMBOLIZE)
124 #endif // !defined(GLOG_NO_SYMBOLIZE_DETECTION)
125 
126 #ifndef ARRAYSIZE
127 // There is a better way, but this is good enough for our purpose.
128 # define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
129 #endif
130 
132 
133 namespace glog_internal_namespace_ {
134 
135 #ifdef HAVE___ATTRIBUTE__
136 # define ATTRIBUTE_NOINLINE __attribute__ ((noinline))
137 # define HAVE_ATTRIBUTE_NOINLINE
138 #elif defined(GLOG_OS_WINDOWS)
139 # define ATTRIBUTE_NOINLINE __declspec(noinline)
140 # define HAVE_ATTRIBUTE_NOINLINE
141 #else
142 # define ATTRIBUTE_NOINLINE
143 #endif
144 
145 const char* ProgramInvocationShortName();
146 
148 
149 int64 UsecToCycles(int64 usec);
150 WallTime WallTime_Now();
151 
153 bool PidHasChanged();
154 
155 pid_t GetTID();
156 
157 const std::string& MyUserName();
158 
159 // Get the part of filepath after the last path separator.
160 // (Doesn't modify filepath, contrary to basename() in libgen.h.)
161 const char* const_basename(const char* filepath);
162 
163 // Wrapper of __sync_val_compare_and_swap. If the GCC extension isn't
164 // defined, we try the CPU specific logics (we only support x86 and
165 // x86_64 for now) first, then use a naive implementation, which has a
166 // race condition.
167 template<typename T>
168 inline T sync_val_compare_and_swap(T* ptr, T oldval, T newval) {
169 #if defined(HAVE___SYNC_VAL_COMPARE_AND_SWAP)
170  return __sync_val_compare_and_swap(ptr, oldval, newval);
171 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
172  T ret;
173  __asm__ __volatile__("lock; cmpxchg %1, (%2);"
174  :"=a"(ret)
175  // GCC may produces %sil or %dil for
176  // constraint "r", but some of apple's gas
177  // dosn't know the 8 bit registers.
178  // We use "q" to avoid these registers.
179  :"q"(newval), "q"(ptr), "a"(oldval)
180  :"memory", "cc");
181  return ret;
182 #else
183  T ret = *ptr;
184  if (ret == oldval) {
185  *ptr = newval;
186  }
187  return ret;
188 #endif
189 }
190 
191 void DumpStackTraceToString(std::string* stacktrace);
192 
193 struct CrashReason {
195 
196  const char* filename;
198  const char* message;
199 
200  // We'll also store a bit of stack trace context at the time of crash as
201  // it may not be available later on.
202  void* stack[32];
203  int depth;
204 };
205 
206 void SetCrashReason(const CrashReason* r);
207 
208 void InitGoogleLoggingUtilities(const char* argv0);
210 
211 } // namespace glog_internal_namespace_
212 
213 _END_GOOGLE_NAMESPACE_
214 
215 using namespace GOOGLE_NAMESPACE::glog_internal_namespace_;
216 
217 #endif // UTILITIES_H__
glog_internal_namespace_::CrashReason
Definition: utilities.h:193
glog_internal_namespace_::PidHasChanged
bool PidHasChanged()
Definition: utilities.cc:250
glog_internal_namespace_::CrashReason::message
const char * message
Definition: utilities.h:198
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
glog_internal_namespace_::WallTime_Now
WallTime WallTime_Now()
Definition: utilities.cc:240
glog_internal_namespace_::CrashReason::stack
void * stack[32]
Definition: utilities.h:202
glog_internal_namespace_
Definition: logging_custom_prefix_unittest.cc:1003
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
T
#define T(upbtypeconst, upbtype, ctype, default_value)
glog_internal_namespace_::CrashReason::filename
const char * filename
Definition: utilities.h:196
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
glog_internal_namespace_::SetCrashReason
void SetCrashReason(const CrashReason *r)
Definition: utilities.cc:358
glog_internal_namespace_::CrashReason::line_number
int line_number
Definition: utilities.h:197
mutex.h
glog_internal_namespace_::CrashReason::depth
int depth
Definition: utilities.h:203
glog_internal_namespace_::CycleClock_Now
int64 CycleClock_Now()
Definition: utilities.cc:229
depth
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:2859
glog_internal_namespace_::InitGoogleLoggingUtilities
void InitGoogleLoggingUtilities(const char *argv0)
Definition: utilities.cc:364
_START_GOOGLE_NAMESPACE_
Definition: signalhandler.cc:51
glog_internal_namespace_::DumpStackTraceToString
void DumpStackTraceToString(std::string *stacktrace)
r
GLboolean r
Definition: glcorearb.h:3228
glog_internal_namespace_::CrashReason::CrashReason
CrashReason()
Definition: utilities.h:194
glog_internal_namespace_::GetMainThreadPid
int32 GetMainThreadPid()
Definition: utilities.cc:246
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
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
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


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