PageRenderTime 44ms CodeModel.GetById 34ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/glog/src/utilities.h

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