United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7126185 Clean up lasterror handling, add os::get_last_error()
JDK-7126185 : Clean up lasterror handling, add os::get_last_error()

Details
Type:
Enhancement
Submit Date:
2011-12-30
Status:
Closed
Updated Date:
2012-03-29
Project Name:
JDK
Resolved Date:
2012-03-29
Component:
hotspot
OS:
generic
Sub-Component:
runtime
CPU:
generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
hs23
Fixed Versions:
hs23 (b10)

Related Reports
Backport:
Backport:

Sub Tasks

Description
In os_windows.cpp, os::lasterror and getLastErrorString are identical.  The
latter should be eliminated.  In addition, Java Flight Recorder uses errno
and its Windows equivalent for error reporting, so add os::get_last_error()
to retrieve it.

                                    

Comments
SUGGESTED FIX

> diff os.hpp
503d502
<   static int get_last_error();

> diff os_posix.cpp
62,65d61
< int os::get_last_error() {
<   return errno;
< }
< 

> diff os_windows.cpp
134a135
> static int getLastErrorString(char *buf, size_t len);
1465c1466
<   // else call os::lasterror to obtain system error message
---
>   // else call getLastErrorString to obtain system error message
1469c1470
<   lasterror(ebuf, (size_t) ebuflen);
---
>   getLastErrorString(ebuf, (size_t) ebuflen);
1502c1503
<     // file i/o error - report os::lasterror(...) msg
---
>     // file i/o error - report getLastErrorString(...) msg
1545c1546
<   // but some other error took place - report os::lasterror(...) msg
---
>   // but some other error took place - report getLastErrorString(...) msg
1812,1818d1812
< int os::get_last_error() {
<   int error = GetLastError();
<   if (error == 0)
<     error = errno;
<   return error;
< }
< 
4786a4781,4819
> static int getLastErrorString(char *buf, size_t len)
> {
>     long errval;
> 
>     if ((errval = GetLastError()) != 0)
>     {
>       /* DOS error */
>       size_t n = (size_t)FormatMessage(
>             FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
>             NULL,
>             errval,
>             0,
>             buf,
>             (DWORD)len,
>             NULL);
>       if (n > 3) {
>         /* Drop final '.', CR, LF */
>         if (buf[n - 1] == '\n') n--;
>         if (buf[n - 1] == '\r') n--;
>         if (buf[n - 1] == '.') n--;
>         buf[n] = '\0';
>       }
>       return (int)n;
>     }
> 
>     if (errno != 0)
>     {
>       /* C runtime error that has no corresponding DOS error code */
>       const char *s = strerror(errno);
>       size_t n = strlen(s);
>       if (n >= len) n = len - 1;
>       strncpy(buf, s, n);
>       buf[n] = '\0';
>       return (int)n;
>     }
>     return 0;
> }
> 
>
                                     
2011-12-30
EVALUATION

Ok.
                                     
2011-12-30
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-rt/hotspot/rev/b16494a69d3d
                                     
2012-01-04
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-main/hotspot/rev/b16494a69d3d
                                     
2012-01-07
EVALUATION

http://hg.openjdk.java.net/lambda/lambda/hotspot/rev/b16494a69d3d
                                     
2012-03-22



Hardware and Software, Engineered to Work Together