Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 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 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 59 | #include <glog/logging.h> |
| 60 | |
| 61 | #if defined(GLOG_OS_WINDOWS) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 62 | # include "port.h" |
| 63 | #endif |
| 64 | |
| 65 | #include "config.h" |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 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" |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 91 | #elif defined(HAVE__UNWIND_BACKTRACE) && defined(HAVE__UNWIND_GETIP) |
| 92 | # define STACKTRACE_H "stacktrace_unwind-inl.h" |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 93 | #elif !defined(NO_FRAME_POINTER) |
| 94 | # if defined(__i386__) && __GNUC__ >= 2 |
| 95 | # define STACKTRACE_H "stacktrace_x86-inl.h" |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 96 | # elif (defined(__ppc__) || defined(__PPC__)) && __GNUC__ >= 2 |
| 97 | # define STACKTRACE_H "stacktrace_powerpc-inl.h" |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 98 | # elif defined(GLOG_OS_WINDOWS) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 99 | # define STACKTRACE_H "stacktrace_windows-inl.h" |
| 100 | # endif |
| 101 | #endif |
| 102 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 103 | #if !defined(STACKTRACE_H) && defined(HAVE_EXECINFO_BACKTRACE) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 104 | # define STACKTRACE_H "stacktrace_generic-inl.h" |
| 105 | #endif |
| 106 | |
| 107 | #if defined(STACKTRACE_H) |
| 108 | # define HAVE_STACKTRACE |
| 109 | #endif |
| 110 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 111 | #ifndef GLOG_NO_SYMBOLIZE_DETECTION |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 112 | #ifndef HAVE_SYMBOLIZE |
| 113 | // defined by gcc |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 114 | #if defined(__ELF__) && defined(GLOG_OS_LINUX) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 115 | # define HAVE_SYMBOLIZE |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 116 | #elif defined(GLOG_OS_MACOSX) && defined(HAVE_DLADDR) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 117 | // Use dladdr to symbolize. |
| 118 | # define HAVE_SYMBOLIZE |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 119 | #elif defined(GLOG_OS_WINDOWS) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 120 | // Use DbgHelp to symbolize |
| 121 | # define HAVE_SYMBOLIZE |
| 122 | #endif |
| 123 | #endif // !defined(HAVE_SYMBOLIZE) |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 124 | #endif // !defined(GLOG_NO_SYMBOLIZE_DETECTION) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 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 | |
| 131 | _START_GOOGLE_NAMESPACE_ |
| 132 | |
| 133 | namespace glog_internal_namespace_ { |
| 134 | |
| 135 | #ifdef HAVE___ATTRIBUTE__ |
| 136 | # define ATTRIBUTE_NOINLINE __attribute__ ((noinline)) |
| 137 | # define HAVE_ATTRIBUTE_NOINLINE |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame^] | 138 | #elif defined(GLOG_OS_WINDOWS) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 147 | int64 CycleClock_Now(); |
| 148 | |
| 149 | int64 UsecToCycles(int64 usec); |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 150 | WallTime WallTime_Now(); |
| 151 | |
| 152 | int32 GetMainThreadPid(); |
| 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 { |
| 194 | CrashReason() : filename(0), line_number(0), message(0), depth(0) {} |
| 195 | |
| 196 | const char* filename; |
| 197 | int line_number; |
| 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); |
| 209 | void ShutdownGoogleLoggingUtilities(); |
| 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__ |