duke@435: /* stefank@2314: * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "code/nmethod.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "oops/methodDataOop.hpp" stefank@2314: #include "oops/methodOop.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/deoptimization.hpp" stefank@2314: #include "runtime/vmThread.hpp" stefank@2314: #include "utilities/xmlstream.hpp" duke@435: duke@435: void xmlStream::initialize(outputStream* out) { duke@435: _out = out; duke@435: _last_flush = 0; duke@435: _markup_state = BODY; duke@435: _text_init._outer_xmlStream = this; duke@435: _text = &_text_init; duke@435: duke@435: #ifdef ASSERT duke@435: _element_depth = 0; duke@435: int init_len = 100; duke@435: char* init_buf = NEW_C_HEAP_ARRAY(char, init_len); duke@435: _element_close_stack_low = init_buf; duke@435: _element_close_stack_high = init_buf + init_len; duke@435: _element_close_stack_ptr = init_buf + init_len - 1; duke@435: _element_close_stack_ptr[0] = '\0'; duke@435: #endif duke@435: duke@435: // Make sure each log uses the same base for time stamps. duke@435: if (is_open()) { duke@435: _out->time_stamp().update_to(1); duke@435: } duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: xmlStream::~xmlStream() { duke@435: FREE_C_HEAP_ARRAY(char, _element_close_stack_low); duke@435: } duke@435: #endif duke@435: duke@435: // Pass the given chars directly to _out. duke@435: void xmlStream::write(const char* s, size_t len) { duke@435: if (!is_open()) return; duke@435: duke@435: out()->write(s, len); never@657: update_position(s, len); duke@435: } duke@435: duke@435: duke@435: // Pass the given chars directly to _out, except that duke@435: // we watch for special "<&>" chars. duke@435: // This is suitable for either attribute text or for body text. duke@435: // We don't fool with "" chars duke@435: for (size_t i = 0; i < len; i++) { duke@435: char ch = s[i]; duke@435: // Escape special chars. duke@435: const char* esc = NULL; duke@435: switch (ch) { duke@435: // These are important only in attrs, but we do them always: duke@435: case '\'': esc = "'"; break; duke@435: case '"': esc = """; break; duke@435: case '<': esc = "<"; break; duke@435: case '&': esc = "&"; break; duke@435: // This is a freebie. duke@435: case '>': esc = ">"; break; duke@435: } duke@435: if (esc != NULL) { duke@435: if (written < i) { duke@435: out()->write(&s[written], i - written); duke@435: written = i; duke@435: } duke@435: out()->print_raw(esc); duke@435: written++; duke@435: } duke@435: } duke@435: duke@435: // Print the clean remainder. Usually, it is all of s. duke@435: if (written < len) { duke@435: out()->write(&s[written], len - written); duke@435: } duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs XML text, with special characters quoted. duke@435: void xmlStream::text(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_text(format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: #define BUFLEN 2*K /* max size of output of individual print methods */ duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_tag(bool push, const char* format, va_list ap) { duke@435: assert_if_no_error(!inside_attrs(), "cannot print tag inside attrs"); duke@435: char buffer[BUFLEN]; duke@435: size_t len; duke@435: const char* kind = do_vsnprintf(buffer, BUFLEN, format, ap, false, len); duke@435: see_tag(kind, push); duke@435: print_raw("<"); duke@435: write(kind, len); duke@435: _markup_state = (push ? HEAD : ELEM); duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: /// Debugging goo to make sure element tags nest properly. duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::see_tag(const char* tag, bool push) { duke@435: assert_if_no_error(!inside_attrs(), "cannot start new element inside attrs"); duke@435: if (!push) return; duke@435: duke@435: // tag goes up until either null or space: duke@435: const char* tag_end = strchr(tag, ' '); duke@435: size_t tag_len = (tag_end == NULL) ? strlen(tag) : tag_end - tag; duke@435: assert(tag_len > 0, "tag must not be empty"); duke@435: // push the tag onto the stack, pulling down the pointer duke@435: char* old_ptr = _element_close_stack_ptr; duke@435: char* old_low = _element_close_stack_low; duke@435: char* push_ptr = old_ptr - (tag_len+1); duke@435: if (push_ptr < old_low) { duke@435: int old_len = _element_close_stack_high - old_ptr; duke@435: int new_len = old_len * 2; duke@435: if (new_len < 100) new_len = 100; duke@435: char* new_low = NEW_C_HEAP_ARRAY(char, new_len); duke@435: char* new_high = new_low + new_len; duke@435: char* new_ptr = new_high - old_len; duke@435: memcpy(new_ptr, old_ptr, old_len); duke@435: _element_close_stack_high = new_high; duke@435: _element_close_stack_low = new_low; duke@435: _element_close_stack_ptr = new_ptr; duke@435: FREE_C_HEAP_ARRAY(char, old_low); duke@435: push_ptr = new_ptr - (tag_len+1); duke@435: } duke@435: assert(push_ptr >= _element_close_stack_low, "in range"); duke@435: memcpy(push_ptr, tag, tag_len); duke@435: push_ptr[tag_len] = 0; duke@435: _element_close_stack_ptr = push_ptr; duke@435: _element_depth += 1; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::pop_tag(const char* tag) { duke@435: assert_if_no_error(!inside_attrs(), "cannot close element inside attrs"); duke@435: assert(_element_depth > 0, "must be in an element to close"); duke@435: assert(*tag != 0, "tag must not be empty"); duke@435: char* cur_tag = _element_close_stack_ptr; duke@435: bool bad_tag = false; duke@435: while (*cur_tag != 0 && strcmp(cur_tag, tag) != 0) { duke@435: this->print_cr(" ", cur_tag); duke@435: _element_close_stack_ptr = (cur_tag += strlen(cur_tag) + 1); duke@435: _element_depth -= 1; duke@435: bad_tag = true; duke@435: } duke@435: if (*cur_tag == 0) { duke@435: bad_tag = true; duke@435: } else { duke@435: // Pop the stack, by skipping over the tag and its null. duke@435: _element_close_stack_ptr = cur_tag + strlen(cur_tag) + 1; duke@435: _element_depth -= 1; duke@435: } duke@435: if (bad_tag && !VMThread::should_terminate() && !is_error_reported()) duke@435: assert(false, "bad tag in log"); duke@435: } duke@435: #endif duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // First word in formatted string is element kind, and any subsequent duke@435: // words must be XML attributes. Outputs "". duke@435: void xmlStream::elem(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_elem(format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_elem(const char* format, va_list ap) { duke@435: va_begin_elem(format, ap); duke@435: end_elem(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // First word in formatted string is element kind, and any subsequent duke@435: // words must be XML attributes. Outputs "". duke@435: void xmlStream::begin_elem(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_tag(false, format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_begin_elem(const char* format, va_list ap) { duke@435: va_tag(false, format, ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs "/>". duke@435: void xmlStream::end_elem() { duke@435: assert(_markup_state == ELEM, "misplaced end_elem"); duke@435: print_raw("/>\n"); duke@435: _markup_state = BODY; duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs formatted text, followed by "/>". duke@435: void xmlStream::end_elem(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: out()->vprint(format, ap); duke@435: va_end(ap); duke@435: end_elem(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // First word in formatted string is element kind, and any subsequent duke@435: // words must be XML attributes. Outputs "". duke@435: void xmlStream::head(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_head(format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_head(const char* format, va_list ap) { duke@435: va_begin_head(format, ap); duke@435: end_head(); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // First word in formatted string is element kind, and any subsequent duke@435: // words must be XML attributes. Outputs "". duke@435: void xmlStream::begin_head(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_tag(true, format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_begin_head(const char* format, va_list ap) { duke@435: va_tag(true, format, ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs ">". duke@435: void xmlStream::end_head() { duke@435: assert(_markup_state == HEAD, "misplaced end_head"); duke@435: print_raw(">\n"); duke@435: _markup_state = BODY; duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs formatted text, followed by ">". duke@435: void xmlStream::end_head(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: out()->vprint(format, ap); duke@435: va_end(ap); duke@435: end_head(); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs "". duke@435: void xmlStream::tail(const char* kind) { duke@435: pop_tag(kind); duke@435: print_raw("\n"); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs " ". duke@435: void xmlStream::done(const char* format, ...) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: va_done(format, ap); duke@435: va_end(ap); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Outputs " ". duke@435: // Because done_raw() doesn't need to format strings, it's simpler than duke@435: // done(), and can be called safely by fatal error handler. duke@435: void xmlStream::done_raw(const char* kind) { duke@435: print_raw("<"); duke@435: print_raw(kind); duke@435: print_raw("_done stamp='"); duke@435: out()->stamp(); duke@435: print_raw_cr("'/>"); duke@435: print_raw(""); duke@435: } duke@435: duke@435: // ------------------------------------------------------------------ duke@435: void xmlStream::va_done(const char* format, va_list ap) { duke@435: char buffer[200]; jcoomes@1844: guarantee(strlen(format) + 10 < sizeof(buffer), "bigger format buffer"); duke@435: const char* kind = format; duke@435: const char* kind_end = strchr(kind, ' '); duke@435: size_t kind_len = (kind_end != NULL) ? (kind_end - kind) : strlen(kind); duke@435: strncpy(buffer, kind, kind_len); duke@435: strcpy(buffer + kind_len, "_done"); duke@435: strcat(buffer, format + kind_len); duke@435: // Output the trailing event with the timestamp. duke@435: va_begin_elem(buffer, ap); duke@435: stamp(); duke@435: end_elem(); duke@435: // Output the tail-tag of the enclosing element. duke@435: buffer[kind_len] = 0; duke@435: tail(buffer); duke@435: } duke@435: duke@435: // Output a timestamp attribute. duke@435: void xmlStream::stamp() { duke@435: assert_if_no_error(inside_attrs(), "stamp must be an attribute"); duke@435: print_raw(" stamp='"); duke@435: out()->stamp(); duke@435: print_raw("'"); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Output a method attribute, in the form " method='pkg/cls name sig'". duke@435: // This is used only when there is no ciMethod available. duke@435: void xmlStream::method(methodHandle method) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (method.is_null()) return; duke@435: print_raw(" method='"); duke@435: method_text(method); duke@435: print("' bytes='%d'", method->code_size()); duke@435: print(" count='%d'", method->invocation_count()); duke@435: int bec = method->backedge_count(); duke@435: if (bec != 0) print(" backedge_count='%d'", bec); duke@435: print(" iicount='%d'", method->interpreter_invocation_count()); duke@435: int throwouts = method->interpreter_throwout_count(); duke@435: if (throwouts != 0) print(" throwouts='%d'", throwouts); duke@435: methodDataOop mdo = method->method_data(); duke@435: if (mdo != NULL) { duke@435: uint cnt; duke@435: cnt = mdo->decompile_count(); duke@435: if (cnt != 0) print(" decompiles='%d'", cnt); duke@435: for (uint reason = 0; reason < mdo->trap_reason_limit(); reason++) { duke@435: cnt = mdo->trap_count(reason); duke@435: if (cnt != 0) print(" %s_traps='%d'", Deoptimization::trap_reason_name(reason), cnt); duke@435: } duke@435: cnt = mdo->overflow_trap_count(); duke@435: if (cnt != 0) print(" overflow_traps='%d'", cnt); duke@435: cnt = mdo->overflow_recompile_count(); duke@435: if (cnt != 0) print(" overflow_recompiles='%d'", cnt); duke@435: } duke@435: } duke@435: duke@435: void xmlStream::method_text(methodHandle method) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (method.is_null()) return; duke@435: //method->print_short_name(text()); duke@435: method->method_holder()->klass_part()->name()->print_symbol_on(text()); duke@435: print_raw(" "); // " " is easier for tools to parse than "::" duke@435: method->name()->print_symbol_on(text()); duke@435: print_raw(" "); // separator duke@435: method->signature()->print_symbol_on(text()); duke@435: } duke@435: duke@435: duke@435: // ------------------------------------------------------------------ duke@435: // Output a klass attribute, in the form " klass='pkg/cls'". duke@435: // This is used only when there is no ciKlass available. duke@435: void xmlStream::klass(KlassHandle klass) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (klass.is_null()) return; duke@435: print_raw(" klass='"); duke@435: klass_text(klass); duke@435: print_raw("'"); duke@435: } duke@435: duke@435: void xmlStream::klass_text(KlassHandle klass) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (klass.is_null()) return; duke@435: //klass->print_short_name(log->out()); duke@435: klass->name()->print_symbol_on(out()); duke@435: } duke@435: coleenp@2497: void xmlStream::name(const Symbol* name) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); coleenp@2497: if (name == NULL) return; duke@435: print_raw(" name='"); duke@435: name_text(name); duke@435: print_raw("'"); duke@435: } duke@435: coleenp@2497: void xmlStream::name_text(const Symbol* name) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); coleenp@2497: if (name == NULL) return; duke@435: //name->print_short_name(text()); duke@435: name->print_symbol_on(text()); duke@435: } duke@435: duke@435: void xmlStream::object(const char* attr, Handle x) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (x.is_null()) return; duke@435: print_raw(" "); duke@435: print_raw(attr); duke@435: print_raw("='"); duke@435: object_text(x); duke@435: print_raw("'"); duke@435: } duke@435: duke@435: void xmlStream::object_text(Handle x) { duke@435: assert_if_no_error(inside_attrs(), "printing attributes"); duke@435: if (x.is_null()) return; duke@435: //x->print_value_on(text()); duke@435: if (x->is_method()) duke@435: method_text(methodOop(x())); duke@435: else if (x->is_klass()) duke@435: klass_text(klassOop(x())); duke@435: else duke@435: x->print_value_on(text()); duke@435: } duke@435: duke@435: duke@435: void xmlStream::flush() { duke@435: out()->flush(); duke@435: _last_flush = count(); duke@435: } duke@435: duke@435: void xmlTextStream::flush() { duke@435: if (_outer_xmlStream == NULL) return; duke@435: _outer_xmlStream->flush(); duke@435: } duke@435: duke@435: void xmlTextStream::write(const char* str, size_t len) { duke@435: if (_outer_xmlStream == NULL) return; duke@435: _outer_xmlStream->write_text(str, len); duke@435: update_position(str, len); duke@435: }