src/share/vm/utilities/xmlstream.cpp

Thu, 27 Jan 2011 16:11:27 -0800

author
coleenp
date
Thu, 27 Jan 2011 16:11:27 -0800
changeset 2497
3582bf76420e
parent 2314
f95d63e2154a
child 2708
1d1603768966
permissions
-rw-r--r--

6990754: Use native memory and reference counting to implement SymbolTable
Summary: move symbols from permgen into C heap and reference count them
Reviewed-by: never, acorn, jmasa, stefank

duke@435 1 /*
stefank@2314 2 * Copyright (c) 2002, 2010, 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"
stefank@2314 29 #include "oops/methodDataOop.hpp"
stefank@2314 30 #include "oops/methodOop.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;
duke@435 46 char* init_buf = NEW_C_HEAP_ARRAY(char, init_len);
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() {
duke@435 61 FREE_C_HEAP_ARRAY(char, _element_close_stack_low);
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;
duke@435 158 char* new_low = NEW_C_HEAP_ARRAY(char, new_len);
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;
duke@435 165 FREE_C_HEAP_ARRAY(char, old_low);
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 }
duke@435 195 if (bad_tag && !VMThread::should_terminate() && !is_error_reported())
duke@435 196 assert(false, "bad tag in log");
duke@435 197 }
duke@435 198 #endif
duke@435 199
duke@435 200
duke@435 201 // ------------------------------------------------------------------
duke@435 202 // First word in formatted string is element kind, and any subsequent
duke@435 203 // words must be XML attributes. Outputs "<kind .../>".
duke@435 204 void xmlStream::elem(const char* format, ...) {
duke@435 205 va_list ap;
duke@435 206 va_start(ap, format);
duke@435 207 va_elem(format, ap);
duke@435 208 va_end(ap);
duke@435 209 }
duke@435 210
duke@435 211 // ------------------------------------------------------------------
duke@435 212 void xmlStream::va_elem(const char* format, va_list ap) {
duke@435 213 va_begin_elem(format, ap);
duke@435 214 end_elem();
duke@435 215 }
duke@435 216
duke@435 217
duke@435 218 // ------------------------------------------------------------------
duke@435 219 // First word in formatted string is element kind, and any subsequent
duke@435 220 // words must be XML attributes. Outputs "<kind ...", not including "/>".
duke@435 221 void xmlStream::begin_elem(const char* format, ...) {
duke@435 222 va_list ap;
duke@435 223 va_start(ap, format);
duke@435 224 va_tag(false, format, ap);
duke@435 225 va_end(ap);
duke@435 226 }
duke@435 227
duke@435 228 // ------------------------------------------------------------------
duke@435 229 void xmlStream::va_begin_elem(const char* format, va_list ap) {
duke@435 230 va_tag(false, format, ap);
duke@435 231 }
duke@435 232
duke@435 233 // ------------------------------------------------------------------
duke@435 234 // Outputs "/>".
duke@435 235 void xmlStream::end_elem() {
duke@435 236 assert(_markup_state == ELEM, "misplaced end_elem");
duke@435 237 print_raw("/>\n");
duke@435 238 _markup_state = BODY;
duke@435 239 }
duke@435 240
duke@435 241 // ------------------------------------------------------------------
duke@435 242 // Outputs formatted text, followed by "/>".
duke@435 243 void xmlStream::end_elem(const char* format, ...) {
duke@435 244 va_list ap;
duke@435 245 va_start(ap, format);
duke@435 246 out()->vprint(format, ap);
duke@435 247 va_end(ap);
duke@435 248 end_elem();
duke@435 249 }
duke@435 250
duke@435 251
duke@435 252 // ------------------------------------------------------------------
duke@435 253 // First word in formatted string is element kind, and any subsequent
duke@435 254 // words must be XML attributes. Outputs "<kind ...>".
duke@435 255 void xmlStream::head(const char* format, ...) {
duke@435 256 va_list ap;
duke@435 257 va_start(ap, format);
duke@435 258 va_head(format, ap);
duke@435 259 va_end(ap);
duke@435 260 }
duke@435 261
duke@435 262 // ------------------------------------------------------------------
duke@435 263 void xmlStream::va_head(const char* format, va_list ap) {
duke@435 264 va_begin_head(format, ap);
duke@435 265 end_head();
duke@435 266 }
duke@435 267
duke@435 268 // ------------------------------------------------------------------
duke@435 269 // First word in formatted string is element kind, and any subsequent
duke@435 270 // words must be XML attributes. Outputs "<kind ...", not including ">".
duke@435 271 void xmlStream::begin_head(const char* format, ...) {
duke@435 272 va_list ap;
duke@435 273 va_start(ap, format);
duke@435 274 va_tag(true, format, ap);
duke@435 275 va_end(ap);
duke@435 276 }
duke@435 277
duke@435 278 // ------------------------------------------------------------------
duke@435 279 void xmlStream::va_begin_head(const char* format, va_list ap) {
duke@435 280 va_tag(true, format, ap);
duke@435 281 }
duke@435 282
duke@435 283 // ------------------------------------------------------------------
duke@435 284 // Outputs ">".
duke@435 285 void xmlStream::end_head() {
duke@435 286 assert(_markup_state == HEAD, "misplaced end_head");
duke@435 287 print_raw(">\n");
duke@435 288 _markup_state = BODY;
duke@435 289 }
duke@435 290
duke@435 291
duke@435 292 // ------------------------------------------------------------------
duke@435 293 // Outputs formatted text, followed by ">".
duke@435 294 void xmlStream::end_head(const char* format, ...) {
duke@435 295 va_list ap;
duke@435 296 va_start(ap, format);
duke@435 297 out()->vprint(format, ap);
duke@435 298 va_end(ap);
duke@435 299 end_head();
duke@435 300 }
duke@435 301
duke@435 302
duke@435 303 // ------------------------------------------------------------------
duke@435 304 // Outputs "</kind>".
duke@435 305 void xmlStream::tail(const char* kind) {
duke@435 306 pop_tag(kind);
duke@435 307 print_raw("</");
duke@435 308 print_raw(kind);
duke@435 309 print_raw(">\n");
duke@435 310 }
duke@435 311
duke@435 312 // ------------------------------------------------------------------
duke@435 313 // Outputs "<kind_done ... stamp='D.DD'/> </kind>".
duke@435 314 void xmlStream::done(const char* format, ...) {
duke@435 315 va_list ap;
duke@435 316 va_start(ap, format);
duke@435 317 va_done(format, ap);
duke@435 318 va_end(ap);
duke@435 319 }
duke@435 320
duke@435 321 // ------------------------------------------------------------------
duke@435 322 // Outputs "<kind_done stamp='D.DD'/> </kind>".
duke@435 323 // Because done_raw() doesn't need to format strings, it's simpler than
duke@435 324 // done(), and can be called safely by fatal error handler.
duke@435 325 void xmlStream::done_raw(const char* kind) {
duke@435 326 print_raw("<");
duke@435 327 print_raw(kind);
duke@435 328 print_raw("_done stamp='");
duke@435 329 out()->stamp();
duke@435 330 print_raw_cr("'/>");
duke@435 331 print_raw("</");
duke@435 332 print_raw(kind);
duke@435 333 print_raw_cr(">");
duke@435 334 }
duke@435 335
duke@435 336 // ------------------------------------------------------------------
duke@435 337 void xmlStream::va_done(const char* format, va_list ap) {
duke@435 338 char buffer[200];
jcoomes@1844 339 guarantee(strlen(format) + 10 < sizeof(buffer), "bigger format buffer");
duke@435 340 const char* kind = format;
duke@435 341 const char* kind_end = strchr(kind, ' ');
duke@435 342 size_t kind_len = (kind_end != NULL) ? (kind_end - kind) : strlen(kind);
duke@435 343 strncpy(buffer, kind, kind_len);
duke@435 344 strcpy(buffer + kind_len, "_done");
duke@435 345 strcat(buffer, format + kind_len);
duke@435 346 // Output the trailing event with the timestamp.
duke@435 347 va_begin_elem(buffer, ap);
duke@435 348 stamp();
duke@435 349 end_elem();
duke@435 350 // Output the tail-tag of the enclosing element.
duke@435 351 buffer[kind_len] = 0;
duke@435 352 tail(buffer);
duke@435 353 }
duke@435 354
duke@435 355 // Output a timestamp attribute.
duke@435 356 void xmlStream::stamp() {
duke@435 357 assert_if_no_error(inside_attrs(), "stamp must be an attribute");
duke@435 358 print_raw(" stamp='");
duke@435 359 out()->stamp();
duke@435 360 print_raw("'");
duke@435 361 }
duke@435 362
duke@435 363
duke@435 364 // ------------------------------------------------------------------
duke@435 365 // Output a method attribute, in the form " method='pkg/cls name sig'".
duke@435 366 // This is used only when there is no ciMethod available.
duke@435 367 void xmlStream::method(methodHandle method) {
duke@435 368 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 369 if (method.is_null()) return;
duke@435 370 print_raw(" method='");
duke@435 371 method_text(method);
duke@435 372 print("' bytes='%d'", method->code_size());
duke@435 373 print(" count='%d'", method->invocation_count());
duke@435 374 int bec = method->backedge_count();
duke@435 375 if (bec != 0) print(" backedge_count='%d'", bec);
duke@435 376 print(" iicount='%d'", method->interpreter_invocation_count());
duke@435 377 int throwouts = method->interpreter_throwout_count();
duke@435 378 if (throwouts != 0) print(" throwouts='%d'", throwouts);
duke@435 379 methodDataOop mdo = method->method_data();
duke@435 380 if (mdo != NULL) {
duke@435 381 uint cnt;
duke@435 382 cnt = mdo->decompile_count();
duke@435 383 if (cnt != 0) print(" decompiles='%d'", cnt);
duke@435 384 for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) {
duke@435 385 cnt = mdo->trap_count(reason);
duke@435 386 if (cnt != 0) print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt);
duke@435 387 }
duke@435 388 cnt = mdo->overflow_trap_count();
duke@435 389 if (cnt != 0) print(" overflow_traps='%d'", cnt);
duke@435 390 cnt = mdo->overflow_recompile_count();
duke@435 391 if (cnt != 0) print(" overflow_recompiles='%d'", cnt);
duke@435 392 }
duke@435 393 }
duke@435 394
duke@435 395 void xmlStream::method_text(methodHandle method) {
duke@435 396 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 397 if (method.is_null()) return;
duke@435 398 //method->print_short_name(text());
duke@435 399 method->method_holder()->klass_part()->name()->print_symbol_on(text());
duke@435 400 print_raw(" "); // " " is easier for tools to parse than "::"
duke@435 401 method->name()->print_symbol_on(text());
duke@435 402 print_raw(" "); // separator
duke@435 403 method->signature()->print_symbol_on(text());
duke@435 404 }
duke@435 405
duke@435 406
duke@435 407 // ------------------------------------------------------------------
duke@435 408 // Output a klass attribute, in the form " klass='pkg/cls'".
duke@435 409 // This is used only when there is no ciKlass available.
duke@435 410 void xmlStream::klass(KlassHandle klass) {
duke@435 411 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 412 if (klass.is_null()) return;
duke@435 413 print_raw(" klass='");
duke@435 414 klass_text(klass);
duke@435 415 print_raw("'");
duke@435 416 }
duke@435 417
duke@435 418 void xmlStream::klass_text(KlassHandle klass) {
duke@435 419 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 420 if (klass.is_null()) return;
duke@435 421 //klass->print_short_name(log->out());
duke@435 422 klass->name()->print_symbol_on(out());
duke@435 423 }
duke@435 424
coleenp@2497 425 void xmlStream::name(const Symbol* name) {
duke@435 426 assert_if_no_error(inside_attrs(), "printing attributes");
coleenp@2497 427 if (name == NULL) return;
duke@435 428 print_raw(" name='");
duke@435 429 name_text(name);
duke@435 430 print_raw("'");
duke@435 431 }
duke@435 432
coleenp@2497 433 void xmlStream::name_text(const Symbol* name) {
duke@435 434 assert_if_no_error(inside_attrs(), "printing attributes");
coleenp@2497 435 if (name == NULL) return;
duke@435 436 //name->print_short_name(text());
duke@435 437 name->print_symbol_on(text());
duke@435 438 }
duke@435 439
duke@435 440 void xmlStream::object(const char* attr, Handle x) {
duke@435 441 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 442 if (x.is_null()) return;
duke@435 443 print_raw(" ");
duke@435 444 print_raw(attr);
duke@435 445 print_raw("='");
duke@435 446 object_text(x);
duke@435 447 print_raw("'");
duke@435 448 }
duke@435 449
duke@435 450 void xmlStream::object_text(Handle x) {
duke@435 451 assert_if_no_error(inside_attrs(), "printing attributes");
duke@435 452 if (x.is_null()) return;
duke@435 453 //x->print_value_on(text());
duke@435 454 if (x->is_method())
duke@435 455 method_text(methodOop(x()));
duke@435 456 else if (x->is_klass())
duke@435 457 klass_text(klassOop(x()));
duke@435 458 else
duke@435 459 x->print_value_on(text());
duke@435 460 }
duke@435 461
duke@435 462
duke@435 463 void xmlStream::flush() {
duke@435 464 out()->flush();
duke@435 465 _last_flush = count();
duke@435 466 }
duke@435 467
duke@435 468 void xmlTextStream::flush() {
duke@435 469 if (_outer_xmlStream == NULL) return;
duke@435 470 _outer_xmlStream->flush();
duke@435 471 }
duke@435 472
duke@435 473 void xmlTextStream::write(const char* str, size_t len) {
duke@435 474 if (_outer_xmlStream == NULL) return;
duke@435 475 _outer_xmlStream->write_text(str, len);
duke@435 476 update_position(str, len);
duke@435 477 }

mercurial