2
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "code/nmethod.hpp"
27
#include "memory/allocation.hpp"
28
#include "memory/allocation.inline.hpp"
29
#include "memory/resourceArea.hpp"
30
#include "oops/methodData.hpp"
31
#include "oops/method.inline.hpp"
32
#include "oops/oop.inline.hpp"
33
#include "runtime/deoptimization.hpp"
34
#include "runtime/handles.inline.hpp"
35
#include "runtime/vmOperations.hpp"
36
#include "runtime/vmThread.hpp"
37
#include "utilities/vmError.hpp"
38
#include "utilities/xmlstream.hpp"
40
// Do not assert this condition if there's already another error reported.
41
#define assert_if_no_error(cond, msg) \
42
vmassert((cond) || VMError::is_error_reported(), msg)
44
void xmlStream::initialize(outputStream* out) {
48
_text_init._outer_xmlStream = this;
54
char* init_buf = NEW_C_HEAP_ARRAY(char, init_len, mtInternal);
55
_element_close_stack_low = init_buf;
56
_element_close_stack_high = init_buf + init_len;
57
_element_close_stack_ptr = init_buf + init_len - 1;
58
_element_close_stack_ptr[0] = '\0';
61
// Make sure each log uses the same base for time stamps.
63
_out->time_stamp().update_to(1);
68
xmlStream::~xmlStream() {
69
FREE_C_HEAP_ARRAY(char, _element_close_stack_low);
73
// Pass the given chars directly to _out.
74
void xmlStream::write(const char* s, size_t len) {
75
if (!is_open()) return;
78
update_position(s, len);
82
// Pass the given chars directly to _out, except that
83
// we watch for special "<&>" chars.
84
// This is suitable for either attribute text or for body text.
85
// We don't fool with "<![CDATA[" quotes, just single-character entities.
86
// This makes it easier for dumb tools to parse the output.
87
void xmlStream::write_text(const char* s, size_t len) {
88
if (!is_open()) return;
91
// All normally printed material goes inside XML quotes.
92
// This leaves the output free to include markup also.
93
// Scan the string looking for inadvertent "<&>" chars
94
for (size_t i = 0; i < len; i++) {
96
// Escape special chars.
97
const char* esc = nullptr;
99
// These are important only in attrs, but we do them always:
100
case '\'': esc = "'"; break;
101
case '"': esc = """; break;
102
case '<': esc = "<"; break;
103
case '&': esc = "&"; break;
104
// This is a freebie.
105
case '>': esc = ">"; break;
107
if (esc != nullptr) {
109
out()->write(&s[written], i - written);
112
out()->print_raw(esc);
117
// Print the clean remainder. Usually, it is all of s.
119
out()->write(&s[written], len - written);
123
// ------------------------------------------------------------------
124
// Outputs XML text, with special characters quoted.
125
void xmlStream::text(const char* format, ...) {
127
va_start(ap, format);
132
#define BUFLEN 2*K /* max size of output of individual print methods */
134
// ------------------------------------------------------------------
135
void xmlStream::va_tag(bool push, const char* format, va_list ap) {
136
assert_if_no_error(!inside_attrs(), "cannot print tag inside attrs");
139
const char* kind = do_vsnprintf(buffer, BUFLEN, format, ap, false, len);
143
_markup_state = (push ? HEAD : ELEM);
147
/// Debugging goo to make sure element tags nest properly.
149
// ------------------------------------------------------------------
150
void xmlStream::see_tag(const char* tag, bool push) {
151
assert_if_no_error(!inside_attrs(), "cannot start new element inside attrs");
154
// tag goes up until either null or space:
155
const char* tag_end = strchr(tag, ' ');
156
size_t tag_len = (tag_end == nullptr) ? strlen(tag) : tag_end - tag;
157
assert(tag_len > 0, "tag must not be empty");
158
// push the tag onto the stack, pulling down the pointer
159
char* old_ptr = _element_close_stack_ptr;
160
char* old_low = _element_close_stack_low;
161
char* push_ptr = old_ptr - (tag_len+1);
162
if (push_ptr < old_low) {
163
int old_len = pointer_delta_as_int(_element_close_stack_high, old_ptr);
164
int new_len = old_len * 2;
165
if (new_len < 100) new_len = 100;
166
char* new_low = NEW_C_HEAP_ARRAY(char, new_len, mtInternal);
167
char* new_high = new_low + new_len;
168
char* new_ptr = new_high - old_len;
169
memcpy(new_ptr, old_ptr, old_len);
170
_element_close_stack_high = new_high;
171
_element_close_stack_low = new_low;
172
_element_close_stack_ptr = new_ptr;
173
FREE_C_HEAP_ARRAY(char, old_low);
174
push_ptr = new_ptr - (tag_len+1);
176
assert(push_ptr >= _element_close_stack_low, "in range");
177
memcpy(push_ptr, tag, tag_len);
178
push_ptr[tag_len] = 0;
179
_element_close_stack_ptr = push_ptr;
183
// ------------------------------------------------------------------
184
void xmlStream::pop_tag(const char* tag) {
185
assert_if_no_error(!inside_attrs(), "cannot close element inside attrs");
186
assert(_element_depth > 0, "must be in an element to close");
187
assert(*tag != 0, "tag must not be empty");
188
char* cur_tag = _element_close_stack_ptr;
189
bool bad_tag = false;
190
while (*cur_tag != 0 && strcmp(cur_tag, tag) != 0) {
191
this->print_cr("</%s> <!-- missing closing tag -->", cur_tag);
192
_element_close_stack_ptr = (cur_tag += strlen(cur_tag) + 1);
199
// Pop the stack, by skipping over the tag and its null.
200
_element_close_stack_ptr = cur_tag + strlen(cur_tag) + 1;
203
if (bad_tag && !VMThread::should_terminate() && !VM_Exit::vm_exited() &&
204
!VMError::is_error_reported())
206
assert(false, "bad tag in log");
212
// ------------------------------------------------------------------
213
// First word in formatted string is element kind, and any subsequent
214
// words must be XML attributes. Outputs "<kind .../>".
215
void xmlStream::elem(const char* format, ...) {
217
va_start(ap, format);
222
// ------------------------------------------------------------------
223
void xmlStream::va_elem(const char* format, va_list ap) {
224
va_begin_elem(format, ap);
229
// ------------------------------------------------------------------
230
// First word in formatted string is element kind, and any subsequent
231
// words must be XML attributes. Outputs "<kind ...", not including "/>".
232
void xmlStream::begin_elem(const char* format, ...) {
234
va_start(ap, format);
235
va_tag(false, format, ap);
239
// ------------------------------------------------------------------
240
void xmlStream::va_begin_elem(const char* format, va_list ap) {
241
va_tag(false, format, ap);
244
// ------------------------------------------------------------------
246
void xmlStream::end_elem() {
247
assert(_markup_state == ELEM, "misplaced end_elem");
249
_markup_state = BODY;
252
// ------------------------------------------------------------------
253
// Outputs formatted text, followed by "/>".
254
void xmlStream::end_elem(const char* format, ...) {
256
va_start(ap, format);
257
out()->vprint(format, ap);
263
// ------------------------------------------------------------------
264
// First word in formatted string is element kind, and any subsequent
265
// words must be XML attributes. Outputs "<kind ...>".
266
void xmlStream::head(const char* format, ...) {
268
va_start(ap, format);
273
// ------------------------------------------------------------------
274
void xmlStream::va_head(const char* format, va_list ap) {
275
va_begin_head(format, ap);
279
// ------------------------------------------------------------------
280
// First word in formatted string is element kind, and any subsequent
281
// words must be XML attributes. Outputs "<kind ...", not including ">".
282
void xmlStream::begin_head(const char* format, ...) {
284
va_start(ap, format);
285
va_tag(true, format, ap);
289
// ------------------------------------------------------------------
290
void xmlStream::va_begin_head(const char* format, va_list ap) {
291
va_tag(true, format, ap);
294
// ------------------------------------------------------------------
296
void xmlStream::end_head() {
297
assert(_markup_state == HEAD, "misplaced end_head");
299
_markup_state = BODY;
303
// ------------------------------------------------------------------
304
// Outputs formatted text, followed by ">".
305
void xmlStream::end_head(const char* format, ...) {
307
va_start(ap, format);
308
out()->vprint(format, ap);
314
// ------------------------------------------------------------------
316
void xmlStream::tail(const char* kind) {
323
// ------------------------------------------------------------------
324
// Outputs "<kind_done ... stamp='D.DD'/> </kind>".
325
void xmlStream::done(const char* format, ...) {
327
va_start(ap, format);
332
// ------------------------------------------------------------------
333
// Outputs "<kind_done stamp='D.DD'/> </kind>".
334
// Because done_raw() doesn't need to format strings, it's simpler than
335
// done(), and can be called safely by fatal error handler.
336
void xmlStream::done_raw(const char* kind) {
339
print_raw("_done stamp='");
347
// If you remove the PRAGMA, this fails to compile with clang-503.0.40.
349
PRAGMA_FORMAT_NONLITERAL_IGNORED
350
// ------------------------------------------------------------------
351
void xmlStream::va_done(const char* format, va_list ap) {
353
size_t format_len = strlen(format);
354
guarantee(format_len + 10 < sizeof(buffer), "bigger format buffer");
355
const char* kind = format;
356
const char* kind_end = strchr(kind, ' ');
358
if (kind_end != nullptr) {
359
kind_len = kind_end - kind;
360
int n = os::snprintf(buffer, sizeof(buffer), "%.*s_done%s", (int)kind_len, kind, kind + kind_len);
361
assert((size_t)n < sizeof(buffer), "Unexpected number of characters in string");
363
kind_len = format_len;
364
int n = os::snprintf(buffer, sizeof(buffer), "%s_done", kind);
365
assert((size_t)n < sizeof(buffer), "Unexpected number of characters in string");
367
// Output the trailing event with the timestamp.
368
va_begin_elem(buffer, ap);
371
// Output the tail-tag of the enclosing element.
372
buffer[kind_len] = 0;
377
// Output a timestamp attribute.
378
void xmlStream::stamp() {
379
assert_if_no_error(inside_attrs(), "stamp must be an attribute");
380
print_raw(" stamp='");
386
// ------------------------------------------------------------------
387
// Output a method attribute, in the form " method='pkg/cls name sig'".
388
// This is used only when there is no ciMethod available.
389
void xmlStream::method(Method* method) {
390
assert_if_no_error(inside_attrs(), "printing attributes");
391
if (method == nullptr) return;
392
print_raw(" method='");
394
print("' bytes='%d'", method->code_size());
395
print(" count='%d'", method->invocation_count());
396
int bec = method->backedge_count();
397
if (bec != 0) print(" backedge_count='%d'", bec);
398
print(" iicount='%d'", method->interpreter_invocation_count());
399
int throwouts = method->interpreter_throwout_count();
400
if (throwouts != 0) print(" throwouts='%d'", throwouts);
401
MethodData* mdo = method->method_data();
402
if (mdo != nullptr) {
404
cnt = mdo->decompile_count();
405
if (cnt != 0) print(" decompiles='%d'", cnt);
406
for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
407
cnt = mdo->trap_count(reason);
408
if (cnt != 0) print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt);
410
cnt = mdo->overflow_trap_count();
411
if (cnt != 0) print(" overflow_traps='%d'", cnt);
412
cnt = mdo->overflow_recompile_count();
413
if (cnt != 0) print(" overflow_recompiles='%d'", cnt);
417
void xmlStream::method_text(Method* method) {
419
assert_if_no_error(inside_attrs(), "printing attributes");
420
if (method == nullptr) return;
421
text()->print("%s", method->method_holder()->external_name());
422
print_raw(" "); // " " is easier for tools to parse than "::"
423
method->name()->print_symbol_on(text());
424
print_raw(" "); // separator
425
method->signature()->print_symbol_on(text());
429
// ------------------------------------------------------------------
430
// Output a klass attribute, in the form " klass='pkg/cls'".
431
// This is used only when there is no ciKlass available.
432
void xmlStream::klass(Klass* klass) {
433
assert_if_no_error(inside_attrs(), "printing attributes");
434
if (klass == nullptr) return;
435
print_raw(" klass='");
440
void xmlStream::klass_text(Klass* klass) {
441
assert_if_no_error(inside_attrs(), "printing attributes");
442
if (klass == nullptr) return;
443
//klass->print_short_name(log->out());
444
klass->name()->print_symbol_on(out());
447
void xmlStream::name(const Symbol* name) {
448
assert_if_no_error(inside_attrs(), "printing attributes");
449
if (name == nullptr) return;
450
print_raw(" name='");
455
void xmlStream::name_text(const Symbol* name) {
456
assert_if_no_error(inside_attrs(), "printing attributes");
457
if (name == nullptr) return;
458
//name->print_short_name(text());
459
name->print_symbol_on(text());
462
void xmlStream::object(const char* attr, Handle x) {
463
assert_if_no_error(inside_attrs(), "printing attributes");
464
if (x == nullptr) return;
472
void xmlStream::object_text(Handle x) {
473
assert_if_no_error(inside_attrs(), "printing attributes");
474
if (x == nullptr) return;
475
x->print_value_on(text());
479
void xmlStream::object(const char* attr, Metadata* x) {
480
assert_if_no_error(inside_attrs(), "printing attributes");
481
if (x == nullptr) return;
489
void xmlStream::object_text(Metadata* x) {
490
assert_if_no_error(inside_attrs(), "printing attributes");
491
if (x == nullptr) return;
492
//x->print_value_on(text());
494
method_text((Method*)x);
495
else if (x->is_klass())
496
klass_text((Klass*)x);
498
ShouldNotReachHere(); // Add impl if this is reached.
502
void xmlStream::flush() {
504
_last_flush = count();
507
void xmlTextStream::flush() {
508
if (_outer_xmlStream == nullptr) return;
509
_outer_xmlStream->flush();
512
void xmlTextStream::write(const char* str, size_t len) {
513
if (_outer_xmlStream == nullptr) return;
514
_outer_xmlStream->write_text(str, len);
515
update_position(str, len);