Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2006, 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: Maxim Lifantsev |
| 31 | // |
| 32 | // logging_unittest.cc covers the functionality herein |
| 33 | |
| 34 | #include "utilities.h" |
| 35 | |
| 36 | #include <stdarg.h> |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 37 | #include <cstdio> |
| 38 | #include <cerrno> |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 39 | #ifdef HAVE_UNISTD_H |
| 40 | # include <unistd.h> // for close() and write() |
| 41 | #endif |
| 42 | #include <fcntl.h> // for open() |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 43 | #include <ctime> |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 44 | #include "config.h" |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 45 | #include <glog/logging.h> // To pick up flag settings etc. |
| 46 | #include <glog/raw_logging.h> |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 47 | #include "base/commandlineflags.h" |
| 48 | |
| 49 | #ifdef HAVE_STACKTRACE |
| 50 | # include "stacktrace.h" |
| 51 | #endif |
| 52 | |
| 53 | #if defined(HAVE_SYSCALL_H) |
| 54 | #include <syscall.h> // for syscall() |
| 55 | #elif defined(HAVE_SYS_SYSCALL_H) |
| 56 | #include <sys/syscall.h> // for syscall() |
| 57 | #endif |
| 58 | #ifdef HAVE_UNISTD_H |
| 59 | # include <unistd.h> |
| 60 | #endif |
| 61 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 62 | #if (defined(HAVE_SYSCALL_H) || defined(HAVE_SYS_SYSCALL_H)) && \ |
| 63 | (!(defined(GLOG_OS_MACOSX))) && !defined(GLOG_OS_EMSCRIPTEN) |
| 64 | #define safe_write(fd, s, len) syscall(SYS_write, fd, s, len) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 65 | #else |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 66 | // Not so safe, but what can you do? |
| 67 | #define safe_write(fd, s, len) write(fd, s, len) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 68 | #endif |
| 69 | |
Austin Schuh | 6122605 | 2022-06-20 09:40:08 -0700 | [diff] [blame] | 70 | namespace aos { |
| 71 | void FatalUnsetRealtimePriority() __attribute__((weak)); |
| 72 | } |
| 73 | |
| 74 | static void MaybeUnsetRealtime() { |
| 75 | if (&aos::FatalUnsetRealtimePriority != nullptr) { |
| 76 | aos::FatalUnsetRealtimePriority(); |
| 77 | } |
| 78 | } |
| 79 | |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 80 | _START_GOOGLE_NAMESPACE_ |
| 81 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 82 | #if defined(__GNUC__) |
| 83 | #define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) \ |
| 84 | __attribute__((format(archetype, stringIndex, firstToCheck))) |
| 85 | #define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex) \ |
| 86 | __attribute__((format_arg(stringIndex))) |
| 87 | #else |
| 88 | #define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) |
| 89 | #define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex) |
| 90 | #endif |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 91 | |
| 92 | // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths |
| 93 | // that invoke malloc() and getenv() that might acquire some locks. |
| 94 | // If this becomes a problem we should reimplement a subset of vsnprintf |
| 95 | // that does not need locks and malloc. |
| 96 | |
| 97 | // Helper for RawLog__ below. |
| 98 | // *DoRawLog writes to *buf of *size and move them past the written portion. |
| 99 | // It returns true iff there was no overflow or error. |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 100 | GLOG_ATTRIBUTE_FORMAT(printf, 3, 4) |
| 101 | static bool DoRawLog(char** buf, size_t* size, const char* format, ...) { |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 102 | va_list ap; |
| 103 | va_start(ap, format); |
| 104 | int n = vsnprintf(*buf, *size, format, ap); |
| 105 | va_end(ap); |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 106 | if (n < 0 || static_cast<size_t>(n) > *size) return false; |
| 107 | *size -= static_cast<size_t>(n); |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 108 | *buf += n; |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | // Helper for RawLog__ below. |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 113 | inline static bool VADoRawLog(char** buf, size_t* size, |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 114 | const char* format, va_list ap) { |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 115 | #if defined(__GNUC__) |
| 116 | #pragma GCC diagnostic push |
| 117 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 118 | #endif |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 119 | int n = vsnprintf(*buf, *size, format, ap); |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 120 | #if defined(__GNUC__) |
| 121 | #pragma GCC diagnostic pop |
| 122 | #endif |
| 123 | if (n < 0 || static_cast<size_t>(n) > *size) return false; |
| 124 | *size -= static_cast<size_t>(n); |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 125 | *buf += n; |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static const int kLogBufSize = 3000; |
| 130 | static bool crashed = false; |
| 131 | static CrashReason crash_reason; |
| 132 | static char crash_buf[kLogBufSize + 1] = { 0 }; // Will end in '\0' |
| 133 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 134 | GLOG_ATTRIBUTE_FORMAT(printf, 4, 5) |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 135 | void RawLog__(LogSeverity severity, const char* file, int line, |
| 136 | const char* format, ...) { |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 137 | if (!(FLAGS_logtostdout || FLAGS_logtostderr || |
| 138 | severity >= FLAGS_stderrthreshold || FLAGS_alsologtostderr || |
| 139 | !IsGoogleLoggingInitialized())) { |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 140 | return; // this stderr log message is suppressed |
| 141 | } |
| 142 | // can't call localtime_r here: it can allocate |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 143 | char buffer[kLogBufSize]; |
| 144 | char* buf = buffer; |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 145 | size_t size = sizeof(buffer); |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 146 | |
| 147 | // NOTE: this format should match the specification in base/logging.h |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 148 | DoRawLog(&buf, &size, "%c00000000 00:00:00.000000 %5u %s:%d] RAW: ", |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 149 | LogSeverityNames[severity][0], |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 150 | static_cast<unsigned int>(GetTID()), |
| 151 | const_basename(const_cast<char *>(file)), line); |
| 152 | |
| 153 | // Record the position and size of the buffer after the prefix |
| 154 | const char* msg_start = buf; |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 155 | const size_t msg_size = size; |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 156 | |
| 157 | va_list ap; |
| 158 | va_start(ap, format); |
| 159 | bool no_chop = VADoRawLog(&buf, &size, format, ap); |
| 160 | va_end(ap); |
| 161 | if (no_chop) { |
| 162 | DoRawLog(&buf, &size, "\n"); |
| 163 | } else { |
| 164 | DoRawLog(&buf, &size, "RAW_LOG ERROR: The Message was too long!\n"); |
| 165 | } |
| 166 | // We make a raw syscall to write directly to the stderr file descriptor, |
| 167 | // avoiding FILE buffering (to avoid invoking malloc()), and bypassing |
| 168 | // libc (to side-step any libc interception). |
| 169 | // We write just once to avoid races with other invocations of RawLog__. |
| 170 | safe_write(STDERR_FILENO, buffer, strlen(buffer)); |
| 171 | if (severity == GLOG_FATAL) { |
Austin Schuh | 6122605 | 2022-06-20 09:40:08 -0700 | [diff] [blame] | 172 | MaybeUnsetRealtime(); |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 173 | if (!sync_val_compare_and_swap(&crashed, false, true)) { |
| 174 | crash_reason.filename = file; |
| 175 | crash_reason.line_number = line; |
| 176 | memcpy(crash_buf, msg_start, msg_size); // Don't include prefix |
| 177 | crash_reason.message = crash_buf; |
| 178 | #ifdef HAVE_STACKTRACE |
| 179 | crash_reason.depth = |
| 180 | GetStackTrace(crash_reason.stack, ARRAYSIZE(crash_reason.stack), 1); |
| 181 | #else |
| 182 | crash_reason.depth = 0; |
| 183 | #endif |
| 184 | SetCrashReason(&crash_reason); |
| 185 | } |
| 186 | LogMessage::Fail(); // abort() |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | _END_GOOGLE_NAMESPACE_ |