From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24394 invoked by alias); 16 Sep 2011 14:47:49 -0000 Received: (qmail 24385 invoked by uid 22791); 16 Sep 2011 14:47:48 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,TW_CL,TW_RG X-Spam-Check-By: sourceware.org Received: from mel.act-europe.fr (HELO mel.act-europe.fr) (194.98.77.210) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 16 Sep 2011 14:47:31 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 2242DCB0395; Fri, 16 Sep 2011 16:47:32 +0200 (CEST) Received: from mel.act-europe.fr ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wBiChEq4O1JH; Fri, 16 Sep 2011 16:47:21 +0200 (CEST) Received: from ulanbator.act-europe.fr (ulanbator.act-europe.fr [10.10.1.67]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mel.act-europe.fr (Postfix) with ESMTP id C8D67CB000F; Fri, 16 Sep 2011 16:47:21 +0200 (CEST) Subject: Re: [RFA] Preliminary work in fork_inferior Mime-Version: 1.0 (Apple Message framework v1244.3) Content-Type: text/plain; charset=iso-8859-1 From: Tristan Gingold In-Reply-To: <201109161419.55173.pedro@codesourcery.com> Date: Fri, 16 Sep 2011 14:52:00 -0000 Cc: "gdb-patches@sourceware.org ml" Content-Transfer-Encoding: quoted-printable Message-Id: <49D8FDB6-8E7E-46F1-B2FE-DABF6697B87D@adacore.com> References: <201109071409.06452.pedro@codesourcery.com> <8B49B563-BE0B-452C-8956-31E00A880D96@adacore.com> <201109161419.55173.pedro@codesourcery.com> To: Pedro Alves X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2011-09/txt/msg00304.txt.bz2 On Sep 16, 2011, at 3:19 PM, Pedro Alves wrote: > Thanks, this is okay. >=20 > On Friday 16 September 2011 13:28:59, Tristan Gingold wrote: >> + /* If we get here, it's an error. */ >> if (shell) >> { >> - execlp (shell_file, shell_file, "-c", shell_command, (char *) = 0); >> - >> - /* If we get here, it's an error. */ >> fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_= file, >> safe_strerror (errno)); >=20 > I wouldn't mind merging the "Cannot exec" bits of both branches as well, > always printing the whole argv. Ok, so more cleanup. Something like that ? Tristan. diff --git a/gdb/fork-child.c b/gdb/fork-child.c index bb173e7..5dbf1f7 100644 --- a/gdb/fork-child.c +++ b/gdb/fork-child.c @@ -126,9 +126,7 @@ fork_inferior (char *exec_file_arg, char *allargs, char= **env, void (*pre_trace_fun) (void), char *shell_file_arg) { int pid; - char *shell_command; static char default_shell_file[] =3D SHELL_FILE; - int len; /* Set debug_fork then attach to the child while it sleeps, to debug. */ static int debug_fork =3D 0; /* This is set to the result of setpgrp, which if vforked, will be visib= le @@ -141,6 +139,8 @@ fork_inferior (char *exec_file_arg, char *allargs, char= **env, static char **argv; const char *inferior_io_terminal =3D get_inferior_io_terminal (); struct inferior *inf; + int i; + int save_errno; =20 /* If no exec file handed to us, get it from the exec-file command -- with a good, common error message if none is specified. */ @@ -162,16 +162,6 @@ fork_inferior (char *exec_file_arg, char *allargs, cha= r **env, shell =3D 1; } =20 - /* Multiplying the length of exec_file by 4 is to account for the - fact that it may expand when quoted; it is a worst-case number - based on every character being '. */ - len =3D 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*slop *= / 12; - if (exec_wrapper) - len +=3D strlen (exec_wrapper) + 1; - - shell_command =3D (char *) alloca (len); - shell_command[0] =3D '\0'; - if (!shell) { /* We're going to call execvp. Create argument vector. @@ -180,18 +170,29 @@ fork_inferior (char *exec_file_arg, char *allargs, ch= ar **env, argument. */ int argc =3D (strlen (allargs) + 1) / 2 + 2; =20 - argv =3D (char **) xmalloc (argc * sizeof (*argv)); + argv =3D (char **) alloca (argc * sizeof (*argv)); argv[0] =3D exec_file; breakup_args (allargs, &argv[1]); } else { /* We're going to call a shell. */ - + char *shell_command; + int len; char *p; int need_to_quote; const int escape_bang =3D escape_bang_in_quoted_argument (shell_file= ); =20 + /* Multiplying the length of exec_file by 4 is to account for the + fact that it may expand when quoted; it is a worst-case number + based on every character being '. */ + len =3D 5 + 4 * strlen (exec_file) + 1 + strlen (allargs) + 1 + /*sl= op */ 12; + if (exec_wrapper) + len +=3D strlen (exec_wrapper) + 1; + + shell_command =3D (char *) alloca (len); + shell_command[0] =3D '\0'; + strcat (shell_command, "exec "); =20 /* Add any exec wrapper. That may be a program name with arguments,= so @@ -257,6 +258,16 @@ fork_inferior (char *exec_file_arg, char *allargs, cha= r **env, =20 strcat (shell_command, " "); strcat (shell_command, allargs); + + /* If we decided above to start up with a shell, we exec the + shell, "-c" says to interpret the next arg as a shell command + to execute, and this command is "exec + ". */ + argv =3D (char **) alloca (4 * sizeof (char *)); + argv[0] =3D shell_file; + argv[1] =3D "-c"; + argv[2] =3D shell_command; + argv[3] =3D (char *) 0; } =20 /* On some systems an exec will fail if the executable is open. */ @@ -348,44 +359,18 @@ fork_inferior (char *exec_file_arg, char *allargs, ch= ar **env, path to find $SHELL. Rich Pixley says so, and I agree. */ environ =3D env; =20 - /* If we decided above to start up with a shell, we exec the - shell, "-c" says to interpret the next arg as a shell command - to execute, and this command is "exec - ". */ - if (shell) - { - execlp (shell_file, shell_file, "-c", shell_command, (char *) 0); - - /* If we get here, it's an error. */ - fprintf_unfiltered (gdb_stderr, "Cannot exec %s: %s.\n", shell_file, - safe_strerror (errno)); - gdb_flush (gdb_stderr); - _exit (0177); - } - else - { - /* Otherwise, we directly exec the target program with - execvp. */ - int i; - - execvp (exec_file, argv); - - /* If we get here, it's an error. */ - safe_strerror (errno); - fprintf_unfiltered (gdb_stderr, "Cannot exec %s ", exec_file); - - i =3D 1; - while (argv[i] !=3D NULL) - { - if (i !=3D 1) - fprintf_unfiltered (gdb_stderr, " "); - fprintf_unfiltered (gdb_stderr, "%s", argv[i]); - i++; - } - fprintf_unfiltered (gdb_stderr, ".\n"); - gdb_flush (gdb_stderr); - _exit (0177); - } + execvp (argv[0], argv); + + /* If we get here, it's an error. */ + save_errno =3D errno; + fprintf_unfiltered (gdb_stderr, "Cannot exec %s", exec_file); + for (i =3D 1; argv[i] !=3D NULL; i++) + fprintf_unfiltered (gdb_stderr, " %s", argv[i]); + fprintf_unfiltered (gdb_stderr, ".\n"); + fprintf_unfiltered (gdb_stderr, "Error: %s\n", + safe_strerror (save_errno)); + gdb_flush (gdb_stderr); + _exit (0177); } =20 /* Restore our environment in case a vforked child clob'd it. */