diff mbox

PPC sim: Don't close file descriptors 0, 1, or 2

Message ID 20151117133154.72b61a52@pinnacle.lan
State Superseded
Delegated to: Mike Frysinger
Headers show

Commit Message

Kevin Buettner Nov. 17, 2015, 8:31 p.m. UTC
On Tue, 17 Nov 2015 00:41:33 -0500
Mike Frysinger <vapier@gentoo.org> wrote:

> > So sim/common is doing the same thing as my proposed patch for ppc;
> > sim/common is just using a more elegant mechanism to avoid calling
> > close() on these three file descriptors.
> 
> the difference is that this code sequence misbehaves after your change:
> 	close(1);
> 	write(1, "foo", 3);
> under the common sim, the write will return EBADF.
> 
> considering how much of common/ came from ppc/ i'm a little surprised
> virtualization of the fd table didn't.
> 
> it would be nice if we could at least hide these three fds (a static
> array of 3 bools maybe?), but i won't push hard for you to do that.

Do you mean an array which indicates the open / closed status of each
of stdin, stdout, and stderr?  This status would then be used to
return EBADF in the right places when the descriptor is "closed".

If so, I'm willing to pursue this, but I want to make sure that I
understand what you want.

> you should however be making the same change to the other emulation
> layers.  you've done it only to netbsd, but the ppc sim has the same
> code for the other layers too (like unix, and ...).

I've done this for emul_unix.  I've looked at emul_bugapi.c, emul_chirp.c,
and emul_generic.c, but it doesn't appear that any of these call close().

Here's the revised patch...

    PPC sim: Don't close file descriptors 0, 1, or 2.
    
    In my testing, this patch eliminates all of the "unresolved
    testcases", 229 in current upstream GDB, when testing GDB against the
    powerpc simulator.
    
    E.g. for gdb.base/break.exp, I see this in the log file:
    
        (gdb) next
        66	    return (value); /* set breakpoint 19 here */
        (gdb) PASS: gdb.base/break.exp: next over recursive call
        backtrace
        #0  factorial (value=120) at testsuite/gdb.base/break.c:66
        #1  0x10000428 in factorial (value=6) at testsuite/gdb.base/break.c:64
        #2  0x10000370 in main (argc=1, argv=0xdfffee20, envp=0xdfffee28) at testsuite/gdb.base/break.c:47
        (gdb) PASS: gdb.base/break.exp: backtrace from factorial(5.1)
        continue
        Continuing.
        720
        ERROR: Process no longer exists
        UNRESOLVED: gdb.base/break.exp: continue until exit at recursive next test
    
    With this patch/change in place, this bit of the test runs as expected:
    
        (gdb) next
        66	    return (value); /* set breakpoint 19 here */
        (gdb) PASS: gdb.base/break.exp: next over recursive call
        backtrace
        #0  factorial (value=120) at testsuite/gdb.base/break.c:66
        #1  0x10000428 in factorial (value=6) at testsuite/gdb.base/break.c:64
        #2  0x10000370 in main (argc=1, argv=0xdfffee20, envp=0xdfffee28) at testsuite/gdb.base/break.c:47
        (gdb) PASS: gdb.base/break.exp: backtrace from factorial(5.1)
        continue
        Continuing.
        720
        [Inferior 1 (process 42000) exited normally]
    
    This occurs because the powerpc simulator closes, on behalf of the
    testcase, the file descriptors associated with stdin, stdout, and
    stderr.  GDB still needs these descriptors to communicate with the
    user or, in this case, with the testing framework.
    
    I should also note that in the most (if not all) instances where these
    descriptors are closed, they are closed as part of exit() when GDB
    allows the inferior to run to completion.
    
    sim/ppc/ChangeLog:
    
    	* emul_netbsd.c (do_close): Don't close file descriptors 0, 1,
    	or 2.
    	* emul_unix.c (do_unix_close): Likewise.
---
 sim/ppc/emul_netbsd.c | 9 +++++++--
 sim/ppc/emul_unix.c   | 6 +++++-
 2 files changed, 12 insertions(+), 3 deletions(-)

Comments

Mike Frysinger Nov. 17, 2015, 9:05 p.m. UTC | #1
On 17 Nov 2015 13:31, Kevin Buettner wrote:
> On Tue, 17 Nov 2015 00:41:33 -0500 Mike Frysinger wrote:
> > > So sim/common is doing the same thing as my proposed patch for ppc;
> > > sim/common is just using a more elegant mechanism to avoid calling
> > > close() on these three file descriptors.
> > 
> > the difference is that this code sequence misbehaves after your change:
> > 	close(1);
> > 	write(1, "foo", 3);
> > under the common sim, the write will return EBADF.
> > 
> > considering how much of common/ came from ppc/ i'm a little surprised
> > virtualization of the fd table didn't.
> > 
> > it would be nice if we could at least hide these three fds (a static
> > array of 3 bools maybe?), but i won't push hard for you to do that.
> 
> Do you mean an array which indicates the open / closed status of each
> of stdin, stdout, and stderr?  This status would then be used to
> return EBADF in the right places when the descriptor is "closed".

correct.  maybe add a helper func like the common code does, and then
have every wrapper (read, write, etc...) check that before making the
actual call.  i don't think we have to get fancy and preserve exact
behavior since the standard does not require it; i.e. this code:
	close(2)
	int fd = open(...)
fd would normally be 2, but since we haven't really closed it, you'd
get back a higher fd.
-mike
diff mbox

Patch

diff --git a/sim/ppc/emul_netbsd.c b/sim/ppc/emul_netbsd.c
index 6bef370..5e39975 100644
--- a/sim/ppc/emul_netbsd.c
+++ b/sim/ppc/emul_netbsd.c
@@ -440,8 +440,13 @@  do_close(os_emul_data *emul,
 
   SYS(close);
 
-  /* Can't combine these statements, cuz close sets errno. */
-  status = close(d);
+  /* Do not close stdin, stdout, or stderr. GDB may still need access to
+     these descriptors.  */
+  if (d == 0 || d == 1 || d == 2)
+    status = 0;
+  else
+    status = close(d);
+
   emul_write_status(processor, status, errno);
 }
 
diff --git a/sim/ppc/emul_unix.c b/sim/ppc/emul_unix.c
index d72525d..47b2b98 100644
--- a/sim/ppc/emul_unix.c
+++ b/sim/ppc/emul_unix.c
@@ -310,7 +310,11 @@  do_unix_close(os_emul_data *emul,
   if (WITH_TRACE && ppc_trace[trace_os_emul])
     printf_filtered ("%d", d);
 
-  status = close(d);
+  if (d == 0 || d == 1 || d == 2)
+    status = 0;
+  else
+    status = close(d);
+
   emul_write_status(processor, status, errno);
 }