aoqi@0: /* aoqi@0: * Copyright (c) 1997, 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 "compiler/compileLog.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "utilities/defaultStream.hpp" aoqi@0: #include "utilities/ostream.hpp" aoqi@0: #include "utilities/top.hpp" aoqi@0: #include "utilities/xmlstream.hpp" aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "os_linux.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "os_solaris.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "os_windows.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "os_aix.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "os_bsd.inline.hpp" aoqi@0: #endif aoqi@0: aoqi@0: extern "C" void jio_print(const char* s); // Declarationtion of jvm method aoqi@0: aoqi@0: outputStream::outputStream(int width) { aoqi@0: _width = width; aoqi@0: _position = 0; aoqi@0: _newlines = 0; aoqi@0: _precount = 0; aoqi@0: _indentation = 0; aoqi@0: } aoqi@0: aoqi@0: outputStream::outputStream(int width, bool has_time_stamps) { aoqi@0: _width = width; aoqi@0: _position = 0; aoqi@0: _newlines = 0; aoqi@0: _precount = 0; aoqi@0: _indentation = 0; aoqi@0: if (has_time_stamps) _stamp.update(); aoqi@0: } aoqi@0: aoqi@0: void outputStream::update_position(const char* s, size_t len) { aoqi@0: for (size_t i = 0; i < len; i++) { aoqi@0: char ch = s[i]; aoqi@0: if (ch == '\n') { aoqi@0: _newlines += 1; aoqi@0: _precount += _position + 1; aoqi@0: _position = 0; aoqi@0: } else if (ch == '\t') { aoqi@0: int tw = 8 - (_position & 7); aoqi@0: _position += tw; aoqi@0: _precount -= tw-1; // invariant: _precount + _position == total count aoqi@0: } else { aoqi@0: _position += 1; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Execute a vsprintf, using the given buffer if necessary. aoqi@0: // Return a pointer to the formatted string. aoqi@0: const char* outputStream::do_vsnprintf(char* buffer, size_t buflen, aoqi@0: const char* format, va_list ap, aoqi@0: bool add_cr, aoqi@0: size_t& result_len) { aoqi@0: const char* result; aoqi@0: if (add_cr) buflen--; aoqi@0: if (!strchr(format, '%')) { aoqi@0: // constant format string aoqi@0: result = format; aoqi@0: result_len = strlen(result); aoqi@0: if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate aoqi@0: } else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') { aoqi@0: // trivial copy-through format string aoqi@0: result = va_arg(ap, const char*); aoqi@0: result_len = strlen(result); aoqi@0: if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate aoqi@0: } else if (vsnprintf(buffer, buflen, format, ap) >= 0) { aoqi@0: result = buffer; aoqi@0: result_len = strlen(result); aoqi@0: } else { aoqi@0: DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");) aoqi@0: result = buffer; aoqi@0: result_len = buflen - 1; aoqi@0: buffer[result_len] = 0; aoqi@0: } aoqi@0: if (add_cr) { aoqi@0: if (result != buffer) { aoqi@0: strncpy(buffer, result, buflen); aoqi@0: result = buffer; aoqi@0: } aoqi@0: buffer[result_len++] = '\n'; aoqi@0: buffer[result_len] = 0; aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void outputStream::print(const char* format, ...) { aoqi@0: char buffer[O_BUFLEN]; aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len); aoqi@0: write(str, len); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: aoqi@0: void outputStream::print_cr(const char* format, ...) { aoqi@0: char buffer[O_BUFLEN]; aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len); aoqi@0: write(str, len); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: aoqi@0: void outputStream::vprint(const char *format, va_list argptr) { aoqi@0: char buffer[O_BUFLEN]; aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len); aoqi@0: write(str, len); aoqi@0: } aoqi@0: aoqi@0: void outputStream::vprint_cr(const char* format, va_list argptr) { aoqi@0: char buffer[O_BUFLEN]; aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len); aoqi@0: write(str, len); aoqi@0: } aoqi@0: aoqi@0: void outputStream::fill_to(int col) { aoqi@0: int need_fill = col - position(); aoqi@0: sp(need_fill); aoqi@0: } aoqi@0: aoqi@0: void outputStream::move_to(int col, int slop, int min_space) { aoqi@0: if (position() >= col + slop) aoqi@0: cr(); aoqi@0: int need_fill = col - position(); aoqi@0: if (need_fill < min_space) aoqi@0: need_fill = min_space; aoqi@0: sp(need_fill); aoqi@0: } aoqi@0: aoqi@0: void outputStream::put(char ch) { aoqi@0: assert(ch != 0, "please fix call site"); aoqi@0: char buf[] = { ch, '\0' }; aoqi@0: write(buf, 1); aoqi@0: } aoqi@0: aoqi@0: #define SP_USE_TABS false aoqi@0: aoqi@0: void outputStream::sp(int count) { aoqi@0: if (count < 0) return; aoqi@0: if (SP_USE_TABS && count >= 8) { aoqi@0: int target = position() + count; aoqi@0: while (count >= 8) { aoqi@0: this->write("\t", 1); aoqi@0: count -= 8; aoqi@0: } aoqi@0: count = target - position(); aoqi@0: } aoqi@0: while (count > 0) { aoqi@0: int nw = (count > 8) ? 8 : count; aoqi@0: this->write(" ", nw); aoqi@0: count -= nw; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void outputStream::cr() { aoqi@0: this->write("\n", 1); aoqi@0: } aoqi@0: aoqi@0: void outputStream::stamp() { aoqi@0: if (! _stamp.is_updated()) { aoqi@0: _stamp.update(); // start at 0 on first call to stamp() aoqi@0: } aoqi@0: aoqi@0: // outputStream::stamp() may get called by ostream_abort(), use snprintf aoqi@0: // to avoid allocating large stack buffer in print(). aoqi@0: char buf[40]; aoqi@0: jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds()); aoqi@0: print_raw(buf); aoqi@0: } aoqi@0: aoqi@0: void outputStream::stamp(bool guard, aoqi@0: const char* prefix, aoqi@0: const char* suffix) { aoqi@0: if (!guard) { aoqi@0: return; aoqi@0: } aoqi@0: print_raw(prefix); aoqi@0: stamp(); aoqi@0: print_raw(suffix); aoqi@0: } aoqi@0: aoqi@0: void outputStream::date_stamp(bool guard, aoqi@0: const char* prefix, aoqi@0: const char* suffix) { aoqi@0: if (!guard) { aoqi@0: return; aoqi@0: } aoqi@0: print_raw(prefix); aoqi@0: static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz"; aoqi@0: static const int buffer_length = 32; aoqi@0: char buffer[buffer_length]; aoqi@0: const char* iso8601_result = os::iso8601_time(buffer, buffer_length); aoqi@0: if (iso8601_result != NULL) { aoqi@0: print_raw(buffer); aoqi@0: } else { aoqi@0: print_raw(error_time); aoqi@0: } aoqi@0: print_raw(suffix); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: outputStream& outputStream::indent() { aoqi@0: while (_position < _indentation) sp(); aoqi@0: return *this; aoqi@0: } aoqi@0: aoqi@0: void outputStream::print_jlong(jlong value) { aoqi@0: print(JLONG_FORMAT, value); aoqi@0: } aoqi@0: aoqi@0: void outputStream::print_julong(julong value) { aoqi@0: print(JULONG_FORMAT, value); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * This prints out hex data in a 'windbg' or 'xxd' form, where each line is: aoqi@0: * : 8 * aoqi@0: * example: aoqi@0: * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............ aoqi@0: * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... .... aoqi@0: * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......] aoqi@0: * ... aoqi@0: * aoqi@0: * indent is applied to each line. Ends with a CR. aoqi@0: */ aoqi@0: void outputStream::print_data(void* data, size_t len, bool with_ascii) { aoqi@0: size_t limit = (len + 16) / 16 * 16; aoqi@0: for (size_t i = 0; i < limit; ++i) { aoqi@0: if (i % 16 == 0) { aoqi@0: indent().print(SIZE_FORMAT_HEX_W(07)":", i); aoqi@0: } aoqi@0: if (i % 2 == 0) { aoqi@0: print(" "); aoqi@0: } aoqi@0: if (i < len) { aoqi@0: print("%02x", ((unsigned char*)data)[i]); aoqi@0: } else { aoqi@0: print(" "); aoqi@0: } aoqi@0: if ((i + 1) % 16 == 0) { aoqi@0: if (with_ascii) { aoqi@0: print(" "); aoqi@0: for (size_t j = 0; j < 16; ++j) { aoqi@0: size_t idx = i + j - 15; aoqi@0: if (idx < len) { aoqi@0: char c = ((char*)data)[idx]; aoqi@0: print("%c", c >= 32 && c <= 126 ? c : '.'); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: cr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: stringStream::stringStream(size_t initial_size) : outputStream() { aoqi@0: buffer_length = initial_size; aoqi@0: buffer = NEW_RESOURCE_ARRAY(char, buffer_length); aoqi@0: buffer_pos = 0; aoqi@0: buffer_fixed = false; aoqi@0: DEBUG_ONLY(rm = Thread::current()->current_resource_mark();) aoqi@0: } aoqi@0: aoqi@0: // useful for output to fixed chunks of memory, such as performance counters aoqi@0: stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() { aoqi@0: buffer_length = fixed_buffer_size; aoqi@0: buffer = fixed_buffer; aoqi@0: buffer_pos = 0; aoqi@0: buffer_fixed = true; aoqi@0: } aoqi@0: aoqi@0: void stringStream::write(const char* s, size_t len) { aoqi@0: size_t write_len = len; // number of non-null bytes to write aoqi@0: size_t end = buffer_pos + len + 1; // position after write and final '\0' aoqi@0: if (end > buffer_length) { aoqi@0: if (buffer_fixed) { aoqi@0: // if buffer cannot resize, silently truncate aoqi@0: end = buffer_length; aoqi@0: write_len = end - buffer_pos - 1; // leave room for the final '\0' aoqi@0: } else { aoqi@0: // For small overruns, double the buffer. For larger ones, aoqi@0: // increase to the requested size. aoqi@0: if (end < buffer_length * 2) { aoqi@0: end = buffer_length * 2; aoqi@0: } aoqi@0: char* oldbuf = buffer; aoqi@0: assert(rm == NULL || Thread::current()->current_resource_mark() == rm, aoqi@0: "stringStream is re-allocated with a different ResourceMark"); aoqi@0: buffer = NEW_RESOURCE_ARRAY(char, end); aoqi@0: strncpy(buffer, oldbuf, buffer_pos); aoqi@0: buffer_length = end; aoqi@0: } aoqi@0: } aoqi@0: // invariant: buffer is always null-terminated aoqi@0: guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob"); aoqi@0: buffer[buffer_pos + write_len] = 0; aoqi@0: strncpy(buffer + buffer_pos, s, write_len); aoqi@0: buffer_pos += write_len; aoqi@0: aoqi@0: // Note that the following does not depend on write_len. aoqi@0: // This means that position and count get updated aoqi@0: // even when overflow occurs. aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: char* stringStream::as_string() { aoqi@0: char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1); aoqi@0: strncpy(copy, buffer, buffer_pos); aoqi@0: copy[buffer_pos] = 0; // terminating null aoqi@0: return copy; aoqi@0: } aoqi@0: aoqi@0: stringStream::~stringStream() {} aoqi@0: aoqi@0: xmlStream* xtty; aoqi@0: outputStream* tty; aoqi@0: outputStream* gclog_or_tty; aoqi@0: extern Mutex* tty_lock; aoqi@0: aoqi@0: #define EXTRACHARLEN 32 aoqi@0: #define CURRENTAPPX ".current" aoqi@0: #define FILENAMEBUFLEN 1024 aoqi@0: // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS aoqi@0: char* get_datetime_string(char *buf, size_t len) { aoqi@0: os::local_time_string(buf, len); aoqi@0: int i = (int)strlen(buf); aoqi@0: while (i-- >= 0) { aoqi@0: if (buf[i] == ' ') buf[i] = '_'; aoqi@0: else if (buf[i] == ':') buf[i] = '-'; aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: static const char* make_log_name_internal(const char* log_name, const char* force_directory, aoqi@0: int pid, const char* tms) { aoqi@0: const char* basename = log_name; aoqi@0: char file_sep = os::file_separator()[0]; aoqi@0: const char* cp; aoqi@0: char pid_text[32]; aoqi@0: aoqi@0: for (cp = log_name; *cp != '\0'; cp++) { aoqi@0: if (*cp == '/' || *cp == file_sep) { aoqi@0: basename = cp + 1; aoqi@0: } aoqi@0: } aoqi@0: const char* nametail = log_name; aoqi@0: // Compute buffer length aoqi@0: size_t buffer_length; aoqi@0: if (force_directory != NULL) { aoqi@0: buffer_length = strlen(force_directory) + strlen(os::file_separator()) + aoqi@0: strlen(basename) + 1; aoqi@0: } else { aoqi@0: buffer_length = strlen(log_name) + 1; aoqi@0: } aoqi@0: aoqi@0: // const char* star = strchr(basename, '*'); aoqi@0: const char* pts = strstr(basename, "%p"); aoqi@0: int pid_pos = (pts == NULL) ? -1 : (pts - nametail); aoqi@0: aoqi@0: if (pid_pos >= 0) { aoqi@0: jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid); aoqi@0: buffer_length += strlen(pid_text); aoqi@0: } aoqi@0: aoqi@0: pts = strstr(basename, "%t"); aoqi@0: int tms_pos = (pts == NULL) ? -1 : (pts - nametail); aoqi@0: if (tms_pos >= 0) { aoqi@0: buffer_length += strlen(tms); aoqi@0: } aoqi@0: aoqi@0: // Create big enough buffer. aoqi@0: char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal); aoqi@0: aoqi@0: strcpy(buf, ""); aoqi@0: if (force_directory != NULL) { aoqi@0: strcat(buf, force_directory); aoqi@0: strcat(buf, os::file_separator()); aoqi@0: nametail = basename; // completely skip directory prefix aoqi@0: } aoqi@0: aoqi@0: // who is first, %p or %t? aoqi@0: int first = -1, second = -1; aoqi@0: const char *p1st = NULL; aoqi@0: const char *p2nd = NULL; aoqi@0: aoqi@0: if (pid_pos >= 0 && tms_pos >= 0) { aoqi@0: // contains both %p and %t aoqi@0: if (pid_pos < tms_pos) { aoqi@0: // case foo%pbar%tmonkey.log aoqi@0: first = pid_pos; aoqi@0: p1st = pid_text; aoqi@0: second = tms_pos; aoqi@0: p2nd = tms; aoqi@0: } else { aoqi@0: // case foo%tbar%pmonkey.log aoqi@0: first = tms_pos; aoqi@0: p1st = tms; aoqi@0: second = pid_pos; aoqi@0: p2nd = pid_text; aoqi@0: } aoqi@0: } else if (pid_pos >= 0) { aoqi@0: // contains %p only aoqi@0: first = pid_pos; aoqi@0: p1st = pid_text; aoqi@0: } else if (tms_pos >= 0) { aoqi@0: // contains %t only aoqi@0: first = tms_pos; aoqi@0: p1st = tms; aoqi@0: } aoqi@0: aoqi@0: int buf_pos = (int)strlen(buf); aoqi@0: const char* tail = nametail; aoqi@0: aoqi@0: if (first >= 0) { aoqi@0: tail = nametail + first + 2; aoqi@0: strncpy(&buf[buf_pos], nametail, first); aoqi@0: strcpy(&buf[buf_pos + first], p1st); aoqi@0: buf_pos = (int)strlen(buf); aoqi@0: if (second >= 0) { aoqi@0: strncpy(&buf[buf_pos], tail, second - first - 2); aoqi@0: strcpy(&buf[buf_pos + second - first - 2], p2nd); aoqi@0: tail = nametail + second + 2; aoqi@0: } aoqi@0: } aoqi@0: strcat(buf, tail); // append rest of name, or all of name aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: // log_name comes from -XX:LogFile=log_name or -Xloggc:log_name aoqi@0: // in log_name, %p => pid1234 and aoqi@0: // %t => YYYY-MM-DD_HH-MM-SS aoqi@0: static const char* make_log_name(const char* log_name, const char* force_directory) { aoqi@0: char timestr[32]; aoqi@0: get_datetime_string(timestr, sizeof(timestr)); aoqi@0: return make_log_name_internal(log_name, force_directory, os::current_process_id(), aoqi@0: timestr); aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void test_loggc_filename() { aoqi@0: int pid; aoqi@0: char tms[32]; aoqi@0: char i_result[FILENAMEBUFLEN]; aoqi@0: const char* o_result; aoqi@0: get_datetime_string(tms, sizeof(tms)); aoqi@0: pid = os::current_process_id(); aoqi@0: aoqi@0: // test.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test.log", tms); aoqi@0: o_result = make_log_name_internal("test.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: aoqi@0: // test-%t-%p.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%s-pid%u.log", tms, pid); aoqi@0: o_result = make_log_name_internal("test-%t-%p.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t-%%p.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: aoqi@0: // test-%t%p.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%spid%u.log", tms, pid); aoqi@0: o_result = make_log_name_internal("test-%t%p.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t%%p.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: aoqi@0: // %p%t.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u%s.log", pid, tms); aoqi@0: o_result = make_log_name_internal("%p%t.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p%%t.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: aoqi@0: // %p-test.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u-test.log", pid); aoqi@0: o_result = make_log_name_internal("%p-test.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p-test.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: aoqi@0: // %t.log aoqi@0: jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "%s.log", tms); aoqi@0: o_result = make_log_name_internal("%t.log", NULL, pid, tms); aoqi@0: assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%t.log\", NULL)"); aoqi@0: FREE_C_HEAP_ARRAY(char, o_result, mtInternal); aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: fileStream::fileStream(const char* file_name) { aoqi@0: _file = fopen(file_name, "w"); aoqi@0: if (_file != NULL) { aoqi@0: _need_close = true; aoqi@0: } else { aoqi@0: warning("Cannot open file %s due to %s\n", file_name, strerror(errno)); aoqi@0: _need_close = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: fileStream::fileStream(const char* file_name, const char* opentype) { aoqi@0: _file = fopen(file_name, opentype); aoqi@0: if (_file != NULL) { aoqi@0: _need_close = true; aoqi@0: } else { aoqi@0: warning("Cannot open file %s due to %s\n", file_name, strerror(errno)); aoqi@0: _need_close = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void fileStream::write(const char* s, size_t len) { aoqi@0: if (_file != NULL) { aoqi@0: // Make an unused local variable to avoid warning from gcc 4.x compiler. aoqi@0: size_t count = fwrite(s, 1, len, _file); aoqi@0: } aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: long fileStream::fileSize() { aoqi@0: long size = -1; aoqi@0: if (_file != NULL) { aoqi@0: long pos = ::ftell(_file); aoqi@0: if (::fseek(_file, 0, SEEK_END) == 0) { aoqi@0: size = ::ftell(_file); aoqi@0: } aoqi@0: ::fseek(_file, pos, SEEK_SET); aoqi@0: } aoqi@0: return size; aoqi@0: } aoqi@0: aoqi@0: char* fileStream::readln(char *data, int count ) { aoqi@0: char * ret = ::fgets(data, count, _file); aoqi@0: //Get rid of annoying \n char aoqi@0: data[::strlen(data)-1] = '\0'; aoqi@0: return ret; aoqi@0: } aoqi@0: aoqi@0: fileStream::~fileStream() { aoqi@0: if (_file != NULL) { aoqi@0: if (_need_close) fclose(_file); aoqi@0: _file = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void fileStream::flush() { aoqi@0: fflush(_file); aoqi@0: } aoqi@0: aoqi@0: fdStream::fdStream(const char* file_name) { aoqi@0: _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); aoqi@0: _need_close = true; aoqi@0: } aoqi@0: aoqi@0: fdStream::~fdStream() { aoqi@0: if (_fd != -1) { aoqi@0: if (_need_close) close(_fd); aoqi@0: _fd = -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void fdStream::write(const char* s, size_t len) { aoqi@0: if (_fd != -1) { aoqi@0: // Make an unused local variable to avoid warning from gcc 4.x compiler. aoqi@0: size_t count = ::write(_fd, s, (int)len); aoqi@0: } aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: // dump vm version, os version, platform info, build id, aoqi@0: // memory usage and command line flags into header aoqi@0: void gcLogFileStream::dump_loggc_header() { aoqi@0: if (is_open()) { aoqi@0: print_cr("%s", Abstract_VM_Version::internal_vm_info_string()); aoqi@0: os::print_memory_info(this); aoqi@0: print("CommandLine flags: "); aoqi@0: CommandLineFlags::printSetFlags(this); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: gcLogFileStream::~gcLogFileStream() { aoqi@0: if (_file != NULL) { aoqi@0: if (_need_close) fclose(_file); aoqi@0: _file = NULL; aoqi@0: } aoqi@0: if (_file_name != NULL) { aoqi@0: FREE_C_HEAP_ARRAY(char, _file_name, mtInternal); aoqi@0: _file_name = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: gcLogFileStream::gcLogFileStream(const char* file_name) { aoqi@0: _cur_file_num = 0; aoqi@0: _bytes_written = 0L; aoqi@0: _file_name = make_log_name(file_name, NULL); aoqi@0: aoqi@0: // gc log file rotation aoqi@0: if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) { aoqi@0: char tempbuf[FILENAMEBUFLEN]; aoqi@0: jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num); aoqi@0: _file = fopen(tempbuf, "w"); aoqi@0: } else { aoqi@0: _file = fopen(_file_name, "w"); aoqi@0: } aoqi@0: if (_file != NULL) { aoqi@0: _need_close = true; aoqi@0: dump_loggc_header(); aoqi@0: } else { aoqi@0: warning("Cannot open file %s due to %s\n", _file_name, strerror(errno)); aoqi@0: _need_close = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void gcLogFileStream::write(const char* s, size_t len) { aoqi@0: if (_file != NULL) { aoqi@0: size_t count = fwrite(s, 1, len, _file); aoqi@0: _bytes_written += count; aoqi@0: } aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: // rotate_log must be called from VMThread at safepoint. In case need change parameters aoqi@0: // for gc log rotation from thread other than VMThread, a sub type of VM_Operation aoqi@0: // should be created and be submitted to VMThread's operation queue. DO NOT call this aoqi@0: // function directly. Currently, it is safe to rotate log at safepoint through VMThread. aoqi@0: // That is, no mutator threads and concurrent GC threads run parallel with VMThread to aoqi@0: // write to gc log file at safepoint. If in future, changes made for mutator threads or aoqi@0: // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log aoqi@0: // must be synchronized. aoqi@0: void gcLogFileStream::rotate_log(bool force, outputStream* out) { aoqi@0: char time_msg[FILENAMEBUFLEN]; aoqi@0: char time_str[EXTRACHARLEN]; aoqi@0: char current_file_name[FILENAMEBUFLEN]; aoqi@0: char renamed_file_name[FILENAMEBUFLEN]; aoqi@0: aoqi@0: if (!should_rotate(force)) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: Thread *thread = Thread::current(); aoqi@0: assert(thread == NULL || aoqi@0: (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()), aoqi@0: "Must be VMThread at safepoint"); aoqi@0: #endif aoqi@0: if (NumberOfGCLogFiles == 1) { aoqi@0: // rotate in same file aoqi@0: rewind(); aoqi@0: _bytes_written = 0L; aoqi@0: jio_snprintf(time_msg, sizeof(time_msg), "File %s rotated at %s\n", aoqi@0: _file_name, os::local_time_string((char *)time_str, sizeof(time_str))); aoqi@0: write(time_msg, strlen(time_msg)); aoqi@0: aoqi@0: if (out != NULL) { aoqi@0: out->print("%s", time_msg); aoqi@0: } aoqi@0: aoqi@0: dump_loggc_header(); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: #if defined(_WINDOWS) aoqi@0: #ifndef F_OK aoqi@0: #define F_OK 0 aoqi@0: #endif aoqi@0: #endif // _WINDOWS aoqi@0: aoqi@0: // rotate file in names extended_filename.0, extended_filename.1, ..., aoqi@0: // extended_filename.. Current rotation file name will aoqi@0: // have a form of extended_filename..current where i is the current rotation aoqi@0: // file number. After it reaches max file size, the file will be saved and renamed aoqi@0: // with .current removed from its tail. aoqi@0: size_t filename_len = strlen(_file_name); aoqi@0: if (_file != NULL) { aoqi@0: jio_snprintf(renamed_file_name, filename_len + EXTRACHARLEN, "%s.%d", aoqi@0: _file_name, _cur_file_num); aoqi@0: jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX, aoqi@0: _file_name, _cur_file_num); aoqi@0: aoqi@0: const char* msg = force ? "GC log rotation request has been received." aoqi@0: : "GC log file has reached the maximum size."; aoqi@0: jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n", aoqi@0: os::local_time_string((char *)time_str, sizeof(time_str)), aoqi@0: msg, renamed_file_name); aoqi@0: write(time_msg, strlen(time_msg)); aoqi@0: aoqi@0: if (out != NULL) { aoqi@0: out->print("%s", time_msg); aoqi@0: } aoqi@0: aoqi@0: fclose(_file); aoqi@0: _file = NULL; aoqi@0: aoqi@0: bool can_rename = true; aoqi@0: if (access(current_file_name, F_OK) != 0) { aoqi@0: // current file does not exist? aoqi@0: warning("No source file exists, cannot rename\n"); aoqi@0: can_rename = false; aoqi@0: } aoqi@0: if (can_rename) { aoqi@0: if (access(renamed_file_name, F_OK) == 0) { aoqi@0: if (remove(renamed_file_name) != 0) { aoqi@0: warning("Could not delete existing file %s\n", renamed_file_name); aoqi@0: can_rename = false; aoqi@0: } aoqi@0: } else { aoqi@0: // file does not exist, ok to rename aoqi@0: } aoqi@0: } aoqi@0: if (can_rename && rename(current_file_name, renamed_file_name) != 0) { aoqi@0: warning("Could not rename %s to %s\n", _file_name, renamed_file_name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _cur_file_num++; aoqi@0: if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0; aoqi@0: jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX, aoqi@0: _file_name, _cur_file_num); aoqi@0: _file = fopen(current_file_name, "w"); aoqi@0: aoqi@0: if (_file != NULL) { aoqi@0: _bytes_written = 0L; aoqi@0: _need_close = true; aoqi@0: // reuse current_file_name for time_msg aoqi@0: jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, aoqi@0: "%s.%d", _file_name, _cur_file_num); aoqi@0: jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n", aoqi@0: os::local_time_string((char *)time_str, sizeof(time_str)), aoqi@0: current_file_name); aoqi@0: write(time_msg, strlen(time_msg)); aoqi@0: aoqi@0: if (out != NULL) { aoqi@0: out->print("%s", time_msg); aoqi@0: } aoqi@0: aoqi@0: dump_loggc_header(); aoqi@0: // remove the existing file aoqi@0: if (access(current_file_name, F_OK) == 0) { aoqi@0: if (remove(current_file_name) != 0) { aoqi@0: warning("Could not delete existing file %s\n", current_file_name); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: warning("failed to open rotation log file %s due to %s\n" aoqi@0: "Turned off GC log file rotation\n", aoqi@0: _file_name, strerror(errno)); aoqi@0: _need_close = false; aoqi@0: FLAG_SET_DEFAULT(UseGCLogFileRotation, false); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: defaultStream* defaultStream::instance = NULL; aoqi@0: int defaultStream::_output_fd = 1; aoqi@0: int defaultStream::_error_fd = 2; aoqi@0: FILE* defaultStream::_output_stream = stdout; aoqi@0: FILE* defaultStream::_error_stream = stderr; aoqi@0: aoqi@0: #define LOG_MAJOR_VERSION 160 aoqi@0: #define LOG_MINOR_VERSION 1 aoqi@0: aoqi@0: void defaultStream::init() { aoqi@0: _inited = true; aoqi@0: if (LogVMOutput || LogCompilation) { aoqi@0: init_log(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool defaultStream::has_log_file() { aoqi@0: // lazily create log file (at startup, LogVMOutput is false even aoqi@0: // if +LogVMOutput is used, because the flags haven't been parsed yet) aoqi@0: // For safer printing during fatal error handling, do not init logfile aoqi@0: // if a VM error has been reported. aoqi@0: if (!_inited && !is_error_reported()) init(); aoqi@0: return _log_file != NULL; aoqi@0: } aoqi@0: aoqi@0: void defaultStream::init_log() { aoqi@0: // %%% Need a MutexLocker? aoqi@0: const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log"; aoqi@0: const char* try_name = make_log_name(log_name, NULL); aoqi@0: fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name); aoqi@0: if (!file->is_open()) { aoqi@0: // Try again to open the file. aoqi@0: char warnbuf[O_BUFLEN*2]; aoqi@0: jio_snprintf(warnbuf, sizeof(warnbuf), aoqi@0: "Warning: Cannot open log file: %s\n", try_name); aoqi@0: // Note: This feature is for maintainer use only. No need for L10N. aoqi@0: jio_print(warnbuf); aoqi@0: FREE_C_HEAP_ARRAY(char, try_name, mtInternal); aoqi@0: try_name = make_log_name(log_name, os::get_temp_directory()); aoqi@0: jio_snprintf(warnbuf, sizeof(warnbuf), aoqi@0: "Warning: Forcing option -XX:LogFile=%s\n", try_name); aoqi@0: jio_print(warnbuf); aoqi@0: delete file; aoqi@0: file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name); aoqi@0: } aoqi@0: FREE_C_HEAP_ARRAY(char, try_name, mtInternal); aoqi@0: aoqi@0: if (file->is_open()) { aoqi@0: _log_file = file; aoqi@0: xmlStream* xs = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file); aoqi@0: _outer_xmlStream = xs; aoqi@0: if (this == tty) xtty = xs; aoqi@0: // Write XML header. aoqi@0: xs->print_cr(""); aoqi@0: // (For now, don't bother to issue a DTD for this private format.) aoqi@0: jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds(); aoqi@0: // %%% Should be: jlong time_ms = os::start_time_milliseconds(), if aoqi@0: // we ever get round to introduce that method on the os class aoqi@0: xs->head("hotspot_log version='%d %d'" aoqi@0: " process='%d' time_ms='"INT64_FORMAT"'", aoqi@0: LOG_MAJOR_VERSION, LOG_MINOR_VERSION, aoqi@0: os::current_process_id(), (int64_t)time_ms); aoqi@0: // Write VM version header immediately. aoqi@0: xs->head("vm_version"); aoqi@0: xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr(); aoqi@0: xs->tail("name"); aoqi@0: xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr(); aoqi@0: xs->tail("release"); aoqi@0: xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr(); aoqi@0: xs->tail("info"); aoqi@0: xs->tail("vm_version"); aoqi@0: // Record information about the command-line invocation. aoqi@0: xs->head("vm_arguments"); // Cf. Arguments::print_on() aoqi@0: if (Arguments::num_jvm_flags() > 0) { aoqi@0: xs->head("flags"); aoqi@0: Arguments::print_jvm_flags_on(xs->text()); aoqi@0: xs->tail("flags"); aoqi@0: } aoqi@0: if (Arguments::num_jvm_args() > 0) { aoqi@0: xs->head("args"); aoqi@0: Arguments::print_jvm_args_on(xs->text()); aoqi@0: xs->tail("args"); aoqi@0: } aoqi@0: if (Arguments::java_command() != NULL) { aoqi@0: xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command()); aoqi@0: xs->tail("command"); aoqi@0: } aoqi@0: if (Arguments::sun_java_launcher() != NULL) { aoqi@0: xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher()); aoqi@0: xs->tail("launcher"); aoqi@0: } aoqi@0: if (Arguments::system_properties() != NULL) { aoqi@0: xs->head("properties"); aoqi@0: // Print it as a java-style property list. aoqi@0: // System properties don't generally contain newlines, so don't bother with unparsing. aoqi@0: for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { aoqi@0: xs->text()->print_cr("%s=%s", p->key(), p->value()); aoqi@0: } aoqi@0: xs->tail("properties"); aoqi@0: } aoqi@0: xs->tail("vm_arguments"); aoqi@0: // tty output per se is grouped under the ... element. aoqi@0: xs->head("tty"); aoqi@0: // All further non-markup text gets copied to the tty: aoqi@0: xs->_text = this; // requires friend declaration! aoqi@0: } else { aoqi@0: delete(file); aoqi@0: // and leave xtty as NULL aoqi@0: LogVMOutput = false; aoqi@0: DisplayVMOutput = true; aoqi@0: LogCompilation = false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // finish_log() is called during normal VM shutdown. finish_log_on_error() is aoqi@0: // called by ostream_abort() after a fatal error. aoqi@0: // aoqi@0: void defaultStream::finish_log() { aoqi@0: xmlStream* xs = _outer_xmlStream; aoqi@0: xs->done("tty"); aoqi@0: aoqi@0: // Other log forks are appended here, at the End of Time: aoqi@0: CompileLog::finish_log(xs->out()); // write compile logging, if any, now aoqi@0: aoqi@0: xs->done("hotspot_log"); aoqi@0: xs->flush(); aoqi@0: aoqi@0: fileStream* file = _log_file; aoqi@0: _log_file = NULL; aoqi@0: aoqi@0: delete _outer_xmlStream; aoqi@0: _outer_xmlStream = NULL; aoqi@0: aoqi@0: file->flush(); aoqi@0: delete file; aoqi@0: } aoqi@0: aoqi@0: void defaultStream::finish_log_on_error(char *buf, int buflen) { aoqi@0: xmlStream* xs = _outer_xmlStream; aoqi@0: aoqi@0: if (xs && xs->out()) { aoqi@0: aoqi@0: xs->done_raw("tty"); aoqi@0: aoqi@0: // Other log forks are appended here, at the End of Time: aoqi@0: CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now aoqi@0: aoqi@0: xs->done_raw("hotspot_log"); aoqi@0: xs->flush(); aoqi@0: aoqi@0: fileStream* file = _log_file; aoqi@0: _log_file = NULL; aoqi@0: _outer_xmlStream = NULL; aoqi@0: aoqi@0: if (file) { aoqi@0: file->flush(); aoqi@0: aoqi@0: // Can't delete or close the file because delete and fclose aren't aoqi@0: // async-safe. We are about to die, so leave it to the kernel. aoqi@0: // delete file; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: intx defaultStream::hold(intx writer_id) { aoqi@0: bool has_log = has_log_file(); // check before locking aoqi@0: if (// impossible, but who knows? aoqi@0: writer_id == NO_WRITER || aoqi@0: aoqi@0: // bootstrap problem aoqi@0: tty_lock == NULL || aoqi@0: aoqi@0: // can't grab a lock or call Thread::current() if TLS isn't initialized aoqi@0: ThreadLocalStorage::thread() == NULL || aoqi@0: aoqi@0: // developer hook aoqi@0: !SerializeVMOutput || aoqi@0: aoqi@0: // VM already unhealthy aoqi@0: is_error_reported() || aoqi@0: aoqi@0: // safepoint == global lock (for VM only) aoqi@0: (SafepointSynchronize::is_synchronizing() && aoqi@0: Thread::current()->is_VM_thread()) aoqi@0: ) { aoqi@0: // do not attempt to lock unless we know the thread and the VM is healthy aoqi@0: return NO_WRITER; aoqi@0: } aoqi@0: if (_writer == writer_id) { aoqi@0: // already held, no need to re-grab the lock aoqi@0: return NO_WRITER; aoqi@0: } aoqi@0: tty_lock->lock_without_safepoint_check(); aoqi@0: // got the lock aoqi@0: if (writer_id != _last_writer) { aoqi@0: if (has_log) { aoqi@0: _log_file->bol(); aoqi@0: // output a hint where this output is coming from: aoqi@0: _log_file->print_cr("", writer_id); aoqi@0: } aoqi@0: _last_writer = writer_id; aoqi@0: } aoqi@0: _writer = writer_id; aoqi@0: return writer_id; aoqi@0: } aoqi@0: aoqi@0: void defaultStream::release(intx holder) { aoqi@0: if (holder == NO_WRITER) { aoqi@0: // nothing to release: either a recursive lock, or we scribbled (too bad) aoqi@0: return; aoqi@0: } aoqi@0: if (_writer != holder) { aoqi@0: return; // already unlocked, perhaps via break_tty_lock_for_safepoint aoqi@0: } aoqi@0: _writer = NO_WRITER; aoqi@0: tty_lock->unlock(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Yuck: jio_print does not accept char*/len. aoqi@0: static void call_jio_print(const char* s, size_t len) { aoqi@0: char buffer[O_BUFLEN+100]; aoqi@0: if (len > sizeof(buffer)-1) { aoqi@0: warning("increase O_BUFLEN in ostream.cpp -- output truncated"); aoqi@0: len = sizeof(buffer)-1; aoqi@0: } aoqi@0: strncpy(buffer, s, len); aoqi@0: buffer[len] = '\0'; aoqi@0: jio_print(buffer); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void defaultStream::write(const char* s, size_t len) { aoqi@0: intx thread_id = os::current_thread_id(); aoqi@0: intx holder = hold(thread_id); aoqi@0: aoqi@0: if (DisplayVMOutput && aoqi@0: (_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) { aoqi@0: // print to output stream. It can be redirected by a vfprintf hook aoqi@0: if (s[len] == '\0') { aoqi@0: jio_print(s); aoqi@0: } else { aoqi@0: call_jio_print(s, len); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // print to log file aoqi@0: if (has_log_file()) { aoqi@0: int nl0 = _newlines; aoqi@0: xmlTextStream::write(s, len); aoqi@0: // flush the log file too, if there were any newlines aoqi@0: if (nl0 != _newlines){ aoqi@0: flush(); aoqi@0: } aoqi@0: } else { aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: release(holder); aoqi@0: } aoqi@0: aoqi@0: intx ttyLocker::hold_tty() { aoqi@0: if (defaultStream::instance == NULL) return defaultStream::NO_WRITER; aoqi@0: intx thread_id = os::current_thread_id(); aoqi@0: return defaultStream::instance->hold(thread_id); aoqi@0: } aoqi@0: aoqi@0: void ttyLocker::release_tty(intx holder) { aoqi@0: if (holder == defaultStream::NO_WRITER) return; aoqi@0: defaultStream::instance->release(holder); aoqi@0: } aoqi@0: aoqi@0: bool ttyLocker::release_tty_if_locked() { aoqi@0: intx thread_id = os::current_thread_id(); aoqi@0: if (defaultStream::instance->writer() == thread_id) { aoqi@0: // release the lock and return true so callers know if was aoqi@0: // previously held. aoqi@0: release_tty(thread_id); aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void ttyLocker::break_tty_lock_for_safepoint(intx holder) { aoqi@0: if (defaultStream::instance != NULL && aoqi@0: defaultStream::instance->writer() == holder) { aoqi@0: if (xtty != NULL) { aoqi@0: xtty->print_cr(""); aoqi@0: } aoqi@0: defaultStream::instance->release(holder); aoqi@0: } aoqi@0: // (else there was no lock to break) aoqi@0: } aoqi@0: aoqi@0: void ostream_init() { aoqi@0: if (defaultStream::instance == NULL) { aoqi@0: defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream(); aoqi@0: tty = defaultStream::instance; aoqi@0: aoqi@0: // We want to ensure that time stamps in GC logs consider time 0 aoqi@0: // the time when the JVM is initialized, not the first time we ask aoqi@0: // for a time stamp. So, here, we explicitly update the time stamp aoqi@0: // of tty. aoqi@0: tty->time_stamp().update_to(1); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ostream_init_log() { aoqi@0: // For -Xloggc: option - called in runtime/thread.cpp aoqi@0: // Note : this must be called AFTER ostream_init() aoqi@0: aoqi@0: gclog_or_tty = tty; // default to tty aoqi@0: if (Arguments::gc_log_filename() != NULL) { aoqi@0: fileStream * gclog = new(ResourceObj::C_HEAP, mtInternal) aoqi@0: gcLogFileStream(Arguments::gc_log_filename()); aoqi@0: if (gclog->is_open()) { aoqi@0: // now we update the time stamp of the GC log to be synced up aoqi@0: // with tty. aoqi@0: gclog->time_stamp().update_to(tty->time_stamp().ticks()); aoqi@0: } aoqi@0: gclog_or_tty = gclog; aoqi@0: } aoqi@0: aoqi@0: // If we haven't lazily initialized the logfile yet, do it now, aoqi@0: // to avoid the possibility of lazy initialization during a VM aoqi@0: // crash, which can affect the stability of the fatal error handler. aoqi@0: defaultStream::instance->has_log_file(); aoqi@0: } aoqi@0: aoqi@0: // ostream_exit() is called during normal VM exit to finish log files, flush aoqi@0: // output and free resource. aoqi@0: void ostream_exit() { aoqi@0: static bool ostream_exit_called = false; aoqi@0: if (ostream_exit_called) return; aoqi@0: ostream_exit_called = true; aoqi@0: if (gclog_or_tty != tty) { aoqi@0: delete gclog_or_tty; aoqi@0: } aoqi@0: { aoqi@0: // we temporaly disable PrintMallocFree here aoqi@0: // as otherwise it'll lead to using of almost deleted aoqi@0: // tty or defaultStream::instance in logging facility aoqi@0: // of HeapFree(), see 6391258 aoqi@0: DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);) aoqi@0: if (tty != defaultStream::instance) { aoqi@0: delete tty; aoqi@0: } aoqi@0: if (defaultStream::instance != NULL) { aoqi@0: delete defaultStream::instance; aoqi@0: } aoqi@0: } aoqi@0: tty = NULL; aoqi@0: xtty = NULL; aoqi@0: gclog_or_tty = NULL; aoqi@0: defaultStream::instance = NULL; aoqi@0: } aoqi@0: aoqi@0: // ostream_abort() is called by os::abort() when VM is about to die. aoqi@0: void ostream_abort() { aoqi@0: // Here we can't delete gclog_or_tty and tty, just flush their output aoqi@0: if (gclog_or_tty) gclog_or_tty->flush(); aoqi@0: if (tty) tty->flush(); aoqi@0: aoqi@0: if (defaultStream::instance != NULL) { aoqi@0: static char buf[4096]; aoqi@0: defaultStream::instance->finish_log_on_error(buf, sizeof(buf)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: staticBufferStream::staticBufferStream(char* buffer, size_t buflen, aoqi@0: outputStream *outer_stream) { aoqi@0: _buffer = buffer; aoqi@0: _buflen = buflen; aoqi@0: _outer_stream = outer_stream; aoqi@0: // compile task prints time stamp relative to VM start aoqi@0: _stamp.update_to(1); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::write(const char* c, size_t len) { aoqi@0: _outer_stream->print_raw(c, (int)len); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::flush() { aoqi@0: _outer_stream->flush(); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::print(const char* format, ...) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len); aoqi@0: write(str, len); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::print_cr(const char* format, ...) { aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len); aoqi@0: write(str, len); aoqi@0: va_end(ap); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::vprint(const char *format, va_list argptr) { aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len); aoqi@0: write(str, len); aoqi@0: } aoqi@0: aoqi@0: void staticBufferStream::vprint_cr(const char* format, va_list argptr) { aoqi@0: size_t len; aoqi@0: const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len); aoqi@0: write(str, len); aoqi@0: } aoqi@0: aoqi@0: bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() { aoqi@0: buffer_length = initial_size; aoqi@0: buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal); aoqi@0: buffer_pos = 0; aoqi@0: buffer_fixed = false; aoqi@0: buffer_max = bufmax; aoqi@0: } aoqi@0: aoqi@0: bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() { aoqi@0: buffer_length = fixed_buffer_size; aoqi@0: buffer = fixed_buffer; aoqi@0: buffer_pos = 0; aoqi@0: buffer_fixed = true; aoqi@0: buffer_max = bufmax; aoqi@0: } aoqi@0: aoqi@0: void bufferedStream::write(const char* s, size_t len) { aoqi@0: aoqi@0: if(buffer_pos + len > buffer_max) { aoqi@0: flush(); aoqi@0: } aoqi@0: aoqi@0: size_t end = buffer_pos + len; aoqi@0: if (end >= buffer_length) { aoqi@0: if (buffer_fixed) { aoqi@0: // if buffer cannot resize, silently truncate aoqi@0: len = buffer_length - buffer_pos - 1; aoqi@0: } else { aoqi@0: // For small overruns, double the buffer. For larger ones, aoqi@0: // increase to the requested size. aoqi@0: if (end < buffer_length * 2) { aoqi@0: end = buffer_length * 2; aoqi@0: } aoqi@0: buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal); aoqi@0: buffer_length = end; aoqi@0: } aoqi@0: } aoqi@0: memcpy(buffer + buffer_pos, s, len); aoqi@0: buffer_pos += len; aoqi@0: update_position(s, len); aoqi@0: } aoqi@0: aoqi@0: char* bufferedStream::as_string() { aoqi@0: char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); aoqi@0: strncpy(copy, buffer, buffer_pos); aoqi@0: copy[buffer_pos] = 0; // terminating null aoqi@0: return copy; aoqi@0: } aoqi@0: aoqi@0: bufferedStream::~bufferedStream() { aoqi@0: if (!buffer_fixed) { aoqi@0: FREE_C_HEAP_ARRAY(char, buffer, mtInternal); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: #if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE) aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: #include aoqi@0: #endif aoqi@0: aoqi@0: // Network access aoqi@0: networkStream::networkStream() : bufferedStream(1024*10, 1024*10) { aoqi@0: aoqi@0: _socket = -1; aoqi@0: aoqi@0: int result = os::socket(AF_INET, SOCK_STREAM, 0); aoqi@0: if (result <= 0) { aoqi@0: assert(false, "Socket could not be created!"); aoqi@0: } else { aoqi@0: _socket = result; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int networkStream::read(char *buf, size_t len) { aoqi@0: return os::recv(_socket, buf, (int)len, 0); aoqi@0: } aoqi@0: aoqi@0: void networkStream::flush() { aoqi@0: if (size() != 0) { aoqi@0: int result = os::raw_send(_socket, (char *)base(), size(), 0); aoqi@0: assert(result != -1, "connection error"); aoqi@0: assert(result == (int)size(), "didn't send enough data"); aoqi@0: } aoqi@0: reset(); aoqi@0: } aoqi@0: aoqi@0: networkStream::~networkStream() { aoqi@0: close(); aoqi@0: } aoqi@0: aoqi@0: void networkStream::close() { aoqi@0: if (_socket != -1) { aoqi@0: flush(); aoqi@0: os::socket_close(_socket); aoqi@0: _socket = -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool networkStream::connect(const char *ip, short port) { aoqi@0: aoqi@0: struct sockaddr_in server; aoqi@0: server.sin_family = AF_INET; aoqi@0: server.sin_port = htons(port); aoqi@0: aoqi@0: server.sin_addr.s_addr = inet_addr(ip); aoqi@0: if (server.sin_addr.s_addr == (uint32_t)-1) { aoqi@0: struct hostent* host = os::get_host_by_name((char*)ip); aoqi@0: if (host != NULL) { aoqi@0: memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length); aoqi@0: } else { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in)); aoqi@0: return (result >= 0); aoqi@0: } aoqi@0: aoqi@0: #endif