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 "ci/ciMethod.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: aoqi@0: CompileLog* CompileLog::_first = NULL; aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::CompileLog aoqi@0: CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id) aoqi@0: : _context(_context_buffer, sizeof(_context_buffer)) aoqi@0: { aoqi@0: initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp, true)); aoqi@0: _file_end = 0; aoqi@0: _thread_id = thread_id; aoqi@0: aoqi@0: _identities_limit = 0; aoqi@0: _identities_capacity = 400; aoqi@0: _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler); aoqi@0: _file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler); aoqi@0: strcpy((char*)_file, file_name); aoqi@0: aoqi@0: // link into the global list aoqi@0: { MutexLocker locker(CompileTaskAlloc_lock); aoqi@0: _next = _first; aoqi@0: _first = this; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: CompileLog::~CompileLog() { aoqi@0: delete _out; aoqi@0: _out = NULL; aoqi@0: FREE_C_HEAP_ARRAY(char, _identities, mtCompiler); aoqi@0: FREE_C_HEAP_ARRAY(char, _file, mtCompiler); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // see_tag, pop_tag: Override the default do-nothing methods on xmlStream. aoqi@0: // These methods provide a hook for managing the the extra context markup. aoqi@0: void CompileLog::see_tag(const char* tag, bool push) { aoqi@0: if (_context.size() > 0 && _out != NULL) { aoqi@0: _out->write(_context.base(), _context.size()); aoqi@0: _context.reset(); aoqi@0: } aoqi@0: xmlStream::see_tag(tag, push); aoqi@0: } aoqi@0: void CompileLog::pop_tag(const char* tag) { aoqi@0: _context.reset(); // toss any context info. aoqi@0: xmlStream::pop_tag(tag); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::identify aoqi@0: int CompileLog::identify(ciBaseObject* obj) { aoqi@0: if (obj == NULL) return 0; aoqi@0: int id = obj->ident(); aoqi@0: if (id < 0) return id; aoqi@0: // If it has already been identified, just return the id. aoqi@0: if (id < _identities_limit && _identities[id] != 0) return id; aoqi@0: // Lengthen the array, if necessary. aoqi@0: if (id >= _identities_capacity) { aoqi@0: int new_cap = _identities_capacity * 2; aoqi@0: if (new_cap <= id) new_cap = id + 100; aoqi@0: _identities = REALLOC_C_HEAP_ARRAY(char, _identities, new_cap, mtCompiler); aoqi@0: _identities_capacity = new_cap; aoqi@0: } aoqi@0: while (id >= _identities_limit) { aoqi@0: _identities[_identities_limit++] = 0; aoqi@0: } aoqi@0: assert(id < _identities_limit, "oob"); aoqi@0: // Mark this id as processed. aoqi@0: // (Be sure to do this before any recursive calls to identify.) aoqi@0: _identities[id] = 1; // mark aoqi@0: aoqi@0: // Now, print the object's identity once, in detail. aoqi@0: if (obj->is_metadata()) { aoqi@0: ciMetadata* mobj = obj->as_metadata(); aoqi@0: if (mobj->is_klass()) { aoqi@0: ciKlass* klass = mobj->as_klass(); aoqi@0: begin_elem("klass id='%d'", id); aoqi@0: name(klass->name()); aoqi@0: if (!klass->is_loaded()) { aoqi@0: print(" unloaded='1'"); aoqi@0: } else { aoqi@0: print(" flags='%d'", klass->modifier_flags()); aoqi@0: } aoqi@0: end_elem(); aoqi@0: } else if (mobj->is_method()) { aoqi@0: ciMethod* method = mobj->as_method(); aoqi@0: ciSignature* sig = method->signature(); aoqi@0: // Pre-identify items that we will need! aoqi@0: identify(sig->return_type()); aoqi@0: for (int i = 0; i < sig->count(); i++) { aoqi@0: identify(sig->type_at(i)); aoqi@0: } aoqi@0: begin_elem("method id='%d' holder='%d'", aoqi@0: id, identify(method->holder())); aoqi@0: name(method->name()); aoqi@0: print(" return='%d'", identify(sig->return_type())); aoqi@0: if (sig->count() > 0) { aoqi@0: print(" arguments='"); aoqi@0: for (int i = 0; i < sig->count(); i++) { aoqi@0: print((i == 0) ? "%d" : " %d", identify(sig->type_at(i))); aoqi@0: } aoqi@0: print("'"); aoqi@0: } aoqi@0: if (!method->is_loaded()) { aoqi@0: print(" unloaded='1'"); aoqi@0: } else { aoqi@0: print(" flags='%d'", (jchar) method->flags().as_int()); aoqi@0: // output a few metrics aoqi@0: print(" bytes='%d'", method->code_size()); aoqi@0: method->log_nmethod_identity(this); 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: } aoqi@0: end_elem(); aoqi@0: } else if (mobj->is_type()) { aoqi@0: BasicType type = mobj->as_type()->basic_type(); aoqi@0: elem("type id='%d' name='%s'", id, type2name(type)); aoqi@0: } else { aoqi@0: // Should not happen. aoqi@0: elem("unknown id='%d'", id); aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: } else if (obj->is_symbol()) { aoqi@0: begin_elem("symbol id='%d'", id); aoqi@0: name(obj->as_symbol()); aoqi@0: end_elem(); aoqi@0: } else { aoqi@0: // Should not happen. aoqi@0: elem("unknown id='%d'", id); aoqi@0: } aoqi@0: return id; aoqi@0: } aoqi@0: aoqi@0: void CompileLog::name(ciSymbol* name) { aoqi@0: if (name == NULL) return; aoqi@0: print(" name='"); aoqi@0: name->print_symbol_on(text()); // handles quoting conventions aoqi@0: print("'"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::clear_identities aoqi@0: // Forget which identities have been printed. aoqi@0: void CompileLog::clear_identities() { aoqi@0: _identities_limit = 0; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::finish_log_on_error aoqi@0: // aoqi@0: // Note: This function is called after fatal error, avoid unnecessary memory aoqi@0: // or stack allocation, use only async-safe functions. It's possible JVM is aoqi@0: // only partially initialized. aoqi@0: void CompileLog::finish_log_on_error(outputStream* file, char* buf, int buflen) { aoqi@0: static bool called_exit = false; aoqi@0: if (called_exit) return; aoqi@0: called_exit = true; aoqi@0: aoqi@0: CompileLog* log = _first; aoqi@0: while (log != NULL) { aoqi@0: log->flush(); aoqi@0: const char* partial_file = log->file(); aoqi@0: int partial_fd = open(partial_file, O_RDONLY); aoqi@0: if (partial_fd != -1) { aoqi@0: // print/print_cr may need to allocate large stack buffer to format aoqi@0: // strings, here we use snprintf() and print_raw() instead. aoqi@0: file->print_raw(""); aoqi@0: aoqi@0: size_t nr; // number read into buf from partial log aoqi@0: // Copy data up to the end of the last element: aoqi@0: julong to_read = log->_file_end; aoqi@0: while (to_read > 0) { aoqi@0: if (to_read < (julong)buflen) aoqi@0: nr = (size_t)to_read; aoqi@0: else nr = buflen; aoqi@0: nr = read(partial_fd, buf, (int)nr); aoqi@0: if (nr <= 0) break; aoqi@0: to_read -= (julong)nr; aoqi@0: file->write(buf, nr); aoqi@0: } aoqi@0: aoqi@0: // Copy any remaining data inside a quote: aoqi@0: bool saw_slop = false; aoqi@0: int end_cdata = 0; // state machine [0..2] watching for too many "]]" aoqi@0: while ((nr = read(partial_fd, buf, buflen)) > 0) { aoqi@0: if (!saw_slop) { aoqi@0: file->print_raw_cr(""); aoqi@0: file->print_raw_cr("write(buf, nr); } aoqi@0: // However, it must sometimes output the buffer in parts, aoqi@0: // in case there is a CDATA quote embedded in the fragment. aoqi@0: const char* bufp; // pointer into buf aoqi@0: size_t nw; // number written in each pass of the following loop: aoqi@0: for (bufp = buf; nr > 0; nr -= nw, bufp += nw) { aoqi@0: // Write up to any problematic CDATA delimiter (usually all of nr). aoqi@0: for (nw = 0; nw < nr; nw++) { aoqi@0: // First, scan ahead into the buf, checking the state machine. aoqi@0: switch (bufp[nw]) { aoqi@0: case ']': aoqi@0: if (end_cdata < 2) end_cdata += 1; // saturating counter aoqi@0: continue; // keep scanning aoqi@0: case '>': aoqi@0: if (end_cdata == 2) break; // found CDATA delimiter! aoqi@0: // else fall through: aoqi@0: default: aoqi@0: end_cdata = 0; aoqi@0: continue; // keep scanning aoqi@0: } aoqi@0: // If we get here, nw is pointing at a bad '>'. aoqi@0: // It is very rare for this to happen. aoqi@0: // However, this code has been tested by introducing aoqi@0: // CDATA sequences into the compilation log. aoqi@0: break; aoqi@0: } aoqi@0: // Now nw is the number of characters to write, usually == nr. aoqi@0: file->write(bufp, nw); aoqi@0: if (nw < nr) { aoqi@0: // We are about to go around the loop again. aoqi@0: // But first, disrupt the ]]> by closing and reopening the quote. aoqi@0: file->print_raw("]]>print_raw_cr("]]>"); aoqi@0: file->print_raw_cr(""); aoqi@0: } aoqi@0: file->print_raw_cr(""); aoqi@0: close(partial_fd); aoqi@0: unlink(partial_file); aoqi@0: } aoqi@0: CompileLog* next_log = log->_next; aoqi@0: delete log; aoqi@0: log = next_log; aoqi@0: } aoqi@0: _first = NULL; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::finish_log aoqi@0: // aoqi@0: // Called during normal shutdown. For now, any clean-up needed in normal aoqi@0: // shutdown is also needed in VM abort, so is covered by finish_log_on_error(). aoqi@0: // Just allocate a buffer and call finish_log_on_error(). aoqi@0: void CompileLog::finish_log(outputStream* file) { aoqi@0: char buf[4 * K]; aoqi@0: finish_log_on_error(file, buf, sizeof(buf)); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::inline_success aoqi@0: // aoqi@0: // Print about successful method inlining. aoqi@0: void CompileLog::inline_success(const char* reason) { aoqi@0: begin_elem("inline_success reason='"); aoqi@0: text("%s", reason); aoqi@0: end_elem("'"); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::inline_fail aoqi@0: // aoqi@0: // Print about failed method inlining. aoqi@0: void CompileLog::inline_fail(const char* reason) { aoqi@0: begin_elem("inline_fail reason='"); aoqi@0: text("%s", reason); aoqi@0: end_elem("'"); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::set_context aoqi@0: // aoqi@0: // Set XML tag as an optional marker - it is printed only if aoqi@0: // there are other entries after until it is reset. aoqi@0: void CompileLog::set_context(const char* format, ...) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: clear_context(); aoqi@0: _context.print("<"); aoqi@0: _context.vprint(format, ap); aoqi@0: _context.print_cr("/>"); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------ aoqi@0: // CompileLog::code_cache_state aoqi@0: // aoqi@0: // Print code cache state. aoqi@0: void CompileLog::code_cache_state() { aoqi@0: begin_elem("code_cache"); aoqi@0: CodeCache::log_state(this); aoqi@0: end_elem("%s", ""); aoqi@0: }