public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add -### option
@ 2001-10-29 13:37 Ira Ruben
  2001-10-29 14:31 ` Graham Stott
       [not found] ` <20011103134058.C11575@redhat.com>
  0 siblings, 2 replies; 14+ messages in thread
From: Ira Ruben @ 2001-10-29 13:37 UTC (permalink / raw)
  To: gcc-patches

This patch adds a new option, -###.  It's similar to -v except that
the driver commands are not executed and all the command arguments
are quoted.  It is useful for shell scripts where they want to
capture the driver-generated lines for some purpose (e.g., for
passing to a debugger).

Ira Ruben
Apple Computer, Inc.

                             -------------------------

2001-10-29  Ira Ruben	<ira@apple.com>

	Add -### option (displays driver-generated commands without
	executing and quotes all arguments).
	* doc/invoke.texi: Add -###.
	* gcc.c (verbose_only_flag) - New flag.
	* (execute): If -### write quoted arguments and return.
	* (process_command): Handle -###.


Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.69
diff -c -3 -p -r1.69 invoke.texi
*** invoke.texi	2001/10/28 19:12:12	1.69
--- invoke.texi	2001/10/29 20:41:40
*************** in the following sections.
*** 152,158 ****
   @xref{Overall Options,,Options Controlling the Kind of Output}.
   @gccoptlist{
   -c  -S  -E  -o @var{file}  -pipe  -pass-exit-codes  -x @var{language} @gol
! -v  --target-help  --help}

   @item C Language Options
   @xref{C Dialect Options,,Options Controlling C Dialect}.
