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