Allow making GDB not automatically connect to the native target.
authorPedro Alves <palves@redhat.com>
Wed, 21 May 2014 17:30:47 +0000 (18:30 +0100)
committerPedro Alves <palves@redhat.com>
Wed, 21 May 2014 17:30:47 +0000 (18:30 +0100)
commit6a3cb8e88a739c967bb9b2d8774bf96b87a7fda4
tree6d970aa7cd42ad75c1a475f4b8265b31959a709e
parent930ee1b1bf8c36a746ea5f7456afba094aabc887
Allow making GDB not automatically connect to the native target.

Sometimes it's useful to be able to disable the automatic connection
to the native target.  E.g., sometimes GDB disconnects from the
extended-remote target I was debugging, without me noticing it, and
then I do "run".  That starts the program locally, and only after a
little head scratch session do I figure out the program is running
locally instead of remotely as intended.  Same thing with "attach",
"info os", etc.

With the patch, we now can have this instead:

 (gdb) set auto-connect-native-target off
 (gdb) target extended-remote :9999
 ...
 *gdb disconnects*
 (gdb) run
 Don't know how to run.  Try "help target".

To still be able to connect to the native target with
auto-connect-native-target set to off, I've made "target native" work
instead of erroring out as today.

Before:

 (gdb) target native
 Use the "run" command to start a native process.

After:

 (gdb) target native
 Done.  Use the "run" command to start a process.
 (gdb) maint print target-stack
 The current target stack is:
   - native (Native process)
   - exec (Local exec file)
   - None (None)
 (gdb) run
 Starting program: ./a.out
 ...

I've also wanted this for the testsuite, when running against the
native-extended-gdbserver.exp board (runs against gdbserver in
extended-remote mode).  With a non-native-target board, it's always a
bug to launch a program with the native target.  Turns out we still
have one such case this patch catches:

 (gdb) break main
 Breakpoint 1 at 0x4009e5: file ../../../src/gdb/testsuite/gdb.base/coremaker.c, line 138.
 (gdb) run
 Don't know how to run.  Try "help target".
 (gdb) FAIL: gdb.base/corefile.exp: run: with core