--- 152,158 ----
   @xref{Overall Options,,Options Controlling the Kind of Output}.
   @gccoptlist{
   -c  -S  -E  -o @var{file}  -pipe  -pass-exit-codes  -x @var{language} @gol
! -v  -###  --target-help  --help}

   @item C Language Options
   @xref{C Dialect Options,,Options Controlling C Dialect}.
*************** all preprocessed C source on standard ou
*** 831,836 ****
--- 831,842 ----
   Print (on standard error output) the commands executed to run the stages
   of compilation.  Also print the version number of the compiler driver
   program and of the preprocessor and the compiler proper.
+
+ @item -###
+ @opindex ###
+ Like @option{-v} except the commands are not executed and all command
+ arguments are quoted.  This is useful for shell scripts to capture the
+ driver-generated command lines.

   @item -pipe
   @opindex pipe
Index: gcc.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/gcc.c,v
retrieving revision 1.261
diff -c -3 -p -r1.261 gcc.c
*** gcc.c	2001/10/28 20:09:10	1.261
--- gcc.c	2001/10/29 20:41:42
*************** static int print_help_list;
*** 185,190 ****
--- 185,197 ----

   static int verbose_flag;

+ /* Flag indicating whether we should ONLY print the command and
+    arguments (like verbose_flag) without executing the command.
+    Displayed arguments are quoted so that the generated command
+    line is suitable for execution.  This is intended for use in
+    shell scripts to capture the driver-generated command line.  */
+ static int verbose_only_flag;
+
   /* Flag indicating to print target specific command line options.  */

   static int target_help_flag;
*************** execute ()
*** 2697,2704 ****
   	{
   	  const char *const *j;

! 	  for (j = commands[i].argv; *j; j++)
! 	    fprintf (stderr, " %s", *j);

   	  /* Print a pipe symbol after all but the last command.  */
   	  if (i + 1 != n_commands)
--- 2704,2718 ----
   	{
   	  const char *const *j;

! 	  if (verbose_only_flag)
! 	    for (j = commands[i].argv; *j; j++)
! 	      {
! 		if (strcmp (*j, "-###") != 0)
! 		  fprintf (stderr, " \"%s\"", *j);
! 	      }
! 	  else
! 	    for (j = commands[i].argv; *j; j++)
! 	      fprintf (stderr, " %s", *j);

   	  /* Print a pipe symbol after all but the last command.  */
   	  if (i + 1 != n_commands)
*************** execute ()
*** 2706,2711 ****
--- 2720,2727 ----
   	  fprintf (stderr, "\n");
   	}
         fflush (stderr);
+       if (verbose_only_flag)
+         return 0;
   #ifdef DEBUG
         notice ("\nGo ahead? (y or n) ");
         fflush (stderr);
*************** process_command (argc, argv)
*** 3415,3420 ****
--- 3431,3446 ----
   	}
         else if (strcmp (argv[i], "-time") == 0)
   	report_times = 1;
+       else if (strcmp (argv[i], "-###") == 0)
+ 	{
+ 	  /* This is like -v except that there is no execution and the
+ 	     echoed arguments are quoted.  It is intended for use in
+ 	     shell scripts to capture the driver-generated command
+ 	     line.  */
+ 	  n_switches++;
+ 	  verbose_only_flag++;
+ 	  verbose_flag++;
+ 	}
         else if (argv[i][0] == '-' && argv[i][1] != 0)
   	{
   	  const char *p = &argv[i][1];
*************** process_command (argc, argv)
*** 3863,3868 ****
--- 3889,3898 ----
   	     multiple children are running at once.  */
   	  else if (report_times)
   	    error ("Warning: -pipe ignored because -time specified");
+ 	}
+       else if (strcmp (argv[i], "-###") == 0)
+ 	{
+ 	  ; /* nothing to do here except keep it out of switches[] array */
   	}
         else if (argv[i][0] == '-' && argv[i][1] != 0)
   	{

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 13:37 [PATCH] Add -### option Ira Ruben
@ 2001-10-29 14:31 ` Graham Stott
  2001-10-29 14:39   ` Ira Ruben
       [not found] ` <20011103134058.C11575@redhat.com>
  1 sibling, 1 reply; 14+ messages in thread
From: Graham Stott @ 2001-10-29 14:31 UTC (permalink / raw)
  To: Ira Ruben; +Cc: gcc-patches

Ira,

Why the wierd name for the new option?

Graham

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 14:31 ` Graham Stott
@ 2001-10-29 14:39   ` Ira Ruben
  2001-10-29 15:31     ` Robert Lipe
  0 siblings, 1 reply; 14+ messages in thread
From: Ira Ruben @ 2001-10-29 14:39 UTC (permalink / raw)
  To: Graham Stott; +Cc: gcc-patches

At 10:30 PM +0000 10/29/01, Graham Stott wrote:

>Ira,
>
>Why the wierd name for the new option?
>
>Graham

(a) It's almost guaranteed never to clash with any existing option :-)

(b) It was suggested by someone here (because I couldn't think of 
anything appropriate either) as an option that did a similar thing on 
Sun systems.

Ira

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 14:39   ` Ira Ruben
@ 2001-10-29 15:31     ` Robert Lipe
  2001-10-29 16:11       ` Ira Ruben
  0 siblings, 1 reply; 14+ messages in thread
From: Robert Lipe @ 2001-10-29 15:31 UTC (permalink / raw)
  To: Ira Ruben; +Cc: Graham Stott, gcc-patches

Graham >Why the wierd name for the new option?

Ira> (b) It was suggested by someone here (because I couldn't think of
Ira> anything appropriate either) as an option that did a similar thing
Ira> on Sun systems.

I know it's not much of a justification, but several System V vendor
compilers support that.  From 'man cc':

  -#, -i  Turn on driver debugging. Print out each command with its options
          and arguments as invoked by cc before the command is executed.

  -##, -ii
          Print out the full pathname of each command, in addition to the
          command, its options and arguments as invoked by cc before the
          command is executed.

  -###, -iii
          The same as -##, except that the command is not executed.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 15:31     ` Robert Lipe
@ 2001-10-29 16:11       ` Ira Ruben
  2001-10-29 16:33         ` Zack Weinberg
  0 siblings, 1 reply; 14+ messages in thread
From: Ira Ruben @ 2001-10-29 16:11 UTC (permalink / raw)
  To: Robert Lipe; +Cc: Graham Stott, gcc-patches

At 5:29 PM -0600 10/29/01, Robert Lipe wrote:

>I know it's not much of a justification, but several System V vendor
>compilers support that.  From 'man cc':
>
>   -#, -i  Turn on driver debugging. Print out each command with its options
>           and arguments as invoked by cc before the command is executed.
>
>   -##, -ii
>           Print out the full pathname of each command, in addition to the
>           command, its options and arguments as invoked by cc before the
>           command is executed.
>
>   -###, -iii
>           The same as -##, except that the command is not executed.

So there is president for -### other than Sun :-)  I forgot to add 
previously that I didn't really care what the option was called 
because it was originally created to be buried down in a bash script 
I have for gcc debugging (as I alluded in the original patch 
message).  In that script I pass a normal xgcc command line, generate 
the driver command command lines using the -###, extract out the cc1* 
line, setup the stuff for gdb, and invoke gdb on the cc1*.

Originally -v was used.  But there are problems with this if what I 
was trying to debug took forever to get there (e.g., lots of 
#includes), or the cc1* blew so I would never reach gdb, or if the -v 
output required stuff to be properly quoted.  The -### solves all 
these problems.  The script invokes gdb rapidly (and I only have to 
process all the #includes during the debugging session not once for 
the -v and once for the debugging session) or if cc1* blows I'm able 
to actually debug it (which is the whole object of this sport)!  This 
is all so I don't have to manually extract the cc1* line out of a -v 
output.  And even doing that quoting is still a potential problem.

Ira

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 16:11       ` Ira Ruben
@ 2001-10-29 16:33         ` Zack Weinberg
  2001-10-29 17:42           ` Daniel Jacobowitz
  2001-10-29 19:50           ` Tom Tromey
  0 siblings, 2 replies; 14+ messages in thread
From: Zack Weinberg @ 2001-10-29 16:33 UTC (permalink / raw)
  To: Ira Ruben; +Cc: Robert Lipe, Graham Stott, gcc-patches

On Mon, Oct 29, 2001 at 04:12:40PM -0800, Ira Ruben wrote:
> At 5:29 PM -0600 10/29/01, Robert Lipe wrote:
> 
> >I know it's not much of a justification, but several System V vendor
> >compilers support that.  From 'man cc':
> >
> >  -#, -i  Turn on driver debugging. Print out each command with its options
> >          and arguments as invoked by cc before the command is executed.
> >
> >  -##, -ii
> >          Print out the full pathname of each command, in addition to the
> >          command, its options and arguments as invoked by cc before the
> >          command is executed.
> >
> >  -###, -iii
> >          The same as -##, except that the command is not executed.
> 
> So there is president for -### other than Sun :-)  I forgot to add 
> previously that I didn't really care what the option was called 
> because it was originally created to be buried down in a bash script 
> I have for gcc debugging (as I alluded in the original patch 
> message).  In that script I pass a normal xgcc command line, generate 
> the driver command command lines using the -###, extract out the cc1* 
> line, setup the stuff for gdb, and invoke gdb on the cc1*.

As a vaguely related thing: I was contemplating, not so long ago, a mode
for the driver where it would run gdb on cc1 for you.  I got stuck because
as far as I know there is no way to set the debuggee's command line arguments
from gdb's command line.  Your script sounds like it solves this - how?

zw

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 16:33         ` Zack Weinberg
@ 2001-10-29 17:42           ` Daniel Jacobowitz
  2001-10-29 19:50           ` Tom Tromey
  1 sibling, 0 replies; 14+ messages in thread
From: Daniel Jacobowitz @ 2001-10-29 17:42 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Ira Ruben, Robert Lipe, Graham Stott, gcc-patches

On Mon, Oct 29, 2001 at 04:33:42PM -0800, Zack Weinberg wrote:
> As a vaguely related thing: I was contemplating, not so long ago, a mode
> for the driver where it would run gdb on cc1 for you.  I got stuck because
> as far as I know there is no way to set the debuggee's command line arguments
> from gdb's command line.  Your script sounds like it solves this - how?

Someone (Tom Tromey?  I think.) posted a GDB patch to fix this
recently.  I think it's still stalled though.

-- 
Daniel Jacobowitz                           Carnegie Mellon University
MontaVista Software                         Debian GNU/Linux Developer

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 16:33         ` Zack Weinberg
  2001-10-29 17:42           ` Daniel Jacobowitz
@ 2001-10-29 19:50           ` Tom Tromey
  2001-11-13 15:03             ` Alexandre Oliva
  1 sibling, 1 reply; 14+ messages in thread
From: Tom Tromey @ 2001-10-29 19:50 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Ira Ruben, Robert Lipe, Graham Stott, gcc-patches

>>>>> "Zack" == Zack Weinberg <zack@codesourcery.com> writes:

Zack> As a vaguely related thing: I was contemplating, not so long
Zack> ago, a mode for the driver where it would run gdb on cc1 for
Zack> you.  I got stuck because as far as I know there is no way to
Zack> set the debuggee's command line arguments from gdb's command
Zack> line.  Your script sounds like it solves this - how?

I've long wanted the same thing.  I've written a patch to gdb which
lets you set the inferior's command-line arguments from the gdb
command line:

    gdb --args cc1 -ffoo -fbar ...

This patch is still in progress.  I need to rewrite it before the gdb
maintainers will accept it.

I also hacked gdb to let me do this.  One problem I ran into is that
gdb doesn't pass its std* file descriptors to the inferior.  Some work
there is required.

Anyway, we're closer to having this than you might think.  Even
without the I/O redirection stuff, the gdb patch is nice because it
makes cutting and pasting a simpler operation.

Tom

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
       [not found]       ` <p05100300b80aa23d95d1@[198.144.204.26]>
@ 2001-11-06 22:41         ` Richard Henderson
  2001-11-13  4:33           ` Richard Henderson
  2001-11-13 15:03           ` Richard Henderson
  0 siblings, 2 replies; 14+ messages in thread
From: Richard Henderson @ 2001-11-06 22:41 UTC (permalink / raw)
  To: Ira Ruben; +Cc: gcc-patches

On Sat, Nov 03, 2001 at 11:47:06PM -0800, Ira Ruben wrote:
> Now I really don't understand what you mean.  Please clearify.

The option '-print-multi-lib' is not passed to subprocesses, but
you do not see any code in whatever-that-function-was to remove it.

I'm saying that _something_ copied -### onto the pass-this-to-subcommands
list, and it shouldn't have.  Rather than put -### on the list, then
have special code to take it out again.

> The option is more-or-less mimicking -v and the check to act upon
> it is where -v does it's action.

But -v *does* get passed to subprocesses.


r~

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-11-06 22:41         ` Richard Henderson
@ 2001-11-13  4:33           ` Richard Henderson
  2001-11-13 15:03           ` Richard Henderson
  1 sibling, 0 replies; 14+ messages in thread
From: Richard Henderson @ 2001-11-13  4:33 UTC (permalink / raw)
  To: Ira Ruben; +Cc: gcc-patches

On Sat, Nov 03, 2001 at 11:47:06PM -0800, Ira Ruben wrote:
> Now I really don't understand what you mean.  Please clearify.

The option '-print-multi-lib' is not passed to subprocesses, but
you do not see any code in whatever-that-function-was to remove it.

I'm saying that _something_ copied -### onto the pass-this-to-subcommands
list, and it shouldn't have.  Rather than put -### on the list, then
have special code to take it out again.

> The option is more-or-less mimicking -v and the check to act upon
> it is where -v does it's action.

But -v *does* get passed to subprocesses.


r~

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
       [not found]                 ` <87g07as3fh.fsf@creche.redhat.com>
@ 2001-11-13 15:03                   ` Ira Ruben
  0 siblings, 0 replies; 14+ messages in thread
From: Ira Ruben @ 2001-11-13 15:03 UTC (permalink / raw)
  To: tromey; +Cc: Alexandre Oliva, gcc-patches

At 5:15 PM -0700 11/19/01, Tom Tromey wrote:

>My `gdb --args' patch is very close to going in.  It just needs one
>more approval.  I have most of the gcc patch written too, but I
>haven't had the time or the need to finish it yet.  Soon you'll be
>able to do this:
>
>     gcc --debug-program=jc1 ... all kinds of stuff ...

I'll look forward to that :-) In the meantime, see below...


>Ira> FWIW, and just in case you're interested, I've enclosed my latest
>Ira> version of debug_gcc bash script (which is where I use the -###).
>Ira> It will work for debugging the driver or the compiler.
>
>Ira> (This file must be converted with BinHex 4.0)
>
>I can't read this.  Isn't this a Mac-specific format?

Arrrrgggghhhh, emailers :-(  I am using Mac Eudora and constantly 
forget that its enclosures don't work for everyone (indeed the email 
to gcc-patches bounced).  Although this makes for a long message 
(sorry), below is the script that didn't make it through as the 
enclosure.

Ira

=========================================================================================

#!/usr/bin/env bash
#
## This script defines the following two commands:
##
## debug_gcc cc   - a command for debugging gcc driver or compiler
## debug_gcc_help - how to use debug_gcc
##
## Basically what debug_gcc does when debugging the driver is pass 
all the compiler
## arguments on to gdb through gdb's -command option using a file 
containing a gdb
## set args command.  It's the same for the compiler except that the set args
## command consists of the arguments passed to the compiler from the driver.
##
## The debug_gcc command contains options to save and/or echo the set 
args file,
## adding a gdb dir command, --help to invoke debug_gcc_help, and of course
## whether you want to debug the driver or compiler.
#

# Ira L. Ruben
# Copyright Apple Computer, Inc. 2000-2001
# Last updated 11/19/01

#----------------------------------------------------------------------------------------#
#
## Help info for the debug_gcc function
#
debug_gcc_help()
{
     echo
     echo "debug_gcc cc [-Bdir/] [-cpp|-driver] 
[--dir=dir1:dir2:...:dirN | --dir=name]"
     echo "             [-ver=n] [--save-commands] [--echo args | dir 
| all] [--help]"
     echo "             cc-options ..."
     echo
     echo "cc                        The compiler to invoke. Note that 
this MUST be the"
     echo "                          first argument."
     echo
     echo "-Bdir/                    Specifies where to find the cc1* 
compiler. Normally"
     echo "                          this may be omitted unless 
debugging a specific"
     echo "                          compiler. Since cc must be the 
first argument the"
     echo "                          -B is recognized as a compiler 
option thus allowing"
     echo "                          a macro to be defined to passed for cc."
     echo
     echo "-cpp                      Debug the preprocessor (or cpp-precomp)."
     echo
     echo "-driver                   Debug the driver. Either -cpp or 
-driver but not both"
     echo "                          may be specified.  If neither are 
specified then the"
     echo "                          compiler phase is to be debugged 
(e.g., cc1plus)."
     echo
     echo "-ver=n                    Version of gcc being used if -cpp 
not specified. This"
     echo "                          can be 2 or 3.  See Notes [2], 
[3], and [4] below."
     echo
     echo "--echo args | dir | all   Echo the gdb set args command 
written to the command"
     echo "                          file, or the gdb dir command, or 
echo both of them."
     echo
     echo "--dir=dir1:dir2:...:dirN  A list of directories for gdb 
that will be placed in"
     echo "                          a gdb dir command placed in the 
command file."
     echo
     echo "--dir=name                Alternative way to specify the 
gdb dir paths. The"
     echo "                          name can either be a filename 
containing a line of"
     echo "                          paths separated by colons (like 
the explicit previous"
     echo "                          form) OR it can be an environment 
variable similarly"
     echo "                          defined with the directory paths. 
It is an error for"
     echo "                          the name to be both an 
environment variable and a"
     echo "                          filename."
     echo
     echo "--help                    Only display this help 
information.  This can be"
     echo "                          specified in place of the cc."
     echo
     echo "--save-commands           Do not delete command file 
(\"debug_gcc_command_file\")."
     echo "                          Setting environment variable 
SAVE_COMMANDS to 1 can"
     echo "                          be used in place of --save-commands."
     echo
     echo
     echo "Note [1]: If the command file \"debug_gcc_command_file\" 
already exists in the"
     echo "          current directory when this script is run it is renamed to"
     echo "          \"debug_gcc_command_file-previous\".  When you 
exit gdb the original"
     echo "          debug_gcc_command_file is restored.  This is only 
mentioned in case"
     echo "          for some reason you don't exit gdb cleanly."
     echo
     echo "Note [2]: Gcc 3.x has an integrated preprocessor.  Thus 
-cpp implies that"
     echo "          a -save-temps will be added to debug the 
stand-alone preprocessor,"
     echo "          cpp0.  For debugging the compiler itself, 
including the integrated"
     echo "          preprocessor, omit the -cpp option."
     echo
     echo "Note [3]: Gcc 2.x always uses a separate preprocessor and 
thus the -cpp is"
     echo "          always required to debug it.  Further, the script 
needs to know when"
     echo "          you want to debug the compiler since it must add 
a -save-temps to the"
     echo "          command to save the temporary file for the 
compiler input.  Thus a"
     echo "          -ver=2 is required when debugging a gcc 2.x 
compiler.  As stated in"
     echo "          Note [2] this is unnecessary for gcc 3.x because 
of it's integrated"
     echo "          preprocessor and the reason you don't want to add 
a -save-temps when"
     echo "          debugging a gcc 3.x compiler is that implies a 
different behavior of"
     echo "          the compiler (i.e., invoking cpp0 and then the 
compiler instead of"
     echo "          just the compiler)."
     echo
     echo "Note [4]: The Gcc 3.x driver (at least the Apple version) 
supports an option"
     echo "          (-###) specifically designed to support script 
like this one.  The"
     echo "          option is basically the same as -v except that 
the displayed"
     echo "          command line is not executed (since we only need 
it for gdb) and"
     echo "          all arguments are quoted.  A -v doesn't quote the 
displayed"
     echo "          arguments leading to possible quoting problems 
passing the arguments"
     echo "          to gdb.  Of course for Gcc 2.x you're just screwed!"
}

#
## debug_gcc cc [-Bdir/] [-cpp|-driver] [-ver=n] 
[--dir=dir1:dir2:...:dirN | --dir=name]
##              [--echo args | dir | all] cc-options...
##
## This script can be used to debug all phases of the gcc compiler; 
driver, preprocessor,
## or gcc compiler.  It invokes gdb on the appropriate component and 
sets up a local
## command script (passed to gdb using its -command option) to define 
all the arguments
## appropriate to that phase along with path definitions so that gdb 
can access the
## compiler source.
##
## If not debugging the driver, the script will add a -v (and 
-save-temps for gcc
## 2.x) option to the command line to allow it to capture the 
phases's tool name
## (e.g., cc1plus) and all the arguments passed to that phase.  The 
tool name is
## what's passed to gdb.  The arguments are used to build a SET args 
gdb command
## which is placed in a command file generated into the current directory.  The
## command file is passed to gdb to execute when gdb is invoked by the script.
##
## Debugging the driver is similar to debugging the preprocessor or 
compiler phases
## except the command line use is basically the one passed to the 
script minus the
## script's own arguments.  Again we have the tool name (e.g., cc) 
and its arguments
## for building the command file and invoking gdb.
##
## This script also allows you to specify a set of directory paths 
for gdb to use
## through a gdb dir command that would be added to the command file.
##
## Note [1]: If the command file "debug_gcc_command_file" already exists in the
##           current directory when this script is run it is renamed to
##           "debug_gcc_command_file-previous".  When you exit gdb the original
##           debug_gcc_command_file is restored.  This is only 
mentioned in case
##	     for some reason you don't exit gdb cleanly.
##
## Note [2]: Gcc 3.x has an integrated preprocessor.  Thus -cpp implies that
##	     a -save-temps will be added to debug the stand-alone preprocessor,
##	     cpp0.  For debugging the compiler itself, including the integrated
##	     preprocessor, omit the -cpp option.
##
## Note [3]: Gcc 2.x always uses a separate preprocessor and thus the -cpp is
##	     always required to debug it.  Further, the script needs 
to know when
##	     you want to debug the compiler since it must add a 
-save-temps to the
##	     command to save the temporary file for the compiler input.  Thus a
##	     -ver=2 is required when debugging a gcc 2.x compiler. 
As stated in
##	     Note [2] this is unnecessary for gcc 3.x because of it's 
integrated
##	     preprocessor and the reason you don't want to add a 
-save-temps when
##           debugging a gcc 3.x compiler is that implies a different 
behavior of
##	     the compiler (i.e., invoking cpp0 and then the compiler instead of
##	     just the compiler).
##
## Note [4]: The Gcc 3.x driver (at least the Apple version) supports 
an option"
##	     (-###) specifically designed to support script like this one.  The
##	     option is basically the same as -v except that the displayed"
##	     command line is not executed (since we only need it for gdb) and"
##	     all arguments are quoted.  A -v doesn't quote the displayed"
##	     arguments leading to possible quoting problems passing 
the arguments"
##	     to gdb.  Of course for Gcc 2.x you're just screwed!"
##
## Options: Must precede any of the gcc command lines options for the compiler.
##
##   cc				The compiler to invoke.  Note that 
this MUST be the
##                              first argument.
##
##   -Bdir/                     Specifies where to find the cc1* 
compiler.  Normally
##                              this may be omitted unless debugging a specific
##                              compiler.  Since cc must be the first 
argument the
##                              -B is recognized as a compiler option 
thus allowing
##                              a macro to be defined to passed for cc.
##
##   -cpp			Debug the preprocessor (or cpp-precomp).
##
##   -driver			Debug the driver.  Either -cpp or 
-driver but not both
##				may be specified.  If neither are 
specified then the
##				compiler phase is to be debugged 
(e.g., cc1plus).
##
##   -ver=n			Version of gcc being used if -cpp not 
specified.  This
##				can be 2 or 3.  See Notes [2], [3], 
and [4] above.
##
##   --echo args | dir | all	Echo the gdb set args command written 
to the command
##                              file, or the gdb dir command, or echo 
both of them.
##
##   --dir=dir1:dir2:...:dirN	A list of directories for gdb that 
will be placed in
##				a gdb dir command placed in the command file.
##
##   --dir=name			Alternative way to specify the gdb 
dir paths. The name can
##				either be a filename containing a 
line of paths separated
##				by colons (like the explicit previous 
form) OR it can
##				be an environment variable similarly 
defined with the
##				directory paths.  It is an error for 
the name to be both
##				an environment variable and a filename.
##
##   --help			Only display this help information. 
This can be
##				specified in place of the cc.
##
##   --save-commands            Do not delete command file 
("debug_gcc_command_file").
##				Setting environment variable SAVE_COMMANDS to 1
##				can be used in place of --save-commands.
##
## Any script option that is not one of the above is assumed to be 
the start of the
## compiler options.
#
debug_gcc()
{
     declare -i done=0 shift_cnt=0 cpp=0 driver=0 previous=0 ver=3 
status cc_args_index=0
     local arg next_arg= cc= objdir= dir= dir1= yn= saw_dir= alius echo= v="-v"
     local save_temps=
     local line= tool= args=
     local save=0 command_file=debug_gcc_command_file
     local -a cc_args
    
     if [ $# -eq 0 ]; then
    	debug_gcc_help
    	return 0
     fi
    
     if [ "$1" != "--help" ] && [ "$1" != "-help" ] && [ "$1" != "-h" ]; then
    	cc=$1
    	shift
     fi
    
     for arg; do
         case $next_arg in
	    -dir | --dir)
		dir="$arg"
		saw_dir="$next_arg"
		next_arg=
		shift_cnt=$shift_cnt+2
		;;
	    -echo | --echo)
	   	echo="$arg"
		next_arg=
		shift_cnt=$shift_cnt+2
	   	;;
	    -ver | --ver)
	   	ver=$arg
		shift_cnt=$shift_cnt+2
	   	;;
	    *)
		case $arg in
	    	    -help | --help | -h)
	    	   	debug_gcc_help
		   	return 0
		   	;;
		    -cpp | --cpp)
		   	cpp=1
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -driver | --driver)
		   	driver=1
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -echo=* | --echo=*)
		   	echo=`echo $arg | sed -e 's/-*echo=//'`
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -echo | --echo)
		   	next_arg=$arg
		   	;;
    	   	    -dir=* | --dir=*)
    	   	   	dir=`echo $arg | sed -e 's/-*dir=//'`
    	   	   	saw_dir=`echo $arg | sed -e 's/\(-*dir\).*$/\1/'`
    	   	   	shift_cnt=$shift_cnt+1
    		   	;;
    		    -dir | --dir)
    		   	next_arg=$arg
    		   	;;
    		    -save-commands | --save-commands)
    		   	save=1
		   	shift_cnt=$shift_cnt+1
    		   	;;
    		    -ver=* | --ver=*)
    	   	   	ver=`echo $arg | sed -e 's/-*ver=//'`
    	   	   	shift_cnt=$shift_cnt+1
    	   	   	;;
    		    -B*)
    		   	cc="$cc $arg"
    		   	shift_cnt=$shift_cnt+1
    		   	;;
    	   	    *)
    	   	   	next_arg=
    	   	   	done=1
    	   	   	;;
       	   	esac
         esac
         if [ $done != 0 ]; then
             break
         fi
     done
    
     shift $shift_cnt
    
     # See if the use explicitly specified -v.  If so we won't need to 
add it when
     # we need to generate the command lines.
    
     for arg; do
         if [ "$arg" = "-v" ]; then
	    v=
	    break
         fi
     done
   
     if [ $driver -ne 0 ] && [ $cpp -ne 0 ]; then
    	echo "What do you want to debug? Driver or preprocessor. You 
can't do both!"
    	return 1
     fi
    
     if [ "$echo" != ""     ] && \
        [ "$echo" != "dir"  ] && \
        [ "$echo" != "args" ] && \
        [ "$echo" != "all"  ]; then
        echo "Invalid --echo specification: $echo"
        return 1
     fi
    
     if [ $ver != 3 -a $ver -ne 2 ]; then
       echo "Invalid compiler version specified (2 or 3 expected): $ver"
       return 1
     fi
    
     if [ "$dir" != "" ] && [ "`echo $dir | sed -e '/:/D'`" != "" ]; then
    	if [ -f "$dir" ]; then
    	    if [ "`eval echo $\`echo $dir\``" != "" ]; then
    	   	echo "$dir is ambiguous -- it's a valid filename and 
a valid environment variable"
    	   	return 1
    	    fi
    	    dir="`cat \"$dir\"`"
    	else
    	    dir="`eval echo $\`echo $dir\``"
    	fi
     fi
    
     if [ "${dir:${#dir}-1:1}" = ":" ]; then
	dir="${dir:0:${#dir}-1}"
     fi
    
     if [ "$saw_dir" != "" ] && [ "$dir" = "" ]; then
    	while [ "$yn" != "y" ] && [ "$yn" != "n" ]; do
    	    echo -n "$saw_dir" 'specified which evaluated to null -- 
do you accept this ? (y,n) '
    	    read -e yn;
    	    yn="${yn:0:1}"
    	done
    	if [ "$yn" = "n" ]; then
    	    return 1
    	fi
     fi
    
     if [ "$cc" = "" ]; then
    	echo "No compiler specifed (see --help for syntax)"
    	return 0
     fi
    
     #
     # If the command line specified a compiler name that was actually 
an alias then
     # we need to get the actual tool to which that name is aliased.
     #
     alius="`alias \"$cc\" 2> /dev/null`"
     if [ $? -eq 0 ]; then
	cc="`echo \"$alius\" | sed -e \"s/alias .*='//\" -e \"s/'\$//\"`"
     fi
    
     #
     # The gcc 3.x drive supports -###, after I added it :-)  It will cause
     # the driver to simply do a -v output without executing the command.
     # It will also quote all the arguments since -v normally doesn't do that
     # and we lose what needs to be quoted if we don't do this.  Of course for
     # gcc 2.x you're screwed.  Do have any pathnames that need quoting.
     #
     if [ $ver -eq 2 ]; then
	save_temps="-save-temps"
     elif [ $driver -eq 0 ]; then
	cc_args[$((cc_args_index++))]='-###'
     fi
    
     #
     # Remove possible -pipe argument.  Must do this in $@ to preserve quoting.
     #
     if [ $driver -ne 0 ]; then
	for arg; do
	    if [ "$arg" != "-pipe" ]; then
	      cc_args[$((cc_args_index++))]="\"$arg\""
	    fi
	done
     else
	for arg; do
	    if [ "$arg" != "-pipe" ]; then
	      cc_args[$((cc_args_index++))]="$arg"
	    fi
	done
     fi
    
     if false; then
    	echo '$cc     =' $cc
    	echo '$cpp    =' $cpp
    	echo '$driver =' $driver
    	echo '$dir    =' $dir
    	echo '$echo   =' $echo
    	echo '$objdir =' $objdir
    	echo '$save   =' $save
    	echo '$v      =' $v
    	echo '$ver    =' $ver
    	echo '$@      =' $@
    	return 1
     fi

     #
     # Set the appropriate command line to use (driver line, 
preprocessor line, or c/c++
     # compiler line) depending on the script options.  The line 
allows us to extract the
     # tool name that is invoked and all the arguments that follow. 
The tool is what
     # is passed to gdb to debug.  The arguments are used to build a 
set args gdb
     # command for a local -command file.
     #
     if [ $driver -ne 0 ]; then
    	#
	# If debugging the driver, the command line is obviously all 
the script's command
	# line options minus the ones specific to the script.
	#
    	line="$cc ""${cc_args[@]}"
    	tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\)\(.*\)$/\1/'`"
    	if [ "$tool" = "" ]; then
    	    echo "Could not find driver name (?)"
    	    return 1
    	fi
     else
	#
	# Compile program and save the -v output in a temp file that 
we can grep for
	# the prerocessor line or c/c++ compiler line.  If the user exlicitly
	# specified -v then we don't have to.  But if we do then we 
must remove it
	# from the gdb args list.  In other words the args list only 
has the -v if
	# the user explicitly specified it.
	#
    	$cc "${cc_args[@]}" $v -c $save_temps 2> /tmp/dbg.$$
    	status=$?
    	if [ $status -gt 1 ]; then
    	    echo "Unexpected status returned from compilation: $status"
    	    more /tmp/dbg.$$
    	    return 1
    	fi
    
	# ignore any compile-time errors
    
    	if false; then
    	    echo '##############' /tmp/dbg.$$ '##############'
    	    more /tmp/dbg.$$
    	    return 1
	fi

	if [ $cpp -ne 0 ]; then
	    line="`cat /tmp/dbg.$$ | grep [-]D__GNUC__`"
	    tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\)\(.*\)$/\1/'`"
	    if [ "$tool" = "" ]; then
		echo "Could not find preprocessor name in the -v 
output (in /tmp/dbg.$$)"
		return 1
	    fi
	else
	    #line="`cat /tmp/dbg.$$ | grep [-]o[\ ] | grep --invert-match /as`"
	    #line="`cat /tmp/dbg.$$ | grep [-]o[\ ] | grep .*cc1.* `"
	    line="`cat /tmp/dbg.$$ | grep .*cc1.*`"
	    tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\).*$/\1/'`"
	    if [ "$tool" = "" ]; then
		echo "Could not find compiler name in the -v output 
