src/share/vm/utilities/ostream.cpp

Tue, 24 Jul 2018 13:22:11 +0800

author
aoqi
date
Tue, 24 Jul 2018 13:22:11 +0800
changeset 9137
dc1769738300
parent 7535
7ae4e26cb1e0
child 9448
73d689add964
permissions
-rw-r--r--

#7048 added Loongson release info to hs_err crash files

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

mercurial