222 void outputStream::date_stamp(bool guard,
223 const char* prefix,
224 const char* suffix) {
225 if (!guard) {
226 return;
227 }
228 print_raw(prefix);
229 static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz";
230 static const int buffer_length = 32;
231 char buffer[buffer_length];
232 const char* iso8601_result = os::iso8601_time(buffer, buffer_length);
233 if (iso8601_result != NULL) {
234 print_raw(buffer);
235 } else {
236 print_raw(error_time);
237 }
238 print_raw(suffix);
239 return;
240 }
241
242 void outputStream::gclog_stamp() {
243 date_stamp(PrintGCDateStamps);
244 stamp(PrintGCTimeStamps);
245 if (PrintGCID) {
246 print("#%u: ", GCId::current());
247 }
248 }
249
250 outputStream& outputStream::indent() {
251 while (_position < _indentation) sp();
252 return *this;
253 }
254
255 void outputStream::print_jlong(jlong value) {
256 print(JLONG_FORMAT, value);
257 }
258
259 void outputStream::print_julong(julong value) {
260 print(JULONG_FORMAT, value);
261 }
262
263 /**
264 * This prints out hex data in a 'windbg' or 'xxd' form, where each line is:
265 * <hex-address>: 8 * <hex-halfword> <ascii translation (optional)>
266 * example:
267 * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............
268 * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... ....
269 * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......]
349 buffer_pos += write_len;
350 }
351
352 // Note that the following does not depend on write_len.
353 // This means that position and count get updated
354 // even when overflow occurs.
355 update_position(s, len);
356 }
357
358 char* stringStream::as_string() {
359 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
360 strncpy(copy, buffer, buffer_pos);
361 copy[buffer_pos] = 0; // terminating null
362 return copy;
363 }
364
365 stringStream::~stringStream() {}
366
367 xmlStream* xtty;
368 outputStream* tty;
369 outputStream* gclog_or_tty;
370 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
371 extern Mutex* tty_lock;
372
373 #define EXTRACHARLEN 32
374 #define CURRENTAPPX ".current"
375 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS
376 char* get_datetime_string(char *buf, size_t len) {
377 os::local_time_string(buf, len);
378 int i = (int)strlen(buf);
379 while (i-- >= 0) {
380 if (buf[i] == ' ') buf[i] = '_';
381 else if (buf[i] == ':') buf[i] = '-';
382 }
383 return buf;
384 }
385
386 static const char* make_log_name_internal(const char* log_name, const char* force_directory,
387 int pid, const char* tms) {
388 const char* basename = log_name;
389 char file_sep = os::file_separator()[0];
465 }
466
467 int buf_pos = (int)strlen(buf);
468 const char* tail = nametail;
469
470 if (first >= 0) {
471 tail = nametail + first + 2;
472 strncpy(&buf[buf_pos], nametail, first);
473 strcpy(&buf[buf_pos + first], p1st);
474 buf_pos = (int)strlen(buf);
475 if (second >= 0) {
476 strncpy(&buf[buf_pos], tail, second - first - 2);
477 strcpy(&buf[buf_pos + second - first - 2], p2nd);
478 tail = nametail + second + 2;
479 }
480 }
481 strcat(buf, tail); // append rest of name, or all of name
482 return buf;
483 }
484
485 // log_name comes from -XX:LogFile=log_name, -Xloggc:log_name or
486 // -XX:DumpLoadedClassList=<file_name>
487 // in log_name, %p => pid1234 and
488 // %t => YYYY-MM-DD_HH-MM-SS
489 static const char* make_log_name(const char* log_name, const char* force_directory) {
490 char timestr[32];
491 get_datetime_string(timestr, sizeof(timestr));
492 return make_log_name_internal(log_name, force_directory, os::current_process_id(),
493 timestr);
494 }
495
496 #ifndef PRODUCT
497 void test_loggc_filename() {
498 int pid;
499 char tms[32];
500 char i_result[JVM_MAXPATHLEN];
501 const char* o_result;
502 get_datetime_string(tms, sizeof(tms));
503 pid = os::current_process_id();
504
505 // test.log
506 jio_snprintf(i_result, JVM_MAXPATHLEN, "test.log", tms);
507 o_result = make_log_name_internal("test.log", NULL, pid, tms);
508 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test.log\", NULL)");
509 FREE_C_HEAP_ARRAY(char, o_result);
510
511 // test-%t-%p.log
512 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%s-pid%u.log", tms, pid);
513 o_result = make_log_name_internal("test-%t-%p.log", NULL, pid, tms);
514 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t-%%p.log\", NULL)");
515 FREE_C_HEAP_ARRAY(char, o_result);
516
517 // test-%t%p.log
518 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%spid%u.log", tms, pid);
519 o_result = make_log_name_internal("test-%t%p.log", NULL, pid, tms);
520 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"test-%%t%%p.log\", NULL)");
521 FREE_C_HEAP_ARRAY(char, o_result);
522
523 // %p%t.log
524 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u%s.log", pid, tms);
525 o_result = make_log_name_internal("%p%t.log", NULL, pid, tms);
526 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p%%t.log\", NULL)");
527 FREE_C_HEAP_ARRAY(char, o_result);
528
529 // %p-test.log
530 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u-test.log", pid);
531 o_result = make_log_name_internal("%p-test.log", NULL, pid, tms);
532 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%p-test.log\", NULL)");
533 FREE_C_HEAP_ARRAY(char, o_result);
534
535 // %t.log
536 jio_snprintf(i_result, JVM_MAXPATHLEN, "%s.log", tms);
537 o_result = make_log_name_internal("%t.log", NULL, pid, tms);
538 assert(strcmp(i_result, o_result) == 0, "failed on testing make_log_name(\"%%t.log\", NULL)");
539 FREE_C_HEAP_ARRAY(char, o_result);
540
541 {
542 // longest filename
543 char longest_name[JVM_MAXPATHLEN];
544 memset(longest_name, 'a', sizeof(longest_name));
545 longest_name[JVM_MAXPATHLEN - 1] = '\0';
546 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
547 assert(strcmp(longest_name, o_result) == 0, "longest name does not match. expected '%s' but got '%s'", longest_name, o_result);
548 FREE_C_HEAP_ARRAY(char, o_result);
549 }
550
551 {
552 // too long file name
553 char too_long_name[JVM_MAXPATHLEN + 100];
554 int too_long_length = sizeof(too_long_name);
555 memset(too_long_name, 'a', too_long_length);
556 too_long_name[too_long_length - 1] = '\0';
557 o_result = make_log_name_internal((const char*)&too_long_name, NULL, pid, tms);
558 assert(o_result == NULL, "Too long file name should return NULL, but got '%s'", o_result);
559 }
560
561 {
562 // too long with timestamp
563 char longest_name[JVM_MAXPATHLEN];
564 memset(longest_name, 'a', JVM_MAXPATHLEN);
565 longest_name[JVM_MAXPATHLEN - 3] = '%';
566 longest_name[JVM_MAXPATHLEN - 2] = 't';
567 longest_name[JVM_MAXPATHLEN - 1] = '\0';
568 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
569 assert(o_result == NULL, "Too long file name after timestamp expansion should return NULL, but got '%s'", o_result);
570 }
571
572 {
573 // too long with pid
574 char longest_name[JVM_MAXPATHLEN];
575 memset(longest_name, 'a', JVM_MAXPATHLEN);
576 longest_name[JVM_MAXPATHLEN - 3] = '%';
577 longest_name[JVM_MAXPATHLEN - 2] = 'p';
578 longest_name[JVM_MAXPATHLEN - 1] = '\0';
579 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
580 assert(o_result == NULL, "Too long file name after pid expansion should return NULL, but got '%s'", o_result);
581 }
582 }
583 #endif // PRODUCT
584
585 fileStream::fileStream(const char* file_name) {
586 _file = fopen(file_name, "w");
587 if (_file != NULL) {
588 _need_close = true;
589 } else {
590 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
591 _need_close = false;
592 }
593 }
594
595 fileStream::fileStream(const char* file_name, const char* opentype) {
596 _file = fopen(file_name, opentype);
597 if (_file != NULL) {
598 _need_close = true;
599 } else {
600 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
601 _need_close = false;
602 }
603 }
604
643 fdStream::fdStream(const char* file_name) {
644 _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
645 _need_close = true;
646 }
647
648 fdStream::~fdStream() {
649 if (_fd != -1) {
650 if (_need_close) close(_fd);
651 _fd = -1;
652 }
653 }
654
655 void fdStream::write(const char* s, size_t len) {
656 if (_fd != -1) {
657 // Make an unused local variable to avoid warning from gcc 4.x compiler.
658 size_t count = ::write(_fd, s, (int)len);
659 }
660 update_position(s, len);
661 }
662
663 // dump vm version, os version, platform info, build id,
664 // memory usage and command line flags into header
665 void gcLogFileStream::dump_loggc_header() {
666 if (is_open()) {
667 print_cr("%s", Abstract_VM_Version::internal_vm_info_string());
668 os::print_memory_info(this);
669 print("CommandLine flags: ");
670 CommandLineFlags::printSetFlags(this);
671 }
672 }
673
674 gcLogFileStream::~gcLogFileStream() {
675 if (_file != NULL) {
676 if (_need_close) fclose(_file);
677 _file = NULL;
678 }
679 if (_file_name != NULL) {
680 FREE_C_HEAP_ARRAY(char, _file_name);
681 _file_name = NULL;
682 }
683 }
684
685 gcLogFileStream::gcLogFileStream(const char* file_name) {
686 _cur_file_num = 0;
687 _bytes_written = 0L;
688 _file_name = make_log_name(file_name, NULL);
689
690 if (_file_name == NULL) {
691 warning("Cannot open file %s: file name is too long.\n", file_name);
692 _need_close = false;
693 UseGCLogFileRotation = false;
694 return;
695 }
696
697 // gc log file rotation
698 if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) {
699 char tempbuf[JVM_MAXPATHLEN];
700 jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
701 _file = fopen(tempbuf, "w");
702 } else {
703 _file = fopen(_file_name, "w");
704 }
705 if (_file != NULL) {
706 _need_close = true;
707 dump_loggc_header();
708 } else {
709 warning("Cannot open file %s due to %s\n", _file_name, strerror(errno));
710 _need_close = false;
711 }
712 }
713
714 void gcLogFileStream::write(const char* s, size_t len) {
715 if (_file != NULL) {
716 size_t count = fwrite(s, 1, len, _file);
717 _bytes_written += count;
718 }
719 update_position(s, len);
720 }
721
722 // rotate_log must be called from VMThread at safepoint. In case need change parameters
723 // for gc log rotation from thread other than VMThread, a sub type of VM_Operation
724 // should be created and be submitted to VMThread's operation queue. DO NOT call this
725 // function directly. Currently, it is safe to rotate log at safepoint through VMThread.
726 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to
727 // write to gc log file at safepoint. If in future, changes made for mutator threads or
728 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
729 // must be synchronized.
730 void gcLogFileStream::rotate_log(bool force, outputStream* out) {
731 char time_msg[O_BUFLEN];
732 char time_str[EXTRACHARLEN];
733 char current_file_name[JVM_MAXPATHLEN];
734 char renamed_file_name[JVM_MAXPATHLEN];
735
736 if (!should_rotate(force)) {
737 return;
738 }
739
740 #ifdef ASSERT
741 Thread *thread = Thread::current_or_null();
742 assert(thread == NULL ||
743 (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
744 "Must be VMThread at safepoint");
745 #endif
746 if (NumberOfGCLogFiles == 1) {
747 // rotate in same file
748 rewind();
749 _bytes_written = 0L;
750 jio_snprintf(time_msg, sizeof(time_msg), "File %s rotated at %s\n",
751 _file_name, os::local_time_string((char *)time_str, sizeof(time_str)));
752 write(time_msg, strlen(time_msg));
753
754 if (out != NULL) {
755 out->print("%s", time_msg);
756 }
757
758 dump_loggc_header();
759 return;
760 }
761
762 #if defined(_WINDOWS)
763 #ifndef F_OK
764 #define F_OK 0
765 #endif
766 #endif // _WINDOWS
767
768 // rotate file in names extended_filename.0, extended_filename.1, ...,
769 // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
770 // have a form of extended_filename.<i>.current where i is the current rotation
771 // file number. After it reaches max file size, the file will be saved and renamed
772 // with .current removed from its tail.
773 if (_file != NULL) {
774 jio_snprintf(renamed_file_name, JVM_MAXPATHLEN, "%s.%d",
775 _file_name, _cur_file_num);
776 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN,
777 "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
778 if (result >= JVM_MAXPATHLEN) {
779 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
780 return;
781 }
782
783 const char* msg = force ? "GC log rotation request has been received."
784 : "GC log file has reached the maximum size.";
785 jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n",
786 os::local_time_string((char *)time_str, sizeof(time_str)),
787 msg, renamed_file_name);
788 write(time_msg, strlen(time_msg));
789
790 if (out != NULL) {
791 out->print("%s", time_msg);
792 }
793
794 fclose(_file);
795 _file = NULL;
796
797 bool can_rename = true;
798 if (access(current_file_name, F_OK) != 0) {
799 // current file does not exist?
800 warning("No source file exists, cannot rename\n");
801 can_rename = false;
802 }
803 if (can_rename) {
804 if (access(renamed_file_name, F_OK) == 0) {
805 if (remove(renamed_file_name) != 0) {
806 warning("Could not delete existing file %s\n", renamed_file_name);
807 can_rename = false;
808 }
809 } else {
810 // file does not exist, ok to rename
811 }
812 }
813 if (can_rename && rename(current_file_name, renamed_file_name) != 0) {
814 warning("Could not rename %s to %s\n", _file_name, renamed_file_name);
815 }
816 }
817
818 _cur_file_num++;
819 if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
820 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN, "%s.%d" CURRENTAPPX,
821 _file_name, _cur_file_num);
822 if (result >= JVM_MAXPATHLEN) {
823 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
824 return;
825 }
826
827 _file = fopen(current_file_name, "w");
828
829 if (_file != NULL) {
830 _bytes_written = 0L;
831 _need_close = true;
832 // reuse current_file_name for time_msg
833 jio_snprintf(current_file_name, JVM_MAXPATHLEN,
834 "%s.%d", _file_name, _cur_file_num);
835 jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n",
836 os::local_time_string((char *)time_str, sizeof(time_str)), current_file_name);
837 write(time_msg, strlen(time_msg));
838
839 if (out != NULL) {
840 out->print("%s", time_msg);
841 }
842
843 dump_loggc_header();
844 // remove the existing file
845 if (access(current_file_name, F_OK) == 0) {
846 if (remove(current_file_name) != 0) {
847 warning("Could not delete existing file %s\n", current_file_name);
848 }
849 }
850 } else {
851 warning("failed to open rotation log file %s due to %s\n"
852 "Turned off GC log file rotation\n",
853 _file_name, strerror(errno));
854 _need_close = false;
855 FLAG_SET_DEFAULT(UseGCLogFileRotation, false);
856 }
857 }
858
859 defaultStream* defaultStream::instance = NULL;
860 int defaultStream::_output_fd = 1;
861 int defaultStream::_error_fd = 2;
862 FILE* defaultStream::_output_stream = stdout;
863 FILE* defaultStream::_error_stream = stderr;
864
865 #define LOG_MAJOR_VERSION 160
866 #define LOG_MINOR_VERSION 1
867
868 void defaultStream::init() {
869 _inited = true;
870 if (LogVMOutput || LogCompilation) {
871 init_log();
872 }
873 }
874
875 bool defaultStream::has_log_file() {
876 // lazily create log file (at startup, LogVMOutput is false even
877 // if +LogVMOutput is used, because the flags haven't been parsed yet)
878 // For safer printing during fatal error handling, do not init logfile
1177 }
1178 defaultStream::instance->release(holder);
1179 }
1180 // (else there was no lock to break)
1181 }
1182
1183 void ostream_init() {
1184 if (defaultStream::instance == NULL) {
1185 defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream();
1186 tty = defaultStream::instance;
1187
1188 // We want to ensure that time stamps in GC logs consider time 0
1189 // the time when the JVM is initialized, not the first time we ask
1190 // for a time stamp. So, here, we explicitly update the time stamp
1191 // of tty.
1192 tty->time_stamp().update_to(1);
1193 }
1194 }
1195
1196 void ostream_init_log() {
1197 // For -Xloggc:<file> option - called in runtime/thread.cpp
1198 // Note : this must be called AFTER ostream_init()
1199
1200 gclog_or_tty = tty; // default to tty
1201 if (Arguments::gc_log_filename() != NULL) {
1202 fileStream * gclog = new(ResourceObj::C_HEAP, mtInternal)
1203 gcLogFileStream(Arguments::gc_log_filename());
1204 if (gclog->is_open()) {
1205 // now we update the time stamp of the GC log to be synced up
1206 // with tty.
1207 gclog->time_stamp().update_to(tty->time_stamp().ticks());
1208 }
1209 gclog_or_tty = gclog;
1210 }
1211
1212 #if INCLUDE_CDS
1213 // For -XX:DumpLoadedClassList=<file> option
1214 if (DumpLoadedClassList != NULL) {
1215 const char* list_name = make_log_name(DumpLoadedClassList, NULL);
1216 classlist_file = new(ResourceObj::C_HEAP, mtInternal)
1217 fileStream(list_name);
1218 FREE_C_HEAP_ARRAY(char, list_name);
1219 }
1220 #endif
1221
1222 // If we haven't lazily initialized the logfile yet, do it now,
1223 // to avoid the possibility of lazy initialization during a VM
1224 // crash, which can affect the stability of the fatal error handler.
1225 defaultStream::instance->has_log_file();
1226 }
1227
1228 // ostream_exit() is called during normal VM exit to finish log files, flush
1229 // output and free resource.
1230 void ostream_exit() {
1231 static bool ostream_exit_called = false;
1232 if (ostream_exit_called) return;
1233 ostream_exit_called = true;
1234 #if INCLUDE_CDS
1235 if (classlist_file != NULL) {
1236 delete classlist_file;
1237 }
1238 #endif
1239 if (gclog_or_tty != tty) {
1240 delete gclog_or_tty;
1241 }
1242 {
1243 // we temporaly disable PrintMallocFree here
1244 // as otherwise it'll lead to using of almost deleted
1245 // tty or defaultStream::instance in logging facility
1246 // of HeapFree(), see 6391258
1247 DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);)
1248 if (tty != defaultStream::instance) {
1249 delete tty;
1250 }
1251 if (defaultStream::instance != NULL) {
1252 delete defaultStream::instance;
1253 }
1254 }
1255 tty = NULL;
1256 xtty = NULL;
1257 gclog_or_tty = NULL;
1258 defaultStream::instance = NULL;
1259 }
1260
1261 // ostream_abort() is called by os::abort() when VM is about to die.
1262 void ostream_abort() {
1263 // Here we can't delete gclog_or_tty and tty, just flush their output
1264 if (gclog_or_tty) gclog_or_tty->flush();
1265 if (tty) tty->flush();
1266
1267 if (defaultStream::instance != NULL) {
1268 static char buf[4096];
1269 defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
1270 }
1271 }
1272
1273 staticBufferStream::staticBufferStream(char* buffer, size_t buflen,
1274 outputStream *outer_stream) {
1275 _buffer = buffer;
1276 _buflen = buflen;
1277 _outer_stream = outer_stream;
1278 // compile task prints time stamp relative to VM start
1279 _stamp.update_to(1);
1280 }
1281
1282 void staticBufferStream::write(const char* c, size_t len) {
1283 _outer_stream->print_raw(c, (int)len);
1284 }
|
222 void outputStream::date_stamp(bool guard,
223 const char* prefix,
224 const char* suffix) {
225 if (!guard) {
226 return;
227 }
228 print_raw(prefix);
229 static const char error_time[] = "yyyy-mm-ddThh:mm:ss.mmm+zzzz";
230 static const int buffer_length = 32;
231 char buffer[buffer_length];
232 const char* iso8601_result = os::iso8601_time(buffer, buffer_length);
233 if (iso8601_result != NULL) {
234 print_raw(buffer);
235 } else {
236 print_raw(error_time);
237 }
238 print_raw(suffix);
239 return;
240 }
241
242 outputStream& outputStream::indent() {
243 while (_position < _indentation) sp();
244 return *this;
245 }
246
247 void outputStream::print_jlong(jlong value) {
248 print(JLONG_FORMAT, value);
249 }
250
251 void outputStream::print_julong(julong value) {
252 print(JULONG_FORMAT, value);
253 }
254
255 /**
256 * This prints out hex data in a 'windbg' or 'xxd' form, where each line is:
257 * <hex-address>: 8 * <hex-halfword> <ascii translation (optional)>
258 * example:
259 * 0000000: 7f44 4f46 0102 0102 0000 0000 0000 0000 .DOF............
260 * 0000010: 0000 0000 0000 0040 0000 0020 0000 0005 .......@... ....
261 * 0000020: 0000 0000 0000 0040 0000 0000 0000 015d .......@.......]
341 buffer_pos += write_len;
342 }
343
344 // Note that the following does not depend on write_len.
345 // This means that position and count get updated
346 // even when overflow occurs.
347 update_position(s, len);
348 }
349
350 char* stringStream::as_string() {
351 char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
352 strncpy(copy, buffer, buffer_pos);
353 copy[buffer_pos] = 0; // terminating null
354 return copy;
355 }
356
357 stringStream::~stringStream() {}
358
359 xmlStream* xtty;
360 outputStream* tty;
361 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
362 extern Mutex* tty_lock;
363
364 #define EXTRACHARLEN 32
365 #define CURRENTAPPX ".current"
366 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS
367 char* get_datetime_string(char *buf, size_t len) {
368 os::local_time_string(buf, len);
369 int i = (int)strlen(buf);
370 while (i-- >= 0) {
371 if (buf[i] == ' ') buf[i] = '_';
372 else if (buf[i] == ':') buf[i] = '-';
373 }
374 return buf;
375 }
376
377 static const char* make_log_name_internal(const char* log_name, const char* force_directory,
378 int pid, const char* tms) {
379 const char* basename = log_name;
380 char file_sep = os::file_separator()[0];
456 }
457
458 int buf_pos = (int)strlen(buf);
459 const char* tail = nametail;
460
461 if (first >= 0) {
462 tail = nametail + first + 2;
463 strncpy(&buf[buf_pos], nametail, first);
464 strcpy(&buf[buf_pos + first], p1st);
465 buf_pos = (int)strlen(buf);
466 if (second >= 0) {
467 strncpy(&buf[buf_pos], tail, second - first - 2);
468 strcpy(&buf[buf_pos + second - first - 2], p2nd);
469 tail = nametail + second + 2;
470 }
471 }
472 strcat(buf, tail); // append rest of name, or all of name
473 return buf;
474 }
475
476 // log_name comes from -XX:LogFile=log_name or
477 // -XX:DumpLoadedClassList=<file_name>
478 // in log_name, %p => pid1234 and
479 // %t => YYYY-MM-DD_HH-MM-SS
480 static const char* make_log_name(const char* log_name, const char* force_directory) {
481 char timestr[32];
482 get_datetime_string(timestr, sizeof(timestr));
483 return make_log_name_internal(log_name, force_directory, os::current_process_id(),
484 timestr);
485 }
486
487 fileStream::fileStream(const char* file_name) {
488 _file = fopen(file_name, "w");
489 if (_file != NULL) {
490 _need_close = true;
491 } else {
492 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
493 _need_close = false;
494 }
495 }
496
497 fileStream::fileStream(const char* file_name, const char* opentype) {
498 _file = fopen(file_name, opentype);
499 if (_file != NULL) {
500 _need_close = true;
501 } else {
502 warning("Cannot open file %s due to %s\n", file_name, strerror(errno));
503 _need_close = false;
504 }
505 }
506
545 fdStream::fdStream(const char* file_name) {
546 _fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
547 _need_close = true;
548 }
549
550 fdStream::~fdStream() {
551 if (_fd != -1) {
552 if (_need_close) close(_fd);
553 _fd = -1;
554 }
555 }
556
557 void fdStream::write(const char* s, size_t len) {
558 if (_fd != -1) {
559 // Make an unused local variable to avoid warning from gcc 4.x compiler.
560 size_t count = ::write(_fd, s, (int)len);
561 }
562 update_position(s, len);
563 }
564
565 defaultStream* defaultStream::instance = NULL;
566 int defaultStream::_output_fd = 1;
567 int defaultStream::_error_fd = 2;
568 FILE* defaultStream::_output_stream = stdout;
569 FILE* defaultStream::_error_stream = stderr;
570
571 #define LOG_MAJOR_VERSION 160
572 #define LOG_MINOR_VERSION 1
573
574 void defaultStream::init() {
575 _inited = true;
576 if (LogVMOutput || LogCompilation) {
577 init_log();
578 }
579 }
580
581 bool defaultStream::has_log_file() {
582 // lazily create log file (at startup, LogVMOutput is false even
583 // if +LogVMOutput is used, because the flags haven't been parsed yet)
584 // For safer printing during fatal error handling, do not init logfile
883 }
884 defaultStream::instance->release(holder);
885 }
886 // (else there was no lock to break)
887 }
888
889 void ostream_init() {
890 if (defaultStream::instance == NULL) {
891 defaultStream::instance = new(ResourceObj::C_HEAP, mtInternal) defaultStream();
892 tty = defaultStream::instance;
893
894 // We want to ensure that time stamps in GC logs consider time 0
895 // the time when the JVM is initialized, not the first time we ask
896 // for a time stamp. So, here, we explicitly update the time stamp
897 // of tty.
898 tty->time_stamp().update_to(1);
899 }
900 }
901
902 void ostream_init_log() {
903 // Note : this must be called AFTER ostream_init()
904
905 #if INCLUDE_CDS
906 // For -XX:DumpLoadedClassList=<file> option
907 if (DumpLoadedClassList != NULL) {
908 const char* list_name = make_log_name(DumpLoadedClassList, NULL);
909 classlist_file = new(ResourceObj::C_HEAP, mtInternal)
910 fileStream(list_name);
911 FREE_C_HEAP_ARRAY(char, list_name);
912 }
913 #endif
914
915 // If we haven't lazily initialized the logfile yet, do it now,
916 // to avoid the possibility of lazy initialization during a VM
917 // crash, which can affect the stability of the fatal error handler.
918 defaultStream::instance->has_log_file();
919 }
920
921 // ostream_exit() is called during normal VM exit to finish log files, flush
922 // output and free resource.
923 void ostream_exit() {
924 static bool ostream_exit_called = false;
925 if (ostream_exit_called) return;
926 ostream_exit_called = true;
927 #if INCLUDE_CDS
928 if (classlist_file != NULL) {
929 delete classlist_file;
930 }
931 #endif
932 {
933 // we temporaly disable PrintMallocFree here
934 // as otherwise it'll lead to using of almost deleted
935 // tty or defaultStream::instance in logging facility
936 // of HeapFree(), see 6391258
937 DEBUG_ONLY(FlagSetting fs(PrintMallocFree, false);)
938 if (tty != defaultStream::instance) {
939 delete tty;
940 }
941 if (defaultStream::instance != NULL) {
942 delete defaultStream::instance;
943 }
944 }
945 tty = NULL;
946 xtty = NULL;
947 defaultStream::instance = NULL;
948 }
949
950 // ostream_abort() is called by os::abort() when VM is about to die.
951 void ostream_abort() {
952 // Here we can't delete tty, just flush its output
953 if (tty) tty->flush();
954
955 if (defaultStream::instance != NULL) {
956 static char buf[4096];
957 defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
958 }
959 }
960
961 staticBufferStream::staticBufferStream(char* buffer, size_t buflen,
962 outputStream *outer_stream) {
963 _buffer = buffer;
964 _buflen = buflen;
965 _outer_stream = outer_stream;
966 // compile task prints time stamp relative to VM start
967 _stamp.update_to(1);
968 }
969
970 void staticBufferStream::write(const char* c, size_t len) {
971 _outer_stream->print_raw(c, (int)len);
972 }
|