1 /*
   2  * Copyright (c) 1997, 2019, 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_UTILITIES_EVENTS_HPP
  26 #define SHARE_UTILITIES_EVENTS_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/mutexLocker.hpp"
  30 #include "runtime/thread.hpp"
  31 #include "utilities/formatBuffer.hpp"
  32 #include "utilities/vmError.hpp"
  33 
  34 // Events and EventMark provide interfaces to log events taking place in the vm.
  35 // This facility is extremly useful for post-mortem debugging. The eventlog
  36 // often provides crucial information about events leading up to the crash.
  37 //
  38 // Abstractly the logs can record whatever they way but normally they
  39 // would record at least a timestamp and the current Thread, along
  40 // with whatever data they need in a ring buffer.  Commonly fixed
  41 // length text messages are recorded for simplicity but other
  42 // strategies could be used.  Several logs are provided by default but
  43 // new instances can be created as needed.
  44 
  45 // The base event log dumping class that is registered for dumping at
  46 // crash time.  This is a very generic interface that is mainly here
  47 // for completeness.  Normally the templated EventLogBase would be
  48 // subclassed to provide different log types.
  49 class EventLog : public CHeapObj<mtInternal> {
  50   friend class Events;
  51 
  52  private:
  53   EventLog* _next;
  54 
  55   EventLog* next() const { return _next; }
  56 
  57  public:
  58   // Automatically registers the log so that it will be printed during
  59   // crashes.
  60   EventLog();
  61 
  62   virtual void print_log_on(outputStream* out) = 0;
  63 };
  64 
  65 
  66 // A templated subclass of EventLog that provides basic ring buffer
  67 // functionality.  Most event loggers should subclass this, possibly
  68 // providing a more featureful log function if the existing copy
  69 // semantics aren't appropriate.  The name is used as the label of the
  70 // log when it is dumped during a crash.
  71 template <class T> class EventLogBase : public EventLog {
  72   template <class X> class EventRecord : public CHeapObj<mtInternal> {
  73    public:
  74     double  timestamp;
  75     Thread* thread;
  76     X       data;
  77   };
  78 
  79  protected:
  80   Mutex           _mutex;
  81   const char*     _name;
  82   int             _length;
  83   int             _index;
  84   int             _count;
  85   EventRecord<T>* _records;
  86 
  87  public:
  88   EventLogBase<T>(const char* name, int length = LogEventsBufferEntries):
  89     _mutex(Mutex::event, name, false, Monitor::_safepoint_check_never),
  90     _name(name),
  91     _length(length),
  92     _index(0),
  93     _count(0) {
  94     _records = new EventRecord<T>[length];
  95   }
  96 
  97   double fetch_timestamp() {
  98     return os::elapsedTime();
  99   }
 100 
 101   // move the ring buffer to next open slot and return the index of
 102   // the slot to use for the current message.  Should only be called
 103   // while mutex is held.
 104   int compute_log_index() {
 105     int index = _index;
 106     if (_count < _length) _count++;
 107     _index++;
 108     if (_index >= _length) _index = 0;
 109     return index;
 110   }
 111 
 112   bool should_log() {
 113     // Don't bother adding new entries when we're crashing.  This also
 114     // avoids mutating the ring buffer when printing the log.
 115     return !VMError::fatal_error_in_progress();
 116   }
 117 
 118   // Print the contents of the log
 119   void print_log_on(outputStream* out);
 120 
 121  private:
 122   void print_log_impl(outputStream* out);
 123 
 124   // Print a single element.  A templated implementation might need to
 125   // be declared by subclasses.
 126   void print(outputStream* out, T& e);
 127 
 128   void print(outputStream* out, EventRecord<T>& e) {
 129     out->print("Event: %.3f ", e.timestamp);
 130     if (e.thread != NULL) {
 131       out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread));
 132     }
 133     print(out, e.data);
 134   }
 135 };
 136 
 137 // A simple wrapper class for fixed size text messages.
 138 template <size_t bufsz>
 139 class FormatStringLogMessage : public FormatBuffer<bufsz> {
 140 };
 141 typedef FormatStringLogMessage<256> StringLogMessage;
 142 typedef FormatStringLogMessage<512> ExtendedStringLogMessage;
 143 
 144 // A simple ring buffer of fixed size text messages.
 145 template <size_t bufsz>
 146 class FormatStringEventLog : public EventLogBase< FormatStringLogMessage<bufsz> > {
 147  public:
 148   FormatStringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase< FormatStringLogMessage<bufsz> >(name, count) {}
 149 
 150   void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) {
 151     if (!this->should_log()) return;
 152 
 153     double timestamp = this->fetch_timestamp();
 154     MutexLocker ml(&this->_mutex, Mutex::_no_safepoint_check_flag);
 155     int index = this->compute_log_index();
 156     this->_records[index].thread = thread;
 157     this->_records[index].timestamp = timestamp;
 158     this->_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     this->logv(thread, format, ap);
 165     va_end(ap);
 166   }
 167 };
 168 typedef FormatStringEventLog<256> StringEventLog;
 169 typedef FormatStringEventLog<512> ExtendedStringEventLog;
 170 
 171 class InstanceKlass;
 172 
 173 // Event log for class unloading events to materialize the class name in place in the log stream.
 174 class UnloadingEventLog : public EventLogBase<StringLogMessage> {
 175  public:
 176   UnloadingEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {}
 177 
 178   void log(Thread* thread, InstanceKlass* ik);
 179 };
 180 
 181 // Event log for exceptions
 182 class ExceptionsEventLog : public ExtendedStringEventLog {
 183  public:
 184   ExceptionsEventLog(const char* name, int count = LogEventsBufferEntries) : ExtendedStringEventLog(name, count) {}
 185 
 186   void log(Thread* thread, Handle h_exception, const char* message, const char* file, int line);
 187 };
 188 
 189 
 190 class Events : AllStatic {
 191   friend class EventLog;
 192 
 193  private:
 194   static EventLog* _logs;
 195 
 196   // A log for generic messages that aren't well categorized.
 197   static StringEventLog* _messages;
 198 
 199   // A log for internal exception related messages, like internal
 200   // throws and implicit exceptions.
 201   static ExceptionsEventLog* _exceptions;
 202 
 203   // Deoptization related messages
 204   static StringEventLog* _deopt_messages;
 205 
 206   // Redefinition related messages
 207   static StringEventLog* _redefinitions;
 208 
 209   // Class unloading events
 210   static UnloadingEventLog* _class_unloading;
 211  public:
 212   static void print_all(outputStream* out);
 213 
 214   // Dump all events to the tty
 215   static void print();
 216 
 217   // Logs a generic message with timestamp and format as printf.
 218   static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 219 
 220   // Log exception related message
 221   static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 222   static void log_exception(Thread* thread, Handle h_exception, const char* message, const char* file, int line);
 223 
 224   static void log_redefinition(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 225 
 226   static void log_class_unloading(Thread* thread, InstanceKlass* ik);
 227 
 228   static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 229 
 230   // Register default loggers
 231   static void init();
 232 };
 233 
 234 inline void Events::log(Thread* thread, const char* format, ...) {
 235   if (LogEvents && _messages != NULL) {
 236     va_list ap;
 237     va_start(ap, format);
 238     _messages->logv(thread, format, ap);
 239     va_end(ap);
 240   }
 241 }
 242 
 243 inline void Events::log_exception(Thread* thread, const char* format, ...) {
 244   if (LogEvents && _exceptions != NULL) {
 245     va_list ap;
 246     va_start(ap, format);
 247     _exceptions->logv(thread, format, ap);
 248     va_end(ap);
 249   }
 250 }
 251 
 252 inline void Events::log_exception(Thread* thread, Handle h_exception, const char* message, const char* file, int line) {
 253   if (LogEvents && _exceptions != NULL) {
 254     _exceptions->log(thread, h_exception, message, file, line);
 255   }
 256 }
 257 
 258 inline void Events::log_redefinition(Thread* thread, const char* format, ...) {
 259   if (LogEvents && _redefinitions != NULL) {
 260     va_list ap;
 261     va_start(ap, format);
 262     _redefinitions->logv(thread, format, ap);
 263     va_end(ap);
 264   }
 265 }
 266 
 267 inline void Events::log_class_unloading(Thread* thread, InstanceKlass* ik) {
 268   if (LogEvents && _class_unloading != NULL) {
 269     _class_unloading->log(thread, ik);
 270   }
 271 }
 272 
 273 inline void Events::log_deopt_message(Thread* thread, const char* format, ...) {
 274   if (LogEvents && _deopt_messages != NULL) {
 275     va_list ap;
 276     va_start(ap, format);
 277     _deopt_messages->logv(thread, format, ap);
 278     va_end(ap);
 279   }
 280 }
 281 
 282 
 283 template <class T>
 284 inline void EventLogBase<T>::print_log_on(outputStream* out) {
 285   if (Thread::current_or_null() == NULL) {
 286     // Not yet attached? Don't try to use locking
 287     print_log_impl(out);
 288   } else {
 289     MutexLocker ml(&_mutex, Mutex::_no_safepoint_check_flag);
 290     print_log_impl(out);
 291   }
 292 }
 293 
 294 // Dump the ring buffer entries that current have entries.
 295 template <class T>
 296 inline void EventLogBase<T>::print_log_impl(outputStream* out) {
 297   out->print_cr("%s (%d events):", _name, _count);
 298   if (_count == 0) {
 299     out->print_cr("No events");
 300     out->cr();
 301     return;
 302   }
 303 
 304   if (_count < _length) {
 305     for (int i = 0; i < _count; i++) {
 306       print(out, _records[i]);
 307     }
 308   } else {
 309     for (int i = _index; i < _length; i++) {
 310       print(out, _records[i]);
 311     }
 312     for (int i = 0; i < _index; i++) {
 313       print(out, _records[i]);
 314     }
 315   }
 316   out->cr();
 317 }
 318 
 319 // Implement a printing routine for the StringLogMessage
 320 template <>
 321 inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) {
 322   out->print_raw(lm);
 323   out->cr();
 324 }
 325 
 326 // Implement a printing routine for the ExtendedStringLogMessage
 327 template <>
 328 inline void EventLogBase<ExtendedStringLogMessage>::print(outputStream* out, ExtendedStringLogMessage& lm) {
 329   out->print_raw(lm);
 330   out->cr();
 331 }
 332 
 333 // Place markers for the beginning and end up of a set of events.
 334 // These end up in the default log.
 335 class EventMark : public StackObj {
 336   StringLogMessage _buffer;
 337 
 338  public:
 339   // log a begin event, format as printf
 340   EventMark(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 341   // log an end event
 342   ~EventMark();
 343 };
 344 
 345 #endif // SHARE_UTILITIES_EVENTS_HPP