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