ostream.cpp revision 100
0N/A/*
0N/A * Copyright 1997-2007 Sun Microsystems, Inc. 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A *
0N/A */
0N/A
0N/A# include "incls/_precompiled.incl"
0N/A# include "incls/_ostream.cpp.incl"
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
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
0N/Avoid outputStream::indent() {
0N/A while (_position < _indentation) sp();
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
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
0N/Avoid fileStream::write(const char* s, size_t len) {
0N/A if (_file != NULL) fwrite(s, 1, len, _file);
0N/A update_position(s, len);
0N/A}
0N/A
0N/AfileStream::~fileStream() {
0N/A if (_file != NULL) {
0N/A if (_need_close) fclose(_file);
0N/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) {
0N/A if (_fd != -1) ::write(_fd, s, (int)len);
0N/A update_position(s, len);
0N/A}
0N/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
0N/Astatic const char* make_log_name(const char* log_name, const char* force_directory, char* buf) {
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
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 const char* star = strchr(basename, '*');
0N/A int star_pos = (star == NULL) ? -1 : (star - nametail);
0N/A
0N/A if (star_pos >= 0) {
0N/A // convert foo*bar.log to foo123bar.log
0N/A int buf_pos = (int) strlen(buf);
0N/A strncpy(&buf[buf_pos], nametail, star_pos);
0N/A sprintf(&buf[buf_pos + star_pos], "%u", os::current_process_id());
0N/A nametail += star_pos + 1; // skip prefix and star
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";
0N/A char buf[O_BUFLEN*2];
0N/A const char* try_name = make_log_name(log_name, NULL, buf);
0N/A fileStream* file = new(ResourceObj::C_HEAP) fileStream(try_name);
0N/A if (!file->is_open()) {
0N/A // Try again to open the file.
0N/A char warnbuf[O_BUFLEN*2];
0N/A sprintf(warnbuf, "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);
0N/A try_name = make_log_name("hs_pid*.log", os::get_temp_directory(), buf);
0N/A sprintf(warnbuf, "Warning: Forcing option -XX:LogFile=%s\n", try_name);
0N/A jio_print(warnbuf);
0N/A delete file;
0N/A file = new(ResourceObj::C_HEAP) fileStream(try_name);
0N/A }
0N/A if (file->is_open()) {
0N/A _log_file = file;
0N/A xmlStream* xs = new(ResourceObj::C_HEAP) 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:
0N/A _log_file->print_cr("<writer thread='"INTX_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
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) {
0N/A defaultStream::instance = new(ResourceObj::C_HEAP) 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) {
0N/A fileStream * gclog = new(ResourceObj::C_HEAP)
0N/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 gclog_or_tty = gclog;
0N/A }
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;
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
0N/AbufferedStream::bufferedStream(size_t initial_size) : outputStream() {
0N/A buffer_length = initial_size;
0N/A buffer = NEW_C_HEAP_ARRAY(char, buffer_length);
0N/A buffer_pos = 0;
0N/A buffer_fixed = false;
0N/A}
0N/A
0N/AbufferedStream::bufferedStream(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 bufferedStream::write(const char* s, size_t len) {
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 }
0N/A buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end);
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) {
0N/A FREE_C_HEAP_ARRAY(char, buffer);
0N/A }
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/A#if defined(SOLARIS) || defined(LINUX)
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
0N/AnetworkStream::networkStream() {
0N/A
0N/A _socket = -1;
0N/A
0N/A hpi::initialize_socket_library();
0N/A
0N/A int result = hpi::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) {
0N/A return hpi::recv(_socket, buf, (int)len, 0);
0N/A}
0N/A
0N/Avoid networkStream::flush() {
0N/A if (size() != 0) {
0N/A hpi::send(_socket, (char *)base(), (int)size(), 0);
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();
0N/A hpi::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);
0N/A if (server.sin_addr.s_addr == (unsigned long)-1) {
0N/A#ifdef _WINDOWS
0N/A struct hostent* host = hpi::get_host_by_name((char*)ip);
0N/A#else
0N/A struct hostent* host = gethostbyname(ip);
0N/A#endif
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
0N/A int result = hpi::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
0N/A return (result >= 0);
0N/A}
0N/A
0N/A#endif