src/share/vm/utilities/xmlstream.cpp

Tue, 19 Aug 2014 08:34:25 -0400

author
zgu
date
Tue, 19 Aug 2014 08:34:25 -0400
changeset 7078
c6211b707068
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
permissions
-rw-r--r--

8055007: NMT2: emptyStack missing in minimal build
Summary: Refactored emptyStack to a static member of NativeCallStack, which is accessible in minimal build.
Reviewed-by: coleenp, dholmes

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

mercurial