0N/A/*
4272N/A * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "compiler/compileLog.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/arguments.hpp"
1879N/A#include "utilities/defaultStream.hpp"
1879N/A#include "utilities/ostream.hpp"
1879N/A#include "utilities/top.hpp"
1879N/A#include "utilities/xmlstream.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "os_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "os_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "os_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "os_bsd.inline.hpp"
2796N/A#endif
0N/A
0N/Aextern "C" void jio_print(const char* s); // Declarationtion of jvm method
0N/A
0N/AoutputStream::outputStream(int width) {
0N/A _width = width;
0N/A _position = 0;
0N/A _newlines = 0;
0N/A _precount = 0;
0N/A _indentation = 0;
0N/A}
0N/A
0N/AoutputStream::outputStream(int width, bool has_time_stamps) {
0N/A _width = width;
0N/A _position = 0;
0N/A _newlines = 0;
0N/A _precount = 0;
0N/A _indentation = 0;
0N/A if (has_time_stamps) _stamp.update();
0N/A}
0N/A
0N/Avoid outputStream::update_position(const char* s, size_t len) {
0N/A for (size_t i = 0; i < len; i++) {
0N/A char ch = s[i];
0N/A if (ch == '\n') {
0N/A _newlines += 1;
0N/A _precount += _position + 1;
0N/A _position = 0;
0N/A } else if (ch == '\t') {
100N/A int tw = 8 - (_position & 7);
100N/A _position += tw;
100N/A _precount -= tw-1; // invariant: _precount + _position == total count
0N/A } else {
0N/A _position += 1;
0N/A }
0N/A }
0N/A}
0N/A
0N/A// Execute a vsprintf, using the given buffer if necessary.
0N/A// Return a pointer to the formatted string.
0N/Aconst char* outputStream::do_vsnprintf(char* buffer, size_t buflen,
0N/A const char* format, va_list ap,
0N/A bool add_cr,
0N/A size_t& result_len) {
0N/A const char* result;
0N/A if (add_cr) buflen--;
0N/A if (!strchr(format, '%')) {
0N/A // constant format string
0N/A result = format;
0N/A result_len = strlen(result);
0N/A if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate
0N/A } else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') {
0N/A // trivial copy-through format string
0N/A result = va_arg(ap, const char*);
0N/A result_len = strlen(result);
0N/A if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate
0N/A } else if (vsnprintf(buffer, buflen, format, ap) >= 0) {
0N/A result = buffer;
0N/A result_len = strlen(result);
0N/A } else {
0N/A DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");)
0N/A result = buffer;
0N/A result_len = buflen - 1;
0N/A buffer[result_len] = 0;
0N/A }
0N/A if (add_cr) {
0N/A if (result != buffer) {
0N/A strncpy(buffer, result, buflen);
0N/A result = buffer;
0N/A }
0N/A buffer[result_len++] = '\n';
0N/A buffer[result_len] = 0;
0N/A }
0N/A return result;
0N/A}
0N/A
0N/Avoid outputStream::print(const char* format, ...) {
0N/A char buffer[O_BUFLEN];
0N/A va_list ap;
0N/A va_start(ap, format);
0N/A size_t len;
0N/A const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len);
0N/A write(str, len);
0N/A va_end(ap);
0N/A}
0N/A
0N/Avoid outputStream::print_cr(const char* format, ...) {
0N/A char buffer[O_BUFLEN];
0N/A va_list ap;
0N/A va_start(ap, format);
0N/A size_t len;
0N/A const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len);
0N/A write(str, len);
0N/A va_end(ap);
0N/A}
0N/A
0N/Avoid outputStream::vprint(const char *format, va_list argptr) {
0N/A char buffer[O_BUFLEN];
0N/A size_t len;
0N/A const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len);
0N/A write(str, len);
0N/A}
0N/A
0N/Avoid outputStream::vprint_cr(const char* format, va_list argptr) {
0N/A char buffer[O_BUFLEN];
0N/A size_t len;
0N/A const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len);
0N/A write(str, len);
0N/A}
0N/A
0N/Avoid outputStream::fill_to(int col) {
100N/A int need_fill = col - position();
100N/A sp(need_fill);
100N/A}
100N/A
100N/Avoid outputStream::move_to(int col, int slop, int min_space) {
100N/A if (position() >= col + slop)
100N/A cr();
100N/A int need_fill = col - position();
100N/A if (need_fill < min_space)
100N/A need_fill = min_space;
100N/A sp(need_fill);
0N/A}
0N/A
0N/Avoid outputStream::put(char ch) {
0N/A assert(ch != 0, "please fix call site");
0N/A char buf[] = { ch, '\0' };
0N/A write(buf, 1);
0N/A}
0N/A
100N/A#define SP_USE_TABS false
100N/A
100N/Avoid outputStream::sp(int count) {
100N/A if (count < 0) return;
100N/A if (SP_USE_TABS && count >= 8) {
100N/A int target = position() + count;
100N/A while (count >= 8) {
100N/A this->write("\t", 1);
100N/A count -= 8;
100N/A }
100N/A count = target - position();
100N/A }
100N/A while (count > 0) {
100N/A int nw = (count > 8) ? 8 : count;
100N/A this->write(" ", nw);
100N/A count -= nw;
100N/A }
0N/A}
0N/A
0N/Avoid outputStream::cr() {
0N/A this->write("\n", 1);
0N/A}
0N/A
0N/Avoid outputStream::stamp() {
0N/A if (! _stamp.is_updated()) {
0N/A _stamp.update(); // start at 0 on first call to stamp()
0N/A }
0N/A
0N/A // outputStream::stamp() may get called by ostream_abort(), use snprintf
0N/A // to avoid allocating large stack buffer in print().
0N/A char buf[40];
0N/A jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds());
0N/A print_raw(buf);
0N/A}
0N/A
342N/Avoid outputStream::stamp(bool guard,
342N/A const char* prefix,
342N/A const char* suffix) {
342N/A if (!guard) {
342N/A return;
342N/A }
342N/A print_raw(prefix);
342N/A stamp();
342N/A print_raw(suffix);
342N/A}
342N/A
0N/Avoid outputStream::date_stamp(bool guard,
0N/A const char* prefix,
0N/A const char* suffix) {
0N/A if (!guard) {
0N/A return;
0N/A }
0N/A print_raw(prefix);
0N/A static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz";
0N/A static const int buffer_length = 32;
0N/A char buffer[buffer_length];
0N/A const char* iso8601_result = os::iso8601_time(buffer, buffer_length);
0N/A if (iso8601_result != NULL) {
0N/A print_raw(buffer);
0N/A } else {
0N/A print_raw(error_time);
0N/A }
0N/A print_raw(suffix);
0N/A return;
0N/A}
0N/A
3955N/AoutputStream& outputStream::indent() {
0N/A while (_position < _indentation) sp();
3955N/A return *this;
0N/A}
0N/A
0N/Avoid outputStream::print_jlong(jlong value) {
0N/A // N.B. Same as INT64_FORMAT
0N/A print(os::jlong_format_specifier(), value);
0N/A}
0N/A
0N/Avoid outputStream::print_julong(julong value) {
0N/A // N.B. Same as UINT64_FORMAT
0N/A print(os::julong_format_specifier(), value);
0N/A}
0N/A
3955N/A/**
3955N/A * This prints out hex data in a 'windbg' or 'xxd' form, where each line is:
3955N/A * <hex-address>: 8 * <hex-halfword> <ascii translation (optional)>
3955N/A * example:
3955N/A * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............
3955N/A * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... ....
3955N/A * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......]
3955N/A * ...
3955N/A *
3955N/A * indent is applied to each line. Ends with a CR.
3955N/A */
3955N/Avoid outputStream::print_data(void* data, size_t len, bool with_ascii) {
3955N/A size_t limit = (len + 16) / 16 * 16;
3955N/A for (size_t i = 0; i < limit; ++i) {
3955N/A if (i % 16 == 0) {
3955N/A indent().print("%07x:", i);
3955N/A }
3955N/A if (i % 2 == 0) {
3955N/A print(" ");
3955N/A }
3955N/A if (i < len) {
3955N/A print("%02x", ((unsigned char*)data)[i]);
3955N/A } else {
3955N/A print(" ");
3955N/A }
3955N/A if ((i + 1) % 16 == 0) {
3955N/A if (with_ascii) {
3955N/A print(" ");
3955N/A for (size_t j = 0; j < 16; ++j) {
3955N/A size_t idx = i + j - 15;
3955N/A if (idx < len) {
3955N/A char c = ((char*)data)[idx];
3955N/A print("%c", c >= 32 && c <= 126 ? c : '.');
3955N/A }
3955N/A }
3955N/A }
3955N/A print_cr("");
3955N/A }
3955N/A }
3955N/A}
3955N/A
0N/AstringStream::stringStream(size_t initial_size) : outputStream() {
0N/A buffer_length = initial_size;
0N/A buffer = NEW_RESOURCE_ARRAY(char, buffer_length);
0N/A buffer_pos = 0;
0N/A buffer_fixed = false;
0N/A}
0N/A
0N/A// useful for output to fixed chunks of memory, such as performance counters
0N/AstringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
0N/A buffer_length = fixed_buffer_size;
0N/A buffer = fixed_buffer;
0N/A buffer_pos = 0;
0N/A buffer_fixed = true;
0N/A}
0N/A
0N/Avoid stringStream::write(const char* s, size_t len) {
0N/A size_t write_len = len; // number of non-null bytes to write
0N/A size_t end = buffer_pos + len + 1; // position after write and final '\0'
0N/A if (end > buffer_length) {
0N/A if (buffer_fixed) {
0N/A // if buffer cannot resize, silently truncate
0N/A end = buffer_length;
0N/A write_len = end - buffer_pos - 1; // leave room for the final '\0'
0N/A } else {
0N/A // For small overruns, double the buffer. For larger ones,
0N/A // increase to the requested size.
0N/A if (end < buffer_length * 2) {
0N/A end = buffer_length * 2;
0N/A }
0N/A char* oldbuf = buffer;
0N/A buffer = NEW_RESOURCE_ARRAY(char, end);
0N/A strncpy(buffer, oldbuf, buffer_pos);
0N/A buffer_length = end;
0N/A }
0N/A }
0N/A // invariant: buffer is always null-terminated
0N/A guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
0N/A buffer[buffer_pos + write_len] = 0;
0N/A strncpy(buffer + buffer_pos, s, write_len);
0N/A buffer_pos += write_len;
0N/A
0N/A // Note that the following does not depend on write_len.
0N/A // This means that position and count get updated
0N/A // even when overflow occurs.
0N/A update_position(s, len);
0N/A}
0N/A
0N/Achar* stringStream::as_string() {
0N/A char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1);
0N/A strncpy(copy, buffer, buffer_pos);
0N/A copy[buffer_pos] = 0; // terminating null
0N/A return copy;
0N/A}
0N/A
0N/AstringStream::~stringStream() {}
0N/A
0N/AxmlStream* xtty;
0N/AoutputStream* tty;
0N/AoutputStream* gclog_or_tty;
0N/Aextern Mutex* tty_lock;
0N/A
0N/AfileStream::fileStream(const char* file_name) {
0N/A _file = fopen(file_name, "w");
0N/A _need_close = true;
0N/A}
0N/A
2080N/AfileStream::fileStream(const char* file_name, const char* opentype) {
2080N/A _file = fopen(file_name, opentype);
2080N/A _need_close = true;
2080N/A}
2080N/A
0N/Avoid fileStream::write(const char* s, size_t len) {
513N/A if (_file != NULL) {
513N/A // Make an unused local variable to avoid warning from gcc 4.x compiler.
513N/A size_t count = fwrite(s, 1, len, _file);
513N/A }
0N/A update_position(s, len);
0N/A}
0N/A
2080N/Along fileStream::fileSize() {
2080N/A long size = -1;
2080N/A if (_file != NULL) {
2080N/A long pos = ::ftell(_file);
2080N/A if (::fseek(_file, 0, SEEK_END) == 0) {
2080N/A size = ::ftell(_file);
2080N/A }
2080N/A ::fseek(_file, pos, SEEK_SET);
2080N/A }
2080N/A return size;
2080N/A}
2080N/A
2080N/Achar* fileStream::readln(char *data, int count ) {
2080N/A char * ret = ::fgets(data, count, _file);
2080N/A //Get rid of annoying \n char
2080N/A data[::strlen(data)-1] = '\0';
2080N/A return ret;
2080N/A}
2080N/A
0N/AfileStream::~fileStream() {
0N/A if (_file != NULL) {
0N/A if (_need_close) fclose(_file);
2592N/A _file = NULL;
0N/A }
0N/A}
0N/A
0N/Avoid fileStream::flush() {
0N/A fflush(_file);
0N/A}
0N/A
0N/AfdStream::fdStream(const char* file_name) {
0N/A _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
0N/A _need_close = true;
0N/A}
0N/A
0N/AfdStream::~fdStream() {
0N/A if (_fd != -1) {
0N/A if (_need_close) close(_fd);
0N/A _fd = -1;
0N/A }
0N/A}
0N/A
0N/Avoid fdStream::write(const char* s, size_t len) {
513N/A if (_fd != -1) {
513N/A // Make an unused local variable to avoid warning from gcc 4.x compiler.
513N/A size_t count = ::write(_fd, s, (int)len);
513N/A }
0N/A update_position(s, len);
0N/A}
0N/A
2592N/ArotatingFileStream::~rotatingFileStream() {
2592N/A if (_file != NULL) {
2592N/A if (_need_close) fclose(_file);
2592N/A _file = NULL;
3863N/A FREE_C_HEAP_ARRAY(char, _file_name, mtInternal);
2592N/A _file_name = NULL;
2592N/A }
2592N/A}
2592N/A
2592N/ArotatingFileStream::rotatingFileStream(const char* file_name) {
2592N/A _cur_file_num = 0;
4272N/A _bytes_written = 0L;
3863N/A _file_name = NEW_C_HEAP_ARRAY(char, strlen(file_name)+10, mtInternal);
2592N/A jio_snprintf(_file_name, strlen(file_name)+10, "%s.%d", file_name, _cur_file_num);
2592N/A _file = fopen(_file_name, "w");
2592N/A _need_close = true;
2592N/A}
2592N/A
2592N/ArotatingFileStream::rotatingFileStream(const char* file_name, const char* opentype) {
2592N/A _cur_file_num = 0;
4272N/A _bytes_written = 0L;
3863N/A _file_name = NEW_C_HEAP_ARRAY(char, strlen(file_name)+10, mtInternal);
2592N/A jio_snprintf(_file_name, strlen(file_name)+10, "%s.%d", file_name, _cur_file_num);
2592N/A _file = fopen(_file_name, opentype);
2592N/A _need_close = true;
2592N/A}
2592N/A
2592N/Avoid rotatingFileStream::write(const char* s, size_t len) {
4272N/A if (_file != NULL) {
2592N/A size_t count = fwrite(s, 1, len, _file);
4272N/A _bytes_written += count;
2592N/A }
2592N/A update_position(s, len);
2592N/A}
2592N/A
2592N/A// rotate_log must be called from VMThread at safepoint. In case need change parameters
2592N/A// for gc log rotation from thread other than VMThread, a sub type of VM_Operation
2592N/A// should be created and be submitted to VMThread's operation queue. DO NOT call this
2592N/A// function directly. Currently, it is safe to rotate log at safepoint through VMThread.
2592N/A// That is, no mutator threads and concurrent GC threads run parallel with VMThread to
2592N/A// write to gc log file at safepoint. If in future, changes made for mutator threads or
2592N/A// concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
2592N/A// must be synchronized.
2592N/Avoid rotatingFileStream::rotate_log() {
4272N/A if (_bytes_written < (jlong)GCLogFileSize) {
4272N/A return;
4272N/A }
4272N/A
2592N/A#ifdef ASSERT
2592N/A Thread *thread = Thread::current();
2592N/A assert(thread == NULL ||
2592N/A (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
2592N/A "Must be VMThread at safepoint");
2592N/A#endif
2592N/A if (NumberOfGCLogFiles == 1) {
2592N/A // rotate in same file
2592N/A rewind();
4272N/A _bytes_written = 0L;
2592N/A return;
2592N/A }
2592N/A
2592N/A // rotate file in names file.0, file.1, file.2, ..., file.<MaxGCLogFileNumbers-1>
2592N/A // close current file, rotate to next file
2592N/A if (_file != NULL) {
2592N/A _cur_file_num ++;
2592N/A if (_cur_file_num >= NumberOfGCLogFiles) _cur_file_num = 0;
2592N/A jio_snprintf(_file_name, strlen(Arguments::gc_log_filename()) + 10, "%s.%d",
2592N/A Arguments::gc_log_filename(), _cur_file_num);
2592N/A fclose(_file);
2592N/A _file = NULL;
2592N/A }
2592N/A _file = fopen(_file_name, "w");
2592N/A if (_file != NULL) {
4272N/A _bytes_written = 0L;
2592N/A _need_close = true;
2592N/A } else {
2592N/A tty->print_cr("failed to open rotation log file %s due to %s\n",
2592N/A _file_name, strerror(errno));
2592N/A _need_close = false;
2592N/A }
2592N/A}
2592N/A
0N/AdefaultStream* defaultStream::instance = NULL;
0N/Aint defaultStream::_output_fd = 1;
0N/Aint defaultStream::_error_fd = 2;
0N/AFILE* defaultStream::_output_stream = stdout;
0N/AFILE* defaultStream::_error_stream = stderr;
0N/A
0N/A#define LOG_MAJOR_VERSION 160
0N/A#define LOG_MINOR_VERSION 1
0N/A
0N/Avoid defaultStream::init() {
0N/A _inited = true;
0N/A if (LogVMOutput || LogCompilation) {
0N/A init_log();
0N/A }
0N/A}
0N/A
0N/Abool defaultStream::has_log_file() {
0N/A // lazily create log file (at startup, LogVMOutput is false even
0N/A // if +LogVMOutput is used, because the flags haven't been parsed yet)
0N/A // For safer printing during fatal error handling, do not init logfile
0N/A // if a VM error has been reported.
0N/A if (!_inited && !is_error_reported()) init();
0N/A return _log_file != NULL;
0N/A}
0N/A
1353N/Astatic const char* make_log_name(const char* log_name, const char* force_directory) {
0N/A const char* basename = log_name;
0N/A char file_sep = os::file_separator()[0];
0N/A const char* cp;
0N/A for (cp = log_name; *cp != '\0'; cp++) {
0N/A if (*cp == '/' || *cp == file_sep) {
0N/A basename = cp+1;
0N/A }
0N/A }
0N/A const char* nametail = log_name;
0N/A
1353N/A // Compute buffer length
1353N/A size_t buffer_length;
1353N/A if (force_directory != NULL) {
1353N/A buffer_length = strlen(force_directory) + strlen(os::file_separator()) +
1353N/A strlen(basename) + 1;
1353N/A } else {
1353N/A buffer_length = strlen(log_name) + 1;
1353N/A }
1353N/A
1353N/A const char* star = strchr(basename, '*');
1353N/A int star_pos = (star == NULL) ? -1 : (star - nametail);
2247N/A int skip = 1;
2247N/A if (star == NULL) {
2247N/A // Try %p
2247N/A star = strstr(basename, "%p");
2247N/A if (star != NULL) {
2247N/A skip = 2;
2247N/A }
2247N/A }
2247N/A star_pos = (star == NULL) ? -1 : (star - nametail);
1353N/A
1353N/A char pid[32];
1353N/A if (star_pos >= 0) {
1353N/A jio_snprintf(pid, sizeof(pid), "%u", os::current_process_id());
1353N/A buffer_length += strlen(pid);
1353N/A }
1353N/A
1353N/A // Create big enough buffer.
3863N/A char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
1353N/A
0N/A strcpy(buf, "");
0N/A if (force_directory != NULL) {
0N/A strcat(buf, force_directory);
0N/A strcat(buf, os::file_separator());
0N/A nametail = basename; // completely skip directory prefix
0N/A }
0N/A
0N/A if (star_pos >= 0) {
2247N/A // convert foo*bar.log or foo%pbar.log to foo123bar.log
0N/A int buf_pos = (int) strlen(buf);
0N/A strncpy(&buf[buf_pos], nametail, star_pos);
1353N/A strcpy(&buf[buf_pos + star_pos], pid);
2247N/A nametail += star_pos + skip; // skip prefix and pid format
0N/A }
0N/A
0N/A strcat(buf, nametail); // append rest of name, or all of name
0N/A return buf;
0N/A}
0N/A
0N/Avoid defaultStream::init_log() {
0N/A // %%% Need a MutexLocker?
0N/A const char* log_name = LogFile != NULL ? LogFile : "hotspot.log";
1353N/A const char* try_name = make_log_name(log_name, NULL);
3863N/A fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
0N/A if (!file->is_open()) {
0N/A // Try again to open the file.
0N/A char warnbuf[O_BUFLEN*2];
1353N/A jio_snprintf(warnbuf, sizeof(warnbuf),
1353N/A "Warning: Cannot open log file: %s\n", try_name);
0N/A // Note: This feature is for maintainer use only. No need for L10N.
0N/A jio_print(warnbuf);
3863N/A FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
2247N/A try_name = make_log_name("hs_pid%p.log", os::get_temp_directory());
1353N/A jio_snprintf(warnbuf, sizeof(warnbuf),
1353N/A "Warning: Forcing option -XX:LogFile=%s\n", try_name);
0N/A jio_print(warnbuf);
0N/A delete file;
3863N/A file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
3863N/A FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
0N/A }
0N/A if (file->is_open()) {
0N/A _log_file = file;
3863N/A xmlStream* xs = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file);
0N/A _outer_xmlStream = xs;
0N/A if (this == tty) xtty = xs;
0N/A // Write XML header.
0N/A xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>");
0N/A // (For now, don't bother to issue a DTD for this private format.)
0N/A jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds();
0N/A // %%% Should be: jlong time_ms = os::start_time_milliseconds(), if
0N/A // we ever get round to introduce that method on the os class
0N/A xs->head("hotspot_log version='%d %d'"
0N/A " process='%d' time_ms='"INT64_FORMAT"'",
0N/A LOG_MAJOR_VERSION, LOG_MINOR_VERSION,
0N/A os::current_process_id(), time_ms);
0N/A // Write VM version header immediately.
0N/A xs->head("vm_version");
0N/A xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr();
0N/A xs->tail("name");
0N/A xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr();
0N/A xs->tail("release");
0N/A xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr();
0N/A xs->tail("info");
0N/A xs->tail("vm_version");
0N/A // Record information about the command-line invocation.
0N/A xs->head("vm_arguments"); // Cf. Arguments::print_on()
0N/A if (Arguments::num_jvm_flags() > 0) {
0N/A xs->head("flags");
0N/A Arguments::print_jvm_flags_on(xs->text());
0N/A xs->tail("flags");
0N/A }
0N/A if (Arguments::num_jvm_args() > 0) {
0N/A xs->head("args");
0N/A Arguments::print_jvm_args_on(xs->text());
0N/A xs->tail("args");
0N/A }
0N/A if (Arguments::java_command() != NULL) {
0N/A xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command());
0N/A xs->tail("command");
0N/A }
0N/A if (Arguments::sun_java_launcher() != NULL) {
0N/A xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher());
0N/A xs->tail("launcher");
0N/A }
0N/A if (Arguments::system_properties() != NULL) {
0N/A xs->head("properties");
0N/A // Print it as a java-style property list.
0N/A // System properties don't generally contain newlines, so don't bother with unparsing.
0N/A for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
0N/A xs->text()->print_cr("%s=%s", p->key(), p->value());
0N/A }
0N/A xs->tail("properties");
0N/A }
0N/A xs->tail("vm_arguments");
0N/A // tty output per se is grouped under the <tty>...</tty> element.
0N/A xs->head("tty");
0N/A // All further non-markup text gets copied to the tty:
0N/A xs->_text = this; // requires friend declaration!
0N/A } else {
0N/A delete(file);
0N/A // and leave xtty as NULL
0N/A LogVMOutput = false;
0N/A DisplayVMOutput = true;
0N/A LogCompilation = false;
0N/A }
0N/A}
0N/A
0N/A// finish_log() is called during normal VM shutdown. finish_log_on_error() is
0N/A// called by ostream_abort() after a fatal error.
0N/A//
0N/Avoid defaultStream::finish_log() {
0N/A xmlStream* xs = _outer_xmlStream;
0N/A xs->done("tty");
0N/A
0N/A // Other log forks are appended here, at the End of Time:
0N/A CompileLog::finish_log(xs->out()); // write compile logging, if any, now
0N/A
0N/A xs->done("hotspot_log");
0N/A xs->flush();
0N/A
0N/A fileStream* file = _log_file;
0N/A _log_file = NULL;
0N/A
0N/A delete _outer_xmlStream;
0N/A _outer_xmlStream = NULL;
0N/A
0N/A file->flush();
0N/A delete file;
0N/A}
0N/A
0N/Avoid defaultStream::finish_log_on_error(char *buf, int buflen) {
0N/A xmlStream* xs = _outer_xmlStream;
0N/A
0N/A if (xs && xs->out()) {
0N/A
0N/A xs->done_raw("tty");
0N/A
0N/A // Other log forks are appended here, at the End of Time:
0N/A CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now
0N/A
0N/A xs->done_raw("hotspot_log");
0N/A xs->flush();
0N/A
0N/A fileStream* file = _log_file;
0N/A _log_file = NULL;
0N/A _outer_xmlStream = NULL;
0N/A
0N/A if (file) {
0N/A file->flush();
0N/A
0N/A // Can't delete or close the file because delete and fclose aren't
0N/A // async-safe. We are about to die, so leave it to the kernel.
0N/A // delete file;
0N/A }
0N/A }
0N/A}
0N/A
0N/Aintx defaultStream::hold(intx writer_id) {
0N/A bool has_log = has_log_file(); // check before locking
0N/A if (// impossible, but who knows?
0N/A writer_id == NO_WRITER ||
0N/A
0N/A // bootstrap problem
0N/A tty_lock == NULL ||
0N/A
0N/A // can't grab a lock or call Thread::current() if TLS isn't initialized
0N/A ThreadLocalStorage::thread() == NULL ||
0N/A
0N/A // developer hook
0N/A !SerializeVMOutput ||
0N/A
0N/A // VM already unhealthy
0N/A is_error_reported() ||
0N/A
0N/A // safepoint == global lock (for VM only)
0N/A (SafepointSynchronize::is_synchronizing() &&
0N/A Thread::current()->is_VM_thread())
0N/A ) {
0N/A // do not attempt to lock unless we know the thread and the VM is healthy
0N/A return NO_WRITER;
0N/A }
0N/A if (_writer == writer_id) {
0N/A // already held, no need to re-grab the lock
0N/A return NO_WRITER;
0N/A }
0N/A tty_lock->lock_without_safepoint_check();
0N/A // got the lock
0N/A if (writer_id != _last_writer) {
0N/A if (has_log) {
0N/A _log_file->bol();
0N/A // output a hint where this output is coming from:
4027N/A _log_file->print_cr("<writer thread='" UINTX_FORMAT "'/>", writer_id);
0N/A }
0N/A _last_writer = writer_id;
0N/A }
0N/A _writer = writer_id;
0N/A return writer_id;
0N/A}
0N/A
0N/Avoid defaultStream::release(intx holder) {
0N/A if (holder == NO_WRITER) {
0N/A // nothing to release: either a recursive lock, or we scribbled (too bad)
0N/A return;
0N/A }
0N/A if (_writer != holder) {
0N/A return; // already unlocked, perhaps via break_tty_lock_for_safepoint
0N/A }
0N/A _writer = NO_WRITER;
0N/A tty_lock->unlock();
0N/A}
0N/A
0N/A
0N/A// Yuck: jio_print does not accept char*/len.
0N/Astatic void call_jio_print(const char* s, size_t len) {
0N/A char buffer[O_BUFLEN+100];
0N/A if (len > sizeof(buffer)-1) {
0N/A warning("increase O_BUFLEN in ostream.cpp -- output truncated");
0N/A len = sizeof(buffer)-1;
0N/A }
0N/A strncpy(buffer, s, len);
0N/A buffer[len] = '\0';
0N/A jio_print(buffer);
0N/A}
0N/A
0N/A
0N/Avoid defaultStream::write(const char* s, size_t len) {
0N/A intx thread_id = os::current_thread_id();
0N/A intx holder = hold(thread_id);
0N/A
0N/A if (DisplayVMOutput &&
0N/A (_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) {
0N/A // print to output stream. It can be redirected by a vfprintf hook
0N/A if (s[len] == '\0') {
0N/A jio_print(s);
0N/A } else {
0N/A call_jio_print(s, len);
0N/A }
0N/A }
0N/A
0N/A // print to log file
0N/A if (has_log_file()) {
0N/A int nl0 = _newlines;
0N/A xmlTextStream::write(s, len);
0N/A // flush the log file too, if there were any newlines
0N/A if (nl0 != _newlines){
0N/A flush();
0N/A }
0N/A } else {
0N/A update_position(s, len);
0N/A }
0N/A
0N/A release(holder);
0N/A}
0N/A
0N/Aintx ttyLocker::hold_tty() {
0N/A if (defaultStream::instance == NULL) return defaultStream::NO_WRITER;
0N/A intx thread_id = os::current_thread_id();
0N/A return defaultStream::instance->hold(thread_id);
0N/A}
0N/A
0N/Avoid ttyLocker::release_tty(intx holder) {
0N/A if (holder == defaultStream::NO_WRITER) return;
0N/A defaultStream::instance->release(holder);
0N/A}
0N/A
2135N/Abool ttyLocker::release_tty_if_locked() {
2135N/A intx thread_id = os::current_thread_id();
2135N/A if (defaultStream::instance->writer() == thread_id) {
2135N/A // release the lock and return true so callers know if was
2135N/A // previously held.
2135N/A release_tty(thread_id);
2135N/A return true;
2135N/A }
2135N/A return false;
2135N/A}
2135N/A
0N/Avoid ttyLocker::break_tty_lock_for_safepoint(intx holder) {
0N/A if (defaultStream::instance != NULL &&
0N/A defaultStream::instance->writer() == holder) {
0N/A if (xtty != NULL) {
0N/A xtty->print_cr("<!-- safepoint while printing -->");
0N/A }
0N/A defaultStream::instance->release(holder);
0N/A }
0N/A // (else there was no lock to break)
0N/A}
0N/A
0N/Avoid ostream_init() {
0N/A if (defaultStream::instance == NULL) {
3863N/A defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream();
0N/A tty = defaultStream::instance;
0N/A
0N/A // We want to ensure that time stamps in GC logs consider time 0
0N/A // the time when the JVM is initialized, not the first time we ask
0N/A // for a time stamp. So, here, we explicitly update the time stamp
0N/A // of tty.
0N/A tty->time_stamp().update_to(1);
0N/A }
0N/A}
0N/A
0N/Avoid ostream_init_log() {
0N/A // For -Xloggc:<file> option - called in runtime/thread.cpp
0N/A // Note : this must be called AFTER ostream_init()
0N/A
0N/A gclog_or_tty = tty; // default to tty
0N/A if (Arguments::gc_log_filename() != NULL) {
2592N/A fileStream * gclog = UseGCLogFileRotation ?
3863N/A new(ResourceObj::C_HEAP, mtInternal)
2592N/A rotatingFileStream(Arguments::gc_log_filename()) :
3863N/A new(ResourceObj::C_HEAP, mtInternal)
2592N/A fileStream(Arguments::gc_log_filename());
0N/A if (gclog->is_open()) {
0N/A // now we update the time stamp of the GC log to be synced up
0N/A // with tty.
0N/A gclog->time_stamp().update_to(tty->time_stamp().ticks());
0N/A }
2592N/A gclog_or_tty = gclog;
0N/A }
0N/A
0N/A // If we haven't lazily initialized the logfile yet, do it now,
0N/A // to avoid the possibility of lazy initialization during a VM
0N/A // crash, which can affect the stability of the fatal error handler.
0N/A defaultStream::instance->has_log_file();
0N/A}
0N/A
0N/A// ostream_exit() is called during normal VM exit to finish log files, flush
0N/A// output and free resource.
0N/Avoid ostream_exit() {
0N/A static bool ostream_exit_called = false;
0N/A if (ostream_exit_called) return;
0N/A ostream_exit_called = true;
0N/A if (gclog_or_tty != tty) {
0N/A delete gclog_or_tty;
0N/A }
0N/A {
0N/A // we temporaly disable PrintMallocFree here
0N/A // as otherwise it'll lead to using of almost deleted
0N/A // tty or defaultStream::instance in logging facility
0N/A // of HeapFree(), see 6391258
0N/A DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);)
0N/A if (tty != defaultStream::instance) {
0N/A delete tty;
0N/A }
0N/A if (defaultStream::instance != NULL) {
0N/A delete defaultStream::instance;
0N/A }
0N/A }
0N/A tty = NULL;
0N/A xtty = NULL;
0N/A gclog_or_tty = NULL;
0N/A defaultStream::instance = NULL;
0N/A}
0N/A
0N/A// ostream_abort() is called by os::abort() when VM is about to die.
0N/Avoid ostream_abort() {
0N/A // Here we can't delete gclog_or_tty and tty, just flush their output
0N/A if (gclog_or_tty) gclog_or_tty->flush();
0N/A if (tty) tty->flush();
0N/A
0N/A if (defaultStream::instance != NULL) {
0N/A static char buf[4096];
0N/A defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
0N/A }
0N/A}
0N/A
0N/AstaticBufferStream::staticBufferStream(char* buffer, size_t buflen,
0N/A outputStream *outer_stream) {
0N/A _buffer = buffer;
0N/A _buflen = buflen;
0N/A _outer_stream = outer_stream;
2267N/A // compile task prints time stamp relative to VM start
2267N/A _stamp.update_to(1);
0N/A}
0N/A
0N/Avoid staticBufferStream::write(const char* c, size_t len) {
0N/A _outer_stream->print_raw(c, (int)len);
0N/A}
0N/A
0N/Avoid staticBufferStream::flush() {
0N/A _outer_stream->flush();
0N/A}
0N/A
0N/Avoid staticBufferStream::print(const char* format, ...) {
0N/A va_list ap;
0N/A va_start(ap, format);
0N/A size_t len;
0N/A const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len);
0N/A write(str, len);
0N/A va_end(ap);
0N/A}
0N/A
0N/Avoid staticBufferStream::print_cr(const char* format, ...) {
0N/A va_list ap;
0N/A va_start(ap, format);
0N/A size_t len;
0N/A const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len);
0N/A write(str, len);
0N/A va_end(ap);
0N/A}
0N/A
0N/Avoid staticBufferStream::vprint(const char *format, va_list argptr) {
0N/A size_t len;
0N/A const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len);
0N/A write(str, len);
0N/A}
0N/A
0N/Avoid staticBufferStream::vprint_cr(const char* format, va_list argptr) {
0N/A size_t len;
0N/A const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len);
0N/A write(str, len);
0N/A}
0N/A
222N/AbufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
0N/A buffer_length = initial_size;
3863N/A buffer = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
0N/A buffer_pos = 0;
0N/A buffer_fixed = false;
222N/A buffer_max = bufmax;
0N/A}
0N/A
222N/AbufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
0N/A buffer_length = fixed_buffer_size;
0N/A buffer = fixed_buffer;
0N/A buffer_pos = 0;
0N/A buffer_fixed = true;
222N/A buffer_max = bufmax;
0N/A}
0N/A
0N/Avoid bufferedStream::write(const char* s, size_t len) {
222N/A
222N/A if(buffer_pos + len > buffer_max) {
222N/A flush();
222N/A }
222N/A
0N/A size_t end = buffer_pos + len;
0N/A if (end >= buffer_length) {
0N/A if (buffer_fixed) {
0N/A // if buffer cannot resize, silently truncate
0N/A len = buffer_length - buffer_pos - 1;
0N/A } else {
0N/A // For small overruns, double the buffer. For larger ones,
0N/A // increase to the requested size.
0N/A if (end < buffer_length * 2) {
0N/A end = buffer_length * 2;
0N/A }
3863N/A buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
0N/A buffer_length = end;
0N/A }
0N/A }
0N/A memcpy(buffer + buffer_pos, s, len);
0N/A buffer_pos += len;
0N/A update_position(s, len);
0N/A}
0N/A
0N/Achar* bufferedStream::as_string() {
0N/A char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1);
0N/A strncpy(copy, buffer, buffer_pos);
0N/A copy[buffer_pos] = 0; // terminating null
0N/A return copy;
0N/A}
0N/A
0N/AbufferedStream::~bufferedStream() {
0N/A if (!buffer_fixed) {
3863N/A FREE_C_HEAP_ARRAY(char, buffer, mtInternal);
0N/A }
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/A
2796N/A#if defined(SOLARIS) || defined(LINUX) || defined(_ALLBSD_SOURCE)
0N/A#include <sys/types.h>
0N/A#include <sys/socket.h>
0N/A#include <netinet/in.h>
0N/A#include <arpa/inet.h>
0N/A#endif
0N/A
0N/A// Network access
222N/AnetworkStream::networkStream() : bufferedStream(1024*10, 1024*10) {
0N/A
0N/A _socket = -1;
0N/A
1887N/A int result = os::socket(AF_INET, SOCK_STREAM, 0);
0N/A if (result <= 0) {
0N/A assert(false, "Socket could not be created!");
0N/A } else {
0N/A _socket = result;
0N/A }
0N/A}
0N/A
0N/Aint networkStream::read(char *buf, size_t len) {
1887N/A return os::recv(_socket, buf, (int)len, 0);
0N/A}
0N/A
0N/Avoid networkStream::flush() {
0N/A if (size() != 0) {
2993N/A int result = os::raw_send(_socket, (char *)base(), size(), 0);
222N/A assert(result != -1, "connection error");
222N/A assert(result == (int)size(), "didn't send enough data");
0N/A }
0N/A reset();
0N/A}
0N/A
0N/AnetworkStream::~networkStream() {
0N/A close();
0N/A}
0N/A
0N/Avoid networkStream::close() {
0N/A if (_socket != -1) {
0N/A flush();
1887N/A os::socket_close(_socket);
0N/A _socket = -1;
0N/A }
0N/A}
0N/A
0N/Abool networkStream::connect(const char *ip, short port) {
0N/A
0N/A struct sockaddr_in server;
0N/A server.sin_family = AF_INET;
0N/A server.sin_port = htons(port);
0N/A
0N/A server.sin_addr.s_addr = inet_addr(ip);
80N/A if (server.sin_addr.s_addr == (uint32_t)-1) {
1887N/A struct hostent* host = os::get_host_by_name((char*)ip);
0N/A if (host != NULL) {
0N/A memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A
1887N/A int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
0N/A return (result >= 0);
0N/A}
0N/A
0N/A#endif