This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[libiberty] support piping stderr
- From: Vladimir Prus <vladimir at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: DJ Delorie <dj at redhat dot com>
- Date: Wed, 31 Jan 2007 17:51:39 +0300
- Subject: [libiberty] support piping stderr
At the moment, libiberty allows to either capture program's
stderr to a file, or don't do anything. However, when
gdb runs in MI mode, a starts a debug stub with:
target remote | stub .....
it is very desirable to capture stub's stderr and pass
it to gdb, so that gdb frontend can conveniently see
everything the stub sends to stderr. Using file is not
very convenient, since stderr output should be passed
immediately.
This patch extends libiberty to allow capturing stderr,
introducing:
- New flag PEX_STDERR_TO_PIPE
- New function pex_read_err
For now, PEX_STDERR_TO_PIPE is only allowed on the
last program in the pipeline.
I've tested this together with a gdb patch to capture stderr,
on mingw, and it appears to work fine. Is this OK?
- Volodya
include/
* libiberty.h (PEX_STDERR_TO_PIPE): New define.
(pex_read_err): New function.
libiberty/
* pex-common.h (struct pex_obj): New fields
stderr_pipe and read_err.
* pex-common.c (pex_init_common): Initialize
stderr_pipe.
(pex_run_in_environment): Add error checking
for PEX_STDERR_TO_PIPE. Create a pipe
for stderr if necessary.
(pex_read_err): New.
(pex_free): Close read_err.
--- include/libiberty.h (revision 3262)
+++ include/libiberty.h (local)
@@ -400,6 +400,12 @@ extern struct pex_obj *pex_init (int fla
PEX_BINARY_INPUT. */
#define PEX_BINARY_OUTPUT 0x20
+/* Capture stderr to a pipe. The output can be read by
+ calling pex_read_err and reading from the returned
+ FILE object. This flag may be specified only for
+ the last program in a pipeline. */
+#define PEX_STDERR_TO_PIPE 0x40
+
/* Execute one program. Returns NULL on success. On error returns an
error string (typically just the name of a system call); the error
string is statically allocated.
@@ -521,6 +527,14 @@ extern FILE *pex_input_pipe (struct pex_
extern FILE *pex_read_output (struct pex_obj *, int binary);
+/* Read the standard error of the last program to be executed.
+ pex_run can not be called after this. BINARY should be non-zero if
+ the file should be opened in binary mode; this is ignored on Unix.
+ Returns NULL on error. Don't call fclose on the returned FILE; it
+ will be closed by pex_free. */
+
+extern FILE *pex_read_err (struct pex_obj *, int binary);
+
/* Return exit status of all programs in VECTOR. COUNT indicates the
size of VECTOR. The status codes in the vector are in the order of
the calls to pex_run. Returns 0 on error, 1 on success. */
--- libiberty/pex-common.c (revision 3262)
+++ libiberty/pex-common.c (local)
@@ -62,6 +62,7 @@ pex_init_common (int flags, const char *
obj->next_input = STDIN_FILE_NO;
obj->next_input_name = NULL;
obj->next_input_name_allocated = 0;
+ obj->stderr_pipe = -1;
obj->count = 0;
obj->children = NULL;
obj->status = NULL;
@@ -69,6 +70,7 @@ pex_init_common (int flags, const char *
obj->number_waited = 0;
obj->input_file = NULL;
obj->read_output = NULL;
+ obj->read_err = NULL;
obj->remove_count = 0;
obj->remove = NULL;
obj->funcs = funcs;
@@ -282,8 +284,39 @@ pex_run_in_environment (struct pex_obj *
/* Set ERRDES. */
+ if (errname != NULL && (flags & PEX_STDERR_TO_PIPE))
+ {
+ *err = 0;
+ errmsg = "both ERRNAME and PEX_STDERR_TO_PIPE specified.";
+ goto error_exit;
+ }
+
+ if (obj->stderr_pipe != -1)
+ {
+ *err = 0;
+ errmsg = "PEX_STDERR_TO_PIPE used in the middle of pipeline";
+ goto error_exit;
+ }
+
if (errname == NULL)
- errdes = STDERR_FILE_NO;
+ {
+ if (flags & PEX_STDERR_TO_PIPE)
+ {
+ if (obj->funcs->pipe (obj, p, (flags & PEX_BINARY_OUTPUT) != 0) < 0)
+ {
+ *err = errno;
+ errmsg = "pipe";
+ goto error_exit;
+ }
+
+ errdes = p[WRITE_PORT];
+ obj->stderr_pipe = p[READ_PORT];
+ }
+ else
+ {
+ errdes = STDERR_FILE_NO;
+ }
+ }
else
{
/* We assume that stderr is in text mode--it certainly shouldn't
@@ -465,6 +498,17 @@ pex_read_output (struct pex_obj *obj, in
return obj->read_output;
}
+FILE *pex_read_err (struct pex_obj *obj, int binary)
+{
+ int o;
+
+ o = obj->stderr_pipe;
+ if (o < 0 || o == STDIN_FILE_NO)
+ return NULL;
+ obj->read_err = obj->funcs->fdopenr (obj, o, binary);
+ return obj->read_err;
+}
+
/* Get the exit status and, if requested, the resource time for all
the child processes. Return 0 on failure, 1 on success. */
@@ -578,6 +622,8 @@ pex_free (struct pex_obj *obj)
free (obj->time);
if (obj->read_output != NULL)
fclose (obj->read_output);
+ if (obj->read_err != NULL)
+ fclose (obj->read_err);
if (obj->remove_count > 0)
{
--- libiberty/pex-common.h (revision 3262)
+++ libiberty/pex-common.h (local)
@@ -59,6 +59,8 @@ struct pex_obj
char *next_input_name;
/* Whether next_input_name was allocated using malloc. */
int next_input_name_allocated;
+ /* If not -1, stderr pipe from the last process. */
+ int stderr_pipe;
/* Number of child processes. */
int count;
/* PIDs of child processes; array allocated using malloc. */
@@ -73,6 +75,8 @@ struct pex_obj
FILE *input_file;
/* FILE created by pex_read_output. */
FILE *read_output;
+ /* FILE created by pex_read_err. */
+ FILE *read_err;
/* Number of temporary files to remove. */
int remove_count;
/* List of temporary files to remove; array allocated using malloc