(in /tmp/dbg.$$)"
		return 1
	    fi
	    # do -version first so -v removal doesn't screw it up.
	    line="`echo \"$line\" | sed -e 's/-version//'`"
	    if [ "$v" = "-v" ]; then
		line="`echo \"$line\" | sed -e 's/-v//'`"
	    fi
	    # remove null but quoted arguments...
	    line="`echo \"$line\" | sed -e 's/\"\"//g'`"
	fi

	rm -f /tmp/dbg.$$			# don't need this any more
     fi
    
     #
     # Once the tool name is known and we have the appropriate command 
line from it we
     # can extract all the arguments we'll need to build the gdb set 
args command for the
     # -command file.
     #
     # Note, the original line (commented out below) blows in sed 
apparently when $line
     # is more than 1098 characters.  The second one doesn't.  Go figure!
     #
     #args="`echo $line | sed -e 's,^[ ]*'"$tool"'[ ]*\(.*\)$,\1,'`"
     args="`echo \"$line\" | sed -e 's,'\"$tool\"',,'`"
    
     #
     # At last we got all of what we were after; tool and its 
arguments.  Create a
     # -command file in the current directory containing a set args to 
define the
     # tools arguments.
     #
    
     if [ $save -eq 0 ] && [ -e $command_file ] && [ "$SAVE_COMMANDS" 
!= "1" ]; then
    	mv $command_file ${command_file}-previous
    	previous=1
     fi
    
     if [ "$args" != "" ];then
    	echo "set args $args" > $command_file	# set args arguments...
    	if [ "$echo" = "args" ] || [ "$echo" = "all" ]; then
    	    echo "$command_file: set args $args"
    	fi
     fi
    
     #
     # If the --dir script options was specified add it to the command 
