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