1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_UTILITIES_EVENTS_HPP 26 #define SHARE_VM_UTILITIES_EVENTS_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/mutexLocker.hpp" 30 #include "runtime/thread.hpp" 31 #include "utilities/vmError.hpp" 32 33 // Events and EventMark provide interfaces to log events taking place in the vm. 34 // This facility is extremly useful for post-mortem debugging. The eventlog 35 // often provides crucial information about events leading up to the crash. 36 // 37 // Abstractly the logs can record whatever they way but normally they 38 // would record at least a timestamp and the current Thread, along 39 // with whatever data they need in a ring buffer. Commonly fixed 40 // length text messages are recorded for simplicity but other 41 // strategies could be used. Several logs are provided by default but 42 // new instances can be created as needed. 43 44 // The base event log dumping class that is registered for dumping at 45 // crash time. This is a very generic interface that is mainly here 46 // for completeness. Normally the templated EventLogBase would be 47 // subclassed to provide different log types. 48 class EventLog : public CHeapObj<mtInternal> { 49 friend class Events; 50 51 private: 52 EventLog* _next; 53 54 EventLog* next() const { return _next; } 55 56 public: 57 // Automatically registers the log so that it will be printed during 58 // crashes. 59 EventLog(); 60 61 virtual void print_log_on(outputStream* out) = 0; 62 }; 63 64 65 // A templated subclass of EventLog that provides basic ring buffer 66 // functionality. Most event loggers should subclass this, possibly 67 // providing a more featureful log function if the existing copy 68 // semantics aren't appropriate. The name is used as the label of the 69 // log when it is dumped during a crash. 70 template <class T> class EventLogBase : public EventLog { 71 template <class X> class EventRecord : public CHeapObj<mtInternal> { 72 public: 73 double timestamp; 74 Thread* thread; 75 X data; 76 }; 77 78 protected: 79 Mutex _mutex; 80 const char* _name; 81 int _length; 82 int _index; 83 int _count; 84 EventRecord<T>* _records; 85 86 public: 87 EventLogBase<T>(const char* name, int length = LogEventsBufferEntries): 88 _name(name), 89 _length(length), 90 _count(0), 91 _index(0), 92 _mutex(Mutex::event, name, false, Monitor::_safepoint_check_never) { 93 _records = new EventRecord<T>[length]; 94 } 95 96 double fetch_timestamp() { 97 return os::elapsedTime(); 98 } 99 100 // move the ring buffer to next open slot and return the index of 101 // the slot to use for the current message. Should only be called 102 // while mutex is held. 103 int compute_log_index() { 104 int index = _index; 105 if (_count < _length) _count++; 106 _index++; 107 if (_index >= _length) _index = 0; 108 return index; 109 } 110 111 bool should_log() { 112 // Don't bother adding new entries when we're crashing. This also 113 // avoids mutating the ring buffer when printing the log. 114 return !VMError::fatal_error_in_progress(); 115 } 116 117 // Print the contents of the log 118 void print_log_on(outputStream* out); 119 120 private: 121 void print_log_impl(outputStream* out); 122 123 // Print a single element. A templated implementation might need to 124 // be declared by subclasses. 125 void print(outputStream* out, T& e); 126 127 void print(outputStream* out, EventRecord<T>& e) { 128 out->print("Event: %.3f ", e.timestamp); 129 if (e.thread != NULL) { 130 out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread)); 131 } 132 print(out, e.data); 133 } 134 }; 135 136 // A simple wrapper class for fixed size text messages. 137 class StringLogMessage : public FormatBuffer<256> { 138 public: 139 // Wrap this buffer in a stringStream. 140 stringStream stream() { 141 return stringStream(_buf, size()); 142 } 143 }; 144 145 // A simple ring buffer of fixed size text messages. 146 class StringEventLog : public EventLogBase<StringLogMessage> { 147 public: 148 StringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {} 149 150 void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) { 151 if (!should_log()) return; 152 153 double timestamp = fetch_timestamp(); 154 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag); 155 int index = compute_log_index(); 156 _records[index].thread = thread; 157 _records[index].timestamp = timestamp; 158 _records[index].data.printv(format, ap); 159 } 160 161 void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(3, 4) { 162 va_list ap; 163 va_start(ap, format); 164 logv(thread, format, ap); 165 va_end(ap); 166 } 167 168 }; 169 170 171 172 class Events : AllStatic { 173 friend class EventLog; 174 175 private: 176 static EventLog* _logs; 177 178 // A log for generic messages that aren't well categorized. 179 static StringEventLog* _messages; 180 181 // A log for internal exception related messages, like internal 182 // throws and implicit exceptions. 183 static StringEventLog* _exceptions; 184 185 // Deoptization related messages 186 static StringEventLog* _deopt_messages; 187 188 // Redefinition related messages 189 static StringEventLog* _redefinitions; 190 191 public: 192 static void print_all(outputStream* out); 193 194 // Dump all events to the tty 195 static void print(); 196 197 // Logs a generic message with timestamp and format as printf. 198 static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 199 200 // Log exception related message 201 static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 202 203 static void log_redefinition(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 204 205 static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 206 207 // Register default loggers 208 static void init(); 209 }; 210 211 inline void Events::log(Thread* thread, const char* format, ...) { 212 if (LogEvents) { 213 va_list ap; 214 va_start(ap, format); 215 _messages->logv(thread, format, ap); 216 va_end(ap); 217 } 218 } 219 220 inline void Events::log_exception(Thread* thread, const char* format, ...) { 221 if (LogEvents) { 222 va_list ap; 223 va_start(ap, format); 224 _exceptions->logv(thread, format, ap); 225 va_end(ap); 226 } 227 } 228 229 inline void Events::log_redefinition(Thread* thread, const char* format, ...) { 230 if (LogEvents) { 231 va_list ap; 232 va_start(ap, format); 233 _redefinitions->logv(thread, format, ap); 234 va_end(ap); 235 } 236 } 237 238 inline void Events::log_deopt_message(Thread* thread, const char* format, ...) { 239 if (LogEvents) { 240 va_list ap; 241 va_start(ap, format); 242 _deopt_messages->logv(thread, format, ap); 243 va_end(ap); 244 } 245 } 246 247 248 template <class T> 249 inline void EventLogBase<T>::print_log_on(outputStream* out) { 250 if (Thread::current_or_null() == NULL) { 251 // Not yet attached? Don't try to use locking 252 print_log_impl(out); 253 } else { 254 MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag); 255 print_log_impl(out); 256 } 257 } 258 259 // Dump the ring buffer entries that current have entries. 260 template <class T> 261 inline void EventLogBase<T>::print_log_impl(outputStream* out) { 262 out->print_cr("%s (%d events):", _name, _count); 263 if (_count == 0) { 264 out->print_cr("No events"); 265 out->cr(); 266 return; 267 } 268 269 if (_count < _length) { 270 for (int i = 0; i < _count; i++) { 271 print(out, _records[i]); 272 } 273 } else { 274 for (int i = _index; i < _length; i++) { 275 print(out, _records[i]); 276 } 277 for (int i = 0; i < _index; i++) { 278 print(out, _records[i]); 279 } 280 } 281 out->cr(); 282 } 283 284 // Implement a printing routine for the StringLogMessage 285 template <> 286 inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) { 287 out->print_raw(lm); 288 out->cr(); 289 } 290 291 // Place markers for the beginning and end up of a set of events. 292 // These end up in the default log. 293 class EventMark : public StackObj { 294 StringLogMessage _buffer; 295 296 public: 297 // log a begin event, format as printf 298 EventMark(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); 299 // log an end event 300 ~EventMark(); 301 }; 302 303 #endif // SHARE_VM_UTILITIES_EVENTS_HPP