file too as a
     # dir gdb command.  The --dir arguments are defines as being in 
the syntax that
     # gdb expects.
     #
     if [ "$dir" != "" ]; then
    	echo "dir $dir"	>> $command_file	# dir all the useful 
compiler directories
    	if [ "$echo" = "dir" ] || [ "$echo" = "all" ]; then
    	    echo "$command_file: dir $dir"
    	fi
     fi
    
     #
     # Finally we can invoke gdb...
     #
     if [ "$echo" != "" ]; then
    	echo "gdb -command $command_file $tool"
     fi
     gdb -command $command_file "$tool"		# debug that sucker
    
     #
     # Delete the command file and rename any previous one (unless 
-save-commands
     # was specified).
     #
     if [ $save -eq 0 ] && [ "$SAVE_COMMANDS" != "1" ]; then
         rm -f $command_file			# now we don't need 
this any more
         if [ $previous -ne 0 ]; then
             mv ${command_file}-previous $command_file
         fi
     fi
}

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-11-06 22:41         ` Richard Henderson
  2001-11-13  4:33           ` Richard Henderson
@ 2001-11-13 15:03           ` Richard Henderson
  1 sibling, 0 replies; 14+ messages in thread
From: Richard Henderson @ 2001-11-13 15:03 UTC (permalink / raw)
  To: Ira Ruben; +Cc: gcc-patches

On Sat, Nov 03, 2001 at 11:47:06PM -0800, Ira Ruben wrote:
> Now I really don't understand what you mean.  Please clearify.

The option '-print-multi-lib' is not passed to subprocesses, but
you do not see any code in whatever-that-function-was to remove it.

I'm saying that _something_ copied -### onto the pass-this-to-subcommands
list, and it shouldn't have.  Rather than put -### on the list, then
have special code to take it out again.

> The option is more-or-less mimicking -v and the check to act upon
> it is where -v does it's action.

But -v *does* get passed to subprocesses.


r~

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
  2001-10-29 19:50           ` Tom Tromey
