blob: 4e7fc87ec436bdf530eff2cced57438941e6b563 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2005, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Sanjay Ghemawat <opensource@google.com>
33
34#include <config.h>
35#include "internal_logging.h"
36#include <stdarg.h> // for va_end, va_start
37#include <stdio.h> // for vsnprintf, va_list, etc
38#include <stdlib.h> // for abort
39#include <string.h> // for strlen, memcpy
40#ifdef HAVE_UNISTD_H
41#include <unistd.h> // for write()
42#endif
43
44#include <gperftools/malloc_extension.h>
45#include "base/logging.h" // for perftools_vsnprintf
46#include "base/spinlock.h" // for SpinLockHolder, SpinLock
47
48static const int kLogBufSize = 800;
49
50// Variables for storing crash output. Allocated statically since we
51// may not be able to heap-allocate while crashing.
52static SpinLock crash_lock(base::LINKER_INITIALIZED);
53static bool crashed = false;
54static const int kStatsBufferSize = 16 << 10;
55static char stats_buffer[kStatsBufferSize] = { 0 };
56
57namespace tcmalloc {
58
59static void WriteMessage(const char* msg, int length) {
60 write(STDERR_FILENO, msg, length);
61}
62
63void (*log_message_writer)(const char* msg, int length) = WriteMessage;
64
65
66class Logger {
67 public:
68 bool Add(const LogItem& item);
69 bool AddStr(const char* str, int n);
70 bool AddNum(uint64_t num, int base); // base must be 10 or 16.
71
72 static const int kBufSize = 200;
73 char* p_;
74 char* end_;
75 char buf_[kBufSize];
76};
77
78void Log(LogMode mode, const char* filename, int line,
79 LogItem a, LogItem b, LogItem c, LogItem d) {
80 Logger state;
81 state.p_ = state.buf_;
82 state.end_ = state.buf_ + sizeof(state.buf_);
83 state.AddStr(filename, strlen(filename))
84 && state.AddStr(":", 1)
85 && state.AddNum(line, 10)
86 && state.AddStr("]", 1)
87 && state.Add(a)
88 && state.Add(b)
89 && state.Add(c)
90 && state.Add(d);
91
92 // Teminate with newline
93 if (state.p_ >= state.end_) {
94 state.p_ = state.end_ - 1;
95 }
96 *state.p_ = '\n';
97 state.p_++;
98
99 int msglen = state.p_ - state.buf_;
100 if (mode == kLog) {
101 (*log_message_writer)(state.buf_, msglen);
102 return;
103 }
104
105 bool first_crash = false;
106 {
107 SpinLockHolder l(&crash_lock);
108 if (!crashed) {
109 crashed = true;
110 first_crash = true;
111 }
112 }
113
114 (*log_message_writer)(state.buf_, msglen);
115 if (first_crash && mode == kCrashWithStats) {
116 MallocExtension::instance()->GetStats(stats_buffer, kStatsBufferSize);
117 (*log_message_writer)(stats_buffer, strlen(stats_buffer));
118 }
119
120 abort();
121}
122
123bool Logger::Add(const LogItem& item) {
124 // Separate items with spaces
125 if (p_ < end_) {
126 *p_ = ' ';
127 p_++;
128 }
129
130 switch (item.tag_) {
131 case LogItem::kStr:
132 return AddStr(item.u_.str, strlen(item.u_.str));
133 case LogItem::kUnsigned:
134 return AddNum(item.u_.unum, 10);
135 case LogItem::kSigned:
136 if (item.u_.snum < 0) {
137 // The cast to uint64_t is intentionally before the negation
138 // so that we do not attempt to negate -2^63.
139 return AddStr("-", 1)
140 && AddNum(- static_cast<uint64_t>(item.u_.snum), 10);
141 } else {
142 return AddNum(static_cast<uint64_t>(item.u_.snum), 10);
143 }
144 case LogItem::kPtr:
145 return AddStr("0x", 2)
146 && AddNum(reinterpret_cast<uintptr_t>(item.u_.ptr), 16);
147 default:
148 return false;
149 }
150}
151
152bool Logger::AddStr(const char* str, int n) {
153 if (end_ - p_ < n) {
154 return false;
155 } else {
156 memcpy(p_, str, n);
157 p_ += n;
158 return true;
159 }
160}
161
162bool Logger::AddNum(uint64_t num, int base) {
163 static const char kDigits[] = "0123456789abcdef";
164 char space[22]; // more than enough for 2^64 in smallest supported base (10)
165 char* end = space + sizeof(space);
166 char* pos = end;
167 do {
168 pos--;
169 *pos = kDigits[num % base];
170 num /= base;
171 } while (num > 0 && pos > space);
172 return AddStr(pos, end - pos);
173}
174
175} // end tcmalloc namespace
176
177void TCMalloc_Printer::printf(const char* format, ...) {
178 if (left_ > 0) {
179 va_list ap;
180 va_start(ap, format);
181 const int r = perftools_vsnprintf(buf_, left_, format, ap);
182 va_end(ap);
183 if (r < 0) {
184 // Perhaps an old glibc that returns -1 on truncation?
185 left_ = 0;
186 } else if (r > left_) {
187 // Truncation
188 left_ = 0;
189 } else {
190 left_ -= r;
191 buf_ += r;
192 }
193 }
194}