xmlstream.cpp revision 12953:d342027d6321
1/* 2 * Copyright (c) 2002, 2017, 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 "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.hpp" 32#include "oops/oop.inline.hpp" 33#include "runtime/deoptimization.hpp" 34#include "runtime/vmThread.hpp" 35#include "utilities/xmlstream.hpp" 36 37// Do not assert this condition if there's already another error reported. 38#define assert_if_no_error(cond, msg) \ 39 vmassert((cond) || is_error_reported(), msg) 40 41void xmlStream::initialize(outputStream* out) { 42 _out = out; 43 _last_flush = 0; 44 _markup_state = BODY; 45 _text_init._outer_xmlStream = this; 46 _text = &_text_init; 47 48#ifdef ASSERT 49 _element_depth = 0; 50 int init_len = 100; 51 char* init_buf = NEW_C_HEAP_ARRAY(char, init_len, mtInternal); 52 _element_close_stack_low = init_buf; 53 _element_close_stack_high = init_buf + init_len; 54 _element_close_stack_ptr = init_buf + init_len - 1; 55 _element_close_stack_ptr[0] = '\0'; 56#endif 57 58 // Make sure each log uses the same base for time stamps. 59 if (is_open()) { 60 _out->time_stamp().update_to(1); 61 } 62} 63 64#ifdef ASSERT 65xmlStream::~xmlStream() { 66 FREE_C_HEAP_ARRAY(char, _element_close_stack_low); 67} 68#endif 69 70// Pass the given chars directly to _out. 71void xmlStream::write(const char* s, size_t len) { 72 if (!is_open()) return; 73 74 out()->write(s, len); 75 update_position(s, len); 76} 77 78 79// Pass the given chars directly to _out, except that 80// we watch for special "<&>" chars. 81// This is suitable for either attribute text or for body text. 82// We don't fool with "<![CDATA[" quotes, just single-character entities. 83// This makes it easier for dumb tools to parse the output. 84void xmlStream::write_text(const char* s, size_t len) { 85 if (!is_open()) return; 86 87 size_t written = 0; 88 // All normally printed material goes inside XML quotes. 89 // This leaves the output free to include markup also. 90 // Scan the string looking for inadvertant "<&>" chars 91 for (size_t i = 0; i < len; i++) { 92 char ch = s[i]; 93 // Escape special chars. 94 const char* esc = NULL; 95 switch (ch) { 96 // These are important only in attrs, but we do them always: 97 case '\'': esc = "'"; break; 98 case '"': esc = """; break; 99 case '<': esc = "<"; break; 100 case '&': esc = "&"; break; 101 // This is a freebie. 102 case '>': esc = ">"; break; 103 } 104 if (esc != NULL) { 105 if (written < i) { 106 out()->write(&s[written], i - written); 107 written = i; 108 } 109 out()->print_raw(esc); 110 written++; 111 } 112 } 113 114 // Print the clean remainder. Usually, it is all of s. 115 if (written < len) { 116 out()->write(&s[written], len - written); 117 } 118} 119 120// ------------------------------------------------------------------ 121// Outputs XML text, with special characters quoted. 122void xmlStream::text(const char* format, ...) { 123 va_list ap; 124 va_start(ap, format); 125 va_text(format, ap); 126 va_end(ap); 127} 128 129#define BUFLEN 2*K /* max size of output of individual print methods */ 130 131// ------------------------------------------------------------------ 132void xmlStream::va_tag(bool push, const char* format, va_list ap) { 133 assert_if_no_error(!inside_attrs(), "cannot print tag inside attrs"); 134 char buffer[BUFLEN]; 135 size_t len; 136 const char* kind = do_vsnprintf(buffer, BUFLEN, format, ap, false, len); 137 see_tag(kind, push); 138 print_raw("<"); 139 write(kind, len); 140 _markup_state = (push ? HEAD : ELEM); 141} 142 143#ifdef ASSERT 144/// Debugging goo to make sure element tags nest properly. 145 146// ------------------------------------------------------------------ 147void xmlStream::see_tag(const char* tag, bool push) { 148 assert_if_no_error(!inside_attrs(), "cannot start new element inside attrs"); 149 if (!push) return; 150 151 // tag goes up until either null or space: 152 const char* tag_end = strchr(tag, ' '); 153 size_t tag_len = (tag_end == NULL) ? strlen(tag) : tag_end - tag; 154 assert(tag_len > 0, "tag must not be empty"); 155 // push the tag onto the stack, pulling down the pointer 156 char* old_ptr = _element_close_stack_ptr; 157 char* old_low = _element_close_stack_low; 158 char* push_ptr = old_ptr - (tag_len+1); 159 if (push_ptr < old_low) { 160 int old_len = _element_close_stack_high - old_ptr; 161 int new_len = old_len * 2; 162 if (new_len < 100) new_len = 100; 163 char* new_low = NEW_C_HEAP_ARRAY(char, new_len, mtInternal); 164 char* new_high = new_low + new_len; 165 char* new_ptr = new_high - old_len; 166 memcpy(new_ptr, old_ptr, old_len); 167 _element_close_stack_high = new_high; 168 _element_close_stack_low = new_low; 169 _element_close_stack_ptr = new_ptr; 170 FREE_C_HEAP_ARRAY(char, old_low); 171 push_ptr = new_ptr - (tag_len+1); 172 } 173 assert(push_ptr >= _element_close_stack_low, "in range"); 174 memcpy(push_ptr, tag, tag_len); 175 push_ptr[tag_len] = 0; 176 _element_close_stack_ptr = push_ptr; 177 _element_depth += 1; 178} 179 180// ------------------------------------------------------------------ 181void xmlStream::pop_tag(const char* tag) { 182 assert_if_no_error(!inside_attrs(), "cannot close element inside attrs"); 183 assert(_element_depth > 0, "must be in an element to close"); 184 assert(*tag != 0, "tag must not be empty"); 185 char* cur_tag = _element_close_stack_ptr; 186 bool bad_tag = false; 187 while (*cur_tag != 0 && strcmp(cur_tag, tag) != 0) { 188 this->print_cr("</%s> <!-- missing closing tag -->", cur_tag); 189 _element_close_stack_ptr = (cur_tag += strlen(cur_tag) + 1); 190 _element_depth -= 1; 191 bad_tag = true; 192 } 193 if (*cur_tag == 0) { 194 bad_tag = true; 195 } else { 196 // Pop the stack, by skipping over the tag and its null. 197 _element_close_stack_ptr = cur_tag + strlen(cur_tag) + 1; 198 _element_depth -= 1; 199 } 200 if (bad_tag && !VMThread::should_terminate() && !VM_Exit::vm_exited() && 201 !is_error_reported()) 202 { 203 assert(false, "bad tag in log"); 204 } 205} 206#endif 207 208 209// ------------------------------------------------------------------ 210// First word in formatted string is element kind, and any subsequent 211// words must be XML attributes. Outputs "<kind .../>". 212void xmlStream::elem(const char* format, ...) { 213 va_list ap; 214 va_start(ap, format); 215 va_elem(format, ap); 216 va_end(ap); 217} 218 219// ------------------------------------------------------------------ 220void xmlStream::va_elem(const char* format, va_list ap) { 221 va_begin_elem(format, ap); 222 end_elem(); 223} 224 225 226// ------------------------------------------------------------------ 227// First word in formatted string is element kind, and any subsequent 228// words must be XML attributes. Outputs "<kind ...", not including "/>". 229void xmlStream::begin_elem(const char* format, ...) { 230 va_list ap; 231 va_start(ap, format); 232 va_tag(false, format, ap); 233 va_end(ap); 234} 235 236// ------------------------------------------------------------------ 237void xmlStream::va_begin_elem(const char* format, va_list ap) { 238 va_tag(false, format, ap); 239} 240 241// ------------------------------------------------------------------ 242// Outputs "/>". 243void xmlStream::end_elem() { 244 assert(_markup_state == ELEM, "misplaced end_elem"); 245 print_raw("/>\n"); 246 _markup_state = BODY; 247} 248 249// ------------------------------------------------------------------ 250// Outputs formatted text, followed by "/>". 251void xmlStream::end_elem(const char* format, ...) { 252 va_list ap; 253 va_start(ap, format); 254 out()->vprint(format, ap); 255 va_end(ap); 256 end_elem(); 257} 258 259 260// ------------------------------------------------------------------ 261// First word in formatted string is element kind, and any subsequent 262// words must be XML attributes. Outputs "<kind ...>". 263void xmlStream::head(const char* format, ...) { 264 va_list ap; 265 va_start(ap, format); 266 va_head(format, ap); 267 va_end(ap); 268} 269 270// ------------------------------------------------------------------ 271void xmlStream::va_head(const char* format, va_list ap) { 272 va_begin_head(format, ap); 273 end_head(); 274} 275 276// ------------------------------------------------------------------ 277// First word in formatted string is element kind, and any subsequent 278// words must be XML attributes. Outputs "<kind ...", not including ">". 279void xmlStream::begin_head(const char* format, ...) { 280 va_list ap; 281 va_start(ap, format); 282 va_tag(true, format, ap); 283 va_end(ap); 284} 285 286// ------------------------------------------------------------------ 287void xmlStream::va_begin_head(const char* format, va_list ap) { 288 va_tag(true, format, ap); 289} 290 291// ------------------------------------------------------------------ 292// Outputs ">". 293void xmlStream::end_head() { 294 assert(_markup_state == HEAD, "misplaced end_head"); 295 print_raw(">\n"); 296 _markup_state = BODY; 297} 298 299 300// ------------------------------------------------------------------ 301// Outputs formatted text, followed by ">". 302void xmlStream::end_head(const char* format, ...) { 303 va_list ap; 304 va_start(ap, format); 305 out()->vprint(format, ap); 306 va_end(ap); 307 end_head(); 308} 309 310 311// ------------------------------------------------------------------ 312// Outputs "</kind>". 313void xmlStream::tail(const char* kind) { 314 pop_tag(kind); 315 print_raw("</"); 316 print_raw(kind); 317 print_raw(">\n"); 318} 319 320// ------------------------------------------------------------------ 321// Outputs "<kind_done ... stamp='D.DD'/> </kind>". 322void xmlStream::done(const char* format, ...) { 323 va_list ap; 324 va_start(ap, format); 325 va_done(format, ap); 326 va_end(ap); 327} 328 329// ------------------------------------------------------------------ 330// Outputs "<kind_done stamp='D.DD'/> </kind>". 331// Because done_raw() doesn't need to format strings, it's simpler than 332// done(), and can be called safely by fatal error handler. 333void xmlStream::done_raw(const char* kind) { 334 print_raw("<"); 335 print_raw(kind); 336 print_raw("_done stamp='"); 337 out()->stamp(); 338 print_raw_cr("'/>"); 339 print_raw("</"); 340 print_raw(kind); 341 print_raw_cr(">"); 342} 343 344// If you remove the PRAGMA, this fails to compile with clang-503.0.40. 345PRAGMA_DIAG_PUSH 346PRAGMA_FORMAT_NONLITERAL_IGNORED 347// ------------------------------------------------------------------ 348void xmlStream::va_done(const char* format, va_list ap) { 349 char buffer[200]; 350 size_t format_len = strlen(format); 351 guarantee(format_len + 10 < sizeof(buffer), "bigger format buffer"); 352 const char* kind = format; 353 const char* kind_end = strchr(kind, ' '); 354 size_t kind_len = (kind_end != NULL) ? (kind_end - kind) : format_len; 355 strncpy(buffer, kind, kind_len); 356 strcpy(buffer + kind_len, "_done"); 357 if (kind_end != NULL) { 358 strncat(buffer, format + kind_len, sizeof(buffer) - (kind_len + 5 /* _done */) - 1); 359 } 360 // Output the trailing event with the timestamp. 361 va_begin_elem(buffer, ap); 362 stamp(); 363 end_elem(); 364 // Output the tail-tag of the enclosing element. 365 buffer[kind_len] = 0; 366 tail(buffer); 367} 368PRAGMA_DIAG_POP 369 370// Output a timestamp attribute. 371void xmlStream::stamp() { 372 assert_if_no_error(inside_attrs(), "stamp must be an attribute"); 373 print_raw(" stamp='"); 374 out()->stamp(); 375 print_raw("'"); 376} 377 378 379// ------------------------------------------------------------------ 380// Output a method attribute, in the form " method='pkg/cls name sig'". 381// This is used only when there is no ciMethod available. 382void xmlStream::method(methodHandle method) { 383 assert_if_no_error(inside_attrs(), "printing attributes"); 384 if (method.is_null()) return; 385 print_raw(" method='"); 386 method_text(method); 387 print("' bytes='%d'", method->code_size()); 388 print(" count='%d'", method->invocation_count()); 389 int bec = method->backedge_count(); 390 if (bec != 0) print(" backedge_count='%d'", bec); 391 print(" iicount='%d'", method->interpreter_invocation_count()); 392 int throwouts = method->interpreter_throwout_count(); 393 if (throwouts != 0) print(" throwouts='%d'", throwouts); 394 MethodData* mdo = method->method_data(); 395 if (mdo != NULL) { 396 uint cnt; 397 cnt = mdo->decompile_count(); 398 if (cnt != 0) print(" decompiles='%d'", cnt); 399 for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) { 400 cnt = mdo->trap_count(reason); 401 if (cnt != 0) print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt); 402 } 403 cnt = mdo->overflow_trap_count(); 404 if (cnt != 0) print(" overflow_traps='%d'", cnt); 405 cnt = mdo->overflow_recompile_count(); 406 if (cnt != 0) print(" overflow_recompiles='%d'", cnt); 407 } 408} 409 410void xmlStream::method_text(methodHandle method) { 411 ResourceMark rm; 412 assert_if_no_error(inside_attrs(), "printing attributes"); 413 if (method.is_null()) return; 414 text()->print("%s", method->method_holder()->external_name()); 415 print_raw(" "); // " " is easier for tools to parse than "::" 416 method->name()->print_symbol_on(text()); 417 print_raw(" "); // separator 418 method->signature()->print_symbol_on(text()); 419} 420 421 422// ------------------------------------------------------------------ 423// Output a klass attribute, in the form " klass='pkg/cls'". 424// This is used only when there is no ciKlass available. 425void xmlStream::klass(Klass* klass) { 426 assert_if_no_error(inside_attrs(), "printing attributes"); 427 if (klass == NULL) return; 428 print_raw(" klass='"); 429 klass_text(klass); 430 print_raw("'"); 431} 432 433void xmlStream::klass_text(Klass* klass) { 434 assert_if_no_error(inside_attrs(), "printing attributes"); 435 if (klass == NULL) return; 436 //klass->print_short_name(log->out()); 437 klass->name()->print_symbol_on(out()); 438} 439 440void xmlStream::name(const Symbol* name) { 441 assert_if_no_error(inside_attrs(), "printing attributes"); 442 if (name == NULL) return; 443 print_raw(" name='"); 444 name_text(name); 445 print_raw("'"); 446} 447 448void xmlStream::name_text(const Symbol* name) { 449 assert_if_no_error(inside_attrs(), "printing attributes"); 450 if (name == NULL) return; 451 //name->print_short_name(text()); 452 name->print_symbol_on(text()); 453} 454 455void xmlStream::object(const char* attr, Handle x) { 456 assert_if_no_error(inside_attrs(), "printing attributes"); 457 if (x == NULL) return; 458 print_raw(" "); 459 print_raw(attr); 460 print_raw("='"); 461 object_text(x); 462 print_raw("'"); 463} 464 465void xmlStream::object_text(Handle x) { 466 assert_if_no_error(inside_attrs(), "printing attributes"); 467 if (x == NULL) return; 468 x->print_value_on(text()); 469} 470 471 472void xmlStream::object(const char* attr, Metadata* x) { 473 assert_if_no_error(inside_attrs(), "printing attributes"); 474 if (x == NULL) return; 475 print_raw(" "); 476 print_raw(attr); 477 print_raw("='"); 478 object_text(x); 479 print_raw("'"); 480} 481 482void xmlStream::object_text(Metadata* x) { 483 assert_if_no_error(inside_attrs(), "printing attributes"); 484 if (x == NULL) return; 485 //x->print_value_on(text()); 486 if (x->is_method()) 487 method_text((Method*)x); 488 else if (x->is_klass()) 489 klass_text((Klass*)x); 490 else 491 ShouldNotReachHere(); // Add impl if this is reached. 492} 493 494 495void xmlStream::flush() { 496 out()->flush(); 497 _last_flush = count(); 498} 499 500void xmlTextStream::flush() { 501 if (_outer_xmlStream == NULL) return; 502 _outer_xmlStream->flush(); 503} 504 505void xmlTextStream::write(const char* str, size_t len) { 506 if (_outer_xmlStream == NULL) return; 507 _outer_xmlStream->write_text(str, len); 508 update_position(str, len); 509} 510