src/share/vm/utilities/ostream.cpp

Wed, 24 Sep 2014 12:19:07 -0700

author
simonis
date
Wed, 24 Sep 2014 12:19:07 -0700
changeset 7553
f43fad8786fc
parent 7089
6e0cb14ce59b
child 7476
c2844108a708
permissions
-rw-r--r--

8058345: Refactor native stack printing from vmError.cpp to debug.cpp to make it available in gdb as well
Summary: Also fix stack trace on x86 to enable walking of runtime stubs and native wrappers
Reviewed-by: kvn

     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "compiler/compileLog.hpp"
    27 #include "gc_implementation/shared/gcId.hpp"
    28 #include "oops/oop.inline.hpp"
    29 #include "runtime/arguments.hpp"
    30 #include "utilities/defaultStream.hpp"
    31 #include "utilities/ostream.hpp"
    32 #include "utilities/top.hpp"
    33 #include "utilities/xmlstream.hpp"
    34 #ifdef TARGET_OS_FAMILY_linux
    35 # include "os_linux.inline.hpp"
    36 #endif
    37 #ifdef TARGET_OS_FAMILY_solaris
    38 # include "os_solaris.inline.hpp"
    39 #endif
    40 #ifdef TARGET_OS_FAMILY_windows
    41 # include "os_windows.inline.hpp"
    42 #endif
    43 #ifdef TARGET_OS_FAMILY_aix
    44 # include "os_aix.inline.hpp"
    45 #endif
    46 #ifdef TARGET_OS_FAMILY_bsd
    47 # include "os_bsd.inline.hpp"
    48 #endif
    50 extern "C" void jio_print(const char* s); // Declarationtion of jvm method
    52 outputStream::outputStream(int width) {
    53   _width       = width;
    54   _position    = 0;
    55   _newlines    = 0;
    56   _precount    = 0;
    57   _indentation = 0;
    58 }
    60 outputStream::outputStream(int width, bool has_time_stamps) {
    61   _width       = width;
    62   _position    = 0;
    63   _newlines    = 0;
    64   _precount    = 0;
    65   _indentation = 0;
    66   if (has_time_stamps)  _stamp.update();
    67 }
    69 void outputStream::update_position(const char* s, size_t len) {
    70   for (size_t i = 0; i < len; i++) {
    71     char ch = s[i];
    72     if (ch == '\n') {
    73       _newlines += 1;
    74       _precount += _position + 1;
    75       _position = 0;
    76     } else if (ch == '\t') {
    77       int tw = 8 - (_position & 7);
    78       _position += tw;
    79       _precount -= tw-1;  // invariant:  _precount + _position == total count
    80     } else {
    81       _position += 1;
    82     }
    83   }
    84 }
    86 // Execute a vsprintf, using the given buffer if necessary.
    87 // Return a pointer to the formatted string.
    88 const char* outputStream::do_vsnprintf(char* buffer, size_t buflen,
    89                                        const char* format, va_list ap,
    90                                        bool add_cr,
    91                                        size_t& result_len) {
    92   const char* result;
    93   if (add_cr)  buflen--;
    94   if (!strchr(format, '%')) {
    95     // constant format string
    96     result = format;
    97     result_len = strlen(result);
    98     if (add_cr && result_len >= buflen)  result_len = buflen-1;  // truncate
    99   } else if (format[0] == '%' && format[1] == 's' && format[2] == '\0') {
   100     // trivial copy-through format string
   101     result = va_arg(ap, const char*);
   102     result_len = strlen(result);
   103     if (add_cr && result_len >= buflen)  result_len = buflen-1;  // truncate
   104   } else if (vsnprintf(buffer, buflen, format, ap) >= 0) {
   105     result = buffer;
   106     result_len = strlen(result);
   107   } else {
   108     DEBUG_ONLY(warning("increase O_BUFLEN in ostream.hpp -- output truncated");)
   109     result = buffer;
   110     result_len = buflen - 1;
   111     buffer[result_len] = 0;
   112   }
   113   if (add_cr) {
   114     if (result != buffer) {
   115       strncpy(buffer, result, buflen);
   116       result = buffer;
   117     }
   118     buffer[result_len++] = '\n';
   119     buffer[result_len] = 0;
   120   }
   121   return result;
   122 }
   124 void outputStream::print(const char* format, ...) {
   125   char buffer[O_BUFLEN];
   126   va_list ap;
   127   va_start(ap, format);
   128   size_t len;
   129   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, false, len);
   130   write(str, len);
   131   va_end(ap);
   132 }
   134 void outputStream::print_cr(const char* format, ...) {
   135   char buffer[O_BUFLEN];
   136   va_list ap;
   137   va_start(ap, format);
   138   size_t len;
   139   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, ap, true, len);
   140   write(str, len);
   141   va_end(ap);
   142 }
   144 void outputStream::vprint(const char *format, va_list argptr) {
   145   char buffer[O_BUFLEN];
   146   size_t len;
   147   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, false, len);
   148   write(str, len);
   149 }
   151 void outputStream::vprint_cr(const char* format, va_list argptr) {
   152   char buffer[O_BUFLEN];
   153   size_t len;
   154   const char* str = do_vsnprintf(buffer, O_BUFLEN, format, argptr, true, len);
   155   write(str, len);
   156 }
   158 void outputStream::fill_to(int col) {
   159   int need_fill = col - position();
   160   sp(need_fill);
   161 }
   163 void outputStream::move_to(int col, int slop, int min_space) {
   164   if (position() >= col + slop)
   165     cr();
   166   int need_fill = col - position();
   167   if (need_fill < min_space)
   168     need_fill = min_space;
   169   sp(need_fill);
   170 }
   172 void outputStream::put(char ch) {
   173   assert(ch != 0, "please fix call site");
   174   char buf[] = { ch, '\0' };
   175   write(buf, 1);
   176 }
   178 #define SP_USE_TABS false
   180 void outputStream::sp(int count) {
   181   if (count < 0)  return;
   182   if (SP_USE_TABS && count >= 8) {
   183     int target = position() + count;
   184     while (count >= 8) {
   185       this->write("\t", 1);
   186       count -= 8;
   187     }
   188     count = target - position();
   189   }
   190   while (count > 0) {
   191     int nw = (count > 8) ? 8 : count;
   192     this->write("        ", nw);
   193     count -= nw;
   194   }
   195 }
   197 void outputStream::cr() {
   198   this->write("\n", 1);
   199 }
   201 void outputStream::stamp() {
   202   if (! _stamp.is_updated()) {
   203     _stamp.update(); // start at 0 on first call to stamp()
   204   }
   206   // outputStream::stamp() may get called by ostream_abort(), use snprintf
   207   // to avoid allocating large stack buffer in print().
   208   char buf[40];
   209   jio_snprintf(buf, sizeof(buf), "%.3f", _stamp.seconds());
   210   print_raw(buf);
   211 }
   213 void outputStream::stamp(bool guard,
   214                          const char* prefix,
   215                          const char* suffix) {
   216   if (!guard) {
   217     return;
   218   }
   219   print_raw(prefix);
   220   stamp();
   221   print_raw(suffix);
   222 }
   224 void outputStream::date_stamp(bool guard,
   225                               const char* prefix,
   226                               const char* suffix) {
   227   if (!guard) {
   228     return;
   229   }
   230   print_raw(prefix);
   231   static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz";
   232   static const int buffer_length = 32;
   233   char buffer[buffer_length];
   234   const char* iso8601_result = os::iso8601_time(buffer, buffer_length);
   235   if (iso8601_result != NULL) {
   236     print_raw(buffer);
   237   } else {
   238     print_raw(error_time);
   239   }
   240   print_raw(suffix);
   241   return;
   242 }
   244 void outputStream::gclog_stamp(const GCId& gc_id) {
   245   date_stamp(PrintGCDateStamps);
   246   stamp(PrintGCTimeStamps);
   247   if (PrintGCID) {
   248     print("#%u: ", gc_id.id());
   249   }
   250 }
   252 outputStream& outputStream::indent() {
   253   while (_position < _indentation) sp();
   254   return *this;
   255 }
   257 void outputStream::print_jlong(jlong value) {
   258   print(JLONG_FORMAT, value);
   259 }
   261 void outputStream::print_julong(julong value) {
   262   print(JULONG_FORMAT, value);
   263 }
   265 /**
   266  * This prints out hex data in a 'windbg' or 'xxd' form, where each line is:
   267  *   <hex-address>: 8 * <hex-halfword> <ascii translation (optional)>
   268  * example:
   269  * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000  .DOF............
   270  * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005  .......@... ....
   271  * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d  .......@.......]
   272  * ...
   273  *
   274  * indent is applied to each line.  Ends with a CR.
   275  */
   276 void outputStream::print_data(void* data, size_t len, bool with_ascii) {
   277   size_t limit = (len + 16) / 16 * 16;
   278   for (size_t i = 0; i < limit; ++i) {
   279     if (i % 16 == 0) {
   280       indent().print(SIZE_FORMAT_HEX_W(07)":", i);
   281     }
   282     if (i % 2 == 0) {
   283       print(" ");
   284     }
   285     if (i < len) {
   286       print("%02x", ((unsigned char*)data)[i]);
   287     } else {
   288       print("  ");
   289     }
   290     if ((i + 1) % 16 == 0) {
   291       if (with_ascii) {
   292         print("  ");
   293         for (size_t j = 0; j < 16; ++j) {
   294           size_t idx = i + j - 15;
   295           if (idx < len) {
   296             char c = ((char*)data)[idx];
   297             print("%c", c >= 32 && c <= 126 ? c : '.');
   298           }
   299         }
   300       }
   301       cr();
   302     }
   303   }
   304 }
   306 stringStream::stringStream(size_t initial_size) : outputStream() {
   307   buffer_length = initial_size;
   308   buffer        = NEW_RESOURCE_ARRAY(char, buffer_length);
   309   buffer_pos    = 0;
   310   buffer_fixed  = false;
   311   DEBUG_ONLY(rm = Thread::current()->current_resource_mark();)
   312 }
   314 // useful for output to fixed chunks of memory, such as performance counters
   315 stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
   316   buffer_length = fixed_buffer_size;
   317   buffer        = fixed_buffer;
   318   buffer_pos    = 0;
   319   buffer_fixed  = true;
   320 }
   322 void stringStream::write(const char* s, size_t len) {
   323   size_t write_len = len;               // number of non-null bytes to write
   324   size_t end = buffer_pos + len + 1;    // position after write and final '\0'
   325   if (end > buffer_length) {
   326     if (buffer_fixed) {
   327       // if buffer cannot resize, silently truncate
   328       end = buffer_length;
   329       write_len = end - buffer_pos - 1; // leave room for the final '\0'
   330     } else {
   331       // For small overruns, double the buffer.  For larger ones,
   332       // increase to the requested size.
   333       if (end < buffer_length * 2) {
   334         end = buffer_length * 2;
   335       }
   336       char* oldbuf = buffer;
   337       assert(rm == NULL || Thread::current()->current_resource_mark() == rm,
   338              "stringStream is re-allocated with a different ResourceMark");
   339       buffer = NEW_RESOURCE_ARRAY(char, end);
   340       strncpy(buffer, oldbuf, buffer_pos);
   341       buffer_length = end;
   342     }
   343   }
   344   // invariant: buffer is always null-terminated
   345   guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
   346   buffer[buffer_pos + write_len] = 0;
   347   strncpy(buffer + buffer_pos, s, write_len);
   348   buffer_pos += write_len;
   350   // Note that the following does not depend on write_len.
   351   // This means that position and count get updated
   352   // even when overflow occurs.
   353   update_position(s, len);
   354 }
   356 char* stringStream::as_string() {
   357   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
   358   strncpy(copy, buffer, buffer_pos);
   359   copy[buffer_pos] = 0;  // terminating null
   360   return copy;
   361 }
   363 stringStream::~stringStream() {}
   365 xmlStream*   xtty;
   366 outputStream* tty;
   367 outputStream* gclog_or_tty;
   368 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
   369 extern Mutex* tty_lock;
   371 #define EXTRACHARLEN   32
   372 #define CURRENTAPPX    ".current"
   373 #define FILENAMEBUFLEN  1024
   374 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS
   375 char* get_datetime_string(char *buf, size_t len) {
   376   os::local_time_string(buf, len);
   377   int i = (int)strlen(buf);
   378   while (i-- >= 0) {
   379     if (buf[i] == ' ') buf[i] = '_';
   380     else if (buf[i] == ':') buf[i] = '-';
   381   }
   382   return buf;
   383 }
   385 static const char* make_log_name_internal(const char* log_name, const char* force_directory,
   386                                                 int pid, const char* tms) {
   387   const char* basename = log_name;
   388   char file_sep = os::file_separator()[0];
   389   const char* cp;
   390   char  pid_text[32];
   392   for (cp = log_name; *cp != '\0'; cp++) {
   393     if (*cp == '/' || *cp == file_sep) {
   394       basename = cp + 1;
   395     }
   396   }
   397   const char* nametail = log_name;
   398   // Compute buffer length
   399   size_t buffer_length;
   400   if (force_directory != NULL) {
   401     buffer_length = strlen(force_directory) + strlen(os::file_separator()) +
   402                     strlen(basename) + 1;
   403   } else {
   404     buffer_length = strlen(log_name) + 1;
   405   }
   407   // const char* star = strchr(basename, '*');
   408   const char* pts = strstr(basename, "%p");
   409   int pid_pos = (pts == NULL) ? -1 : (pts - nametail);
   411   if (pid_pos >= 0) {
   412     jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid);
   413     buffer_length += strlen(pid_text);
   414   }
   416   pts = strstr(basename, "%t");
   417   int tms_pos = (pts == NULL) ? -1 : (pts - nametail);
   418   if (tms_pos >= 0) {
   419     buffer_length += strlen(tms);
   420   }
   422   // Create big enough buffer.
   423   char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
   425   strcpy(buf, "");
   426   if (force_directory != NULL) {
   427     strcat(buf, force_directory);
   428     strcat(buf, os::file_separator());
   429     nametail = basename;       // completely skip directory prefix
   430   }
   432   // who is first, %p or %t?
   433   int first = -1, second = -1;
   434   const char *p1st = NULL;
   435   const char *p2nd = NULL;
   437   if (pid_pos >= 0 && tms_pos >= 0) {
   438     // contains both %p and %t
   439     if (pid_pos < tms_pos) {
   440       // case foo%pbar%tmonkey.log
   441       first  = pid_pos;
   442       p1st   = pid_text;
   443       second = tms_pos;
   444       p2nd   = tms;
   445     } else {
   446       // case foo%tbar%pmonkey.log
   447       first  = tms_pos;
   448       p1st   = tms;
   449       second = pid_pos;
   450       p2nd   = pid_text;
   451     }
   452   } else if (pid_pos >= 0) {
   453     // contains %p only
   454     first  = pid_pos;
   455     p1st   = pid_text;
   456   } else if (tms_pos >= 0) {
   457     // contains %t only
   458     first  = tms_pos;
   459     p1st   = tms;
   460   }
   462   int buf_pos = (int)strlen(buf);
   463   const char* tail = nametail;
   465   if (first >= 0) {
   466     tail = nametail + first + 2;
   467     strncpy(&buf[buf_pos], nametail, first);
   468     strcpy(&buf[buf_pos + first], p1st);
   469     buf_pos = (int)strlen(buf);
   470     if (second >= 0) {
   471       strncpy(&buf[buf_pos], tail, second - first - 2);
   472       strcpy(&buf[buf_pos + second - first - 2], p2nd);
   473       tail = nametail + second + 2;
   474     }
   475   }
   476   strcat(buf, tail);      // append rest of name, or all of name
   477   return buf;
   478 }
   480 // log_name comes from -XX:LogFile=log_name, -Xloggc:log_name or
   481 // -XX:DumpLoadedClassList=<file_name>
   482 // in log_name, %p => pid1234 and
   483 //              %t => YYYY-MM-DD_HH-MM-SS
   484 static const char* make_log_name(const char* log_name, const char* force_directory) {
   485   char timestr[32];
   486   get_datetime_string(timestr, sizeof(timestr));
   487   return make_log_name_internal(log_name, force_directory, os::current_process_id(),
   488                                 timestr);
   489 }
   491 #ifndef PRODUCT
   492 void test_loggc_filename() {
   493   int pid;
   494   char  tms[32];
   495   char  i_result[FILENAMEBUFLEN];
   496   const char* o_result;
   497   get_datetime_string(tms, sizeof(tms));
   498   pid = os::current_process_id();
   500   // test.log
   501   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test.log", tms);
   502   o_result = make_log_name_internal("test.log", NULL, pid, tms);
   503   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test.log\", NULL)");
   504   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   506   // test-%t-%p.log
   507   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%s-pid%u.log", tms, pid);
   508   o_result = make_log_name_internal("test-%t-%p.log", NULL, pid, tms);
   509   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t-%%p.log\", NULL)");
   510   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   512   // test-%t%p.log
   513   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%spid%u.log", tms, pid);
   514   o_result = make_log_name_internal("test-%t%p.log", NULL, pid, tms);
   515   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t%%p.log\", NULL)");
   516   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   518   // %p%t.log
   519   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u%s.log", pid, tms);
   520   o_result = make_log_name_internal("%p%t.log", NULL, pid, tms);
   521   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p%%t.log\", NULL)");
   522   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   524   // %p-test.log
   525   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u-test.log", pid);
   526   o_result = make_log_name_internal("%p-test.log", NULL, pid, tms);
   527   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p-test.log\", NULL)");
   528   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   530   // %t.log
   531   jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "%s.log", tms);
   532   o_result = make_log_name_internal("%t.log", NULL, pid, tms);
   533   assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%t.log\", NULL)");
   534   FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
   535 }
   536 #endif // PRODUCT
   538 fileStream::fileStream(const char* file_name) {
   539   _file = fopen(file_name, "w");
   540   if (_file != NULL) {
   541     _need_close = true;
   542   } else {
   543     warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
   544     _need_close = false;
   545   }
   546 }
   548 fileStream::fileStream(const char* file_name, const char* opentype) {
   549   _file = fopen(file_name, opentype);
   550   if (_file != NULL) {
   551     _need_close = true;
   552   } else {
   553     warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
   554     _need_close = false;
   555   }
   556 }
   558 void fileStream::write(const char* s, size_t len) {
   559   if (_file != NULL)  {
   560     // Make an unused local variable to avoid warning from gcc 4.x compiler.
   561     size_t count = fwrite(s, 1, len, _file);
   562   }
   563   update_position(s, len);
   564 }
   566 long fileStream::fileSize() {
   567   long size = -1;
   568   if (_file != NULL) {
   569     long pos  = ::ftell(_file);
   570     if (::fseek(_file, 0, SEEK_END) == 0) {
   571       size = ::ftell(_file);
   572     }
   573     ::fseek(_file, pos, SEEK_SET);
   574   }
   575   return size;
   576 }
   578 char* fileStream::readln(char *data, int count ) {
   579   char * ret = ::fgets(data, count, _file);
   580   //Get rid of annoying \n char
   581   data[::strlen(data)-1] = '\0';
   582   return ret;
   583 }
   585 fileStream::~fileStream() {
   586   if (_file != NULL) {
   587     if (_need_close) fclose(_file);
   588     _file      = NULL;
   589   }
   590 }
   592 void fileStream::flush() {
   593   fflush(_file);
   594 }
   596 fdStream::fdStream(const char* file_name) {
   597   _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
   598   _need_close = true;
   599 }
   601 fdStream::~fdStream() {
   602   if (_fd != -1) {
   603     if (_need_close) close(_fd);
   604     _fd = -1;
   605   }
   606 }
   608 void fdStream::write(const char* s, size_t len) {
   609   if (_fd != -1) {
   610     // Make an unused local variable to avoid warning from gcc 4.x compiler.
   611     size_t count = ::write(_fd, s, (int)len);
   612   }
   613   update_position(s, len);
   614 }
   616 // dump vm version, os version, platform info, build id,
   617 // memory usage and command line flags into header
   618 void gcLogFileStream::dump_loggc_header() {
   619   if (is_open()) {
   620     print_cr("%s", Abstract_VM_Version::internal_vm_info_string());
   621     os::print_memory_info(this);
   622     print("CommandLine flags: ");
   623     CommandLineFlags::printSetFlags(this);
   624   }
   625 }
   627 gcLogFileStream::~gcLogFileStream() {
   628   if (_file != NULL) {
   629     if (_need_close) fclose(_file);
   630     _file = NULL;
   631   }
   632   if (_file_name != NULL) {
   633     FREE_C_HEAP_ARRAY(char, _file_name, mtInternal);
   634     _file_name = NULL;
   635   }
   636 }
   638 gcLogFileStream::gcLogFileStream(const char* file_name) {
   639   _cur_file_num = 0;
   640   _bytes_written = 0L;
   641   _file_name = make_log_name(file_name, NULL);
   643   // gc log file rotation
   644   if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) {
   645     char tempbuf[FILENAMEBUFLEN];
   646     jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
   647     _file = fopen(tempbuf, "w");
   648   } else {
   649     _file = fopen(_file_name, "w");
   650   }
   651   if (_file != NULL) {
   652     _need_close = true;
   653     dump_loggc_header();
   654   } else {
   655     warning("Cannot open file %s due to %s\n", _file_name, strerror(errno));
   656     _need_close = false;
   657   }
   658 }
   660 void gcLogFileStream::write(const char* s, size_t len) {
   661   if (_file != NULL) {
   662     size_t count = fwrite(s, 1, len, _file);
   663     _bytes_written += count;
   664   }
   665   update_position(s, len);
   666 }
   668 // rotate_log must be called from VMThread at safepoint. In case need change parameters
   669 // for gc log rotation from thread other than VMThread, a sub type of VM_Operation
   670 // should be created and be submitted to VMThread's operation queue. DO NOT call this
   671 // function directly. Currently, it is safe to rotate log at safepoint through VMThread.
   672 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to
   673 // write to gc log file at safepoint. If in future, changes made for mutator threads or
   674 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
   675 // must be synchronized.
   676 void gcLogFileStream::rotate_log(bool force, outputStream* out) {
   677   char time_msg[FILENAMEBUFLEN];
   678   char time_str[EXTRACHARLEN];
   679   char current_file_name[FILENAMEBUFLEN];
   680   char renamed_file_name[FILENAMEBUFLEN];
   682   if (!should_rotate(force)) {
   683     return;
   684   }
   686 #ifdef ASSERT
   687   Thread *thread = Thread::current();
   688   assert(thread == NULL ||
   689          (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
   690          "Must be VMThread at safepoint");
   691 #endif
   692   if (NumberOfGCLogFiles == 1) {
   693     // rotate in same file
   694     rewind();
   695     _bytes_written = 0L;
   696     jio_snprintf(time_msg, sizeof(time_msg), "File  %s rotated at %s\n",
   697                  _file_name, os::local_time_string((char *)time_str, sizeof(time_str)));
   698     write(time_msg, strlen(time_msg));
   700     if (out != NULL) {
   701       out->print("%s", time_msg);
   702     }
   704     dump_loggc_header();
   705     return;
   706   }
   708 #if defined(_WINDOWS)
   709 #ifndef F_OK
   710 #define F_OK 0
   711 #endif
   712 #endif // _WINDOWS
   714   // rotate file in names extended_filename.0, extended_filename.1, ...,
   715   // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
   716   // have a form of extended_filename.<i>.current where i is the current rotation
   717   // file number. After it reaches max file size, the file will be saved and renamed
   718   // with .current removed from its tail.
   719   size_t filename_len = strlen(_file_name);
   720   if (_file != NULL) {
   721     jio_snprintf(renamed_file_name, filename_len + EXTRACHARLEN, "%s.%d",
   722                  _file_name, _cur_file_num);
   723     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
   724                  _file_name, _cur_file_num);
   726     const char* msg = force ? "GC log rotation request has been received."
   727                             : "GC log file has reached the maximum size.";
   728     jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n",
   729                      os::local_time_string((char *)time_str, sizeof(time_str)),
   730                                                          msg, renamed_file_name);
   731     write(time_msg, strlen(time_msg));
   733     if (out != NULL) {
   734       out->print("%s", time_msg);
   735     }
   737     fclose(_file);
   738     _file = NULL;
   740     bool can_rename = true;
   741     if (access(current_file_name, F_OK) != 0) {
   742       // current file does not exist?
   743       warning("No source file exists, cannot rename\n");
   744       can_rename = false;
   745     }
   746     if (can_rename) {
   747       if (access(renamed_file_name, F_OK) == 0) {
   748         if (remove(renamed_file_name) != 0) {
   749           warning("Could not delete existing file %s\n", renamed_file_name);
   750           can_rename = false;
   751         }
   752       } else {
   753         // file does not exist, ok to rename
   754       }
   755     }
   756     if (can_rename && rename(current_file_name, renamed_file_name) != 0) {
   757       warning("Could not rename %s to %s\n", _file_name, renamed_file_name);
   758     }
   759   }
   761   _cur_file_num++;
   762   if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
   763   jio_snprintf(current_file_name,  filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
   764                _file_name, _cur_file_num);
   765   _file = fopen(current_file_name, "w");
   767   if (_file != NULL) {
   768     _bytes_written = 0L;
   769     _need_close = true;
   770     // reuse current_file_name for time_msg
   771     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN,
   772                  "%s.%d", _file_name, _cur_file_num);
   773     jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n",
   774                            os::local_time_string((char *)time_str, sizeof(time_str)),
   775                            current_file_name);
   776     write(time_msg, strlen(time_msg));
   778     if (out != NULL) {
   779       out->print("%s", time_msg);
   780     }
   782     dump_loggc_header();
   783     // remove the existing file
   784     if (access(current_file_name, F_OK) == 0) {
   785       if (remove(current_file_name) != 0) {
   786         warning("Could not delete existing file %s\n", current_file_name);
   787       }
   788     }
   789   } else {
   790     warning("failed to open rotation log file %s due to %s\n"
   791             "Turned off GC log file rotation\n",
   792                   _file_name, strerror(errno));
   793     _need_close = false;
   794     FLAG_SET_DEFAULT(UseGCLogFileRotation, false);
   795   }
   796 }
   798 defaultStream* defaultStream::instance = NULL;
   799 int defaultStream::_output_fd = 1;
   800 int defaultStream::_error_fd  = 2;
   801 FILE* defaultStream::_output_stream = stdout;
   802 FILE* defaultStream::_error_stream  = stderr;
   804 #define LOG_MAJOR_VERSION 160
   805 #define LOG_MINOR_VERSION 1
   807 void defaultStream::init() {
   808   _inited = true;
   809   if (LogVMOutput || LogCompilation) {
   810     init_log();
   811   }
   812 }
   814 bool defaultStream::has_log_file() {
   815   // lazily create log file (at startup, LogVMOutput is false even
   816   // if +LogVMOutput is used, because the flags haven't been parsed yet)
   817   // For safer printing during fatal error handling, do not init logfile
   818   // if a VM error has been reported.
   819   if (!_inited && !is_error_reported())  init();
   820   return _log_file != NULL;
   821 }
   823 void defaultStream::init_log() {
   824   // %%% Need a MutexLocker?
   825   const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log";
   826   const char* try_name = make_log_name(log_name, NULL);
   827   fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
   828   if (!file->is_open()) {
   829     // Try again to open the file.
   830     char warnbuf[O_BUFLEN*2];
   831     jio_snprintf(warnbuf, sizeof(warnbuf),
   832                  "Warning:  Cannot open log file: %s\n", try_name);
   833     // Note:  This feature is for maintainer use only.  No need for L10N.
   834     jio_print(warnbuf);
   835     FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
   836     try_name = make_log_name(log_name, os::get_temp_directory());
   837     jio_snprintf(warnbuf, sizeof(warnbuf),
   838                  "Warning:  Forcing option -XX:LogFile=%s\n", try_name);
   839     jio_print(warnbuf);
   840     delete file;
   841     file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
   842   }
   843   FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
   845   if (file->is_open()) {
   846     _log_file = file;
   847     xmlStream* xs = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file);
   848     _outer_xmlStream = xs;
   849     if (this == tty)  xtty = xs;
   850     // Write XML header.
   851     xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>");
   852     // (For now, don't bother to issue a DTD for this private format.)
   853     jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds();
   854     // %%% Should be: jlong time_ms = os::start_time_milliseconds(), if
   855     // we ever get round to introduce that method on the os class
   856     xs->head("hotspot_log version='%d %d'"
   857              " process='%d' time_ms='"INT64_FORMAT"'",
   858              LOG_MAJOR_VERSION, LOG_MINOR_VERSION,
   859              os::current_process_id(), (int64_t)time_ms);
   860     // Write VM version header immediately.
   861     xs->head("vm_version");
   862     xs->head("name"); xs->text("%s", VM_Version::vm_name()); xs->cr();
   863     xs->tail("name");
   864     xs->head("release"); xs->text("%s", VM_Version::vm_release()); xs->cr();
   865     xs->tail("release");
   866     xs->head("info"); xs->text("%s", VM_Version::internal_vm_info_string()); xs->cr();
   867     xs->tail("info");
   868     xs->tail("vm_version");
   869     // Record information about the command-line invocation.
   870     xs->head("vm_arguments");  // Cf. Arguments::print_on()
   871     if (Arguments::num_jvm_flags() > 0) {
   872       xs->head("flags");
   873       Arguments::print_jvm_flags_on(xs->text());
   874       xs->tail("flags");
   875     }
   876     if (Arguments::num_jvm_args() > 0) {
   877       xs->head("args");
   878       Arguments::print_jvm_args_on(xs->text());
   879       xs->tail("args");
   880     }
   881     if (Arguments::java_command() != NULL) {
   882       xs->head("command"); xs->text()->print_cr("%s", Arguments::java_command());
   883       xs->tail("command");
   884     }
   885     if (Arguments::sun_java_launcher() != NULL) {
   886       xs->head("launcher"); xs->text()->print_cr("%s", Arguments::sun_java_launcher());
   887       xs->tail("launcher");
   888     }
   889     if (Arguments::system_properties() !=  NULL) {
   890       xs->head("properties");
   891       // Print it as a java-style property list.
   892       // System properties don't generally contain newlines, so don't bother with unparsing.
   893       for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
   894         xs->text()->print_cr("%s=%s", p->key(), p->value());
   895       }
   896       xs->tail("properties");
   897     }
   898     xs->tail("vm_arguments");
   899     // tty output per se is grouped under the <tty>...</tty> element.
   900     xs->head("tty");
   901     // All further non-markup text gets copied to the tty:
   902     xs->_text = this;  // requires friend declaration!
   903   } else {
   904     delete(file);
   905     // and leave xtty as NULL
   906     LogVMOutput = false;
   907     DisplayVMOutput = true;
   908     LogCompilation = false;
   909   }
   910 }
   912 // finish_log() is called during normal VM shutdown. finish_log_on_error() is
   913 // called by ostream_abort() after a fatal error.
   914 //
   915 void defaultStream::finish_log() {
   916   xmlStream* xs = _outer_xmlStream;
   917   xs->done("tty");
   919   // Other log forks are appended here, at the End of Time:
   920   CompileLog::finish_log(xs->out());  // write compile logging, if any, now
   922   xs->done("hotspot_log");
   923   xs->flush();
   925   fileStream* file = _log_file;
   926   _log_file = NULL;
   928   delete _outer_xmlStream;
   929   _outer_xmlStream = NULL;
   931   file->flush();
   932   delete file;
   933 }
   935 void defaultStream::finish_log_on_error(char *buf, int buflen) {
   936   xmlStream* xs = _outer_xmlStream;
   938   if (xs && xs->out()) {
   940     xs->done_raw("tty");
   942     // Other log forks are appended here, at the End of Time:
   943     CompileLog::finish_log_on_error(xs->out(), buf, buflen);  // write compile logging, if any, now
   945     xs->done_raw("hotspot_log");
   946     xs->flush();
   948     fileStream* file = _log_file;
   949     _log_file = NULL;
   950     _outer_xmlStream = NULL;
   952     if (file) {
   953       file->flush();
   955       // Can't delete or close the file because delete and fclose aren't
   956       // async-safe. We are about to die, so leave it to the kernel.
   957       // delete file;
   958     }
   959   }
   960 }
   962 intx defaultStream::hold(intx writer_id) {
   963   bool has_log = has_log_file();  // check before locking
   964   if (// impossible, but who knows?
   965       writer_id == NO_WRITER ||
   967       // bootstrap problem
   968       tty_lock == NULL ||
   970       // can't grab a lock or call Thread::current() if TLS isn't initialized
   971       ThreadLocalStorage::thread() == NULL ||
   973       // developer hook
   974       !SerializeVMOutput ||
   976       // VM already unhealthy
   977       is_error_reported() ||
   979       // safepoint == global lock (for VM only)
   980       (SafepointSynchronize::is_synchronizing() &&
   981        Thread::current()->is_VM_thread())
   982       ) {
   983     // do not attempt to lock unless we know the thread and the VM is healthy
   984     return NO_WRITER;
   985   }
   986   if (_writer == writer_id) {
   987     // already held, no need to re-grab the lock
   988     return NO_WRITER;
   989   }
   990   tty_lock->lock_without_safepoint_check();
   991   // got the lock
   992   if (writer_id != _last_writer) {
   993     if (has_log) {
   994       _log_file->bol();
   995       // output a hint where this output is coming from:
   996       _log_file->print_cr("<writer thread='" UINTX_FORMAT "'/>", writer_id);
   997     }
   998     _last_writer = writer_id;
   999   }
  1000   _writer = writer_id;
  1001   return writer_id;
  1004 void defaultStream::release(intx holder) {
  1005   if (holder == NO_WRITER) {
  1006     // nothing to release:  either a recursive lock, or we scribbled (too bad)
  1007     return;
  1009   if (_writer != holder) {
  1010     return;  // already unlocked, perhaps via break_tty_lock_for_safepoint
  1012   _writer = NO_WRITER;
  1013   tty_lock->unlock();
  1017 // Yuck:  jio_print does not accept char*/len.
  1018 static void call_jio_print(const char* s, size_t len) {
  1019   char buffer[O_BUFLEN+100];
  1020   if (len > sizeof(buffer)-1) {
  1021     warning("increase O_BUFLEN in ostream.cpp -- output truncated");
  1022     len = sizeof(buffer)-1;
  1024   strncpy(buffer, s, len);
  1025   buffer[len] = '\0';
  1026   jio_print(buffer);
  1030 void defaultStream::write(const char* s, size_t len) {
  1031   intx thread_id = os::current_thread_id();
  1032   intx holder = hold(thread_id);
  1034   if (DisplayVMOutput &&
  1035       (_outer_xmlStream == NULL || !_outer_xmlStream->inside_attrs())) {
  1036     // print to output stream. It can be redirected by a vfprintf hook
  1037     if (s[len] == '\0') {
  1038       jio_print(s);
  1039     } else {
  1040       call_jio_print(s, len);
  1044   // print to log file
  1045   if (has_log_file()) {
  1046     int nl0 = _newlines;
  1047     xmlTextStream::write(s, len);
  1048     // flush the log file too, if there were any newlines
  1049     if (nl0 != _newlines){
  1050       flush();
  1052   } else {
  1053     update_position(s, len);
  1056   release(holder);
  1059 intx ttyLocker::hold_tty() {
  1060   if (defaultStream::instance == NULL)  return defaultStream::NO_WRITER;
  1061   intx thread_id = os::current_thread_id();
  1062   return defaultStream::instance->hold(thread_id);
  1065 void ttyLocker::release_tty(intx holder) {
  1066   if (holder == defaultStream::NO_WRITER)  return;
  1067   defaultStream::instance->release(holder);
  1070 bool ttyLocker::release_tty_if_locked() {
  1071   intx thread_id = os::current_thread_id();
  1072   if (defaultStream::instance->writer() == thread_id) {
  1073     // release the lock and return true so callers know if was
  1074     // previously held.
  1075     release_tty(thread_id);
  1076     return true;
  1078   return false;
  1081 void ttyLocker::break_tty_lock_for_safepoint(intx holder) {
  1082   if (defaultStream::instance != NULL &&
  1083       defaultStream::instance->writer() == holder) {
  1084     if (xtty != NULL) {
  1085       xtty->print_cr("<!-- safepoint while printing -->");
  1087     defaultStream::instance->release(holder);
  1089   // (else there was no lock to break)
  1092 void ostream_init() {
  1093   if (defaultStream::instance == NULL) {
  1094     defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream();
  1095     tty = defaultStream::instance;
  1097     // We want to ensure that time stamps in GC logs consider time 0
  1098     // the time when the JVM is initialized, not the first time we ask
  1099     // for a time stamp. So, here, we explicitly update the time stamp
  1100     // of tty.
  1101     tty->time_stamp().update_to(1);
  1105 void ostream_init_log() {
  1106   // For -Xloggc:<file> option - called in runtime/thread.cpp
  1107   // Note : this must be called AFTER ostream_init()
  1109   gclog_or_tty = tty; // default to tty
  1110   if (Arguments::gc_log_filename() != NULL) {
  1111     fileStream * gclog  = new(ResourceObj::C_HEAP, mtInternal)
  1112                              gcLogFileStream(Arguments::gc_log_filename());
  1113     if (gclog->is_open()) {
  1114       // now we update the time stamp of the GC log to be synced up
  1115       // with tty.
  1116       gclog->time_stamp().update_to(tty->time_stamp().ticks());
  1118     gclog_or_tty = gclog;
  1121 #if INCLUDE_CDS
  1122   // For -XX:DumpLoadedClassList=<file> option
  1123   if (DumpLoadedClassList != NULL) {
  1124     const char* list_name = make_log_name(DumpLoadedClassList, NULL);
  1125     classlist_file = new(ResourceObj::C_HEAP, mtInternal)
  1126                          fileStream(list_name);
  1127     FREE_C_HEAP_ARRAY(char, list_name, mtInternal);
  1129 #endif
  1131   // If we haven't lazily initialized the logfile yet, do it now,
  1132   // to avoid the possibility of lazy initialization during a VM
  1133   // crash, which can affect the stability of the fatal error handler.
  1134   defaultStream::instance->has_log_file();
  1137 // ostream_exit() is called during normal VM exit to finish log files, flush
  1138 // output and free resource.
  1139 void ostream_exit() {
  1140   static bool ostream_exit_called = false;
  1141   if (ostream_exit_called)  return;
  1142   ostream_exit_called = true;
  1143 #if INCLUDE_CDS
  1144   if (classlist_file != NULL) {
  1145     delete classlist_file;
  1147 #endif
  1148   if (gclog_or_tty != tty) {
  1149       delete gclog_or_tty;
  1152       // we temporaly disable PrintMallocFree here
  1153       // as otherwise it'll lead to using of almost deleted
  1154       // tty or defaultStream::instance in logging facility
  1155       // of HeapFree(), see 6391258
  1156       DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);)
  1157       if (tty != defaultStream::instance) {
  1158           delete tty;
  1160       if (defaultStream::instance != NULL) {
  1161           delete defaultStream::instance;
  1164   tty = NULL;
  1165   xtty = NULL;
  1166   gclog_or_tty = NULL;
  1167   defaultStream::instance = NULL;
  1170 // ostream_abort() is called by os::abort() when VM is about to die.
  1171 void ostream_abort() {
  1172   // Here we can't delete gclog_or_tty and tty, just flush their output
  1173   if (gclog_or_tty) gclog_or_tty->flush();
  1174   if (tty) tty->flush();
  1176   if (defaultStream::instance != NULL) {
  1177     static char buf[4096];
  1178     defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
  1182 staticBufferStream::staticBufferStream(char* buffer, size_t buflen,
  1183                                        outputStream *outer_stream) {
  1184   _buffer = buffer;
  1185   _buflen = buflen;
  1186   _outer_stream = outer_stream;
  1187   // compile task prints time stamp relative to VM start
  1188   _stamp.update_to(1);
  1191 void staticBufferStream::write(const char* c, size_t len) {
  1192   _outer_stream->print_raw(c, (int)len);
  1195 void staticBufferStream::flush() {
  1196   _outer_stream->flush();
  1199 void staticBufferStream::print(const char* format, ...) {
  1200   va_list ap;
  1201   va_start(ap, format);
  1202   size_t len;
  1203   const char* str = do_vsnprintf(_buffer, _buflen, format, ap, false, len);
  1204   write(str, len);
  1205   va_end(ap);
  1208 void staticBufferStream::print_cr(const char* format, ...) {
  1209   va_list ap;
  1210   va_start(ap, format);
  1211   size_t len;
  1212   const char* str = do_vsnprintf(_buffer, _buflen, format, ap, true, len);
  1213   write(str, len);
  1214   va_end(ap);
  1217 void staticBufferStream::vprint(const char *format, va_list argptr) {
  1218   size_t len;
  1219   const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, false, len);
  1220   write(str, len);
  1223 void staticBufferStream::vprint_cr(const char* format, va_list argptr) {
  1224   size_t len;
  1225   const char* str = do_vsnprintf(_buffer, _buflen, format, argptr, true, len);
  1226   write(str, len);
  1229 bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
  1230   buffer_length = initial_size;
  1231   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
  1232   buffer_pos    = 0;
  1233   buffer_fixed  = false;
  1234   buffer_max    = bufmax;
  1237 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
  1238   buffer_length = fixed_buffer_size;
  1239   buffer        = fixed_buffer;
  1240   buffer_pos    = 0;
  1241   buffer_fixed  = true;
  1242   buffer_max    = bufmax;
  1245 void bufferedStream::write(const char* s, size_t len) {
  1247   if(buffer_pos + len > buffer_max) {
  1248     flush();
  1251   size_t end = buffer_pos + len;
  1252   if (end >= buffer_length) {
  1253     if (buffer_fixed) {
  1254       // if buffer cannot resize, silently truncate
  1255       len = buffer_length - buffer_pos - 1;
  1256     } else {
  1257       // For small overruns, double the buffer.  For larger ones,
  1258       // increase to the requested size.
  1259       if (end < buffer_length * 2) {
  1260         end = buffer_length * 2;
  1262       buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
  1263       buffer_length = end;
  1266   memcpy(buffer + buffer_pos, s, len);
  1267   buffer_pos += len;
  1268   update_position(s, len);
  1271 char* bufferedStream::as_string() {
  1272   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1);
  1273   strncpy(copy, buffer, buffer_pos);
  1274   copy[buffer_pos] = 0;  // terminating null
  1275   return copy;
  1278 bufferedStream::~bufferedStream() {
  1279   if (!buffer_fixed) {
  1280     FREE_C_HEAP_ARRAY(char, buffer, mtInternal);
  1284 #ifndef PRODUCT
  1286 #if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE)
  1287 #include <sys/types.h>
  1288 #include <sys/socket.h>
  1289 #include <netinet/in.h>
  1290 #include <arpa/inet.h>
  1291 #endif
  1293 // Network access
  1294 networkStream::networkStream() : bufferedStream(1024*10, 1024*10) {
  1296   _socket = -1;
  1298   int result = os::socket(AF_INET, SOCK_STREAM, 0);
  1299   if (result <= 0) {
  1300     assert(false, "Socket could not be created!");
  1301   } else {
  1302     _socket = result;
  1306 int networkStream::read(char *buf, size_t len) {
  1307   return os::recv(_socket, buf, (int)len, 0);
  1310 void networkStream::flush() {
  1311   if (size() != 0) {
  1312     int result = os::raw_send(_socket, (char *)base(), size(), 0);
  1313     assert(result != -1, "connection error");
  1314     assert(result == (int)size(), "didn't send enough data");
  1316   reset();
  1319 networkStream::~networkStream() {
  1320   close();
  1323 void networkStream::close() {
  1324   if (_socket != -1) {
  1325     flush();
  1326     os::socket_close(_socket);
  1327     _socket = -1;
  1331 bool networkStream::connect(const char *ip, short port) {
  1333   struct sockaddr_in server;
  1334   server.sin_family = AF_INET;
  1335   server.sin_port = htons(port);
  1337   server.sin_addr.s_addr = inet_addr(ip);
  1338   if (server.sin_addr.s_addr == (uint32_t)-1) {
  1339     struct hostent* host = os::get_host_by_name((char*)ip);
  1340     if (host != NULL) {
  1341       memcpy(&server.sin_addr, host->h_addr_list[0], host->h_length);
  1342     } else {
  1343       return false;
  1348   int result = os::connect(_socket, (struct sockaddr*)&server, sizeof(struct sockaddr_in));
  1349   return (result >= 0);
  1352 #endif

mercurial