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 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "oops/instanceKlass.hpp"
  28 #include "runtime/mutexLocker.hpp"
  29 #include "runtime/os.inline.hpp"
  30 #include "runtime/osThread.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadCritical.hpp"
  33 #include "runtime/timer.hpp"
  34 #include "utilities/events.hpp"
  35 
  36 
  37 EventLog* Events::_logs = NULL;
  38 StringEventLog* Events::_messages = NULL;
  39 ExtendedStringEventLog* Events::_exceptions = NULL;
  40 StringEventLog* Events::_redefinitions = NULL;
  41 UnloadingEventLog* Events::_class_unloading = NULL;
  42 StringEventLog* Events::_deopt_messages = NULL;
  43 
  44 EventLog::EventLog() {
  45   // This normally done during bootstrap when we're only single
  46   // threaded but use a ThreadCritical to ensure inclusion in case
  47   // some are created slightly late.
  48   ThreadCritical tc;
  49   _next = Events::_logs;
  50   Events::_logs = this;
  51 }
  52 
  53 // For each registered event logger, print out the current contents of
  54 // the buffer.  This is normally called when the JVM is crashing.
  55 void Events::print_all(outputStream* out) {
  56   EventLog* log = _logs;
  57   while (log != NULL) {
  58     log->print_log_on(out);
  59     log = log->next();
  60   }
  61 }
  62 
  63 void Events::print() {
  64   print_all(tty);
  65 }
  66 
  67 void Events::init() {
  68   if (LogEvents) {
  69     _messages = new StringEventLog("Events");
  70     _exceptions = new ExtendedStringEventLog("Internal exceptions");
  71     _redefinitions = new StringEventLog("Classes redefined");
  72     _class_unloading = new UnloadingEventLog("Classes unloaded");
  73     _deopt_messages = new StringEventLog("Deoptimization events");
  74   }
  75 }
  76 
  77 void eventlog_init() {
  78   Events::init();
  79 }
  80 
  81 ///////////////////////////////////////////////////////////////////////////
  82 // EventMark
  83 
  84 EventMark::EventMark(const char* format, ...) {
  85   if (LogEvents) {
  86     va_list ap;
  87     va_start(ap, format);
  88     // Save a copy of begin message and log it.
  89     _buffer.printv(format, ap);
  90     Events::log(NULL, "%s", _buffer.buffer());
  91     va_end(ap);
  92   }
  93 }
  94 
  95 EventMark::~EventMark() {
  96   if (LogEvents) {
  97     // Append " done" to the begin message and log it
  98     _buffer.append(" done");
  99     Events::log(NULL, "%s", _buffer.buffer());
 100   }
 101 }
 102 
 103 void UnloadingEventLog::log(Thread* thread, InstanceKlass* ik) {
 104   if (!should_log()) return;
 105 
 106   double timestamp = fetch_timestamp();
 107   // Unloading events are single threaded.
 108   int index = compute_log_index();
 109   _records[index].thread = thread;
 110   _records[index].timestamp = timestamp;
 111   stringStream st = _records[index].data.stream();
 112   st.print("Unloading class " INTPTR_FORMAT " ", p2i(ik));
 113   ik->name()->print_value_on(&st);
 114 }