@ 2001-11-13 15:03             ` Alexandre Oliva
       [not found]               ` <p05100300b81f32ebb325@[17.202.44.152]>
  0 siblings, 1 reply; 14+ messages in thread
From: Alexandre Oliva @ 2001-11-13 15:03 UTC (permalink / raw)
  To: tromey; +Cc: Zack Weinberg, Ira Ruben, Robert Lipe, Graham Stott, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 871 bytes --]

On Oct 30, 2001, Tom Tromey <tromey@redhat.com> wrote:

>>>>>> "Zack" == Zack Weinberg <zack@codesourcery.com> writes:
Zack> As a vaguely related thing: I was contemplating, not so long
Zack> ago, a mode for the driver where it would run gdb on cc1 for
Zack> you.  I got stuck because as far as I know there is no way to
Zack> set the debuggee's command line arguments from gdb's command
Zack> line.  Your script sounds like it solves this - how?

> I've long wanted the same thing.  I've written a patch to gdb which
> lets you set the inferior's command-line arguments from the gdb
> command line:

>     gdb --args cc1 -ffoo -fbar ...

> This patch is still in progress.  I need to rewrite it before the gdb
> maintainers will accept it.

Meanwhile, this is what I use.  Unfortunately, it's not whitespace- or
shell-quoting-safe, but it has been of great help to me.


