ostream.cpp revision 342:37f87013dfd8
1250199Sgrehan/* 2298446Ssephe * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 3250199Sgrehan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4250199Sgrehan * 5250199Sgrehan * This code is free software; you can redistribute it and/or modify it 6250199Sgrehan * under the terms of the GNU General Public License version 2 only, as 7250199Sgrehan * published by the Free Software Foundation. 8250199Sgrehan * 9250199Sgrehan * This code is distributed in the hope that it will be useful, but WITHOUT 10250199Sgrehan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11250199Sgrehan * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12250199Sgrehan * version 2 for more details (a copy is included in the LICENSE file that 13250199Sgrehan * accompanied this code). 14250199Sgrehan * 15250199Sgrehan * You should have received a copy of the GNU General Public License version 16250199Sgrehan * 2 along with this work; if not, write to the Free Software Foundation, 17250199Sgrehan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18250199Sgrehan * 19250199Sgrehan * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20250199Sgrehan * CA 95054 USA or visit www.sun.com if you need additional information or 21250199Sgrehan * have any questions. 22250199Sgrehan * 23250199Sgrehan */ 24250199Sgrehan 25250199Sgrehan# include "incls/_precompiled.incl" 26250199Sgrehan# include "incls/_ostream.cpp.incl" 27250199Sgrehan 28250199Sgrehanextern "C" void jio_print(const char* s); // Declarationtion of jvm method 29250199Sgrehan 30250199SgrehanoutputStream::outputStream(int width) { 31250199Sgrehan _width = width; 32256276Sdim _position = 0; 33256276Sdim _newlines = 0; 34250199Sgrehan _precount = 0; 35250199Sgrehan _indentation = 0; 36250199Sgrehan} 37250199Sgrehan 38250199SgrehanoutputStream::outputStream(int width, bool has_time_stamps) { 39250199Sgrehan _width = width; 40250199Sgrehan _position = 0; 41293873Ssephe _newlines = 0; 42250199Sgrehan _precount = 0; 43250199Sgrehan _indentation = 0; 44250199Sgrehan if (has_time_stamps) _stamp.update(); 45250199Sgrehan} 46250199Sgrehan 47250199Sgrehanvoid outputStream::update_position(const char* s, size_t len) { 48250199Sgrehan for (size_t i = 0; i < len; i++) { 49250199Sgrehan char ch = s[i]; 50250199Sgrehan if (ch == '\n') { 51250199Sgrehan _newlines += 1; 52250199Sgrehan _precount += _position + 1; 53250199Sgrehan _position = 0; 54250199Sgrehan } else if (ch == '\t') { 55250199Sgrehan int tw = 8 - (_position & 7); 56250199Sgrehan _position += tw; 57282212Swhu _precount -= tw-1; // invariant: _precount + _position == total count 58282212Swhu } else { 59250199Sgrehan _position += 1; 60282212Swhu } 61250199Sgrehan } 62297142Ssephe} 63300102Ssephe 64300102Ssephe// Execute a vsprintf, using the given buffer if necessary. 65250199Sgrehan// Return a pointer to the formatted string. 66293870Ssepheconst char* outputStream::do_vsnprintf(char* buffer, size_t buflen, 67293870Ssephe const char* format, va_list ap, 68250199Sgrehan bool add_cr, 69300102Ssephe size_t& result_len) { 70300102Ssephe const char* result; 71250199Sgrehan if (add_cr) buflen--; 72250199Sgrehan if (!strchr(format, '%')) { 73293870Ssephe // constant format string 74293870Ssephe result = format; 75300574Ssephe result_len = strlen(result); 76300574Ssephe if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate 77250199Sgrehan } else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') { 78300572Ssephe // trivial copy-through format string 79250199Sgrehan result = va_arg(ap, const char*); 80300572Ssephe result_len = strlen(result); 81300108Ssephe if (add_cr && result_len >= buflen) result_len = buflen-1; // truncate 82250199Sgrehan } else if (vsnprintf(buffer, buflen, format, ap) >= 0) { 83300573Ssephe result = buffer; 84300108Ssephe result_len = strlen(result); 85300108Ssephe } else { 86300108Ssephe DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");) 87300108Ssephe result = buffer; 88282212Swhu result_len = buflen - 1; 89292861Sdelphij buffer[result_len] = 0; 90250199Sgrehan } 91292861Sdelphij if (add_cr) { 92292861Sdelphij if (result != buffer) { 93292861Sdelphij strncpy(buffer, result, buflen); 94292861Sdelphij result = buffer; 95295307Ssephe } 96292861Sdelphij buffer[result_len++] = '\n'; 97292861Sdelphij buffer[result_len] = 0; 98292861Sdelphij } 99292861Sdelphij return result; 100292861Sdelphij} 101295307Ssephe 102292861Sdelphijvoid outputStream::print(const char* format, ...) { 103292861Sdelphij char buffer[O_BUFLEN]; 104300108Ssephe va_list ap; 105300108Ssephe va_start(ap, format); 106300108Ssephe size_t len; 107300108Ssephe const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len); 108300108Ssephe write(str, len); 109300108Ssephe va_end(ap); 110300108Ssephe} 111300108Ssephe 112300108Ssephevoid outputStream::print_cr(const char* format, ...) { 113300108Ssephe char buffer[O_BUFLEN]; 114300108Ssephe va_list ap; 115300108Ssephe va_start(ap, format); 116300108Ssephe size_t len; 117300108Ssephe const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len); 118250199Sgrehan write(str, len); 119250199Sgrehan va_end(ap); 120250199Sgrehan} 121250199Sgrehan 122255414Sgrehanvoid outputStream::vprint(const char *format, va_list argptr) { 123300108Ssephe char buffer[O_BUFLEN]; 124250199Sgrehan size_t len; 125250199Sgrehan const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len); 126250199Sgrehan write(str, len); 127250199Sgrehan} 128250199Sgrehan 129250199Sgrehanvoid outputStream::vprint_cr(const char* format, va_list argptr) { 130250199Sgrehan char buffer[O_BUFLEN]; 131250199Sgrehan size_t len; 132250199Sgrehan const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len); 133282212Swhu write(str, len); 134300567Ssephe} 135250199Sgrehan 136300481Ssephevoid outputStream::fill_to(int col) { 137250199Sgrehan int need_fill = col - position(); 138250199Sgrehan sp(need_fill); 139250199Sgrehan} 140250199Sgrehan 141250199Sgrehanvoid outputStream::move_to(int col, int slop, int min_space) { 142250199Sgrehan if (position() >= col + slop) 143300107Ssephe cr(); 144250199Sgrehan int need_fill = col - position(); 145250199Sgrehan if (need_fill < min_space) 146300573Ssephe need_fill = min_space; 147300481Ssephe sp(need_fill); 148250199Sgrehan} 149293873Ssephe 150293873Ssephevoid outputStream::put(char ch) { 151293873Ssephe assert(ch != 0, "please fix call site"); 152293873Ssephe char buf[] = { ch, '\0' }; 153297176Ssephe write(buf, 1); 154297176Ssephe} 155297176Ssephe 156293873Ssephe#define SP_USE_TABS false 157293873Ssephe 158293873Ssephevoid outputStream::sp(int count) { 159293873Ssephe if (count < 0) return; 160293873Ssephe if (SP_USE_TABS && count >= 8) { 161293873Ssephe int target = position() + count; 162297634Ssephe while (count >= 8) { 163297634Ssephe this->write("\t", 1); 164297634Ssephe count -= 8; 165297636Ssephe } 166293873Ssephe count = target - position(); 167297634Ssephe } 168293873Ssephe while (count > 0) { 169293873Ssephe int nw = (count > 8) ? 8 : count; 170293873Ssephe this->write(" ", nw); 171293873Ssephe count -= nw; 172293873Ssephe } 173293873Ssephe} 174293873Ssephe 175293873Ssephevoid outputStream::cr() { 176293873Ssephe this->write("\n", 1); 177293873Ssephe} 178300481Ssephe 179250199Sgrehanvoid outputStream::stamp() { 180300646Ssephe if (! _stamp.is_updated()) { 181300646Ssephe _stamp.update(); // start at 0 on first call to stamp() 182250199Sgrehan } 183250199Sgrehan 184293873Ssephe // outputStream::stamp() may get called by ostream_abort(), use snprintf 185250199Sgrehan // to avoid allocating large stack buffer in print(). 186250199Sgrehan char buf[40]; 187282212Swhu jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds()); 188282212Swhu print_raw(buf); 189282212Swhu} 190300565Ssephe 191300565Ssephevoid outputStream::stamp(bool guard, 192282212Swhu const char* prefix, 193282212Swhu const char* suffix) { 194282212Swhu if (!guard) { 195282212Swhu return; 196282212Swhu } 197282212Swhu print_raw(prefix); 198282212Swhu stamp(); 199282212Swhu print_raw(suffix); 200282212Swhu} 201300573Ssephe 202282212Swhuvoid outputStream::date_stamp(bool guard, 203300567Ssephe const char* prefix, 204282212Swhu const char* suffix) { 205282212Swhu if (!guard) { 206282212Swhu return; 207282212Swhu } 208282212Swhu print_raw(prefix); 209282212Swhu static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz"; 210282212Swhu static const int buffer_length = 32; 211300571Ssephe char buffer[buffer_length]; 212300572Ssephe const char* iso8601_result = os::iso8601_time(buffer, buffer_length); 213300571Ssephe if (iso8601_result != NULL) { 214300571Ssephe print_raw(buffer); 215300571Ssephe } else { 216300571Ssephe print_raw(error_time); 217300571Ssephe } 218300571Ssephe print_raw(suffix); 219300571Ssephe return; 220300571Ssephe} 221300571Ssephe 222300571Ssephevoid outputStream::indent() { 223300571Ssephe while (_position < _indentation) sp(); 224300571Ssephe} 225300571Ssephe 226300571Ssephevoid outputStream::print_jlong(jlong value) { 227300571Ssephe // N.B. Same as INT64_FORMAT 228300571Ssephe print(os::jlong_format_specifier(), value); 229300571Ssephe} 230300571Ssephe 231300571Ssephevoid outputStream::print_julong(julong value) { 232300571Ssephe // N.B. Same as UINT64_FORMAT 233300571Ssephe print(os::julong_format_specifier(), value); 234300572Ssephe} 235300573Ssephe 236300571SsephestringStream::stringStream(size_t initial_size) : outputStream() { 237300571Ssephe buffer_length = initial_size; 238300571Ssephe buffer = NEW_RESOURCE_ARRAY(char, buffer_length); 239300571Ssephe buffer_pos = 0; 240300571Ssephe buffer_fixed = false; 241300571Ssephe} 242300571Ssephe 243300571Ssephe// useful for output to fixed chunks of memory, such as performance counters 244300572SsephestringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() { 245300573Ssephe buffer_length = fixed_buffer_size; 246300571Ssephe buffer = fixed_buffer; 247300571Ssephe buffer_pos = 0; 248300571Ssephe buffer_fixed = true; 249300571Ssephe} 250300571Ssephe 251300571Ssephevoid stringStream::write(const char* s, size_t len) { 252300571Ssephe size_t write_len = len; // number of non-null bytes to write 253300571Ssephe size_t end = buffer_pos + len + 1; // position after write and final '\0' 254300571Ssephe if (end > buffer_length) { 255300571Ssephe if (buffer_fixed) { 256300571Ssephe // if buffer cannot resize, silently truncate 257300571Ssephe end = buffer_length; 258300571Ssephe write_len = end - buffer_pos - 1; // leave room for the final '\0' 259300571Ssephe } else { 260300571Ssephe // For small overruns, double the buffer. For larger ones, 261300571Ssephe // increase to the requested size. 262300571Ssephe if (end < buffer_length * 2) { 263300571Ssephe end = buffer_length * 2; 264300571Ssephe } 265300571Ssephe char* oldbuf = buffer; 266300571Ssephe buffer = NEW_RESOURCE_ARRAY(char, end); 267300571Ssephe strncpy(buffer, oldbuf, buffer_pos); 268300571Ssephe buffer_length = end; 269300571Ssephe } 270300571Ssephe } 271300571Ssephe // invariant: buffer is always null-terminated 272300571Ssephe guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob"); 273300647Ssephe buffer[buffer_pos + write_len] = 0; 274300571Ssephe strncpy(buffer + buffer_pos, s, write_len); 275300571Ssephe buffer_pos += write_len; 276300571Ssephe 277300571Ssephe // Note that the following does not depend on write_len. 278300571Ssephe // This means that position and count get updated 279300571Ssephe // even when overflow occurs. 280300571Ssephe update_position(s, len); 281300571Ssephe} 282300571Ssephe 283300571Ssephechar* stringStream::as_string() { 284300571Ssephe char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); 285300571Ssephe strncpy(copy, buffer, buffer_pos); 286300571Ssephe copy[buffer_pos] = 0; // terminating null 287300571Ssephe return copy; 288300571Ssephe} 289300571Ssephe 290300571SsephestringStream::~stringStream() {} 291300571Ssephe 292300571SsephexmlStream* xtty; 293300571SsepheoutputStream* tty; 294300571SsepheoutputStream* gclog_or_tty; 295300571Ssepheextern Mutex* tty_lock; 296300571Ssephe 297300571SsephefileStream::fileStream(const char* file_name) { 298300571Ssephe _file = fopen(file_name, "w"); 299300571Ssephe _need_close = true; 300300571Ssephe} 301300571Ssephe 302300571Ssephevoid fileStream::write(const char* s, size_t len) { 303300571Ssephe if (_file != NULL) fwrite(s, 1, len, _file); 304300571Ssephe update_position(s, len); 305300571Ssephe} 306300571Ssephe 307300571SsephefileStream::~fileStream() { 308300571Ssephe if (_file != NULL) { 309300571Ssephe if (_need_close) fclose(_file); 310300571Ssephe _file = NULL; 311300571Ssephe } 312300571Ssephe} 313300571Ssephe 314300571Ssephevoid fileStream::flush() { 315300571Ssephe fflush(_file); 316300571Ssephe} 317300571Ssephe 318300571SsephefdStream::fdStream(const char* file_name) { 319300571Ssephe _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666); 320300571Ssephe _need_close = true; 321300571Ssephe} 322300644Ssephe 323300572SsephefdStream::~fdStream() { 324300572Ssephe if (_fd != -1) { 325300572Ssephe if (_need_close) close(_fd); 326300572Ssephe _fd = -1; 327300572Ssephe } 328300644Ssephe} 329300644Ssephe 330300572Ssephevoid fdStream::write(const char* s, size_t len) { 331300572Ssephe if (_fd != -1) ::write(_fd, s, (int)len); 332300572Ssephe update_position(s, len); 333300644Ssephe} 334300644Ssephe 335300573SsephedefaultStream* defaultStream::instance = NULL; 336300572Ssepheint defaultStream::_output_fd = 1; 337300644Ssepheint defaultStream::_error_fd = 2; 338300644SsepheFILE* defaultStream::_output_stream = stdout; 339300644SsepheFILE* defaultStream::_error_stream = stderr; 340300644Ssephe 341300644Ssephe#define LOG_MAJOR_VERSION 160 342300644Ssephe#define LOG_MINOR_VERSION 1 343300573Ssephe 344300572Ssephevoid defaultStream::init() { 345300644Ssephe _inited = true; 346300644Ssephe if (LogVMOutput || LogCompilation) { 347300644Ssephe init_log(); 348300572Ssephe } 349300644Ssephe} 350300572Ssephe 351300572Ssephebool defaultStream::has_log_file() { 352300572Ssephe // lazily create log file (at startup, LogVMOutput is false even 353300572Ssephe // if +LogVMOutput is used, because the flags haven't been parsed yet) 354300572Ssephe // For safer printing during fatal error handling, do not init logfile 355300572Ssephe // if a VM error has been reported. 356300572Ssephe if (!_inited && !is_error_reported()) init(); 357300572Ssephe return _log_file != NULL; 358300573Ssephe} 359300572Ssephe 360300573Ssephestatic const char* make_log_name(const char* log_name, const char* force_directory, char* buf) { 361300573Ssephe const char* basename = log_name; 362300573Ssephe char file_sep = os::file_separator()[0]; 363300572Ssephe const char* cp; 364300573Ssephe for (cp = log_name; *cp != '\0'; cp++) { 365300572Ssephe if (*cp == '/' || *cp == file_sep) { 366300573Ssephe basename = cp+1; 367300573Ssephe } 368300573Ssephe } 369300572Ssephe const char* nametail = log_name; 370300572Ssephe 371300572Ssephe strcpy(buf, ""); 372300572Ssephe if (force_directory != NULL) { 373250199Sgrehan strcat(buf, force_directory); 374300574Ssephe strcat(buf, os::file_separator()); 375300574Ssephe nametail = basename; // completely skip directory prefix 376300574Ssephe } 377300574Ssephe 378300574Ssephe const char* star = strchr(basename, '*'); 379300574Ssephe int star_pos = (star == NULL) ? -1 : (star - nametail); 380300645Ssephe 381300574Ssephe if (star_pos >= 0) { 382300645Ssephe // convert foo*bar.log to foo123bar.log 383300574Ssephe int buf_pos = (int) strlen(buf); 384300574Ssephe strncpy(&buf[buf_pos], nametail, star_pos); 385300574Ssephe sprintf(&buf[buf_pos + star_pos], "%u", os::current_process_id()); 386300574Ssephe nametail += star_pos + 1; // skip prefix and star 387300645Ssephe } 388300645Ssephe 389300574Ssephe strcat(buf, nametail); // append rest of name, or all of name 390300646Ssephe return buf; 391300646Ssephe} 392300646Ssephe 393300574Ssephevoid defaultStream::init_log() { 394300574Ssephe // %%% Need a MutexLocker? 395300646Ssephe const char* log_name = LogFile != NULL ? LogFile : "hotspot.log"; 396300646Ssephe char buf[O_BUFLEN*2]; 397300574Ssephe const char* try_name = make_log_name(log_name, NULL, buf); 398300574Ssephe fileStream* file = new(ResourceObj::C_HEAP) fileStream(try_name); 399300645Ssephe if (!file->is_open()) { 400300574Ssephe // Try again to open the file. 401300646Ssephe char warnbuf[O_BUFLEN*2]; 402300574Ssephe sprintf(warnbuf, "Warning: Cannot open log file: %s\n", try_name); 403300646Ssephe // Note: This feature is for maintainer use only. No need for L10N. 404300574Ssephe jio_print(warnbuf); 405300574Ssephe try_name = make_log_name("hs_pid*.log", os::get_temp_directory(), buf); 406300646Ssephe sprintf(warnbuf, "Warning: Forcing option -XX:LogFile=%s\n", try_name); 407300646Ssephe jio_print(warnbuf); 408300646Ssephe delete file; 409300574Ssephe file = new(ResourceObj::C_HEAP) fileStream(try_name); 410300574Ssephe } 411300645Ssephe if (file->is_open()) { 412300645Ssephe _log_file = file; 413300645Ssephe xmlStream* xs = new(ResourceObj::C_HEAP) xmlStream(file); 414300645Ssephe _outer_xmlStream = xs; 415300645Ssephe if (this == tty) xtty = xs; 416300645Ssephe // Write XML header. 417300645Ssephe xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>"); 418300645Ssephe // (For now, don't bother to issue a DTD for this private format.) 419300645Ssephe jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds(); 420300645Ssephe // %%% Should be: jlong time_ms = os::start_time_milliseconds(), if 421300645Ssephe // we ever get round to introduce that method on the os class 422300645Ssephe xs->head("hotspot_log version='%d %d'" 423300645Ssephe " process='%d' time_ms='"INT64_FORMAT"'", 424300645Ssephe LOG_MAJOR_VERSION, LOG_MINOR_VERSION, 425300574Ssephe os::current_process_id(), time_ms); 426300574Ssephe // Write VM version header immediately. 427300574Ssephe xs->head("vm_version"); 428300574Ssephe xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr(); 429300574Ssephe xs->tail("name"); 430300574Ssephe xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr(); 431300574Ssephe xs->tail("release"); 432300574Ssephe xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr(); 433300645Ssephe xs->tail("info"); 434300645Ssephe xs->tail("vm_version"); 435300645Ssephe // Record information about the command-line invocation. 436300645Ssephe xs->head("vm_arguments"); // Cf. Arguments::print_on() 437300645Ssephe if (Arguments::num_jvm_flags() > 0) { 438300574Ssephe xs->head("flags"); 439300646Ssephe Arguments::print_jvm_flags_on(xs->text()); 440300646Ssephe xs->tail("flags"); 441300646Ssephe } 442300574Ssephe if (Arguments::num_jvm_args() > 0) { 443300646Ssephe xs->head("args"); 444300646Ssephe Arguments::print_jvm_args_on(xs->text()); 445300646Ssephe xs->tail("args"); 446300646Ssephe } 447300646Ssephe if (Arguments::java_command() != NULL) { 448300576Ssephe xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command()); 449300574Ssephe xs->tail("command"); 450300574Ssephe } 451300574Ssephe if (Arguments::sun_java_launcher() != NULL) { 452300574Ssephe xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher()); 453250199Sgrehan xs->tail("launcher"); 454250199Sgrehan } 455250199Sgrehan if (Arguments::system_properties() != NULL) { 456250199Sgrehan xs->head("properties"); 457250199Sgrehan // Print it as a java-style property list. 458250199Sgrehan // System properties don't generally contain newlines, so don't bother with unparsing. 459250199Sgrehan for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { 460250199Sgrehan xs->text()->print_cr("%s=%s", p->key(), p->value()); 461250199Sgrehan } 462250199Sgrehan xs->tail("properties"); 463250199Sgrehan } 464250199Sgrehan xs->tail("vm_arguments"); 465250199Sgrehan // tty output per se is grouped under the <tty>...</tty> element. 466250199Sgrehan xs->head("tty"); 467250199Sgrehan // All further non-markup text gets copied to the tty: 468250199Sgrehan xs->_text = this; // requires friend declaration! 469250199Sgrehan } else { 470250199Sgrehan delete(file); 471250199Sgrehan // and leave xtty as NULL 472250199Sgrehan LogVMOutput = false; 473250199Sgrehan DisplayVMOutput = true; 474250199Sgrehan LogCompilation = false; 475250199Sgrehan } 476250199Sgrehan} 477250199Sgrehan 478250199Sgrehan// finish_log() is called during normal VM shutdown. finish_log_on_error() is 479250199Sgrehan// called by ostream_abort() after a fatal error. 480250199Sgrehan// 481250199Sgrehanvoid defaultStream::finish_log() { 482250199Sgrehan xmlStream* xs = _outer_xmlStream; 483250199Sgrehan xs->done("tty"); 484250199Sgrehan 485250199Sgrehan // Other log forks are appended here, at the End of Time: 486250199Sgrehan CompileLog::finish_log(xs->out()); // write compile logging, if any, now 487250199Sgrehan 488250199Sgrehan xs->done("hotspot_log"); 489250199Sgrehan xs->flush(); 490250199Sgrehan 491250199Sgrehan fileStream* file = _log_file; 492250199Sgrehan _log_file = NULL; 493250199Sgrehan 494250199Sgrehan delete _outer_xmlStream; 495250199Sgrehan _outer_xmlStream = NULL; 496250199Sgrehan 497250199Sgrehan file->flush(); 498297143Ssephe delete file; 499297143Ssephe} 500297143Ssephe 501297143Ssephevoid defaultStream::finish_log_on_error(char *buf, int buflen) { 502297143Ssephe xmlStream* xs = _outer_xmlStream; 503297143Ssephe 504298449Ssephe if (xs && xs->out()) { 505298449Ssephe 506298449Ssephe xs->done_raw("tty"); 507297143Ssephe 508297143Ssephe // Other log forks are appended here, at the End of Time: 509297143Ssephe CompileLog::finish_log_on_error(xs->out(), buf, buflen); // write compile logging, if any, now 510297143Ssephe 511297143Ssephe xs->done_raw("hotspot_log"); 512297143Ssephe xs->flush(); 513297143Ssephe 514297143Ssephe fileStream* file = _log_file; 515297143Ssephe _log_file = NULL; 516297143Ssephe _outer_xmlStream = NULL; 517297143Ssephe 518250199Sgrehan if (file) { 519250199Sgrehan file->flush(); 520250199Sgrehan 521250199Sgrehan // Can't delete or close the file because delete and fclose aren't 522250199Sgrehan // async-safe. We are about to die, so leave it to the kernel. 523250199Sgrehan // delete file; 524250199Sgrehan } 525250199Sgrehan } 526250199Sgrehan} 527250199Sgrehan 528250199Sgrehanintx defaultStream::hold(intx writer_id) { 529250199Sgrehan bool has_log = has_log_file(); // check before locking 530295308Ssephe if (// impossible, but who knows? 531250199Sgrehan writer_id == NO_WRITER || 532250199Sgrehan 533250199Sgrehan // bootstrap problem 534250199Sgrehan tty_lock == NULL || 535250199Sgrehan 536250199Sgrehan // can't grab a lock or call Thread::current() if TLS isn't initialized 537250199Sgrehan ThreadLocalStorage::thread() == NULL || 538250199Sgrehan 539297142Ssephe // developer hook 540297142Ssephe !SerializeVMOutput || 541250199Sgrehan 542297142Ssephe // VM already unhealthy 543297142Ssephe is_error_reported() || 544297142Ssephe 545297142Ssephe // safepoint == global lock (for VM only) 546297142Ssephe (SafepointSynchronize::is_synchronizing() && 547297142Ssephe Thread::current()->is_VM_thread()) 548297142Ssephe ) { 549297142Ssephe // do not attempt to lock unless we know the thread and the VM is healthy 550250199Sgrehan return NO_WRITER; 551250199Sgrehan } 552250199Sgrehan if (_writer == writer_id) { 553250199Sgrehan // already held, no need to re-grab the lock 554250199Sgrehan return NO_WRITER; 555250199Sgrehan } 556250199Sgrehan tty_lock->lock_without_safepoint_check(); 557297142Ssephe // got the lock 558297142Ssephe if (writer_id != _last_writer) { 559297142Ssephe if (has_log) { 560297142Ssephe _log_file->bol(); 561297142Ssephe // output a hint where this output is coming from: 562250199Sgrehan _log_file->print_cr("<writer thread='"INTX_FORMAT"'/>", writer_id); 563300486Ssephe } 564250199Sgrehan _last_writer = writer_id; 565250199Sgrehan } 566250199Sgrehan _writer = writer_id; 567250199Sgrehan return writer_id; 568250199Sgrehan} 569250199Sgrehan 570250199Sgrehanvoid defaultStream::release(intx holder) { 571250199Sgrehan if (holder == NO_WRITER) { 572250199Sgrehan // nothing to release: either a recursive lock, or we scribbled (too bad) 573250199Sgrehan return; 574250199Sgrehan } 575250199Sgrehan if (_writer != holder) { 576250199Sgrehan return; // already unlocked, perhaps via break_tty_lock_for_safepoint 577250199Sgrehan } 578250199Sgrehan _writer = NO_WRITER; 579300486Ssephe tty_lock->unlock(); 580250199Sgrehan} 581250199Sgrehan 582250199Sgrehan 583250199Sgrehan// Yuck: jio_print does not accept char*/len. 584250199Sgrehanstatic void call_jio_print(const char* s, size_t len) { 585300127Ssephe char buffer[O_BUFLEN+100]; 586300127Ssephe if (len > sizeof(buffer)-1) { 587293870Ssephe warning("increase O_BUFLEN in ostream.cpp -- output truncated"); 588300480Ssephe len = sizeof(buffer)-1; 589293870Ssephe } 590250199Sgrehan strncpy(buffer, s, len); 591300129Ssephe buffer[len] = '\0'; 592250199Sgrehan jio_print(buffer); 593293870Ssephe} 594250199Sgrehan 595250199Sgrehan 596282212Swhuvoid defaultStream::write(const char* s, size_t len) { 597250199Sgrehan intx thread_id = os::current_thread_id(); 598250199Sgrehan intx holder = hold(thread_id); 599250199Sgrehan 600250199Sgrehan if (DisplayVMOutput && 601250199Sgrehan (_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) { 602250199Sgrehan // print to output stream. It can be redirected by a vfprintf hook 603250199Sgrehan if (s[len] == '\0') { 604250199Sgrehan jio_print(s); 605250199Sgrehan } else { 606250199Sgrehan call_jio_print(s, len); 607250199Sgrehan } 608250199Sgrehan } 609250199Sgrehan 610250199Sgrehan // print to log file 611300107Ssephe if (has_log_file()) { 612300574Ssephe int nl0 = _newlines; 613250199Sgrehan xmlTextStream::write(s, len); 614250199Sgrehan // flush the log file too, if there were any newlines 615250199Sgrehan if (nl0 != _newlines){ 616250199Sgrehan flush(); 617250199Sgrehan } 618300107Ssephe } else { 619250199Sgrehan update_position(s, len); 620250199Sgrehan } 621300645Ssephe 622250199Sgrehan release(holder); 623300645Ssephe} 624300574Ssephe 625282212Swhuintx ttyLocker::hold_tty() { 626250199Sgrehan if (defaultStream::instance == NULL) return defaultStream::NO_WRITER; 627250199Sgrehan intx thread_id = os::current_thread_id(); 628300645Ssephe return defaultStream::instance->hold(thread_id); 629250199Sgrehan} 630300645Ssephe 631300644Ssephevoid ttyLocker::release_tty(intx holder) { 632300644Ssephe if (holder == defaultStream::NO_WRITER) return; 633300572Ssephe defaultStream::instance->release(holder); 634282212Swhu} 635282212Swhu 636282212Swhuvoid ttyLocker::break_tty_lock_for_safepoint(intx holder) { 637300572Ssephe if (defaultStream::instance != NULL && 638282212Swhu defaultStream::instance->writer() == holder) { 639255414Sgrehan if (xtty != NULL) { 640250199Sgrehan xtty->print_cr("<!-- safepoint while printing -->"); 641250199Sgrehan } 642250199Sgrehan defaultStream::instance->release(holder); 643250199Sgrehan } 644255414Sgrehan // (else there was no lock to break) 645300574Ssephe} 646250199Sgrehan 647300107Ssephevoid ostream_init() { 648300107Ssephe if (defaultStream::instance == NULL) { 649300107Ssephe defaultStream::instance = new(ResourceObj::C_HEAP) defaultStream(); 650300107Ssephe tty = defaultStream::instance; 651300107Ssephe 652300107Ssephe // We want to ensure that time stamps in GC logs consider time 0 653250199Sgrehan // the time when the JVM is initialized, not the first time we ask 654298260Ssephe // for a time stamp. So, here, we explicitly update the time stamp 655298260Ssephe // of tty. 656300486Ssephe tty->time_stamp().update_to(1); 657300486Ssephe } 658298260Ssephe} 659250199Sgrehan 660250199Sgrehanvoid ostream_init_log() { 661300574Ssephe // For -Xloggc:<file> option - called in runtime/thread.cpp 662300645Ssephe // Note : this must be called AFTER ostream_init() 663300572Ssephe 664250199Sgrehan gclog_or_tty = tty; // default to tty 665250199Sgrehan if (Arguments::gc_log_filename() != NULL) { 666250199Sgrehan fileStream * gclog = new(ResourceObj::C_HEAP) 667250199Sgrehan fileStream(Arguments::gc_log_filename()); 668300107Ssephe if (gclog->is_open()) { 669300107Ssephe // now we update the time stamp of the GC log to be synced up 670300107Ssephe // with tty. 671300107Ssephe gclog->time_stamp().update_to(tty->time_stamp().ticks()); 672300107Ssephe gclog_or_tty = gclog; 673250199Sgrehan } 674250199Sgrehan } 675250199Sgrehan 676300102Ssephe // If we haven't lazily initialized the logfile yet, do it now, 677300486Ssephe // to avoid the possibility of lazy initialization during a VM 678300574Ssephe // crash, which can affect the stability of the fatal error handler. 679250199Sgrehan defaultStream::instance->has_log_file(); 680300107Ssephe} 681300107Ssephe 682300107Ssephe// ostream_exit() is called during normal VM exit to finish log files, flush 683300107Ssephe// output and free resource. 684300107Ssephevoid ostream_exit() { 685300107Ssephe static bool ostream_exit_called = false; 686300107Ssephe if (ostream_exit_called) return; 687299746Sjhb ostream_exit_called = true; 688250199Sgrehan if (gclog_or_tty != tty) { 689250199Sgrehan delete gclog_or_tty; 690250199Sgrehan } 691250199Sgrehan { 692250199Sgrehan // we temporaly disable PrintMallocFree here 693250199Sgrehan // as otherwise it'll lead to using of almost deleted 694250199Sgrehan // tty or defaultStream::instance in logging facility 695299746Sjhb // of HeapFree(), see 6391258 696250199Sgrehan DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);) 697250199Sgrehan if (tty != defaultStream::instance) { 698298449Ssephe delete tty; 699250199Sgrehan } 700250199Sgrehan if (defaultStream::instance != NULL) { 701250199Sgrehan delete defaultStream::instance; 702250199Sgrehan } 703300126Ssephe } 704250199Sgrehan tty = NULL; 705300102Ssephe xtty = NULL; 706256425Sgibbs gclog_or_tty = NULL; 707256425Sgibbs defaultStream::instance = NULL; 708299746Sjhb} 709250199Sgrehan 710250199Sgrehan// ostream_abort() is called by os::abort() when VM is about to die. 711256425Sgibbsvoid ostream_abort() { 712256425Sgibbs // Here we can't delete gclog_or_tty and tty, just flush their output 713250199Sgrehan if (gclog_or_tty) gclog_or_tty->flush(); 714250199Sgrehan if (tty) tty->flush(); 715250199Sgrehan 716299746Sjhb if (defaultStream::instance != NULL) { 717250199Sgrehan static char buf[4096]; 718250199Sgrehan defaultStream::instance->finish_log_on_error(buf, sizeof(buf)); 719250199Sgrehan } 720300121Ssephe} 721300121Ssephe 722250199SgrehanstaticBufferStream::staticBufferStream(char* buffer, size_t buflen, 723300487Ssephe outputStream *outer_stream) { 724255414Sgrehan _buffer = buffer; 725250199Sgrehan _buflen = buflen; 726250199Sgrehan _outer_stream = outer_stream; 727250199Sgrehan} 728300571Ssephe 729250199Sgrehanvoid staticBufferStream::write(const char* c, size_t len) { 730300645Ssephe _outer_stream->print_raw(c, (int)len); 731300572Ssephe} 732255414Sgrehan 733250199Sgrehanvoid staticBufferStream::flush() { 734250199Sgrehan _outer_stream->flush(); 735250199Sgrehan} 736250199Sgrehan 737300124Ssephevoid staticBufferStream::print(const char* format, ...) { 738300124Ssephe va_list ap; 739300124Ssephe va_start(ap, format); 740300124Ssephe size_t len; 741300124Ssephe const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len); 742300124Ssephe write(str, len); 743300124Ssephe va_end(ap); 744250199Sgrehan} 745300124Ssephe 746300124Ssephevoid staticBufferStream::print_cr(const char* format, ...) { 747300124Ssephe va_list ap; 748300124Ssephe va_start(ap, format); 749300124Ssephe size_t len; 750300124Ssephe const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len); 751250199Sgrehan write(str, len); 752300124Ssephe va_end(ap); 753300124Ssephe} 754250199Sgrehan 755300102Ssephevoid staticBufferStream::vprint(const char *format, va_list argptr) { 756300102Ssephe size_t len; 757300102Ssephe const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len); 758300102Ssephe write(str, len); 759300102Ssephe} 760250199Sgrehan 761300123Ssephevoid staticBufferStream::vprint_cr(const char* format, va_list argptr) { 762250199Sgrehan size_t len; 763300120Ssephe const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len); 764293870Ssephe write(str, len); 765293870Ssephe} 766250199Sgrehan 767299746SjhbbufferedStream::bufferedStream(size_t initial_size) : outputStream() { 768300126Ssephe buffer_length = initial_size; 769300126Ssephe buffer = NEW_C_HEAP_ARRAY(char, buffer_length); 770300126Ssephe buffer_pos = 0; 771300126Ssephe buffer_fixed = false; 772300126Ssephe} 773300126Ssephe 774299746SjhbbufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() { 775 buffer_length = fixed_buffer_size; 776 buffer = fixed_buffer; 777 buffer_pos = 0; 778 buffer_fixed = true; 779} 780 781void bufferedStream::write(const char* s, size_t len) { 782 size_t end = buffer_pos + len; 783 if (end >= buffer_length) { 784 if (buffer_fixed) { 785 // if buffer cannot resize, silently truncate 786 len = buffer_length - buffer_pos - 1; 787 } else { 788 // For small overruns, double the buffer. For larger ones, 789 // increase to the requested size. 790 if (end < buffer_length * 2) { 791 end = buffer_length * 2; 792 } 793 buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end); 794 buffer_length = end; 795 } 796 } 797 memcpy(buffer + buffer_pos, s, len); 798 buffer_pos += len; 799 update_position(s, len); 800} 801 802char* bufferedStream::as_string() { 803 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1); 804 strncpy(copy, buffer, buffer_pos); 805 copy[buffer_pos] = 0; // terminating null 806 return copy; 807} 808 809bufferedStream::~bufferedStream() { 810 if (!buffer_fixed) { 811 FREE_C_HEAP_ARRAY(char, buffer); 812 } 813} 814 815#ifndef PRODUCT 816 817#if defined(SOLARIS) || defined(LINUX) 818#include <sys/types.h> 819#include <sys/socket.h> 820#include <netinet/in.h> 821#include <arpa/inet.h> 822#endif 823 824// Network access 825networkStream::networkStream() { 826 827 _socket = -1; 828 829 hpi::initialize_socket_library(); 830 831 int result = hpi::socket(AF_INET, SOCK_STREAM, 0); 832 if (result <= 0) { 833 assert(false, "Socket could not be created!"); 834 } else { 835 _socket = result; 836 } 837} 838 839int networkStream::read(char *buf, size_t len) { 840 return hpi::recv(_socket, buf, (int)len, 0); 841} 842 843void networkStream::flush() { 844 if (size() != 0) { 845 hpi::send(_socket, (char *)base(), (int)size(), 0); 846 } 847 reset(); 848} 849 850networkStream::~networkStream() { 851 close(); 852} 853 854void networkStream::close() { 855 if (_socket != -1) { 856 flush(); 857 hpi::socket_close(_socket); 858 _socket = -1; 859 } 860} 861 862bool networkStream::connect(const char *ip, short port) { 863 864 struct sockaddr_in server; 865 server.sin_family = AF_INET; 866 server.sin_port = htons(port); 867 868 server.sin_addr.s_addr = inet_addr(ip); 869 if (server.sin_addr.s_addr == (uint32_t)-1) { 870#ifdef _WINDOWS 871 struct hostent* host = hpi::get_host_by_name((char*)ip); 872#else 873 struct hostent* host = gethostbyname(ip); 874#endif 875 if (host != NULL) { 876 memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length); 877 } else { 878 return false; 879 } 880 } 881 882 883 int result = hpi::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in)); 884 return (result >= 0); 885} 886 887#endif 888