blob: befeac89aec743a34909882c93255857c9e35b4a [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
70_START_GOOGLE_NAMESPACE_
71
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070072#if defined(__GNUC__)
73#define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) \
74 __attribute__((format(archetype, stringIndex, firstToCheck)))
75#define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex) \
76 __attribute__((format_arg(stringIndex)))
77#else
78#define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck)
79#define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex)
80#endif
Austin Schuh906616c2019-01-21 20:25:11 -080081
82// CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths
83// that invoke malloc() and getenv() that might acquire some locks.
84// If this becomes a problem we should reimplement a subset of vsnprintf
85// that does not need locks and malloc.
86
87// Helper for RawLog__ below.
88// *DoRawLog writes to *buf of *size and move them past the written portion.
89// It returns true iff there was no overflow or error.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070090GLOG_ATTRIBUTE_FORMAT(printf, 3, 4)
91static bool DoRawLog(char** buf, size_t* size, const char* format, ...) {
Austin Schuh906616c2019-01-21 20:25:11 -080092 va_list ap;
93 va_start(ap, format);
94 int n = vsnprintf(*buf, *size, format, ap);
95 va_end(ap);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070096 if (n < 0 || static_cast<size_t>(n) > *size) return false;
97 *size -= static_cast<size_t>(n);
Austin Schuh906616c2019-01-21 20:25:11 -080098 *buf += n;
99 return true;
100}
101
102// Helper for RawLog__ below.
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700103inline static bool VADoRawLog(char** buf, size_t* size,
Austin Schuh906616c2019-01-21 20:25:11 -0800104 const char* format, va_list ap) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700105#if defined(__GNUC__)
106#pragma GCC diagnostic push
107#pragma GCC diagnostic ignored "-Wformat-nonliteral"
108#endif
Austin Schuh906616c2019-01-21 20:25:11 -0800109 int n = vsnprintf(*buf, *size, format, ap);
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700110#if defined(__GNUC__)
111#pragma GCC diagnostic pop
112#endif
113 if (n < 0 || static_cast<size_t>(n) > *size) return false;
114 *size -= static_cast<size_t>(n);
Austin Schuh906616c2019-01-21 20:25:11 -0800115 *buf += n;
116 return true;
117}
118
119static const int kLogBufSize = 3000;
120static bool crashed = false;
121static CrashReason crash_reason;
122static char crash_buf[kLogBufSize + 1] = { 0 }; // Will end in '\0'
123
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700124GLOG_ATTRIBUTE_FORMAT(printf, 4, 5)
Austin Schuh906616c2019-01-21 20:25:11 -0800125void RawLog__(LogSeverity severity, const char* file, int line,
126 const char* format, ...) {
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700127 if (!(FLAGS_logtostdout || FLAGS_logtostderr ||
128 severity >= FLAGS_stderrthreshold || FLAGS_alsologtostderr ||
129 !IsGoogleLoggingInitialized())) {
Austin Schuh906616c2019-01-21 20:25:11 -0800130 return; // this stderr log message is suppressed
131 }
132 // can't call localtime_r here: it can allocate
Austin Schuh906616c2019-01-21 20:25:11 -0800133 char buffer[kLogBufSize];
134 char* buf = buffer;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700135 size_t size = sizeof(buffer);
Austin Schuh906616c2019-01-21 20:25:11 -0800136
137 // NOTE: this format should match the specification in base/logging.h
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700138 DoRawLog(&buf, &size, "%c00000000 00:00:00.000000 %5u %s:%d] RAW: ",
Austin Schuh906616c2019-01-21 20:25:11 -0800139 LogSeverityNames[severity][0],
Austin Schuh906616c2019-01-21 20:25:11 -0800140 static_cast<unsigned int>(GetTID()),
141 const_basename(const_cast<char *>(file)), line);
142
143 // Record the position and size of the buffer after the prefix
144 const char* msg_start = buf;
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700145 const size_t msg_size = size;
Austin Schuh906616c2019-01-21 20:25:11 -0800146
147 va_list ap;
148 va_start(ap, format);
149 bool no_chop = VADoRawLog(&buf, &size, format, ap);
150 va_end(ap);
151 if (no_chop) {
152 DoRawLog(&buf, &size, "\n");
153 } else {
154 DoRawLog(&buf, &size, "RAW_LOG ERROR: The Message was too long!\n");
155 }
156 // We make a raw syscall to write directly to the stderr file descriptor,
157 // avoiding FILE buffering (to avoid invoking malloc()), and bypassing
158 // libc (to side-step any libc interception).
159 // We write just once to avoid races with other invocations of RawLog__.
160 safe_write(STDERR_FILENO, buffer, strlen(buffer));
161 if (severity == GLOG_FATAL) {
162 if (!sync_val_compare_and_swap(&crashed, false, true)) {
163 crash_reason.filename = file;
164 crash_reason.line_number = line;
165 memcpy(crash_buf, msg_start, msg_size); // Don't include prefix
166 crash_reason.message = crash_buf;
167#ifdef HAVE_STACKTRACE
168 crash_reason.depth =
169 GetStackTrace(crash_reason.stack, ARRAYSIZE(crash_reason.stack), 1);
170#else
171 crash_reason.depth = 0;
172#endif
173 SetCrashReason(&crash_reason);
174 }
175 LogMessage::Fail(); // abort()
176 }
177}
178
179_END_GOOGLE_NAMESPACE_