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