events.hpp revision 4121
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
df8bdeb362277e8d95a74d6c097341fe97409948johnz * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
df8bdeb362277e8d95a74d6c097341fe97409948johnz * This code is free software; you can redistribute it and/or modify it
df8bdeb362277e8d95a74d6c097341fe97409948johnz * under the terms of the GNU General Public License version 2 only, as
df8bdeb362277e8d95a74d6c097341fe97409948johnz * published by the Free Software Foundation.
df8bdeb362277e8d95a74d6c097341fe97409948johnz * This code is distributed in the hope that it will be useful, but WITHOUT
df8bdeb362277e8d95a74d6c097341fe97409948johnz * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
df8bdeb362277e8d95a74d6c097341fe97409948johnz * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
df8bdeb362277e8d95a74d6c097341fe97409948johnz * version 2 for more details (a copy is included in the LICENSE file that
df8bdeb362277e8d95a74d6c097341fe97409948johnz * accompanied this code).
df8bdeb362277e8d95a74d6c097341fe97409948johnz * You should have received a copy of the GNU General Public License version
df8bdeb362277e8d95a74d6c097341fe97409948johnz * 2 along with this work; if not, write to the Free Software Foundation,
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
df8bdeb362277e8d95a74d6c097341fe97409948johnz * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
df8bdeb362277e8d95a74d6c097341fe97409948johnz * or visit www.oracle.com if you need additional information or have any
df8bdeb362277e8d95a74d6c097341fe97409948johnz * questions.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// Events and EventMark provide interfaces to log events taking place in the vm.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// This facility is extremly useful for post-mortem debugging. The eventlog
df8bdeb362277e8d95a74d6c097341fe97409948johnz// often provides crucial information about events leading up to the crash.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// Abstractly the logs can record whatever they way but normally they
df8bdeb362277e8d95a74d6c097341fe97409948johnz// would record at least a timestamp and the current Thread, along
df8bdeb362277e8d95a74d6c097341fe97409948johnz// with whatever data they need in a ring buffer. Commonly fixed
df8bdeb362277e8d95a74d6c097341fe97409948johnz// length text messages are recorded for simplicity but other
df8bdeb362277e8d95a74d6c097341fe97409948johnz// strategies could be used. Several logs are provided by default but
df8bdeb362277e8d95a74d6c097341fe97409948johnz// new instances can be created as needed.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// The base event log dumping class that is registered for dumping at
df8bdeb362277e8d95a74d6c097341fe97409948johnz// crash time. This is a very generic interface that is mainly here
df8bdeb362277e8d95a74d6c097341fe97409948johnz// for completeness. Normally the templated EventLogBase would be
df8bdeb362277e8d95a74d6c097341fe97409948johnz// subclassed to provide different log types.
df8bdeb362277e8d95a74d6c097341fe97409948johnz friend class Events;
735564919188238196dbd0d320770dda59b38369Anthony Scarpino // Automatically registers the log so that it will be printed during
df8bdeb362277e8d95a74d6c097341fe97409948johnz // crashes.
735564919188238196dbd0d320770dda59b38369Anthony Scarpino virtual void print_log_on(outputStream* out) = 0;
df8bdeb362277e8d95a74d6c097341fe97409948johnz// A templated subclass of EventLog that provides basic ring buffer
df8bdeb362277e8d95a74d6c097341fe97409948johnz// functionality. Most event loggers should subclass this, possibly
df8bdeb362277e8d95a74d6c097341fe97409948johnz// providing a more featureful log function if the existing copy
df8bdeb362277e8d95a74d6c097341fe97409948johnz// semantics aren't appropriate. The name is used as the label of the
df8bdeb362277e8d95a74d6c097341fe97409948johnz// log when it is dumped during a crash.
df8bdeb362277e8d95a74d6c097341fe97409948johnztemplate <class T> class EventLogBase : public EventLog {
df8bdeb362277e8d95a74d6c097341fe97409948johnz template <class X> class EventRecord {
df8bdeb362277e8d95a74d6c097341fe97409948johnz const char* _name;
df8bdeb362277e8d95a74d6c097341fe97409948johnz EventLogBase<T>(const char* name, int length = LogEventsBufferEntries):
df8bdeb362277e8d95a74d6c097341fe97409948johnz // move the ring buffer to next open slot and return the index of
df8bdeb362277e8d95a74d6c097341fe97409948johnz // the slot to use for the current message. Should only be called
df8bdeb362277e8d95a74d6c097341fe97409948johnz // while mutex is held.
735564919188238196dbd0d320770dda59b38369Anthony Scarpino // Don't bother adding new entries when we're crashing. This also
735564919188238196dbd0d320770dda59b38369Anthony Scarpino // avoids mutating the ring buffer when printing the log.
735564919188238196dbd0d320770dda59b38369Anthony Scarpino return !VMError::fatal_error_in_progress();
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Print the contents of the log
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM void print_log_on(outputStream* out);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM void print_log_impl(outputStream* out);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM // Print a single element. A templated implementation might need to
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM // be declared by subclasses.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// A simple wrapper class for fixed size text messages.
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMclass StringLogMessage : public FormatBuffer<256> {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM // Wrap this buffer in a stringStream.
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM// A simple ring buffer of fixed size text messages.
df8bdeb362277e8d95a74d6c097341fe97409948johnzclass StringEventLog : public EventLogBase<StringLogMessage> {
df8bdeb362277e8d95a74d6c097341fe97409948johnz StringEventLog(const char* name, int count = LogEventsBufferEntries) : EventLogBase<StringLogMessage>(name, count) {}
df8bdeb362277e8d95a74d6c097341fe97409948johnz void logv(Thread* thread, const char* format, va_list ap) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz if (!should_log()) return;
df8bdeb362277e8d95a74d6c097341fe97409948johnz MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
9f0bc604621fbb9b9b038e6de7da8f9c46e28608Wyllys Ingersoll void log(Thread* thread, const char* format, ...) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz // A log for generic messages that aren't well categorized.
df8bdeb362277e8d95a74d6c097341fe97409948johnz // A log for internal exception related messages, like internal
df8bdeb362277e8d95a74d6c097341fe97409948johnz // throws and implicit exceptions.
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Deoptization related messages
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Dump all events to the tty
df8bdeb362277e8d95a74d6c097341fe97409948johnz static void print();
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Logs a generic message with timestamp and format as printf.
df8bdeb362277e8d95a74d6c097341fe97409948johnz static void log(Thread* thread, const char* format, ...);
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Log exception related message
df8bdeb362277e8d95a74d6c097341fe97409948johnz static void log_exception(Thread* thread, const char* format, ...);
df8bdeb362277e8d95a74d6c097341fe97409948johnz static void log_deopt_message(Thread* thread, const char* format, ...);
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Register default loggers
df8bdeb362277e8d95a74d6c097341fe97409948johnz static void init();
df8bdeb362277e8d95a74d6c097341fe97409948johnzinline void Events::log(Thread* thread, const char* format, ...) {
df8bdeb362277e8d95a74d6c097341fe97409948johnzinline void Events::log_exception(Thread* thread, const char* format, ...) {
df8bdeb362277e8d95a74d6c097341fe97409948johnzinline void Events::log_deopt_message(Thread* thread, const char* format, ...) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMtemplate <class T>
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMinline void EventLogBase<T>::print_log_on(outputStream* out) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz // Not a regular Java thread so don't bother locking
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM MutexLockerEx ml(&_mutex, Mutex::_no_safepoint_check_flag);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM// Dump the ring buffer entries that current have entries.
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMtemplate <class T>
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMinline void EventLogBase<T>::print_log_impl(outputStream* out) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM out->print_cr("%s (%d events):", _name, _count);
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM for (int i = 0; i < _count; i++) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM for (int i = 0; i < _index; i++) {
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COM// Implement a printing routine for the StringLogMessage
8bab47abcb471dffa36ddbf409a8ef5303398ddfJohn.Zolnowsky@Sun.COMinline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) {
df8bdeb362277e8d95a74d6c097341fe97409948johnz// Place markers for the beginning and end up of a set of events.
df8bdeb362277e8d95a74d6c097341fe97409948johnz// These end up in the default log.
df8bdeb362277e8d95a74d6c097341fe97409948johnz // log a begin event, format as printf
df8bdeb362277e8d95a74d6c097341fe97409948johnz // log an end event
df8bdeb362277e8d95a74d6c097341fe97409948johnz#endif // SHARE_VM_UTILITIES_EVENTS_HPP