On the patch itself, probably the least obvious bit is the need to go
through all targets, and move the unpush_target call to after the
generic_mourn_inferior call instead of before.  This is what
inf-ptrace.c does too, ever since multi-process support was added.
The reason inf-ptrace.c does things in that order is that in the
current multi-process/single-target model, we shouldn't unpush the
target if there are still other live inferiors being debugged.  The
check for that is "have_inferiors ()" (a misnomer nowadays...), which
does:

 have_inferiors (void)
 {
   for (inf = inferior_list; inf; inf = inf->next)
     if (inf->pid != 0)
       return 1;

It's generic_mourn_inferior that ends up clearing inf->pid, so we need
to call it before the have_inferiors check.  To make all native
targets behave the same WRT to explicit "target native", I've added an
inf_child_maybe_unpush_target function that targets call instead of
calling unpush_target directly, and as that includes the
have_inferiors check, I needed to adjust the targets.

Tested on x86_64 Fedora 20, native, and also with the
extended-gdbserver board.

Confirmed a cross build of djgpp gdb still builds.

Smoke tested a cross build of Windows gdb under Wine.

Untested otherwise.

gdb/
2014-05-21  Pedro Alves  <palves@redhat.com>

* inf-child.c (inf_child_ops, inf_child_explicitly_opened): New
globals.
(inf_child_open_target): New function.
(inf_child_open): Use inf_child_open_target to push the target
instead of erroring out.
(inf_child_disconnect, inf_child_close)
(inf_child_maybe_unpush_target): New functions.
(inf_child_target): Install inf_child_disconnect and
inf_child_close.  Store a pointer to the returned object.
* inf-child.h (inf_child_open_target, inf_child_maybe_unpush): New
declarations.
* target.c (auto_connect_native_target): New global.
(show_default_run_target): New function.
(find_default_run_target): Return NULL if automatically connecting
to the native target is disabled.
(_initialize_target): Install set/show auto-connect-native-target.
* NEWS: Mention "set auto-connect-native-target", and "target
native".
* linux-nat.c (super_close): New global.
(linux_nat_close): Call super_close.
(linux_nat_add_target): Store a pointer to the base class's
to_close method.
* inf-ptrace.c (inf_ptrace_mourn_inferior, inf_ptrace_detach): Use
inf_child_maybe_unpush.
* inf-ttrace.c (inf_ttrace_him): Don't push the target if it is
already pushed.
(inf_ttrace_mourn_inferior): Only unpush the target after mourning
the inferior.  Use inf_child_maybe_unpush_target.
(inf_ttrace_attach): Don't push the target if it is already
pushed.
(inf_ttrace_detach): Use inf_child_maybe_unpush_target.
* darwin-nat.c (darwin_mourn_inferior): Only unpush the target
after mourning the inferior.  Use inf_child_maybe_unpush_target.
(darwin_attach_pid): Don't push the target if it is already
pushed.
* gnu-nat.c (gnu_mourn_inferior): Only unpush the target after
mourning the inferior.  Use inf_child_maybe_unpush_target.
(gnu_detach): Use inf_child_maybe_unpush_target.
* go32-nat.c (go32_create_inferior): Don't push the target if it
is already pushed.
(go32_mourn_inferior): Use inf_child_maybe_unpush_target.
* nto-procfs.c (procfs_is_nto_target): Adjust comment.
(procfs_open): Rename to ...
(procfs_open_1): ... this.  Add target_ops parameter.  Adjust
comments.  Can target_preopen before changing node.  Call
inf_child_open_target to push the target explicitly.
(procfs_attach): Don't push the target if it is already pushed.
(procfs_detach): Use inf_child_maybe_unpush_target.
(procfs_create_inferior): Don't push the target if it is already
pushed.
(nto_native_ops): New global.
(procfs_open): Reimplement.
(procfs_native_open): New function.
(init_procfs_targets): Install procfs_native_open as to_open of
"target native".  Store a pointer to the "native" target in
nto_native_ops.
* procfs.c (procfs_attach): Don't push the target if it is already
pushed.
(procfs_detach): Use inf_child_maybe_unpush_target.
(procfs_mourn_inferior): Only unpush the target after mourning the
inferior.  Use inf_child_maybe_unpush_target.
(procfs_init_inferior): Don't push the target if it is already
pushed.
* windows-nat.c (do_initial_windows_stuff): Don't push the target
if it is already pushed.
(windows_detach): Use inf_child_maybe_unpush_target.
(windows_mourn_inferior): Only unpush the target after mourning
the inferior.  Use inf_child_maybe_unpush_target.

gdb/doc/
2014-05-21  Pedro Alves  <palves@redhat.com>

* gdb.texinfo (Starting): Document "set/show
auto-connect-native-target".
(Target Commands): Document "target native".

gdb/testsuite/
2014-05-21  Pedro Alves  <palves@redhat.com>

* boards/gdbserver-base.exp (GDBFLAGS): Set to "set
auto-connect-native-target off".
* gdb.base/auto-connect-native-target.c: New file.
* gdb.base/auto-connect-native-target.exp: New file.
20 files changed:
gdb/ChangeLog
gdb/NEWS
gdb/darwin-nat.c
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/gnu-nat.c
gdb/go32-nat.c
gdb/inf-child.c
gdb/inf-child.h
gdb/inf-ptrace.c
gdb/inf-ttrace.c
gdb/linux-nat.c
gdb/nto-procfs.c
gdb/procfs.c
gdb/target.c
gdb/testsuite/ChangeLog
gdb/testsuite/boards/gdbserver-base.exp
gdb/testsuite/gdb.base/auto-connect-native-target.c [new file with mode: 0644]
gdb/testsuite/gdb.base/auto-connect-native-target.exp [new file with mode: 0644]
gdb/windows-nat.c