[-- Attachment #2: debug --]
[-- Type: application/octet-stream, Size: 1653 bytes --]

#! /bin/sh

# Copyright 1999, 2000, 2001 Free Software Foundation

# This script is Free Software, and it can be copied, distributed and
# modified as defined in the GNU General Public License.  A copy of
# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html

# by Alexandre Oliva <oliva@lsd.ic.unicamp.br>

if test $# -lt 1; then
    echo usage: env DEBUG="<gdb|emacs>" $0 "<program>" args... >&2
    exit 1
fi
: ${DEBUG=emacs}
prog="$1"; shift 1
gdbinitdir=/tmp/.gdbinit.$$
gdbinit=$gdbinitdir/init
mkdir $gdbinitdir ||
{ echo directory $gdbinitdir already exists >&2; exit 1; }
{
    echo "shell rm -rf /tmp/.gdbinit.$$"
    echo "cd `pwd`"
    echo "set args $*"
} >$gdbinit
case `$DEBUG --version </dev/null 2>/dev/null` in
    "GDB"*|"GNU gdb"*)
	# start command-line gdb within the correct working directory,
	# and set up the specified argument list
	exec $DEBUG -command $gdbinit $prog;;
    "DDD"*)
        # start DDD/gdb within the correct working directory,
	# and set up the specified argument list
	exec $DEBUG --debugger "${GDB-gdb} -command $gdbinit" $prog;;
    "GNU Emacs"*)
	# start gdb within GNU Emacs, move into the current working
	# directory and set up the specified argument list
	exec $DEBUG -eval "(progn (gdb \"${GDB-gdb} $prog\") (gud-call \"source $gdbinit\"))";;
    XEmacs*)
	# start gdb within XEmacs, move into the current working
	# directory and set up the specified argument list
	exec $DEBUG -eval "(progn ${GDB+(setq gdb-command-name \"$GDB\")} (gdb \"$prog\") (gdb-call \"source $gdbinit\"))";;
    *) echo you must set DEBUG to either emacs or gdb >&2;;
esac
rm -rf /tmp/.gdbinit.$$
exit 1

[-- Attachment #3: Type: text/plain, Size: 290 bytes --]



-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] Add -### option
@ 2001-10-29 17:19 Ira Ruben
  0 siblings, 0 replies; 14+ messages in thread
From: Ira Ruben @ 2001-10-29 17:19 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: Robert Lipe, Graham Stott, gcc-patches

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 23207 bytes --]

At 4:33 PM -0800 10/29/01, Zack Weinberg wrote:

>As a vaguely related thing: I was contemplating, not so long ago, a mode
>for the driver where it would run gdb on cc1 for you.  I got stuck because
>as far as I know there is no way to set the debuggee's command line arguments
>from gdb's command line.  Your script sounds like it solves this - how?

There were two ways to do this, both involve using a gdb set args command:

1. Place a set args command in a (local) .gdbinit script in the 
current directory assuming that's where you're debugging from.  Gdb 
will always search the cwd for a .gdbinit as well as your home 
directory.

2. Place a set args in a file and use the gdb -command command line 
option which sources in the file just like it would a local .gdbinit).

Form 2 is what I use because I don't have to worry about clobbering a 
previous local .gdbinit (like the one in the gcc object directory). 
FWIW, I've enclosed my script (shown below).  I've extracted it from 
my bash startup environment scripts.  I think it's stand-alone enough 
to run outside that environment but I've never tried it that way.

Ira

PS: Forgive me if anyone is getting this message twice.  I sent it 
with the script as an enclosure from my OS9 machine (where I do mail) 
and the email bounced.  I think it's because my emailer (Eudora) 
compressed the enclosure in an "unacceptable" form (for the 
receivers).  It was mime type application/mac-binhex40.  Just to be 
save this time I stuck it as aprt of this email.

-----------------------------------------------------------------------------------

#
## Help info for the debug_gcc function
#
debug_gcc_help()
{
     echo
     echo "debug_gcc cc [-Bdir/] [-cpp|-driver] 
[--dir=dir1:dir2:...:dirN | --dir=name]"
     echo "             [-ver=n] [--save-commands] [--echo args | dir 
| all] [--help]"
     echo "             cc-options ..."
     echo
     echo "cc                        The compiler to invoke. Note that 
this MUST be the"
     echo "                          first argument."
     echo
     echo "-Bdir/                    Specifies where to find the cc1* 
compiler. Normally"
     echo "                          this may be omitted unless 
debugging a specific"
     echo "                          compiler. Since cc must be the 
first argument the"
     echo "                          -B is recognized as a compiler 
option thus allowing"
     echo "                          a macro to be defined to passed for cc."
     echo
     echo "-cpp                      Debug the preprocessor (or cpp-precomp)."
     echo
     echo "-driver                   Debug the driver. Either -cpp or 
-driver but not both"
     echo "                          may be specified.  If neither are 
specified then the"
     echo "                          compiler phase is to be debugged 
(e.g., cc1plus)."
     echo
     echo "-ver=n                    Version of gcc being used if -cpp 
not specified. This"
     echo "                          can be 2 or 3.  See Notes [2], 
[3], and [4] below."
     echo
     echo "--echo args | dir | all   Echo the gdb set args command 
written to the command"
     echo "                          file, or the gdb dir command, or 
echo both of them."
     echo
     echo "--dir=dir1:dir2:...:dirN  A list of directories for gdb 
that will be placed in"
     echo "                          a gdb dir command placed in the 
command file."
     echo
     echo "--dir=name                Alternative way to specify the 
gdb dir paths. The"
     echo "                          name can either be a filename 
containing a line of"
     echo "                          paths separated by colons (like 
the explicit previous"
     echo "                          form) OR it can be an environment 
variable similarly"
     echo "                          defined with the directory paths. 
It is an error for"
     echo "                          the name to be both an 
environment variable and a"
     echo "                          filename."
     echo
     echo "--help                    Only display this help 
information.  This can be"
     echo "                          specified in place of the cc."
     echo
     echo "--save-commands           Do not delete command file 
(\"debug_gcc_command_file\")."
     echo "                          Setting environment variable 
SAVE_COMMANDS to 1 can"
     echo "                          be used in place of --save-commands."
     echo
     echo
     echo "Note [1]: If the command file \"debug_gcc_command_file\" 
already exists in the"
     echo "          current directory when this script is run it is renamed to"
     echo "          \"debug_gcc_command_file-previous\".  When you 
exit gdb the original"
     echo "          debug_gcc_command_file is restored.  This is only 
mentioned in case"
     echo "          for some reason you don't exit gdb cleanly."
     echo
     echo "Note [2]: Gcc 3.x has an integrated preprocessor.  Thus 
-cpp implies that"
     echo "          a -save-temps will be added to debug the 
stand-alone preprocessor,"
     echo "          cpp0.  For debugging the compiler itself, 
including the integrated"
     echo "          preprocessor, omit the -cpp option."
     echo
     echo "Note [3]: Gcc 2.x always uses a separate preprocessor and 
thus the -cpp is"
     echo "          always required to debug it.  Further, the script 
needs to know when"
     echo "          you want to debug the compiler since it must add 
a -save-temps to the"
     echo "          command to save the temporary file for the 
compiler input.  Thus a"
     echo "          -ver=2 is required when debugging a gcc 2.x 
compiler.  As stated in"
     echo "          Note [2] this is unnecessary for gcc 3.x because 
of it's integrated"
     echo "          preprocessor and the reason you don't want to add 
a -save-temps when"
     echo "          debugging a gcc 3.x compiler is that implies a 
different behavior of"
     echo "          the compiler (i.e., invoking cpp0 and then the 
compiler instead of"
     echo "          just the compiler)."
     echo
     echo "Note [4]: The Gcc 3.x driver (at least the Apple version) 
supports an option"
     echo "          (-###) specifically designed to support script 
like this one.  The"
     echo "          option is basically the same as -v except that 
the displayed"
     echo "          command line is not executed (since we only need 
it for gdb) and"
     echo "          all arguments are quoted.  A -v doesn't quote the 
displayed"
     echo "          arguments leading to possible quoting problems 
passing the arguments"
     echo "          to gdb.  Of course for Gcc 2.x you're just screwed!"
}

