comparison src/os/windows/vm/os_windows.cpp @ 4750:b16494a69d3d

7126185: Clean up lasterror handling, add os::get_last_error() Summary: Add os::get_last_error(), replace getLastErrorString() by os::lasterror() in os_windows.cpp. Reviewed-by: kamg, dholmes Contributed-by: erik.gahlin@oracle.com
author phh
date Tue, 03 Jan 2012 15:11:31 -0500
parents 4b18532913c7
children 2b3acb34791f
comparison
equal deleted inserted replaced
4749:7ab5f6318694 4750:b16494a69d3d
130 #endif 130 #endif
131 131
132 // save DLL module handle, used by GetModuleFileName 132 // save DLL module handle, used by GetModuleFileName
133 133
134 HINSTANCE vm_lib_handle; 134 HINSTANCE vm_lib_handle;
135 static int getLastErrorString(char *buf, size_t len);
136 135
137 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) { 136 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
138 switch (reason) { 137 switch (reason) {
139 case DLL_PROCESS_ATTACH: 138 case DLL_PROCESS_ATTACH:
140 vm_lib_handle = hinst; 139 vm_lib_handle = hinst;
1450 if (result != NULL) 1449 if (result != NULL)
1451 { 1450 {
1452 return result; 1451 return result;
1453 } 1452 }
1454 1453
1455 long errcode = GetLastError(); 1454 DWORD errcode = GetLastError();
1456 if (errcode == ERROR_MOD_NOT_FOUND) { 1455 if (errcode == ERROR_MOD_NOT_FOUND) {
1457 strncpy(ebuf, "Can't find dependent libraries", ebuflen-1); 1456 strncpy(ebuf, "Can't find dependent libraries", ebuflen-1);
1458 ebuf[ebuflen-1]='\0'; 1457 ebuf[ebuflen-1]='\0';
1459 return NULL; 1458 return NULL;
1460 } 1459 }
1461 1460
1462 // Parsing dll below 1461 // Parsing dll below
1463 // If we can read dll-info and find that dll was built 1462 // If we can read dll-info and find that dll was built
1464 // for an architecture other than Hotspot is running in 1463 // for an architecture other than Hotspot is running in
1465 // - then print to buffer "DLL was built for a different architecture" 1464 // - then print to buffer "DLL was built for a different architecture"
1466 // else call getLastErrorString to obtain system error message 1465 // else call os::lasterror to obtain system error message
1467 1466
1468 // Read system error message into ebuf 1467 // Read system error message into ebuf
1469 // It may or may not be overwritten below (in the for loop and just above) 1468 // It may or may not be overwritten below (in the for loop and just above)
1470 getLastErrorString(ebuf, (size_t) ebuflen); 1469 lasterror(ebuf, (size_t) ebuflen);
1471 ebuf[ebuflen-1]='\0'; 1470 ebuf[ebuflen-1]='\0';
1472 int file_descriptor=::open(name, O_RDONLY | O_BINARY, 0); 1471 int file_descriptor=::open(name, O_RDONLY | O_BINARY, 0);
1473 if (file_descriptor<0) 1472 if (file_descriptor<0)
1474 { 1473 {
1475 return NULL; 1474 return NULL;
1498 ); 1497 );
1499 1498
1500 ::close(file_descriptor); 1499 ::close(file_descriptor);
1501 if (failed_to_get_lib_arch) 1500 if (failed_to_get_lib_arch)
1502 { 1501 {
1503 // file i/o error - report getLastErrorString(...) msg 1502 // file i/o error - report os::lasterror(...) msg
1504 return NULL; 1503 return NULL;
1505 } 1504 }
1506 1505
1507 typedef struct 1506 typedef struct
1508 { 1507 {
1541 1540
1542 assert(running_arch_str, 1541 assert(running_arch_str,
1543 "Didn't find runing architecture code in arch_array"); 1542 "Didn't find runing architecture code in arch_array");
1544 1543
1545 // If the architure is right 1544 // If the architure is right
1546 // but some other error took place - report getLastErrorString(...) msg 1545 // but some other error took place - report os::lasterror(...) msg
1547 if (lib_arch == running_arch) 1546 if (lib_arch == running_arch)
1548 { 1547 {
1549 return NULL; 1548 return NULL;
1550 } 1549 }
1551 1550
1773 } 1772 }
1774 1773
1775 // This method is a copy of JDK's sysGetLastErrorString 1774 // This method is a copy of JDK's sysGetLastErrorString
1776 // from src/windows/hpi/src/system_md.c 1775 // from src/windows/hpi/src/system_md.c
1777 1776
1778 size_t os::lasterror(char *buf, size_t len) { 1777 size_t os::lasterror(char* buf, size_t len) {
1779 long errval; 1778 DWORD errval;
1780 1779
1781 if ((errval = GetLastError()) != 0) { 1780 if ((errval = GetLastError()) != 0) {
1782 /* DOS error */ 1781 // DOS error
1783 int n = (int)FormatMessage( 1782 size_t n = (size_t)FormatMessage(
1784 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, 1783 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
1785 NULL, 1784 NULL,
1786 errval, 1785 errval,
1787 0, 1786 0,
1788 buf, 1787 buf,
1789 (DWORD)len, 1788 (DWORD)len,
1790 NULL); 1789 NULL);
1791 if (n > 3) { 1790 if (n > 3) {
1792 /* Drop final '.', CR, LF */ 1791 // Drop final '.', CR, LF
1793 if (buf[n - 1] == '\n') n--; 1792 if (buf[n - 1] == '\n') n--;
1794 if (buf[n - 1] == '\r') n--; 1793 if (buf[n - 1] == '\r') n--;
1795 if (buf[n - 1] == '.') n--; 1794 if (buf[n - 1] == '.') n--;
1796 buf[n] = '\0'; 1795 buf[n] = '\0';
1797 } 1796 }
1798 return n; 1797 return n;
1799 } 1798 }
1800 1799
1801 if (errno != 0) { 1800 if (errno != 0) {
1802 /* C runtime error that has no corresponding DOS error code */ 1801 // C runtime error that has no corresponding DOS error code
1803 const char *s = strerror(errno); 1802 const char* s = strerror(errno);
1804 size_t n = strlen(s); 1803 size_t n = strlen(s);
1805 if (n >= len) n = len - 1; 1804 if (n >= len) n = len - 1;
1806 strncpy(buf, s, n); 1805 strncpy(buf, s, n);
1807 buf[n] = '\0'; 1806 buf[n] = '\0';
1808 return n; 1807 return n;
1809 } 1808 }
1809
1810 return 0; 1810 return 0;
1811 }
1812
1813 int os::get_last_error() {
1814 DWORD error = GetLastError();
1815 if (error == 0)
1816 error = errno;
1817 return (int)error;
1811 } 1818 }
1812 1819
1813 // sun.misc.Signal 1820 // sun.misc.Signal
1814 // NOTE that this is a workaround for an apparent kernel bug where if 1821 // NOTE that this is a workaround for an apparent kernel bug where if
1815 // a signal handler for SIGBREAK is installed then that signal handler 1822 // a signal handler for SIGBREAK is installed then that signal handler
4744 tty->print_cr("C heap has been corrupted (time: %d allocations)", mallocDebugCounter); 4751 tty->print_cr("C heap has been corrupted (time: %d allocations)", mallocDebugCounter);
4745 tty->print_cr("corrupted block near address %#x, length %d", phe.lpData, phe.cbData); 4752 tty->print_cr("corrupted block near address %#x, length %d", phe.lpData, phe.cbData);
4746 fatal("corrupted C heap"); 4753 fatal("corrupted C heap");
4747 } 4754 }
4748 } 4755 }
4749 int err = GetLastError(); 4756 DWORD err = GetLastError();
4750 if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) { 4757 if (err != ERROR_NO_MORE_ITEMS && err != ERROR_CALL_NOT_IMPLEMENTED) {
4751 fatal(err_msg("heap walk aborted with error %d", err)); 4758 fatal(err_msg("heap walk aborted with error %d", err));
4752 } 4759 }
4753 HeapUnlock(heap); 4760 HeapUnlock(heap);
4754 } 4761 }
4775 return EXCEPTION_CONTINUE_EXECUTION; 4782 return EXCEPTION_CONTINUE_EXECUTION;
4776 } 4783 }
4777 4784
4778 return EXCEPTION_CONTINUE_SEARCH; 4785 return EXCEPTION_CONTINUE_SEARCH;
4779 } 4786 }
4780
4781 static int getLastErrorString(char *buf, size_t len)
4782 {
4783 long errval;
4784
4785 if ((errval = GetLastError()) != 0)
4786 {
4787 /* DOS error */
4788 size_t n = (size_t)FormatMessage(
4789 FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
4790 NULL,
4791 errval,
4792 0,
4793 buf,
4794 (DWORD)len,
4795 NULL);
4796 if (n > 3) {
4797 /* Drop final '.', CR, LF */
4798 if (buf[n - 1] == '\n') n--;
4799 if (buf[n - 1] == '\r') n--;
4800 if (buf[n - 1] == '.') n--;
4801 buf[n] = '\0';
4802 }
4803 return (int)n;
4804 }
4805
4806 if (errno != 0)
4807 {
4808 /* C runtime error that has no corresponding DOS error code */
4809 const char *s = strerror(errno);
4810 size_t n = strlen(s);
4811 if (n >= len) n = len - 1;
4812 strncpy(buf, s, n);
4813 buf[n] = '\0';
4814 return (int)n;
4815 }
4816 return 0;
4817 }
4818
4819 4787
4820 // We don't build a headless jre for Windows 4788 // We don't build a headless jre for Windows
4821 bool os::is_headless_jre() { return false; } 4789 bool os::is_headless_jre() { return false; }
4822 4790
4823 4791