src/share/vm/utilities/ostream.cpp

changeset 7476
c2844108a708
parent 7089
6e0cb14ce59b
parent 7448
a4fdab16b621
child 7535
7ae4e26cb1e0
child 9327
f96fcd9e1e1b
equal deleted inserted replaced
7264:8538a606a105 7476:c2844108a708
368 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive 368 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
369 extern Mutex* tty_lock; 369 extern Mutex* tty_lock;
370 370
371 #define EXTRACHARLEN 32 371 #define EXTRACHARLEN 32
372 #define CURRENTAPPX ".current" 372 #define CURRENTAPPX ".current"
373 #define FILENAMEBUFLEN 1024
374 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS 373 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS
375 char* get_datetime_string(char *buf, size_t len) { 374 char* get_datetime_string(char *buf, size_t len) {
376 os::local_time_string(buf, len); 375 os::local_time_string(buf, len);
377 int i = (int)strlen(buf); 376 int i = (int)strlen(buf);
378 while (i-- >= 0) { 377 while (i-- >= 0) {
402 strlen(basename) + 1; 401 strlen(basename) + 1;
403 } else { 402 } else {
404 buffer_length = strlen(log_name) + 1; 403 buffer_length = strlen(log_name) + 1;
405 } 404 }
406 405
407 // const char* star = strchr(basename, '*');
408 const char* pts = strstr(basename, "%p"); 406 const char* pts = strstr(basename, "%p");
409 int pid_pos = (pts == NULL) ? -1 : (pts - nametail); 407 int pid_pos = (pts == NULL) ? -1 : (pts - nametail);
410 408
411 if (pid_pos >= 0) { 409 if (pid_pos >= 0) {
412 jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid); 410 jio_snprintf(pid_text, sizeof(pid_text), "pid%u", pid);
415 413
416 pts = strstr(basename, "%t"); 414 pts = strstr(basename, "%t");
417 int tms_pos = (pts == NULL) ? -1 : (pts - nametail); 415 int tms_pos = (pts == NULL) ? -1 : (pts - nametail);
418 if (tms_pos >= 0) { 416 if (tms_pos >= 0) {
419 buffer_length += strlen(tms); 417 buffer_length += strlen(tms);
418 }
419
420 // File name is too long.
421 if (buffer_length > JVM_MAXPATHLEN) {
422 return NULL;
420 } 423 }
421 424
422 // Create big enough buffer. 425 // Create big enough buffer.
423 char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal); 426 char *buf = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
424 427
490 493
491 #ifndef PRODUCT 494 #ifndef PRODUCT
492 void test_loggc_filename() { 495 void test_loggc_filename() {
493 int pid; 496 int pid;
494 char tms[32]; 497 char tms[32];
495 char i_result[FILENAMEBUFLEN]; 498 char i_result[JVM_MAXPATHLEN];
496 const char* o_result; 499 const char* o_result;
497 get_datetime_string(tms, sizeof(tms)); 500 get_datetime_string(tms, sizeof(tms));
498 pid = os::current_process_id(); 501 pid = os::current_process_id();
499 502
500 // test.log 503 // test.log
501 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test.log", tms); 504 jio_snprintf(i_result, JVM_MAXPATHLEN, "test.log", tms);
502 o_result = make_log_name_internal("test.log", NULL, pid, tms); 505 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)"); 506 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); 507 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
505 508
506 // test-%t-%p.log 509 // test-%t-%p.log
507 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%s-pid%u.log", tms, pid); 510 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%s-pid%u.log", tms, pid);
508 o_result = make_log_name_internal("test-%t-%p.log", NULL, pid, tms); 511 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)"); 512 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); 513 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
511 514
512 // test-%t%p.log 515 // test-%t%p.log
513 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "test-%spid%u.log", tms, pid); 516 jio_snprintf(i_result, JVM_MAXPATHLEN, "test-%spid%u.log", tms, pid);
514 o_result = make_log_name_internal("test-%t%p.log", NULL, pid, tms); 517 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)"); 518 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); 519 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
517 520
518 // %p%t.log 521 // %p%t.log
519 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u%s.log", pid, tms); 522 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u%s.log", pid, tms);
520 o_result = make_log_name_internal("%p%t.log", NULL, pid, tms); 523 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)"); 524 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); 525 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
523 526
524 // %p-test.log 527 // %p-test.log
525 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "pid%u-test.log", pid); 528 jio_snprintf(i_result, JVM_MAXPATHLEN, "pid%u-test.log", pid);
526 o_result = make_log_name_internal("%p-test.log", NULL, pid, tms); 529 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)"); 530 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); 531 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
529 532
530 // %t.log 533 // %t.log
531 jio_snprintf(i_result, sizeof(char)*FILENAMEBUFLEN, "%s.log", tms); 534 jio_snprintf(i_result, JVM_MAXPATHLEN, "%s.log", tms);
532 o_result = make_log_name_internal("%t.log", NULL, pid, tms); 535 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)"); 536 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); 537 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
538
539 {
540 // longest filename
541 char longest_name[JVM_MAXPATHLEN];
542 memset(longest_name, 'a', sizeof(longest_name));
543 longest_name[JVM_MAXPATHLEN - 1] = '\0';
544 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
545 assert(strcmp(longest_name, o_result) == 0, err_msg("longest name does not match. expected '%s' but got '%s'", longest_name, o_result));
546 FREE_C_HEAP_ARRAY(char, o_result, mtInternal);
547 }
548
549 {
550 // too long file name
551 char too_long_name[JVM_MAXPATHLEN + 100];
552 int too_long_length = sizeof(too_long_name);
553 memset(too_long_name, 'a', too_long_length);
554 too_long_name[too_long_length - 1] = '\0';
555 o_result = make_log_name_internal((const char*)&too_long_name, NULL, pid, tms);
556 assert(o_result == NULL, err_msg("Too long file name should return NULL, but got '%s'", o_result));
557 }
558
559 {
560 // too long with timestamp
561 char longest_name[JVM_MAXPATHLEN];
562 memset(longest_name, 'a', JVM_MAXPATHLEN);
563 longest_name[JVM_MAXPATHLEN - 3] = '%';
564 longest_name[JVM_MAXPATHLEN - 2] = 't';
565 longest_name[JVM_MAXPATHLEN - 1] = '\0';
566 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
567 assert(o_result == NULL, err_msg("Too long file name after timestamp expansion should return NULL, but got '%s'", o_result));
568 }
569
570 {
571 // too long with pid
572 char longest_name[JVM_MAXPATHLEN];
573 memset(longest_name, 'a', JVM_MAXPATHLEN);
574 longest_name[JVM_MAXPATHLEN - 3] = '%';
575 longest_name[JVM_MAXPATHLEN - 2] = 'p';
576 longest_name[JVM_MAXPATHLEN - 1] = '\0';
577 o_result = make_log_name_internal((const char*)&longest_name, NULL, pid, tms);
578 assert(o_result == NULL, err_msg("Too long file name after pid expansion should return NULL, but got '%s'", o_result));
579 }
535 } 580 }
536 #endif // PRODUCT 581 #endif // PRODUCT
537 582
538 fileStream::fileStream(const char* file_name) { 583 fileStream::fileStream(const char* file_name) {
539 _file = fopen(file_name, "w"); 584 _file = fopen(file_name, "w");
638 gcLogFileStream::gcLogFileStream(const char* file_name) { 683 gcLogFileStream::gcLogFileStream(const char* file_name) {
639 _cur_file_num = 0; 684 _cur_file_num = 0;
640 _bytes_written = 0L; 685 _bytes_written = 0L;
641 _file_name = make_log_name(file_name, NULL); 686 _file_name = make_log_name(file_name, NULL);
642 687
688 if (_file_name == NULL) {
689 warning("Cannot open file %s: file name is too long.\n", file_name);
690 _need_close = false;
691 UseGCLogFileRotation = false;
692 return;
693 }
694
643 // gc log file rotation 695 // gc log file rotation
644 if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) { 696 if (UseGCLogFileRotation && NumberOfGCLogFiles > 1) {
645 char tempbuf[FILENAMEBUFLEN]; 697 char tempbuf[JVM_MAXPATHLEN];
646 jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num); 698 jio_snprintf(tempbuf, sizeof(tempbuf), "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
647 _file = fopen(tempbuf, "w"); 699 _file = fopen(tempbuf, "w");
648 } else { 700 } else {
649 _file = fopen(_file_name, "w"); 701 _file = fopen(_file_name, "w");
650 } 702 }
672 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to 724 // 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 725 // 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 726 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
675 // must be synchronized. 727 // must be synchronized.
676 void gcLogFileStream::rotate_log(bool force, outputStream* out) { 728 void gcLogFileStream::rotate_log(bool force, outputStream* out) {
677 char time_msg[FILENAMEBUFLEN]; 729 char time_msg[O_BUFLEN];
678 char time_str[EXTRACHARLEN]; 730 char time_str[EXTRACHARLEN];
679 char current_file_name[FILENAMEBUFLEN]; 731 char current_file_name[JVM_MAXPATHLEN];
680 char renamed_file_name[FILENAMEBUFLEN]; 732 char renamed_file_name[JVM_MAXPATHLEN];
681 733
682 if (!should_rotate(force)) { 734 if (!should_rotate(force)) {
683 return; 735 return;
684 } 736 }
685 737
714 // rotate file in names extended_filename.0, extended_filename.1, ..., 766 // rotate file in names extended_filename.0, extended_filename.1, ...,
715 // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will 767 // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
716 // have a form of extended_filename.<i>.current where i is the current rotation 768 // 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 769 // file number. After it reaches max file size, the file will be saved and renamed
718 // with .current removed from its tail. 770 // with .current removed from its tail.
719 size_t filename_len = strlen(_file_name);
720 if (_file != NULL) { 771 if (_file != NULL) {
721 jio_snprintf(renamed_file_name, filename_len + EXTRACHARLEN, "%s.%d", 772 jio_snprintf(renamed_file_name, JVM_MAXPATHLEN, "%s.%d",
722 _file_name, _cur_file_num); 773 _file_name, _cur_file_num);
723 jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX, 774 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN,
724 _file_name, _cur_file_num); 775 "%s.%d" CURRENTAPPX, _file_name, _cur_file_num);
776 if (result >= JVM_MAXPATHLEN) {
777 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
778 return;
779 }
725 780
726 const char* msg = force ? "GC log rotation request has been received." 781 const char* msg = force ? "GC log rotation request has been received."
727 : "GC log file has reached the maximum size."; 782 : "GC log file has reached the maximum size.";
728 jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n", 783 jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n",
729 os::local_time_string((char *)time_str, sizeof(time_str)), 784 os::local_time_string((char *)time_str, sizeof(time_str)),
758 } 813 }
759 } 814 }
760 815
761 _cur_file_num++; 816 _cur_file_num++;
762 if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0; 817 if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
763 jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX, 818 int result = jio_snprintf(current_file_name, JVM_MAXPATHLEN, "%s.%d" CURRENTAPPX,
764 _file_name, _cur_file_num); 819 _file_name, _cur_file_num);
820 if (result >= JVM_MAXPATHLEN) {
821 warning("Cannot create new log file name: %s: file name is too long.\n", current_file_name);
822 return;
823 }
824
765 _file = fopen(current_file_name, "w"); 825 _file = fopen(current_file_name, "w");
766 826
767 if (_file != NULL) { 827 if (_file != NULL) {
768 _bytes_written = 0L; 828 _bytes_written = 0L;
769 _need_close = true; 829 _need_close = true;
770 // reuse current_file_name for time_msg 830 // reuse current_file_name for time_msg
771 jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, 831 jio_snprintf(current_file_name, JVM_MAXPATHLEN,
772 "%s.%d", _file_name, _cur_file_num); 832 "%s.%d", _file_name, _cur_file_num);
773 jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n", 833 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)), 834 os::local_time_string((char *)time_str, sizeof(time_str)), current_file_name);
775 current_file_name);
776 write(time_msg, strlen(time_msg)); 835 write(time_msg, strlen(time_msg));
777 836
778 if (out != NULL) { 837 if (out != NULL) {
779 out->print("%s", time_msg); 838 out->print("%s", time_msg);
780 } 839 }
818 // if a VM error has been reported. 877 // if a VM error has been reported.
819 if (!_inited && !is_error_reported()) init(); 878 if (!_inited && !is_error_reported()) init();
820 return _log_file != NULL; 879 return _log_file != NULL;
821 } 880 }
822 881
882 fileStream* defaultStream::open_file(const char* log_name) {
883 const char* try_name = make_log_name(log_name, NULL);
884 if (try_name == NULL) {
885 warning("Cannot open file %s: file name is too long.\n", log_name);
886 return NULL;
887 }
888
889 fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
890 FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
891 if (file->is_open()) {
892 return file;
893 }
894
895 // Try again to open the file in the temp directory.
896 delete file;
897 char warnbuf[O_BUFLEN*2];
898 jio_snprintf(warnbuf, sizeof(warnbuf), "Warning: Cannot open log file: %s\n", log_name);
899 // Note: This feature is for maintainer use only. No need for L10N.
900 jio_print(warnbuf);
901 try_name = make_log_name(log_name, os::get_temp_directory());
902 if (try_name == NULL) {
903 warning("Cannot open file %s: file name is too long for directory %s.\n", log_name, os::get_temp_directory());
904 return NULL;
905 }
906
907 jio_snprintf(warnbuf, sizeof(warnbuf),
908 "Warning: Forcing option -XX:LogFile=%s\n", try_name);
909 jio_print(warnbuf);
910
911 file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name);
912 FREE_C_HEAP_ARRAY(char, try_name, mtInternal);
913 if (file->is_open()) {
914 return file;
915 }
916
917 delete file;
918 return NULL;
919 }
920
823 void defaultStream::init_log() { 921 void defaultStream::init_log() {
824 // %%% Need a MutexLocker? 922 // %%% Need a MutexLocker?
825 const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log"; 923 const char* log_name = LogFile != NULL ? LogFile : "hotspot_%p.log";
826 const char* try_name = make_log_name(log_name, NULL); 924 fileStream* file = open_file(log_name);
827 fileStream* file = new(ResourceObj::C_HEAP, mtInternal) fileStream(try_name); 925
828 if (!file->is_open()) { 926 if (file != NULL) {
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);
844
845 if (file->is_open()) {
846 _log_file = file; 927 _log_file = file;
847 xmlStream* xs = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file); 928 _outer_xmlStream = new(ResourceObj::C_HEAP, mtInternal) xmlStream(file);
848 _outer_xmlStream = xs; 929 start_log();
930 } else {
931 // and leave xtty as NULL
932 LogVMOutput = false;
933 DisplayVMOutput = true;
934 LogCompilation = false;
935 }
936 }
937
938 void defaultStream::start_log() {
939 xmlStream*xs = _outer_xmlStream;
849 if (this == tty) xtty = xs; 940 if (this == tty) xtty = xs;
850 // Write XML header. 941 // Write XML header.
851 xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>"); 942 xs->print_cr("<?xml version='1.0' encoding='UTF-8'?>");
852 // (For now, don't bother to issue a DTD for this private format.) 943 // (For now, don't bother to issue a DTD for this private format.)
853 jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds(); 944 jlong time_ms = os::javaTimeMillis() - tty->time_stamp().milliseconds();
898 xs->tail("vm_arguments"); 989 xs->tail("vm_arguments");
899 // tty output per se is grouped under the <tty>...</tty> element. 990 // tty output per se is grouped under the <tty>...</tty> element.
900 xs->head("tty"); 991 xs->head("tty");
901 // All further non-markup text gets copied to the tty: 992 // All further non-markup text gets copied to the tty:
902 xs->_text = this; // requires friend declaration! 993 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 } 994 }
911 995
912 // finish_log() is called during normal VM shutdown. finish_log_on_error() is 996 // finish_log() is called during normal VM shutdown. finish_log_on_error() is
913 // called by ostream_abort() after a fatal error. 997 // called by ostream_abort() after a fatal error.
914 // 998 //

mercurial