Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@codesourcery.com>
To: Vladimir Prus <vladimir@codesourcery.com>
Cc: gdb-patches@sourceware.org
Subject: Re: Per-inferior program arguments and io terminal
Date: Thu, 14 Jan 2010 15:49:00 -0000	[thread overview]
Message-ID: <201001141549.11692.pedro@codesourcery.com> (raw)
In-Reply-To: <201001141809.17094.vladimir@codesourcery.com>

On Thursday 14 January 2010 15:09:17, Vladimir Prus wrote:
> On Thursday 14 January 2010 16:37:42 Pedro Alves wrote:
> > Better choices would be: set_inferior_tty_command/show_inferior_tty_command
> > or set_inferior_tty/show_inferior_tty.
> 
> Done. Also done for args function.

Thanks.

> > > @@ -87,6 +88,10 @@ free_inferior (struct inferior *inf)
> > >  {
> > >    discard_all_inferior_continuations (inf);
> > >    inferior_free_data (inf);
> > > +  xfree (inf->args);
> > 
> > > +  xfree (inf->argv);
> > 
> > Hmm, I was going to say that this usually leaks argv[0..argc], and
> > you should use freeargv, but, I now see that the only caller
> > of set_inferior_args_vector is captured_main, and the
> > argc,argv are presently a shallow copy of `main's arguments, so
> > definitely not ok to free this as is.  This definitely needs a
> > comment in the description of inferior->argv, expanded from
> > the comment that was in inferior_argv.
> 
> I've added a comment.

Thanks, but not correct yet...

> @@ -87,6 +88,10 @@ free_inferior (struct inferior *inf)
>  {
>    discard_all_inferior_continuations (inf);
>    inferior_free_data (inf);
> +  xfree (inf->args);
> +  xfree (inf->argv);
> +  xfree (inf->terminal);
> +  free_environ (inf->environment);
>    xfree (inf->private);

Maybe I confused you with saying "shallow" copy.  This
argv comes from a direct `argv' pointer copy:

 void
 set_inferior_args_vector (int argc, char **argv)
 {
  current_inferior ()->argc = argc;
  current_inferior ()->argv = argv;
 }

This is only called from captured_main, like so:

 set_inferior_args_vector (argc - optind, &argv[optind]);

So at best, you'd need to `xfree (&argv[-optind])', but
even that would be broken.  Say, let's ignore that offset.  argv
in that context is a pointer copy coming from:

static int
captured_main (void *data)
{
  struct captured_main_args *context = data;
  int argc = context->argc;
  char **argv = context->argv;

and this context->argv itself comes from:

int
main (int argc, char **argv)
{
  struct captured_main_args args;
  memset (&args, 0, sizeof args);
  args.argc = argc;
  args.argv = argv;
  ^^^^^^^^^^^^^^^^
  args.use_windows = 0;
  args.interpreter_p = INTERP_CONSOLE;
  return gdb_main (&args);
}

so, freeing that would be equivalent to:

 int
 main (int argc, char **argv)
 {
    free (argv);
 }

which is not kosher (try it).   You should _not_ free inferior->argv.

Actually, I'll try it myself:

> ./gdb -q --args /home/pedro/gdb/tests/threads --args foo bar
(gdb) clone-inferior
Added inferior 2.
(gdb) inferior 1
[Switching to inferior 1 [process 0] (/home/pedro/gdb/tests/threads)]
(gdb) inferior 2
[Switching to inferior 2 [process 0] (/home/pedro/gdb/tests/threads)]
(gdb) remove-inferior 1
*** glibc detected *** ./gdb: munmap_chunk(): invalid pointer: 0x00007fff9d5b4fe8 ***
======= Backtrace: =========
/lib/libc.so.6(cfree+0x1b6)[0x7f7f0e86cd46]
./gdb(xfree+0x1c)[0x462f1e]
:
Cancelled (core dumped)

And since you now made me try the patch... :-)

>qr
Warning: trailing whitespace in lines 230,238 of gdb/solib.c
Warning: trailing whitespace in lines 664,673,683 of gdb/main.c
Warning: trailing whitespace in line 266 of gdb/mi/mi-cmd-env.c
Warning: trailing whitespace in lines 129,186,200,213,2653,2654,2655 of gdb/infcmd.c
Refreshed patch per-inferior-args3.diff

-- 
Pedro Alves


  reply	other threads:[~2010-01-14 15:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-13 12:44 Vladimir Prus
2010-01-13 15:49 ` Pedro Alves
2010-01-14  9:27   ` Vladimir Prus
2010-01-14 13:38     ` Pedro Alves
2010-01-14 15:09       ` Vladimir Prus
2010-01-14 15:49         ` Pedro Alves [this message]
2010-01-19 21:53           ` Vladimir Prus
2010-01-20  6:14             ` Pedro Alves
2010-01-20 14:23               ` Vladimir Prus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201001141549.11692.pedro@codesourcery.com \
    --to=pedro@codesourcery.com \
    --cc=gdb-patches@sourceware.org \
    --cc=vladimir@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox