This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[patch] Simulator stdin, stdout, stderr bug
- From: Jesper Nilsson <jesper dot nilsson at axis dot com>
- To: gdb-patches at sourceware dot org
- Cc: jesper dot nilsson at axis dot com, hans-peter dot nilsson at axis dot com
- Date: Wed, 10 Oct 2007 14:18:08 +0200
- Subject: [patch] Simulator stdin, stdout, stderr bug
- References: <20071008145708.GF12687@axis.com>
Hi,
Here is an updated patch, which adds the same changes for
stdout and stderr.
This corrects the xfail in sim/testsuite/sim/cris/c/freopen1.c.
Included in the patch is also the stdin testcase, as
sim/testsuite/sim/cris/c/freopen2.c.
Tested with make -k check-sim 'RUNTESTFLAGS=--target_board=cris-sim'
for cris-axis-elf on i686-pc-linux-gnu.
Best regards,
/^Jesper Nilsson
sim/common/ChangeLog:
2007-10-10 Jesper Nilsson <jesper.nilsson@axis.com>
* callback.c (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add functions.
* syscall.c (cb_syscall): Test for stdin/out/err, not just fd 0/1/2.
include/gdb/ChangeLog:
2007-10-10 Jesper Nilsson <jesper.nilsson@axis.com>
* callback.h (cb_is_stdin, cb_is_stdout, cb_is_stderr): Add prototypes.
sim/testsuite/ChangeLog:
2007-10-10 Jesper Nilsson <jesper.nilsson@axis.com>
* sim/cris/c/freopen2.c: Added testcase.
Index: include/gdb/callback.h
===================================================================
RCS file: /cvs/src/src/include/gdb/callback.h,v
retrieving revision 1.11
diff -u -u -r1.11 callback.h
--- include/gdb/callback.h 23 Aug 2007 18:17:33 -0000 1.11
+++ include/gdb/callback.h 10 Oct 2007 12:00:32 -0000
@@ -315,6 +315,11 @@
/* Translate a value to target endian. */
void cb_store_target_endian PARAMS ((host_callback *, char *, int, long));
+/* Tests for special fds. */
+int cb_is_stdin PARAMS ((host_callback *, int));
+int cb_is_stdout PARAMS ((host_callback *, int));
+int cb_is_stderr PARAMS ((host_callback *, int));
+
/* Perform a system call. */
CB_RC cb_syscall PARAMS ((host_callback *, CB_SYSCALL *));
Index: sim/common/callback.c
===================================================================
RCS file: /cvs/src/src/sim/common/callback.c,v
retrieving revision 1.20
diff -u -u -r1.20 callback.c
--- sim/common/callback.c 24 Aug 2007 14:28:35 -0000 1.20
+++ sim/common/callback.c 10 Oct 2007 12:00:34 -0000
@@ -1136,3 +1136,26 @@
p->evprintf_filtered (p, fmt, ap);
va_end (ap);
}
+
+int
+cb_is_stdin (cb, fd)
+ host_callback *cb;
+ int fd;
+{
+ return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 0;
+}
+int
+cb_is_stdout (cb, fd)
+ host_callback *cb;
+ int fd;
+{
+ return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 1;
+}
+int
+cb_is_stderr (cb, fd)
+ host_callback *cb;
+ int fd;
+{
+ return fdbad (cb, fd) ? 0 : fdmap (cb, fd) == 2;
+}
+
Index: sim/common/syscall.c
===================================================================
RCS file: /cvs/src/src/sim/common/syscall.c,v
retrieving revision 1.11
diff -u -u -r1.11 syscall.c
--- sim/common/syscall.c 24 Aug 2007 14:28:35 -0000 1.11
+++ sim/common/syscall.c 10 Oct 2007 12:00:34 -0000
@@ -291,7 +291,7 @@
while (count > 0)
{
- if (fd == 0)
+ if (cb_is_stdin(cb, fd))
result = (int) (*cb->read_stdin) (cb, buf,
(count < FILE_XFR_SIZE
? count : FILE_XFR_SIZE));
@@ -344,12 +344,12 @@
errcode = EINVAL;
goto FinishSyscall;
}
- if (fd == 1)
+ if (cb_is_stdout(cb, fd))
{
result = (int) (*cb->write_stdout) (cb, buf, bytes_read);
(*cb->flush_stdout) (cb);
}
- else if (fd == 2)
+ else if (cb_is_stderr(cb, fd))
{
result = (int) (*cb->write_stderr) (cb, buf, bytes_read);
(*cb->flush_stderr) (cb);
Index: sim/testsuite/sim/cris/c/freopen1.c
===================================================================
RCS file: /cvs/src/src/sim/testsuite/sim/cris/c/freopen1.c,v
retrieving revision 1.1
diff -u -u -r1.1 freopen1.c
--- sim/testsuite/sim/cris/c/freopen1.c 21 Nov 2005 04:48:19 -0000 1.1
+++ sim/testsuite/sim/cris/c/freopen1.c 10 Oct 2007 12:00:35 -0000
@@ -1,7 +1,4 @@
-/* Check that basic freopen functionality works.
-#xfail: *-*-*
- Currently doesn't work, because syscall.c:cb_syscall case
- CB_SYS_write intercepts writes to fd 1 and 2. */
+/* Check that basic freopen functionality works. */
#include <stdio.h>
#include <stdlib.h>
--- /dev/null 2007-10-06 06:38:38.348072750 +0200
+++ sim/testsuite/sim/cris/c/freopen2.c 2007-10-10 13:51:01.010578000 +0200
@@ -0,0 +1,40 @@
+/* Tests that stdin can be redirected from a normal file. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+main (void)
+{
+ const char* fname = "freopen.dat";
+ const char tsttxt[]
+ = "A random line of text, used to test correct freopen etc.\n";
+ FILE* instream;
+ FILE *old_stderr;
+ char c1;
+
+ /* Like the freopen call in flex. */
+ old_stderr = freopen (fname, "w+", stderr);
+ if (old_stderr == NULL
+ || fwrite (tsttxt, 1, strlen (tsttxt), stderr) != strlen (tsttxt)
+ || fclose (stderr) != 0)
+ {
+ printf ("fail\n");
+ exit (1);
+ }
+
+ instream = freopen(fname, "r", stdin);
+ if (instream == NULL) {
+ printf("fail\n");
+ exit(1);
+ }
+
+ c1 = getc(instream);
+ if (c1 != 'A') {
+ printf("fail\n");
+ exit(1);
+ }
+
+ printf ("pass\n");
+ exit(0);
+}
On Mon, Oct 08, 2007 at 04:57:08PM +0200, Jesper Nilsson wrote:
> Hi!
>
> Some of the testcases in gcc libstdc++ testsuite fail for
> (at least) cris-axis-elf with time out in the execution test.
>
> The testcases in question all use freopen on stdin, here is
> a pruned down C example:
>
> #include <stdio.h>
> #include <assert.h>
>
> int
> main (void)
> {
> const char* name = "freopen.c";
> FILE* stream = freopen(name, "r", stdin);
> assert(stream != NULL);
>
> char c1 = getc(stream);
> printf("c1 = %x\n", c1);
>
> return 0;
> }
>
> When compiled with cris-axis-elf and run in simulator, the above
> code does not read the first character of the file, but tries
> to read one byte from stdin.
>
> This is turns out to be because cb_syscall in
> src/sim/common/callback.c does a simple check for "fd == 0",
> not a real test if the associated (host) fd is actually stdin.
>
> The below patch fixes this, and in so doing reduces the time
> needed for a full run through the testsuite for gcc by a noticeable
> amount.
>
> For reference, here are the testcases which failed previously:
>
> gcc/libstdc++-v3/testsuite/27_io/objects/char/10.cc
> gcc/libstdc++-v3/testsuite/27_io/objects/char/12048-1.cc
> gcc/libstdc++-v3/testsuite/27_io/objects/char/12048-2.cc
> gcc/libstdc++-v3/testsuite/27_io/objects/char/12048-3.cc
> gcc/libstdc++-v3/testsuite/27_io/objects/char/12048-4.cc
>
>
> Tested with
> make -k check 'RUNTESTFLAGS=--target_board=cris-sim\{,-march=v3,-march=v8,-march=v10\}'
> in gcc for cris-axis-elf on i686-pc-linux-gnu with no new regressions.
>
> Best regards,
>
> /^Jesper Nilsson
/^JN - Jesper Nilsson
--
Jesper Nilsson -- jesper.nilsson@axis.com