blob: be02981fe1f792a222cba2bb8cc0befb8c218d7f [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// 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 Kuszmaulba0ac1a2022-08-12 16:29:30 -070037#include <cstdio>
38#include <cerrno>
Austin Schuh906616c2019-01-21 20:25:11 -080039#ifdef HAVE_UNISTD_H
40# include <unistd.h> // for close() and write()
41#endif
42#include <fcntl.h> // for open()
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070043#include <ctime>
Austin Schuh906616c2019-01-21 20:25:11 -080044#include "config.h"
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070045#include <glog/logging.h> // To pick up flag settings etc.
46#include <glog/raw_logging.h>
Austin Schuh906616c2019-01-21 20:25:11 -080047#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 Kuszmaulba0ac1a2022-08-12 16:29:30 -070062#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 Schuh906616c2019-01-21 20:25:11 -080065#else
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070066// Not so safe, but what can you do?
67#define safe_write(fd, s, len) write(fd, s, len)
Austin Schuh906616c2019-01-21 20:25:11 -080068#endif
69
Austin Schuh61226052022-06-20 09:40:08 -070070namespace aos {
71void FatalUnsetRealtimePriority() __attribute__((weak));
72}
73
74static void MaybeUnsetRealtime() {
75 if (&aos::FatalUnsetRealtimePriority != nullptr) {
76 aos::FatalUnsetRealtimePriority();
77 }
78}
79
Austin Schuh906616c2019-01-21 20:25:11 -080080_START_GOOGLE_NAMESPACE_
81
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070082#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 Schuh906616c2019-01-21 20:25:11 -080091
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 Kuszmaulba0ac1a2022-08-12 16:29:30 -0700100GLOG_ATTRIBUTE_FORMAT(printf, 3, 4)
101static bool DoRawLog(char** buf, size_t* size, const char* format, ...) {
Austin Schuh906616c2019-01-21 20:25:11 -0800102 va_list ap;
103 va_start(ap, format);
104 int n = vsnprintf(*buf, *size, format, ap);
105 va_end(ap);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700106 if (n < 0 || static_cast<size_t>(n) > *size) return false;
107 *size -= static_cast<size_t>(n);
Austin Schuh906616c2019-01-21 20:25:11 -0800108 *buf += n;
109 return true;
110}
111
112// Helper for RawLog__ below.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700113inline static bool VADoRawLog(char** buf, size_t* size,
Austin Schuh906616c2019-01-21 20:25:11 -0800114 const char* format, va_list ap) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700115#if defined(__GNUC__)
116#pragma GCC diagnostic push
117#pragma GCC diagnostic ignored "-Wformat-nonliteral"
118#endif
Austin Schuh906616c2019-01-21 20:25:11 -0800119 int n = vsnprintf(*buf, *size, format, ap);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700120#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 Schuh906616c2019-01-21 20:25:11 -0800125 *buf += n;
126 return true;
127}
128
129static const int kLogBufSize = 3000;
130static bool crashed = false;
131static CrashReason crash_reason;
132static char crash_buf[kLogBufSize + 1] = { 0 }; // Will end in '\0'
133
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700134GLOG_ATTRIBUTE_FORMAT(printf, 4, 5)
Austin Schuh906616c2019-01-21 20:25:11 -0800135void RawLog__(LogSeverity severity, const char* file, int line,
136 const char* format, ...) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700137 if (!(FLAGS_logtostdout || FLAGS_logtostderr ||
138 severity >= FLAGS_stderrthreshold || FLAGS_alsologtostderr ||
139 !IsGoogleLoggingInitialized())) {
Austin Schuh906616c2019-01-21 20:25:11 -0800140 return; // this stderr log message is suppressed
141 }
142 // can't call localtime_r here: it can allocate
Austin Schuh906616c2019-01-21 20:25:11 -0800143 char buffer[kLogBufSize];
144 char* buf = buffer;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700145 size_t size = sizeof(buffer);
Austin Schuh906616c2019-01-21 20:25:11 -0800146
147 // NOTE: this format should match the specification in base/logging.h
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700148 DoRawLog(&buf, &size, "%c00000000 00:00:00.000000 %5u %s:%d] RAW: ",
Austin Schuh906616c2019-01-21 20:25:11 -0800149 LogSeverityNames[severity][0],
Austin Schuh906616c2019-01-21 20:25:11 -0800150 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 Kuszmaulba0ac1a2022-08-12 16:29:30 -0700155 const size_t msg_size = size;
Austin Schuh906616c2019-01-21 20:25:11 -0800156
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 Schuh61226052022-06-20 09:40:08 -0700172 MaybeUnsetRealtime();
Austin Schuh906616c2019-01-21 20:25:11 -0800173 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_