#
## debug_gcc cc [-Bdir/] [-cpp|-driver] [-ver=n] 
[--dir=dir1:dir2:...:dirN | --dir=name]
##              [--echo args | dir | all] cc-options...
##
## This script can be used to debug all phases of the gcc compiler; 
driver, preprocessor,
## or gcc compiler.  It invokes gdb on the appropriate component and 
sets up a local
## command script (passed to gdb using its -command option) to define 
all the arguments
## appropriate to that phase along with path definitions so that gdb 
can access the
## compiler source.
##
## If not debugging the driver, the script will add a -v (and 
-save-temps for gcc
## 2.x) option to the command line to allow it to capture the 
phases's tool name
## (e.g., cc1plus) and all the arguments passed to that phase.  The 
tool name is
## what's passed to gdb.  The arguments are used to build a SET args 
gdb command
## which is placed in a command file generated into the current directory.  The
## command file is passed to gdb to execute when gdb is invoked by the script.
##
## Debugging the driver is similar to debugging the preprocessor or 
compiler phases
## except the command line use is basically the one passed to the 
script minus the
## script's own arguments.  Again we have the tool name (e.g., cc) 
and its arguments
## for building the command file and invoking gdb.
##
## This script also allows you to specify a set of directory paths 
for gdb to use
## through a gdb dir command that would be added to the command file.
##
## Note [1]: If the command file "debug_gcc_command_file" already exists in the
##           current directory when this script is run it is renamed to
##           "debug_gcc_command_file-previous".  When you exit gdb the original
##           debug_gcc_command_file is restored.  This is only 
mentioned in case
##	     for some reason you don't exit gdb cleanly.
##
## Note [2]: Gcc 3.x has an integrated preprocessor.  Thus -cpp implies that
##	     a -save-temps will be added to debug the stand-alone preprocessor,
##	     cpp0.  For debugging the compiler itself, including the integrated
##	     preprocessor, omit the -cpp option.
##
## Note [3]: Gcc 2.x always uses a separate preprocessor and thus the -cpp is
##	     always required to debug it.  Further, the script needs 
to know when
##	     you want to debug the compiler since it must add a 
-save-temps to the
##	     command to save the temporary file for the compiler input.  Thus a
##	     -ver=2 is required when debugging a gcc 2.x compiler. 
As stated in
##	     Note [2] this is unnecessary for gcc 3.x because of it's 
integrated
##	     preprocessor and the reason you don't want to add a 
-save-temps when
##           debugging a gcc 3.x compiler is that implies a different 
behavior of
##	     the compiler (i.e., invoking cpp0 and then the compiler instead of
##	     just the compiler).
##
## Note [4]: The Gcc 3.x driver (at least the Apple version) supports 
an option"
##	     (-###) specifically designed to support script like this one.  The
##	     option is basically the same as -v except that the displayed"
##	     command line is not executed (since we only need it for gdb) and"
##	     all arguments are quoted.  A -v doesn't quote the displayed"
##	     arguments leading to possible quoting problems passing 
the arguments"
##	     to gdb.  Of course for Gcc 2.x you're just screwed!"
##
## Options: Must precede any of the gcc command lines options for the compiler.
##
##   cc				The compiler to invoke.  Note that 
this MUST be the
##                              first argument.
##
##   -Bdir/                     Specifies where to find the cc1* 
compiler.  Normally
##                              this may be omitted unless debugging a specific
##                              compiler.  Since cc must be the first 
argument the
##                              -B is recognized as a compiler option 
thus allowing
##                              a macro to be defined to passed for cc.
##
##   -cpp			Debug the preprocessor (or cpp-precomp).
##
##   -driver			Debug the driver.  Either -cpp or 
-driver but not both
##				may be specified.  If neither are 
specified then the
##				compiler phase is to be debugged 
(e.g., cc1plus).
##
##   -ver=n			Version of gcc being used if -cpp not 
specified.  This
##				can be 2 or 3.  See Notes [2], [3], 
and [4] above.
##
##   --echo args | dir | all	Echo the gdb set args command written 
to the command
##                              file, or the gdb dir command, or echo 
both of them.
##
##   --dir=dir1:dir2:...:dirN	A list of directories for gdb that 
will be placed in
##				a gdb dir command placed in the command file.
##
##   --dir=name			Alternative way to specify the gdb 
dir paths. The name can
##				either be a filename containing a 
line of paths separated
##				by colons (like the explicit previous 
form) OR it can
##				be an environment variable similarly 
defined with the
##				directory paths.  It is an error for 
the name to be both
##				an environment variable and a filename.
##
##   --help			Only display this help information. 
This can be
##				specified in place of the cc.
##
##   --save-commands            Do not delete command file 
("debug_gcc_command_file").
##				Setting environment variable SAVE_COMMANDS to 1
##				can be used in place of --save-commands.
##
## Any script option that is not one of the above is assumed to be 
the start of the
## compiler options.
#
debug_gcc()
{
     declare -i done=0 shift_cnt=0 cpp=0 driver=0 previous=0 ver=3 
status cc_args_index=0
     local arg next_arg= cc= objdir= dir= dir1= yn= saw_dir= alius echo= v="-v"
     local save_temps=
     local line= tool= args=
     local save=0 command_file=debug_gcc_command_file
     local -a cc_args
    
     if [ $# -eq 0 ]; then
    	debug_gcc_help
    	return 0
     fi
    
     if [ "$1" != "--help" ] && [ "$1" != "-help" ] && [ "$1" != "-h" ]; then
    	cc=$1
    	shift
     fi
    
     for arg; do
         case $next_arg in
	    -dir | --dir)
		dir="$arg"
		saw_dir="$next_arg"
		next_arg=
		shift_cnt=$shift_cnt+2
		;;
	    -echo | --echo)
	   	echo="$arg"
		next_arg=
		shift_cnt=$shift_cnt+2
	   	;;
	    -ver | --ver)
	   	ver=$arg
		shift_cnt=$shift_cnt+2
	   	;;
	    *)
		case $arg in
	    	    -help | --help | -h)
	    	   	debug_gcc_help
		   	return 0
		   	;;
		    -cpp | --cpp)
		   	cpp=1
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -driver | --driver)
		   	driver=1
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -echo=* | --echo=*)
		   	echo=`echo $arg | sed -e 's/-*echo=//'`
		   	shift_cnt=$shift_cnt+1
		   	;;
		    -echo | --echo)
		   	next_arg=$arg
		   	;;
    	   	    -dir=* | --dir=*)
    	   	   	dir=`echo $arg | sed -e 's/-*dir=//'`
    	   	   	saw_dir=`echo $arg | sed -e 's/\(-*dir\).*$/\1/'`
    	   	   	shift_cnt=$shift_cnt+1
    		   	;;
    		    -dir | --dir)
    		   	next_arg=$arg
    		   	;;
    		    -save-commands | --save-commands)
    		   	save=1
		   	shift_cnt=$shift_cnt+1
    		   	;;
    		    -ver=* | --ver=*)
    	   	   	ver=`echo $arg | sed -e 's/-*ver=//'`
    	   	   	shift_cnt=$shift_cnt+1
    	   	   	;;
    		    -B*)
    		   	cc="$cc $arg"
    		   	shift_cnt=$shift_cnt+1
    		   	;;
    	   	    *)
    	   	   	next_arg=
    	   	   	done=1
    	   	   	;;
       	   	esac
         esac
         if [ $done != 0 ]; then
             break
         fi
     done
    
     shift $shift_cnt
    
     # See if the use explicitly specified -v.  If so we won't need to 
add it when
     # we need to generate the command lines.
    
     for arg; do
         if [ "$arg" = "-v" ]; then
	    v=
	    break
         fi
     done
   
     if [ $driver -ne 0 ] && [ $cpp -ne 0 ]; then
    	echo "What do you want to debug? Driver or preprocessor. You 
can't do both!"
    	return 1
     fi
    
     if [ "$echo" != ""     ] && \
        [ "$echo" != "dir"  ] && \
        [ "$echo" != "args" ] && \
        [ "$echo" != "all"  ]; then
        echo "Invalid --echo specification: $echo"
        return 1
     fi
    
     if [ $ver != 3 -a $ver -ne 2 ]; then
       echo "Invalid compiler version specified (2 or 3 expected): $ver"
       return 1
     fi
    
     if [ "$dir" != "" ] && [ "`echo $dir | sed -e '/:/D'`" != "" ]; then
    	if [ -f "$dir" ]; then
    	    if [ "`eval echo $\`echo $dir\``" != "" ]; then
    	   	echo "$dir is ambiguous -- it's a valid filename and 
a valid environment variable"
    	   	return 1
    	    fi
    	    dir="`cat \"$dir\"`"
    	else
    	    dir="`eval echo $\`echo $dir\``"
    	fi
     fi
    
     if [ "${dir:${#dir}-1:1}" = ":" ]; then
	dir="${dir:0:${#dir}-1}"
     fi
    
     if [ "$saw_dir" != "" ] && [ "$dir" = "" ]; then
    	while [ "$yn" != "y" ] && [ "$yn" != "n" ]; do
    	    echo -n "$saw_dir" 'specified which evaluated to null -- 
