Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/utilities/events.hpp
40949 views
1
/*
2
* Copyright (c) 1997, 2020, 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/globalDefinitions.hpp"
33
#include "utilities/ostream.hpp"
34
#include "utilities/vmError.hpp"
35
36
// Events and EventMark provide interfaces to log events taking place in the vm.
37
// This facility is extremly useful for post-mortem debugging. The eventlog
38
// often provides crucial information about events leading up to the crash.
39
//
40
// Abstractly the logs can record whatever they way but normally they
41
// would record at least a timestamp and the current Thread, along
42
// with whatever data they need in a ring buffer. Commonly fixed
43
// length text messages are recorded for simplicity but other
44
// strategies could be used. Several logs are provided by default but
45
// new instances can be created as needed.
46
47
// The base event log dumping class that is registered for dumping at
48
// crash time. This is a very generic interface that is mainly here
49
// for completeness. Normally the templated EventLogBase would be
50
// subclassed to provide different log types.
51
class EventLog : public CHeapObj<mtInternal> {
52
friend class Events;
53
54
private:
55
EventLog* _next;
56
57
EventLog* next() const { return _next; }
58
59
public:
60
// Automatically registers the log so that it will be printed during
61
// crashes.
62
EventLog();
63
64
// Print log to output stream.
65
virtual void print_log_on(outputStream* out, int max = -1) = 0;
66
67
// Returns true if s matches either the log name or the log handle.
68
virtual bool matches_name_or_handle(const char* s) const = 0;
69
70
// Print log names (for help output of VM.events).
71
virtual void print_names(outputStream* out) const = 0;
72
73
};
74
75
76
// A templated subclass of EventLog that provides basic ring buffer
77
// functionality. Most event loggers should subclass this, possibly
78
// providing a more featureful log function if the existing copy
79
// semantics aren't appropriate. The name is used as the label of the
80
// log when it is dumped during a crash.
81
template <class T> class EventLogBase : public EventLog {
82
template <class X> class EventRecord : public CHeapObj<mtInternal> {
83
public:
84
double timestamp;
85
Thread* thread;
86
X data;
87
};
88
89
protected:
90
Mutex _mutex;
91
// Name is printed out as a header.
92
const char* _name;
93
// Handle is a short specifier used to select this particular event log
94
// for printing (see VM.events command).
95
const char* _handle;
96
int _length;
97
int _index;
98
int _count;
99
EventRecord<T>* _records;
100
101
public:
102
EventLogBase<T>(const char* name, const char* handle, int length = LogEventsBufferEntries):
103
_mutex(Mutex::event, name, true, Mutex::_safepoint_check_never),
104
_name(name),
105
_handle(handle),
106
_length(length),
107
_index(0),
108
_count(0) {
109
_records = new EventRecord<T>[length];
110
}
111
112
double fetch_timestamp() {
113
return os::elapsedTime();
114
}
115
116
// move the ring buffer to next open slot and return the index of
117
// the slot to use for the current message. Should only be called
118
// while mutex is held.
119
int compute_log_index() {
120
int index = _index;
121
if (_count < _length) _count++;
122
_index++;
123
if (_index >= _length) _index = 0;
124
return index;
125
}
126
127
bool should_log() {
128
// Don't bother adding new entries when we're crashing. This also
129
// avoids mutating the ring buffer when printing the log.
130
return !VMError::is_error_reported();
131
}
132
133
// Print the contents of the log
134
void print_log_on(outputStream* out, int max = -1);
135
136
// Returns true if s matches either the log name or the log handle.
137
bool matches_name_or_handle(const char* s) const;
138
139
// Print log names (for help output of VM.events).
140
void print_names(outputStream* out) const;
141
142
private:
143
void print_log_impl(outputStream* out, int max = -1);
144
145
// Print a single element. A templated implementation might need to
146
// be declared by subclasses.
147
void print(outputStream* out, T& e);
148
149
void print(outputStream* out, EventRecord<T>& e) {
150
out->print("Event: %.3f ", e.timestamp);
151
if (e.thread != NULL) {
152
out->print("Thread " INTPTR_FORMAT " ", p2i(e.thread));
153
}
154
print(out, e.data);
155
}
156
};
157
158
// A simple wrapper class for fixed size text messages.
159
template <size_t bufsz>
160
class FormatStringLogMessage : public FormatBuffer<bufsz> {
161
};
162
typedef FormatStringLogMessage<256> StringLogMessage;
163
typedef FormatStringLogMessage<512> ExtendedStringLogMessage;
164
165
// A simple ring buffer of fixed size text messages.
166
template <size_t bufsz>
167
class FormatStringEventLog : public EventLogBase< FormatStringLogMessage<bufsz> > {
168
public:
169
FormatStringEventLog(const char* name, const char* short_name, int count = LogEventsBufferEntries)
170
: EventLogBase< FormatStringLogMessage<bufsz> >(name, short_name, count) {}
171
172
void logv(Thread* thread, const char* format, va_list ap) ATTRIBUTE_PRINTF(3, 0) {
173
if (!this->should_log()) return;
174
175
double timestamp = this->fetch_timestamp();
176
MutexLocker ml(&this->_mutex, Mutex::_no_safepoint_check_flag);
177
int index = this->compute_log_index();
178
this->_records[index].thread = thread;
179
this->_records[index].timestamp = timestamp;
180
this->_records[index].data.printv(format, ap);
181
}
182
183
void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(3, 4) {
184
va_list ap;
185
va_start(ap, format);
186
this->logv(thread, format, ap);
187
va_end(ap);
188
}
189
};
190
typedef FormatStringEventLog<256> StringEventLog;
191
typedef FormatStringEventLog<512> ExtendedStringEventLog;
192
193
class InstanceKlass;
194
195
// Event log for class unloading events to materialize the class name in place in the log stream.
196
class UnloadingEventLog : public EventLogBase<StringLogMessage> {
197
public:
198
UnloadingEventLog(const char* name, const char* short_name, int count = LogEventsBufferEntries)
199
: EventLogBase<StringLogMessage>(name, short_name, count) {}
200
201
void log(Thread* thread, InstanceKlass* ik);
202
};
203
204
// Event log for exceptions
205
class ExceptionsEventLog : public ExtendedStringEventLog {
206
public:
207
ExceptionsEventLog(const char* name, const char* short_name, int count = LogEventsBufferEntries)
208
: ExtendedStringEventLog(name, short_name, count) {}
209
210
void log(Thread* thread, Handle h_exception, const char* message, const char* file, int line);
211
};
212
213
214
class Events : AllStatic {
215
friend class EventLog;
216
217
private:
218
static EventLog* _logs;
219
220
// A log for generic messages that aren't well categorized.
221
static StringEventLog* _messages;
222
223
// A log for VM Operations
224
static StringEventLog* _vm_operations;
225
226
// A log for internal exception related messages, like internal
227
// throws and implicit exceptions.
228
static ExceptionsEventLog* _exceptions;
229
230
// Deoptization related messages
231
static StringEventLog* _deopt_messages;
232
233
// Redefinition related messages
234
static StringEventLog* _redefinitions;
235
236
// Class unloading events
237
static UnloadingEventLog* _class_unloading;
238
public:
239
240
// Print all event logs; limit number of events per event log to be printed with max
241
// (max == -1 prints all events).
242
static void print_all(outputStream* out, int max = -1);
243
244
// Print a single event log specified by name or handle.
245
static void print_one(outputStream* out, const char* log_name, int max = -1);
246
247
// Dump all events to the tty
248
static void print();
249
250
// Logs a generic message with timestamp and format as printf.
251
static void log(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
252
253
static void log_vm_operation(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
254
255
// Log exception related message
256
static void log_exception(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
257
static void log_exception(Thread* thread, Handle h_exception, const char* message, const char* file, int line);
258
259
static void log_redefinition(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
260
261
static void log_class_unloading(Thread* thread, InstanceKlass* ik);
262
263
static void log_deopt_message(Thread* thread, const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
264
265
// Register default loggers
266
static void init();
267
};
268
269
inline void Events::log(Thread* thread, const char* format, ...) {
270
if (LogEvents && _messages != NULL) {
271
va_list ap;
272
va_start(ap, format);
273
_messages->logv(thread, format, ap);
274
va_end(ap);
275
}
276
}
277
278
inline void Events::log_vm_operation(Thread* thread, const char* format, ...) {
279
if (LogEvents && _vm_operations != NULL) {
280
va_list ap;
281
va_start(ap, format);
282
_vm_operations->logv(thread, format, ap);
283
va_end(ap);
284
}
285
}
286
287
inline void Events::log_exception(Thread* thread, const char* format, ...) {
288
if (LogEvents && _exceptions != NULL) {
289
va_list ap;
290
va_start(ap, format);
291
_exceptions->logv(thread, format, ap);
292
va_end(ap);
293
}
294
}
295
296
inline void Events::log_exception(Thread* thread, Handle h_exception, const char* message, const char* file, int line) {
297
if (LogEvents && _exceptions != NULL) {
298
_exceptions->log(thread, h_exception, message, file, line);
299
}
300
}
301
302
inline void Events::log_redefinition(Thread* thread, const char* format, ...) {
303
if (LogEvents && _redefinitions != NULL) {
304
va_list ap;
305
va_start(ap, format);
306
_redefinitions->logv(thread, format, ap);
307
va_end(ap);
308
}
309
}
310
311
inline void Events::log_class_unloading(Thread* thread, InstanceKlass* ik) {
312
if (LogEvents && _class_unloading != NULL) {
313
_class_unloading->log(thread, ik);
314
}
315
}
316
317
inline void Events::log_deopt_message(Thread* thread, const char* format, ...) {
318
if (LogEvents && _deopt_messages != NULL) {
319
va_list ap;
320
va_start(ap, format);
321
_deopt_messages->logv(thread, format, ap);
322
va_end(ap);
323
}
324
}
325
326
template <class T>
327
inline void EventLogBase<T>::print_log_on(outputStream* out, int max) {
328
struct MaybeLocker {
329
Mutex* const _mutex;
330
bool _proceed;
331
bool _locked;
332
333
MaybeLocker(Mutex* mutex) : _mutex(mutex), _proceed(false), _locked(false) {
334
if (Thread::current_or_null() == NULL) {
335
_proceed = true;
336
} else if (VMError::is_error_reported()) {
337
if (_mutex->try_lock_without_rank_check()) {
338
_proceed = _locked = true;
339
}
340
} else {
341
_mutex->lock_without_safepoint_check();
342
_proceed = _locked = true;
343
}
344
}
345
~MaybeLocker() {
346
if (_locked) {
347
_mutex->unlock();
348
}
349
}
350
};
351
352
MaybeLocker ml(&_mutex);
353
354
if (ml._proceed) {
355
print_log_impl(out, max);
356
} else {
357
out->print_cr("%s (%d events):", _name, _count);
358
out->print_cr("No events printed - crash while holding lock");
359
out->cr();
360
}
361
}
362
363
template <class T>
364
inline bool EventLogBase<T>::matches_name_or_handle(const char* s) const {
365
return ::strcasecmp(s, _name) == 0 ||
366
::strcasecmp(s, _handle) == 0;
367
}
368
369
template <class T>
370
inline void EventLogBase<T>::print_names(outputStream* out) const {
371
out->print("\"%s\" : %s", _handle, _name);
372
}
373
374
// Dump the ring buffer entries that current have entries.
375
template <class T>
376
inline void EventLogBase<T>::print_log_impl(outputStream* out, int max) {
377
out->print_cr("%s (%d events):", _name, _count);
378
if (_count == 0) {
379
out->print_cr("No events");
380
out->cr();
381
return;
382
}
383
384
int printed = 0;
385
if (_count < _length) {
386
for (int i = 0; i < _count; i++) {
387
if (max > 0 && printed == max) {
388
break;
389
}
390
print(out, _records[i]);
391
printed ++;
392
}
393
} else {
394
for (int i = _index; i < _length; i++) {
395
if (max > 0 && printed == max) {
396
break;
397
}
398
print(out, _records[i]);
399
printed ++;
400
}
401
for (int i = 0; i < _index; i++) {
402
if (max > 0 && printed == max) {
403
break;
404
}
405
print(out, _records[i]);
406
printed ++;
407
}
408
}
409
410
if (printed == max) {
411
out->print_cr("...(skipped)");
412
}
413
414
out->cr();
415
}
416
417
// Implement a printing routine for the StringLogMessage
418
template <>
419
inline void EventLogBase<StringLogMessage>::print(outputStream* out, StringLogMessage& lm) {
420
out->print_raw(lm);
421
out->cr();
422
}
423
424
// Implement a printing routine for the ExtendedStringLogMessage
425
template <>
426
inline void EventLogBase<ExtendedStringLogMessage>::print(outputStream* out, ExtendedStringLogMessage& lm) {
427
out->print_raw(lm);
428
out->cr();
429
}
430
431
typedef void (*EventLogFunction)(Thread* thread, const char* format, ...);
432
433
class EventMarkBase : public StackObj {
434
EventLogFunction _log_function;
435
StringLogMessage _buffer;
436
437
NONCOPYABLE(EventMarkBase);
438
439
protected:
440
void log_start(const char* format, va_list argp) ATTRIBUTE_PRINTF(2, 0);
441
void log_end();
442
443
EventMarkBase(EventLogFunction log_function);
444
};
445
446
// Place markers for the beginning and end up of a set of events.
447
template <EventLogFunction log_function>
448
class EventMarkWithLogFunction : public EventMarkBase {
449
StringLogMessage _buffer;
450
451
public:
452
// log a begin event, format as printf
453
EventMarkWithLogFunction(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) :
454
EventMarkBase(log_function) {
455
if (LogEvents) {
456
va_list ap;
457
va_start(ap, format);
458
log_start(format, ap);
459
va_end(ap);
460
}
461
}
462
// log an end event
463
~EventMarkWithLogFunction() {
464
if (LogEvents) {
465
log_end();
466
}
467
}
468
};
469
470
// These end up in the default log.
471
typedef EventMarkWithLogFunction<Events::log> EventMark;
472
473
// These end up in the vm_operation log.
474
typedef EventMarkWithLogFunction<Events::log_vm_operation> EventMarkVMOperation;
475
476
#endif // SHARE_UTILITIES_EVENTS_HPP
477
478