do you accept this ? (y,n) '
    	    read -e yn;
    	    yn="${yn:0:1}"
    	done
    	if [ "$yn" = "n" ]; then
    	    return 1
    	fi
     fi
    
     if [ "$cc" = "" ]; then
    	echo "No compiler specifed (see --help for syntax)"
    	return 0
     fi
    
     #
     # If the command line specified a compiler name that was actually 
an alias then
     # we need to get the actual tool to which that name is aliased.
     #
     alius="`alias \"$cc\" 2> /dev/null`"
     if [ $? -eq 0 ]; then
	cc="`echo \"$alius\" | sed -e \"s/alias .*='//\" -e \"s/'\$//\"`"
     fi
    
     #
     # The gcc 3.x drive supports -###, after I added it :-)  It will cause
     # the driver to simply do a -v output without executing the command.
     # It will also quote all the arguments since -v normally doesn't do that
     # and we lose what needs to be quoted if we don't do this.  Of course for
     # gcc 2.x you're screwed.  Do have any pathnames that need quoting.
     #
     if [ $ver -eq 2 ]; then
	save_temps="-save-temps"
     elif [ $driver -eq 0 ]; then
	cc_args[$((cc_args_index++))]='-###'
     fi
    
     #
     # Remove possible -pipe argument.  Must do this in $@ to preserve quoting.
     #
     if [ $driver -ne 0 ]; then
	for arg; do
	    if [ "$arg" != "-pipe" ]; then
	      cc_args[$((cc_args_index++))]="\"$arg\""
	    fi
	done
     else
	for arg; do
	    if [ "$arg" != "-pipe" ]; then
	      cc_args[$((cc_args_index++))]="$arg"
	    fi
	done
     fi
    
     if false; then
    	echo '$cc     =' $cc
    	echo '$cpp    =' $cpp
    	echo '$driver =' $driver
    	echo '$dir    =' $dir
    	echo '$echo   =' $echo
    	echo '$objdir =' $objdir
    	echo '$save   =' $save
    	echo '$v      =' $v
    	echo '$ver    =' $ver
    	echo '$@      =' $@
    	return 1
     fi

     #
     # Set the appropriate command line to use (driver line, 
preprocessor line, or c/c++
     # compiler line) depending on the script options.  The line 
allows us to extract the
     # tool name that is invoked and all the arguments that follow. 
The tool is what
     # is passed to gdb to debug.  The arguments are used to build a 
set args gdb
     # command for a local -command file.
     #
     if [ $driver -ne 0 ]; then
    	#
	# If debugging the driver, the command line is obviously all 
the script's command
	# line options minus the ones specific to the script.
	#
    	line="$cc ""${cc_args[@]}"
    	tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\)\(.*\)$/\1/'`"
    	if [ "$tool" = "" ]; then
    	    echo "Could not find driver name (?)"
    	    return 1
    	fi
     else
	#
	# Compile program and save the -v output in a temp file that 
we can grep for
	# the prerocessor line or c/c++ compiler line.  If the user exlicitly
	# specified -v then we don't have to.  But if we do then we 
must remove it
	# from the gdb args list.  In other words the args list only 
has the -v if
	# the user explicitly specified it.
	#
    	$cc "${cc_args[@]}" $v -c $save_temps 2> /tmp/dbg.$$
    	status=$?
    	if [ $status -gt 1 ]; then
    	    echo "Unexpected status returned from compilation: $status"
    	    more /tmp/dbg.$$
    	    return 1
    	fi
    
	# ignore any compile-time errors
    
    	if false; then
    	    echo '##############' /tmp/dbg.$$ '##############'
    	    more /tmp/dbg.$$
    	    return 1
	fi

	if [ $cpp -ne 0 ]; then
	    line="`cat /tmp/dbg.$$ | grep [-]D__GNUC__`"
	    tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\)\(.*\)$/\1/'`"
	    if [ "$tool" = "" ]; then
		echo "Could not find preprocessor name in the -v 
output (in /tmp/dbg.$$)"
		return 1
	    fi
	else
	    #line="`cat /tmp/dbg.$$ | grep [-]o[\ ] | grep --invert-match /as`"
	    #line="`cat /tmp/dbg.$$ | grep [-]o[\ ] | grep .*cc1.* `"
	    line="`cat /tmp/dbg.$$ | grep .*cc1.*`"
	    tool="`echo $line | sed -e 's/^[ ]*\([^ ]*\).*$/\1/'`"
	    if [ "$tool" = "" ]; then
		echo "Could not find compiler name in the -v output 
(in /tmp/dbg.$$)"
		return 1
	    fi
	    # do -version first so -v removal doesn't screw it up.
	    line="`echo \"$line\" | sed -e 's/-version//'`"
	    if [ "$v" = "-v" ]; then
		line="`echo \"$line\" | sed -e 's/-v//'`"
	    fi
	    # remove null but quoted arguments...
	    line="`echo \"$line\" | sed -e 's/\"\"//g'`"
	fi

	rm -f /tmp/dbg.$$			# don't need this any more
     fi
    
     #
     # Once the tool name is known and we have the appropriate command 
line from it we
     # can extract all the arguments we'll need to build the gdb set 
args command for the
     # -command file.
     #
     # Note, the original line (commented out below) blows in sed 
apparently when $line
     # is more than 1098 characters.  The second one doesn't.  Go figure!
     #
     #args="`echo $line | sed -e 's,^[ ]*'"$tool"'[ ]*\(.*\)$,\1,'`"
     args="`echo \"$line\" | sed -e 's,'\"$tool\"',,'`"
    
     #
     # At last we got all of what we were after; tool and its 
arguments.  Create a
     # -command file in the current directory containing a set args to 
define the
     # tools arguments.
     #
    
     if [ $save -eq 0 ] && [ -e $command_file ] && [ "$SAVE_COMMANDS" 
!= "1" ]; then
    	mv $command_file ${command_file}-previous
    	previous=1
     fi
    
     if [ "$args" != "" ];then
    	echo "set args $args" > $command_file	# set args arguments...
    	if [ "$echo" = "args" ] || [ "$echo" = "all" ]; then
    	    echo "$command_file: set args $args"
    	fi
     fi
    
     #
     # If the --dir script options was specified add it to the command 
file too as a
     # dir gdb command.  The --dir arguments are defines as being in 
the syntax that
     # gdb expects.
     #
     if [ "$dir" != "" ]; then
    	echo "dir $dir"	>> $command_file	# dir all the useful 
compiler directories
    	if [ "$echo" = "dir" ] || [ "$echo" = "all" ]; then
    	    echo "$command_file: dir $dir"
    	fi
     fi
    
     #
     # Finally we can invoke gdb...
     #
     if [ "$echo" != "" ]; then
    	echo "gdb -command $command_file $tool"
     fi
     gdb -command $command_file "$tool"		# debug that sucker
    
     #
     # Delete the command file and rename any previous one (unless 
-save-commands
     # was specified).
     #
     if [ $save -eq 0 ] && [ "$SAVE_COMMANDS" != "1" ]; then
         rm -f $command_file			# now we don't need 
this any more
         if [ $previous -ne 0 ]; then
             mv ${command_file}-previous $command_file
         fi
     fi
}

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2001-11-20  0:54 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-29 13:37 [PATCH] Add -### option Ira Ruben
2001-10-29 14:31 ` Graham Stott
2001-10-29 14:39   ` Ira Ruben
2001-10-29 15:31     ` Robert Lipe
2001-10-29 16:11       ` Ira Ruben
2001-10-29 16:33         ` Zack Weinberg
2001-10-29 17:42           ` Daniel Jacobowitz
2001-10-29 19:50           ` Tom Tromey
2001-11-13 15:03             ` Alexandre Oliva
     [not found]               ` <p05100300b81f32ebb325@[17.202.44.152]>
     [not found]                 ` <87g07as3fh.fsf@creche.redhat.com>
2001-11-13 15:03                   ` Ira Ruben
     [not found] ` <20011103134058.C11575@redhat.com>
     [not found]   ` <p05100300b80a70b4f236@[198.144.204.37]>
     [not found]     ` <20011103224821.A11917@redhat.com>
     [not found]       ` <p05100300b80aa23d95d1@[198.144.204.26]>
2001-11-06 22:41         ` Richard Henderson
2001-11-13  4:33           ` Richard Henderson
2001-11-13 15:03           ` Richard Henderson
2001-10-29 17:19 Ira Ruben

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).