public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 8/8] Allow making GDB not automatically connect to the native target.
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (2 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 1/8] Rename "target djgpp" -> "target native" Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 16:47   ` Eli Zaretskii
                     ` (3 more replies)
  2014-03-17 15:23 ` [PATCH 6/8] go32-nat.c: Don't override to_open Pedro Alves
                   ` (6 subsequent siblings)
  10 siblings, 4 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

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 that 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, I 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 17, 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-03-17  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-03-17  Pedro Alves  <palves@redhat.com>

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

gdb/testsuite/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* boards/native-extended-gdbserver.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.
---
 gdb/NEWS                                           |  10 +
 gdb/darwin-nat.c                                   |   6 +-
 gdb/doc/gdb.texinfo                                |  58 ++++++
 gdb/gnu-nat.c                                      |   4 +-
 gdb/go32-nat.c                                     |   5 +-
 gdb/inf-child.c                                    |  62 +++++-
 gdb/inf-child.h                                    |  11 ++
 gdb/inf-ptrace.c                                   |   6 +-
 gdb/inf-ttrace.c                                   |  10 +-
 gdb/linux-nat.c                                    |   8 +
 gdb/nto-procfs.c                                   |  53 ++++--
 gdb/procfs.c                                       |  13 +-
 gdb/target.c                                       |  48 ++++-
 gdb/testsuite/boards/native-extended-gdbserver.exp |   2 +
 .../gdb.base/auto-connect-native-target.c          |  23 +++
 .../gdb.base/auto-connect-native-target.exp        | 209 +++++++++++++++++++++
 gdb/windows-nat.c                                  |   7 +-
 17 files changed, 488 insertions(+), 47 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.c
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.exp

diff --git a/gdb/NEWS b/gdb/NEWS
index 5bdda0a..b917eb0 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -43,6 +43,12 @@ maint ada show ignore-descriptive-types
   the user manual for more details on descriptive types and the intended
   usage of this option.
 
+set auto-connect-native-target
+
+  Control whether GDB is allowed to automatically connect to the
+  native target for the run, attach, etc. commands when not connected
+  to any target yet.  See also "target native" below.
+
 * New features in the GDB remote stub, GDBserver
 
   ** New option --debug-format=option1[,option2,...] allows one to add
@@ -87,6 +93,10 @@ maint ada show ignore-descriptive-types
   "help target", "info target", "info files", "maint print
   target-stack".
 
+* The "target native" command now really connects to the native
+  target.  This can be used to launch native programs even when "set
+  auto-connect-native-target" is set to off.
+
 * New remote packets
 
 qXfer:btrace:read's annex
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 8650f03..c0e6340 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1161,8 +1161,6 @@ darwin_mourn_inferior (struct target_ops *ops)
   mach_port_t prev;
   int i;
 
-  unpush_target (darwin_ops);
-
   /* Deallocate threads.  */
   if (inf->private->threads)
     {
@@ -1218,6 +1216,7 @@ darwin_mourn_inferior (struct target_ops *ops)
   inf->private = NULL;
 
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 static void
@@ -1453,7 +1452,8 @@ impact on the debugging session."));
 	     "returned: %d"),
 	   kret);
 
-  push_target (darwin_ops);
+  if (!target_is_pushed (darwin_ops))
+    push_target (darwin_ops);
 }
 
 static void
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index de5ac63..2fbacb9 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2146,6 +2146,57 @@ initialization file---such as @file{.cshrc} for C-shell,
 $@file{.zshenv} for the Z shell, or the file specified in the
 @samp{BASH_ENV} environment variable for BASH.
 
+@anchor{set auto-connect-native-target}
+@kindex set auto-connect-native-target
+@item set auto-connect-native-target
+@itemx set auto-connect-native-target on
+@itemx set auto-connect-native-target off
+@itemx show auto-connect-native-target
+
+By default, if not connected to any target yet (e.g., with
+@code{target remote}), the @code{run} command starts your program as a
+native process under @value{GDBN}, on your local machine.  If you're
+sure you don't want to debug programs on your local machine, you can
+tell @value{GDBN} to not connect to the native target automatically
+with the @code{set auto-connect-native-target off} command.
+
+If @code{on}, which is the default, and if @value{GDBN} is not
+connected to a target already, the @code{run} command automaticaly
+connects to the native target, if one is available.
+
+If @code{off}, and if @value{GDBN} is not connected to a target
+already, the @code{run} command fails with an error:
+
+@smallexample
+(@value{GDBP}) run
+Don't know how to run.  Try "help target".
+@end smallexample
+
+If @value{GDBN} is already connected to a target, @value{GDBN} always
+uses it with the @code{run} command.
+
+In any case, you can explicitly connect to the native target with the
+@code{target native} command.  For example,
+
+@smallexample
+(@value{GDBP}) set auto-connect-native-target off
+(@value{GDBP}) run
+Don't know how to run.  Try "help target".
+(@value{GDBP}) target native
+(@value{GDBP}) run
+Starting program: ./a.out
+[Inferior 1 (process 10421) exited normally]
+@end smallexample
+
+In case you connected explicitly to the @code{native} target,
+@value{GDBN} remains connected even if all inferiors exit, ready for
+the next @code{run} command.  Use the @code{disconnect} command to
+disconnect.
+
+Examples of other commands that likewise respect the
+@code{auto-connect-native-target} setting: @code{run}, @code{attach},
+@code{info proc}, @code{info os}.
+
 @kindex set disable-randomization
 @item set disable-randomization
 @itemx set disable-randomization on
@@ -18050,6 +18101,13 @@ provide these.  For info about any processor-specific simulator details,
 see the appropriate section in @ref{Embedded Processors, ,Embedded
 Processors}.
 
+@item target native
+@cindex native target
+Setup for local/native process debugging.  Useful to make the
+@code{run} command spawn native processes (likewise @code{attach},
+etc.@:) even when @code{set auto-connect-native-target} is @code{off}
+(@pxref{set auto-connect-native-target}).
+
 @end table
 
 Different targets are available on different configurations of @value{GDBN};
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 8235811..3317215 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2080,8 +2080,8 @@ gnu_mourn_inferior (struct target_ops *ops)
 {
   inf_debug (gnu_current_inf, "rip");
   inf_detach (gnu_current_inf);
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 \f
@@ -2253,7 +2253,7 @@ gnu_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);
 
-  unpush_target (ops);	/* Pop out of handling an inferior.  */
+  inf_child_maybe_unpush_target (ops);
 }
 \f
 static void
diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c
index e17707a..69edb9d 100644
--- a/gdb/go32-nat.c
+++ b/gdb/go32-nat.c
@@ -710,7 +710,8 @@ go32_create_inferior (struct target_ops *ops, char *exec_file,
   inf = current_inferior ();
   inferior_appeared (inf, SOME_PID);
 
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 
   add_thread_silent (inferior_ptid);
 
@@ -744,7 +745,7 @@ go32_mourn_inferior (struct target_ops *ops)
   delete_thread_silent (ptid);
   prog_has_started = 0;
 
-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
   generic_mourn_inferior ();
 }
 
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 8a99adc..a3c4fe4 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -40,6 +40,11 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+/* A pointer to what is returned by inf_child_target.  Used by
+   inf_child_open to push the most-derived target in reaction to
+   "target native".  */
+static struct target_ops *inf_child_ops = NULL;
+
 /* Helper function for child_wait and the derivatives of child_wait.
    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
    translation of that in OURSTATUS.  */
@@ -109,10 +114,58 @@ inf_child_prepare_to_store (struct target_ops *self,
 {
 }
 
+/* True if the user did "target native".  In that case, we won't
+   unpush the child target automatically when the last inferior is
+   gone.  */
+static int inf_child_explicitly_opened;
+
+/* See inf-child.h.  */
+
+void
+inf_child_open_target (struct target_ops *target, char *arg, int from_tty)
+{
+  target_preopen (from_tty);
+  push_target (target);
+  inf_child_explicitly_opened = 1;
+  if (from_tty)
+    printf_filtered ("Done.  Use the \"run\" command to start a process.\n");
+}
+
 static void
 inf_child_open (char *arg, int from_tty)
 {
-  error (_("Use the \"run\" command to start a process."));
+  inf_child_open_target (inf_child_ops, arg, from_tty);
+}
+
+/* Implement the to_disconnect target_ops method.  */
+
+static void
+inf_child_disconnect (struct target_ops *target, char *args, int from_tty)
+{
+  if (args != NULL)
+    error (_("Argument given to \"disconnect\"."));
+
+  /* This offers to detach/kill current inferiors, and then pops all
+     targets.  */
+  target_preopen (from_tty);
+}
+
+/* Implement the to_close target_ops method.  */
+
+static void
+inf_child_close (struct target_ops *target)
+{
+  /* In case we were forcibly closed.  */
+  inf_child_explicitly_opened = 0;
+}
+
+/* See inf-child.h.  */
+
+void
+inf_child_maybe_unpush_target (struct target_ops *ops)
+{
+  if (!inf_child_explicitly_opened && !have_inferiors ())
+    unpush_target (ops);
 }
 
 static void
@@ -410,6 +463,8 @@ inf_child_target (void)
   t->to_longname = "Native process";
   t->to_doc = "Native process (started by the \"run\" command).";
   t->to_open = inf_child_open;
+  t->to_close = inf_child_close;
+  t->to_disconnect = inf_child_disconnect;
   t->to_post_attach = inf_child_post_attach;
   t->to_fetch_registers = inf_child_fetch_inferior_registers;
   t->to_store_registers = inf_child_store_inferior_registers;
@@ -445,5 +500,10 @@ inf_child_target (void)
   t->to_magic = OPS_MAGIC;
   t->to_use_agent = inf_child_use_agent;
   t->to_can_use_agent = inf_child_can_use_agent;
+
+  /* Store a pointer so we can push the most-derived target from
+     inf_child_open.  */
+  inf_child_ops = t;
+
   return t;
 }
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index 4473cff..3161ac3 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -30,4 +30,15 @@ extern struct target_ops *inf_child_target (void);
 /* This is for native targets which use a unix/POSIX-style waitstatus.  */
 extern void store_waitstatus (struct target_waitstatus *, int);
 
+/* This is to be called by the native target's open routine to push
+   the target, in case it need to override to_open.  */
+
+extern void inf_child_open_target (struct target_ops *target,
+				   char *arg, int from_tty);
+
+/* Unpush the target if it wasn't explicitly open with "target native"
+   and there are no live inferiors left.  */
+
+extern void inf_child_maybe_unpush_target (struct target_ops *ops);
+
 #endif
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index bffb4ba..cc4921b 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -176,8 +176,7 @@ inf_ptrace_mourn_inferior (struct target_ops *ops)
 
   generic_mourn_inferior ();
 
-  if (!have_inferiors ())
-    unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
@@ -297,8 +296,7 @@ inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);
 
-  if (!have_inferiors ())
-    unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* Kill the inferior.  */
diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c
index 802dc85..96105dc 100644
--- a/gdb/inf-ttrace.c
+++ b/gdb/inf-ttrace.c
@@ -644,7 +644,8 @@ inf_ttrace_him (struct target_ops *ops, int pid)
 
   do_cleanups (old_chain);
 
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 
   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
 
@@ -695,8 +696,8 @@ inf_ttrace_mourn_inferior (struct target_ops *ops)
     }
   inf_ttrace_page_dict.count = 0;
 
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* Assuming we just attached the debugger to a new inferior, create
@@ -796,7 +797,8 @@ inf_ttrace_attach (struct target_ops *ops, char *args, int from_tty)
 	      (uintptr_t)&tte, sizeof tte, 0) == -1)
     perror_with_name (("ttrace"));
 
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 
   inf_ttrace_create_threads_after_attach (pid);
 }
@@ -837,7 +839,7 @@ inf_ttrace_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);
 
-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }
 
 static void
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index b615423..7faf0dc 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -201,6 +201,10 @@ static int (*linux_nat_siginfo_fixup) (siginfo_t *,
    Called by our to_xfer_partial.  */
 static target_xfer_partial_ftype *super_xfer_partial;
 
+/* The saved to_close method, inherited from inf-ptrace.c.
+   Called by our to_close.  */
+static void (*super_close) (struct target_ops *);
+
 static unsigned int debug_linux_nat;
 static void
 show_debug_linux_nat (struct ui_file *file, int from_tty,
@@ -4776,6 +4780,8 @@ linux_nat_close (struct target_ops *self)
 
   if (linux_ops->to_close)
     linux_ops->to_close (linux_ops);
+
+  super_close (self);
 }
 
 /* When requests are passed down from the linux-nat layer to the
@@ -4857,6 +4863,8 @@ linux_nat_add_target (struct target_ops *t)
   t->to_async = linux_nat_async;
   t->to_terminal_inferior = linux_nat_terminal_inferior;
   t->to_terminal_ours = linux_nat_terminal_ours;
+
+  super_close = t->to_close;
   t->to_close = linux_nat_close;
 
   /* Methods for non-stop support.  */
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index b328dfa..acef9ca 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -54,8 +54,6 @@ static void (*ofunc) ();
 
 static procfs_run run;
 
-static void procfs_open (char *, int);
-
 static ptid_t do_attach (ptid_t ptid);
 
 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
@@ -71,7 +69,7 @@ static int procfs_remove_hw_watchpoint (struct target_ops *self,
 
 static int procfs_stopped_by_watchpoint (struct target_ops *ops);
 
-/* These two globals are only ever set in procfs_open(), but are
+/* These two globals are only ever set in procfs_open_1, but are
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
@@ -103,12 +101,12 @@ procfs_is_nto_target (bfd *abfd)
   return GDB_OSABI_QNXNTO;
 }
 
-/* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
-   For QNX6 (nto), the only valid arg will be a QNX node string, 
-   eg: "/net/some_node".  If arg is not a valid QNX node, we will
-   default to local.  */
+/* This is called when we call 'target native' or 'target procfs
+   <arg>' from the (gdb) prompt.  For QNX6 (nto), the only valid arg
+   will be a QNX node string, eg: "/net/some_node".  If arg is not a
+   valid QNX node, we will default to local.  */
 static void
-procfs_open (char *arg, int from_tty)
+procfs_open_1 (struct target_ops *ops, char *arg, int from_tty)
 {
   char *nodestr;
   char *endstr;
@@ -117,6 +115,9 @@ procfs_open (char *arg, int from_tty)
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;
 
+  /* Offer to kill previous inferiors before opening this target.  */
+  target_preopen (from_tty);
+
   nto_is_nto_target = procfs_is_nto_target;
 
   /* Set the default node used for spawning to this one,
@@ -197,6 +198,8 @@ procfs_open (char *arg, int from_tty)
 	}
     }
   do_cleanups (cleanups);
+
+  inf_child_open_target (ops, arg, from_tty);
   printf_filtered ("Debugging using %s\n", nto_procfs_path);
 }
 
@@ -628,7 +631,8 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
   inferior_appeared (inf, pid);
   inf->attach_flag = 1;
 
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 
   procfs_find_new_threads (ops);
 }
@@ -904,7 +908,7 @@ procfs_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);
   init_thread_list ();
-  unpush_target (ops);	/* Pop out of handling an inferior.  */
+  inf_child_maybe_unpush_target (ops);
 }
 
 static int
@@ -1023,8 +1027,8 @@ procfs_mourn_inferior (struct target_ops *ops)
     }
   inferior_ptid = null_ptid;
   init_thread_list ();
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* This function breaks up an argument string into an argument
@@ -1207,7 +1211,7 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
       /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
          errn, strerror(errn) ); */
     }
-  push_target (ops);
+  inf_child_maybe_push_target (ops);
   target_terminal_init ();
 
   if (exec_bfd != NULL
@@ -1385,6 +1389,25 @@ procfs_can_run (struct target_ops *self)
 /* "target procfs".  */
 static struct target_ops nto_procfs_ops;
 
+/* "target native".  */
+static struct target_ops *nto_native_ops;
+
+/* to_open implementation for "target procfs".  */
+
+static void
+procfs_open (char *arg, int from_tty)
+{
+  procfs_open_1 (&nto_procfs_ops, arg, from_tty);
+}
+
+/* to_open implementation for "target native".  */
+
+static void
+procfs_native_open (char *arg, int from_tty)
+{
+  procfs_open_1 (nto_native_ops, arg, from_tty);
+}
+
 /* Create the "native" and "procfs" targets.  */
 
 static void
@@ -1395,7 +1418,7 @@ init_procfs_targets (void)
   /* Leave to_shortname as "native".  */
   t->to_longname = "QNX Neutrino local process";
   t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
-  t->to_open = procfs_open;
+  t->to_open = procfs_native_open;
   t->to_attach = procfs_attach;
   t->to_post_attach = procfs_post_attach;
   t->to_detach = procfs_detach;
@@ -1424,6 +1447,8 @@ init_procfs_targets (void)
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
 
+  nto_native_ops = t;
+
   /* Register "target native".  This is the default run target.  */
   add_target (t);
 
@@ -1433,6 +1458,8 @@ init_procfs_targets (void)
   nto_procfs_ops.to_can_run = procfs_can_run;
   t->to_longname = "QNX Neutrino local or remote process";
   t->to_doc = "QNX Neutrino process.  target procfs <node>";
+  t->to_open = procfs_open;
+
   add_target (&nto_procfs_ops);
 }
 
diff --git a/gdb/procfs.c b/gdb/procfs.c
index f0d65d2..80b0a6a 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3062,7 +3062,8 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
       fflush (stdout);
     }
   inferior_ptid = do_attach (pid_to_ptid (pid));
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 }
 
 static void
@@ -3091,7 +3092,7 @@ procfs_detach (struct target_ops *ops, const char *args, int from_tty)
 
   inferior_ptid = null_ptid;
   detach_inferior (pid);
-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }
 
 static ptid_t
@@ -4340,7 +4341,8 @@ procfs_mourn_inferior (struct target_ops *ops)
       if (pi)
 	destroy_procinfo (pi);
     }
-  unpush_target (ops);
+
+  generic_mourn_inferior ();
 
   if (dbx_link_bpt != NULL)
     {
@@ -4349,7 +4351,7 @@ procfs_mourn_inferior (struct target_ops *ops)
       dbx_link_bpt = NULL;
     }
 
-  generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* When GDB forks to create a runnable inferior process, this function
@@ -4367,7 +4369,8 @@ procfs_init_inferior (struct target_ops *ops, int pid)
 
   /* This routine called on the parent side (GDB side)
      after GDB forks the inferior.  */
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 
   if ((pi = create_procinfo (pid, 0)) == NULL)
     perror (_("procfs: out of memory in 'init_inferior'"));
diff --git a/gdb/target.c b/gdb/target.c
index 0d22297..bbb8e5e 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2574,6 +2574,20 @@ target_require_runnable (void)
   internal_error (__FILE__, __LINE__, _("No targets found"));
 }
 
+/* Whether GDB is allowed to fall back to the default run target for
+   "run", "attach", etc. when no target is connected yet.  */
+static int auto_connect_native_target = 1;
+
+static void
+show_auto_connect_native_target (struct ui_file *file, int from_tty,
+				 struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file,
+		    _("Whether GDB may automatically connect to the "
+		      "native target is %s.\n"),
+		    value);
+}
+
 /* Look through the list of possible targets for a target that can
    execute a run or attach command without any other data.  This is
    used to locate the default process stratum.
@@ -2584,23 +2598,28 @@ target_require_runnable (void)
 static struct target_ops *
 find_default_run_target (char *do_mesg)
 {
-  struct target_ops **t;
   struct target_ops *runable = NULL;
-  int count;
 
-  count = 0;
-
-  for (t = target_structs; t < target_structs + target_struct_size;
-       ++t)
+  if (auto_connect_native_target)
     {
-      if ((*t)->to_can_run != delegate_can_run && target_can_run (*t))
+      struct target_ops **t;
+      int count = 0;
+
+      for (t = target_structs; t < target_structs + target_struct_size;
+	   ++t)
 	{
-	  runable = *t;
-	  ++count;
+	  if ((*t)->to_can_run != delegate_can_run && target_can_run (*t))
+	    {
+	      runable = *t;
+	      ++count;
+	    }
 	}
+
+      if (count != 1)
+	runable = NULL;
     }
 
-  if (count != 1)
+  if (runable == NULL)
     {
       if (do_mesg)
 	error (_("Don't know how to %s.  Try \"help target\"."), do_mesg);
@@ -4369,4 +4388,13 @@ When this permission is on, GDB may interrupt/stop the target's execution.\n\
 Otherwise, any attempt to interrupt or stop will be ignored."),
 			   set_target_permissions, NULL,
 			   &setlist, &showlist);
+
+  add_setshow_boolean_cmd ("auto-connect-native-target", class_support,
+			   &auto_connect_native_target, _("\
+Set whether GDB may automatically connect to the native target."), _("\
+Show whether GDB may automatically connect to the native target."), _("\
+When on, and GDB is not connected to a target yet, GDB\n\
+attempts \"run\" and other commands with the native target."),
+			   NULL, show_auto_connect_native_target,
+			   &setlist, &showlist);
 }
diff --git a/gdb/testsuite/boards/native-extended-gdbserver.exp b/gdb/testsuite/boards/native-extended-gdbserver.exp
index 8bb95db..080a22e 100644
--- a/gdb/testsuite/boards/native-extended-gdbserver.exp
+++ b/gdb/testsuite/boards/native-extended-gdbserver.exp
@@ -36,6 +36,8 @@ set_board_info gdb_protocol "extended-remote"
 
 send_user "configuring for gdbserver local testing (extended-remote)\n"
 
+set GDBFLAGS "${GDBFLAGS} -ex \"set auto-connect-native-target off\""
+
 # We must load this explicitly here, and rename the procedures we want
 # to override.  If we didn't do this, given that mi-support.exp is
 # loaded later in the test files, the procedures loaded then would
diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.c b/gdb/testsuite/gdb.base/auto-connect-native-target.c
new file mode 100644
index 0000000..9677e0e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.c
@@ -0,0 +1,23 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+main ()
+{
+  return 0;
+}
+
diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.exp b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
new file mode 100644
index 0000000..ac8c79f
--- /dev/null
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
@@ -0,0 +1,209 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test "set auto-connect-native-target off" and "target native" on
+# native targets.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+# Whether this GDB is configured with a "native" target.
+set have_native 0
+
+set test "help target native"
+gdb_test_multiple $test $test {
+    -re "Undefined target command.* $gdb_prompt $" {
+	set have_native 0
+    }
+    -re "Native process.*$gdb_prompt $" {
+	set have_native 1
+    }
+}
+
+if { !$have_native } {
+    unsupported "No \"target native\" support."
+    return
+}
+
+# Returns the topmost target pushed on the target stack.  TEST is used
+# as test message.
+
+proc get_topmost_target {test} {
+    global gdb_prompt
+
+    set topmost "unknown"
+
+    gdb_test_multiple "maint print target-stack" $test {
+	-re "The current target stack is:\r\n  - (\[^ \]+) .*$gdb_prompt $" {
+	    set topmost $expect_out(1,string)
+	    pass $test
+	}
+    }
+
+    return $topmost
+}
+
+set topmost [get_topmost_target "check whether a target is already connected"]
+
+# Testing against the extended-remote board, for example?
+if { $topmost != "exec" } {
+    unsupported "Already connected to target $topmost."
+    return
+}
+
+# Check which target this board connects to.  If testing with a native
+# target board, this should cause the native target to auto connect.
+if ![runto_main] then {
+    fail "Can't run to main"
+    return 0
+}
+
+# Returns true if the native target is pushed on the target stack.
+# TEST is used as test message.
+
+proc check_native_target {test} {
+    global gdb_prompt
+
+    gdb_test_multiple "maint print target-stack" $test {
+	-re " native .*$gdb_prompt $" {
+	    pass $test
+	    return 1
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    return 0
+}
+
+# Testing against a remote board, for example?
+if { ![check_native_target "check whether board tests the native target"] } {
+    unsupported "Not testing the native target."
+    return
+}
+
+# Kill program.  TEST is used as test message.
+
+proc kill_program {test} {
+    global gdb_prompt
+
+    gdb_test_multiple "kill" $test {
+	-re "Kill the program being debugged\\? .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+}
+
+# Kill the program.  This should pop the target.  The "start" test
+# below will fail otherwise.
+kill_program "kill"
+
+# Now prevent the native target from auto connecting.
+gdb_test_no_output "set auto-connect-native-target off"
+
+# Commands that rely on the native target auto-connecting should no longer work.
+gdb_test "start" "Don't know how to run.*" "start no longer works"
+
+# Explicitly connect to the native target.
+gdb_test "target native" \
+    "Done.  Use the \"run\" command to start a process.*" \
+    "explicitly connect to the native target"
+
+proc test_native_target_remains_pushed {} {
+    gdb_test "maint print target-stack"  \
+	"The current target stack is:\r\n  .* native .* exec .*" \
+	"native target remains pushed"
+}
+
+# Test a set of "inferior gone" scenarios, making sure the target
+# remains pushed.
+
+with_test_prefix "kill" {
+    gdb_test "start" "main.*" "start"
+
+    kill_program "kill"
+
+    test_native_target_remains_pushed
+}
+
+with_test_prefix "detach" {
+    gdb_test "start" "main.*"
+
+    set test "detach"
+    gdb_test_multiple $test $test {
+	-re "Detach the program being debugged\\? .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    test_native_target_remains_pushed
+}
+
+with_test_prefix "run to exit" {
+    gdb_test "start" "Temporary breakpoint .* main .*"
+
+    gdb_test "c" "$inferior_exited_re normally.*"
+
+    test_native_target_remains_pushed
+}
+
+# Now test disconnecting.  Commands that rely on the native target
+# auto-connecting should no longer work (again) after this.
+
+with_test_prefix "disconnect" {
+    gdb_test "start" "Temporary breakpoint .* main .*"
+
+    set test "disconnect"
+    gdb_test_multiple $test $test {
+	-re "A program is being debugged already.* .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    set topmost \
+	[get_topmost_target "check whether the target is no longer connected"]
+
+    set test "no longer connected to a target"
+    if { $topmost == "exec" } {
+	pass $test
+    } else {
+	fail $test
+    }
+
+    gdb_test "start" "Don't know how to run.*" "start no longer works"
+}
+
+# Reenable auto-connecting to the native target.  Plain "start" should
+# start working again.
+gdb_test_no_output "set auto-connect-native-target on"
+
+gdb_test "start" "Temporary breakpoint .* main .*" \
+    "start auto-connects to the native target after reenabling auto-connect"
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 3054d12..6387c5e 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1709,7 +1709,8 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)
 #endif
   current_event.dwProcessId = pid;
   memset (&current_event, 0, sizeof (current_event));
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
   disable_breakpoints_in_shlibs ();
   windows_clear_solib ();
   clear_proceed_status ();
@@ -1897,7 +1898,7 @@ windows_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (current_event.dwProcessId);
 
-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* Try to determine the executable filename.
@@ -2350,8 +2351,8 @@ windows_mourn_inferior (struct target_ops *ops)
       CHECK (CloseHandle (current_process_handle));
       open_process_used = 0;
     }
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }
 
 /* Send a SIGINT to the process group.  This acts just like the user typed a
-- 
1.7.11.7

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

* [PATCH 3/8] Rename "target GNU" -> "target native"
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 15:23 ` [PATCH 7/8] NEWS: Mention native target renames Pedro Alves
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

To be like other native targets.

Leave to_shortname, to_longname, to_doc as inf-child.c sets them:

  t->to_shortname = "native";
  t->to_longname = "Native process";
  t->to_doc = "Native process (started by the \"run\" command).";

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* gnu-nat.c (gnu): Don't override to_shortname, to_longname or
	to_doc.
---
 gdb/gnu-nat.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index aaccb5a..8235811 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2665,10 +2665,6 @@ gnu_target (void)
 {
   struct target_ops *t = inf_child_target ();
 
-  t->to_shortname = "GNU";
-  t->to_longname = "GNU Hurd process";
-  t->to_doc = "GNU Hurd process";
-
   t->to_attach = gnu_attach;
   t->to_attach_no_wait = 1;
   t->to_detach = gnu_detach;
-- 
1.7.11.7

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

* [PATCH 2/8] Rename "target darwin-child" -> "target native"
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (5 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 4/8] Windows: Rename "target child" -> "target native" Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 15:23 ` [PATCH 5/8] nto-procfs.c: Add " Pedro Alves
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

To be like other native targets.

Leave to_shortname, to_longname, to_doc as inf-child.c sets them:

  t->to_shortname = "native";
  t->to_longname = "Native process";
  t->to_doc = "Native process (started by the \"run\" command).";

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* darwin-nat.c (_initialize_darwin_inferior): Don't override
	to_shortname, to_longname or to_doc.
---
 gdb/darwin-nat.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 88cecf3..8650f03 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -2098,10 +2098,6 @@ _initialize_darwin_inferior (void)
 
   darwin_ops = inf_child_target ();
 
-  darwin_ops->to_shortname = "darwin-child";
-  darwin_ops->to_longname = _("Darwin child process");
-  darwin_ops->to_doc =
-    _("Darwin child process (started by the \"run\" command).");
   darwin_ops->to_create_inferior = darwin_create_inferior;
   darwin_ops->to_attach = darwin_attach;
   darwin_ops->to_attach_no_wait = 0;
-- 
1.7.11.7

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

* [PATCH 5/8] nto-procfs.c: Add "target native".
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (6 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 2/8] Rename "target darwin-child" " Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 17:01 ` [PATCH 0.5/8] Rename "target child" to " Pedro Alves
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

This makes QNX/NTO end up with two targets.  It preserves "target
procfs <node>", and adds a "native" target to be like other native
ports.

Not tested.

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* nto-procfs.c (procfs_can_run): New function.
	(nto_procfs_ops): New global.
	(init_procfs_targets): New, based on procfs_target.  Install
	"target native" in addition to "target procfs".
	(_initialize_procfs): Call init_procfs_targets instead of adding
	the target here.
---
 gdb/nto-procfs.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index c552a8e..b328dfa 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -1370,18 +1370,31 @@ procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
   return buf;
 }
 
-/* Create a nto-procfs target.  */
+/* to_can_run implementation for "target procfs".  Note this really
+  means "can this target be the default run target", which there can
+  be only one, and we make it be "target native" like other ports.
+  "target procfs <node>" wouldn't make sense as default run target, as
+  it needs <node>.  */
 
-static struct target_ops *
-procfs_target (void)
+static int
+procfs_can_run (struct target_ops *self)
+{
+  return 0;
+}
+
+/* "target procfs".  */
+static struct target_ops nto_procfs_ops;
+
+/* Create the "native" and "procfs" targets.  */
+
+static void
+init_procfs_targets (void)
 {
   struct target_ops *t = inf_child_target ();
 
-  t->to_shortname = "procfs";
-  t->to_longname = "QNX Neutrino procfs child process";
-  t->to_doc
-    = "QNX Neutrino procfs child process (started by the \"run\" command).\n\
-	target procfs <node>";
+  /* Leave to_shortname as "native".  */
+  t->to_longname = "QNX Neutrino local process";
+  t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
   t->to_open = procfs_open;
   t->to_attach = procfs_attach;
   t->to_post_attach = procfs_post_attach;
@@ -1411,7 +1424,16 @@ procfs_target (void)
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;
 
-  return t;
+  /* Register "target native".  This is the default run target.  */
+  add_target (t);
+
+  /* Register "target procfs <node>".  */
+  nto_procfs_ops = *t;
+  nto_procfs_ops.to_shortname = "procfs";
+  nto_procfs_ops.to_can_run = procfs_can_run;
+  t->to_longname = "QNX Neutrino local or remote process";
+  t->to_doc = "QNX Neutrino process.  target procfs <node>";
+  add_target (&nto_procfs_ops);
 }
 
 #define OSTYPE_NTO 1
@@ -1420,10 +1442,8 @@ void
 _initialize_procfs (void)
 {
   sigset_t set;
-  struct target_ops *t;
 
-  t = procfs_target ();
-  add_target (t);
+  init_procfs_targets ();
 
   /* We use SIGUSR1 to gain control after we block waiting for a process.
      We use sigwaitevent to wait.  */
-- 
1.7.11.7

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

* [PATCH 4/8] Windows: Rename "target child" -> "target native"
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (4 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 6/8] go32-nat.c: Don't override to_open Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 15:23 ` [PATCH 2/8] Rename "target darwin-child" " Pedro Alves
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

To be like other native targets.

Leave to_shortname, to_longname, to_doc as inf-child.c sets them:

  t->to_shortname = "native";
  t->to_longname = "Native process";
  t->to_doc = "Native process (started by the \"run\" command).";

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* windows-nat.c (windows_target): Don't override to_shortname,
	to_longname or to_doc.
---
 gdb/windows-nat.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index fe40c4d..3054d12 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2539,9 +2539,6 @@ windows_target (void)
 {
   struct target_ops *t = inf_child_target ();
 
-  t->to_shortname = "child";
-  t->to_longname = "Win32 child process";
-  t->to_doc = "Win32 child process (started by the \"run\" command).";
   t->to_close = windows_close;
   t->to_attach = windows_attach;
   t->to_attach_no_wait = 1;
-- 
1.7.11.7

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

* [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native").
@ 2014-03-17 15:23 Pedro Alves
  2014-03-17 15:23 ` [PATCH 3/8] Rename "target GNU" -> "target native" Pedro Alves
                   ` (10 more replies)
  0 siblings, 11 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

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 this series, 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 renamed all native targets
to "target native", and made "target native" work instead of erroring
out as today.

E.g., before, on GNU/Linux and most native targets, the native target
is called "child", and trying to open it yields:

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

After the series we get:

 (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 that board, it's always a bug to launch a
program with the native target.  Turns out we still have one such case
this series 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

For convenience, I've also put this at:
  git@github.com:palves/gdb.git target_native_v1

Pedro Alves (8):
  Rename "target djgpp" -> "target native"
  Rename "target darwin-child" -> "target native"
  Rename "target GNU" -> "target native"
  Windows: Rename "target child" -> "target native"
  nto-procfs.c: Add "target native".
  go32-nat.c: Don't override to_open.
  NEWS: Mention native target renames.
  Allow making GDB not automatically connect to the native target.

 gdb/NEWS                                           |  21 +++
 gdb/darwin-nat.c                                   |  10 +-
 gdb/doc/gdb.texinfo                                |  58 ++++++
 gdb/gnu-nat.c                                      |   8 +-
 gdb/go32-nat.c                                     |  16 +-
 gdb/inf-child.c                                    |  62 +++++-
 gdb/inf-child.h                                    |  11 ++
 gdb/inf-ptrace.c                                   |   6 +-
 gdb/inf-ttrace.c                                   |  10 +-
 gdb/linux-nat.c                                    |   8 +
 gdb/nto-procfs.c                                   |  97 +++++++---
 gdb/procfs.c                                       |  13 +-
 gdb/target.c                                       |  48 ++++-
 gdb/testsuite/boards/native-extended-gdbserver.exp |   2 +
 .../gdb.base/auto-connect-native-target.c          |  23 +++
 .../gdb.base/auto-connect-native-target.exp        | 209 +++++++++++++++++++++
 gdb/windows-nat.c                                  |  10 +-
 17 files changed, 531 insertions(+), 81 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.c
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.exp

-- 
1.7.11.7

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

* [PATCH 7/8] NEWS: Mention native target renames.
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
  2014-03-17 15:23 ` [PATCH 3/8] Rename "target GNU" -> "target native" Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 16:45   ` Eli Zaretskii
  2014-03-17 15:23 ` [PATCH 1/8] Rename "target djgpp" -> "target native" Pedro Alves
                   ` (8 subsequent siblings)
  10 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* NEWS: Mention that the "child", "GNU, "djgpp", "darwin-child"
	and "procfs" targets are now called "native" instead.
---
 gdb/NEWS | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 2a384ba..5bdda0a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -76,6 +76,17 @@ maint ada show ignore-descriptive-types
 
 * The "catch syscall" command now works on s390*-linux* targets.
 
+* All native targets are now consistently called "native".
+  Consequently, the "target child", "target GNU", "target djgpp",
+  "target procfs" (Solaris/Irix/OSF/AIX) and "target darwin-child"
+  commands have been replaced with "target native".  The QNX/NTO port
+  leaves the "procfs" in place and adds a "native" target for
+  consistency.  The impact on users should be minimal as these
+  commands previously either throwed an error, or were no-ops.  The
+  target's name is visible in the output of the following commands:
+  "help target", "info target", "info files", "maint print
+  target-stack".
+
 * New remote packets
 
 qXfer:btrace:read's annex
-- 
1.7.11.7

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

* [PATCH 1/8] Rename "target djgpp" -> "target native"
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
  2014-03-17 15:23 ` [PATCH 3/8] Rename "target GNU" -> "target native" Pedro Alves
  2014-03-17 15:23 ` [PATCH 7/8] NEWS: Mention native target renames Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 16:44   ` Eli Zaretskii
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

To be like other native targets.

Leave to_shortname, to_longname, to_doc as inf-child.c sets them:

  t->to_shortname = "native";
  t->to_longname = "Native process";
  t->to_doc = "Native process (started by the \"run\" command).";

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* go32-nat.c (go32_target): Don't override to_shortname,
	to_longname or to_doc.
---
 gdb/go32-nat.c | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c
index 08d803c..ef5278a 100644
--- a/gdb/go32-nat.c
+++ b/gdb/go32-nat.c
@@ -960,10 +960,6 @@ go32_target (void)
 {
   struct target_ops *t = inf_child_target ();
 
-  t->to_shortname = "djgpp";
-  t->to_longname = "djgpp target process";
-  t->to_doc
-    = "Program loaded by djgpp, when gdb is used as an external debugger";
   t->to_open = go32_open;
   t->to_attach = go32_attach;
   t->to_resume = go32_resume;
-- 
1.7.11.7

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

* [PATCH 6/8] go32-nat.c: Don't override to_open.
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (3 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
@ 2014-03-17 15:23 ` Pedro Alves
  2014-03-17 16:44   ` Eli Zaretskii
  2014-03-17 15:23 ` [PATCH 4/8] Windows: Rename "target child" -> "target native" Pedro Alves
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 15:23 UTC (permalink / raw)
  To: gdb-patches

Although the string says "Done.", nothing is pushing the target as is.
Removing the method override let's us fall through to the the base
to_open implemention in inf-child.c, which will push the target in
reaction to "target native" in a follow up patch.

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* go32-nat.c (go32_open): Delete.
	(go32_target): Don't override the to_open method.
---
 gdb/go32-nat.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c
index ef5278a..e17707a 100644
--- a/gdb/go32-nat.c
+++ b/gdb/go32-nat.c
@@ -339,12 +339,6 @@ static struct {
 };
 
 static void
-go32_open (char *name, int from_tty)
-{
-  printf_unfiltered ("Done.  Use the \"run\" command to run the program.\n");
-}
-
-static void
 go32_attach (struct target_ops *ops, char *args, int from_tty)
 {
   error (_("\
@@ -960,7 +954,6 @@ go32_target (void)
 {
   struct target_ops *t = inf_child_target ();
 
-  t->to_open = go32_open;
   t->to_attach = go32_attach;
   t->to_resume = go32_resume;
   t->to_wait = go32_wait;
-- 
1.7.11.7

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

* Re: [PATCH 6/8] go32-nat.c: Don't override to_open.
  2014-03-17 15:23 ` [PATCH 6/8] go32-nat.c: Don't override to_open Pedro Alves
@ 2014-03-17 16:44   ` Eli Zaretskii
  0 siblings, 0 replies; 21+ messages in thread
From: Eli Zaretskii @ 2014-03-17 16:44 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <palves@redhat.com>
> Date: Mon, 17 Mar 2014 15:23:02 +0000
> 
> Although the string says "Done.", nothing is pushing the target as is.
> Removing the method override let's us fall through to the the base
> to_open implemention in inf-child.c, which will push the target in
> reaction to "target native" in a follow up patch.
> 
> gdb/
> 2014-03-17  Pedro Alves  <palves@redhat.com>
> 
> 	* go32-nat.c (go32_open): Delete.
> 	(go32_target): Don't override the to_open method.

OK.

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

* Re: [PATCH 1/8] Rename "target djgpp" -> "target native"
  2014-03-17 15:23 ` [PATCH 1/8] Rename "target djgpp" -> "target native" Pedro Alves
@ 2014-03-17 16:44   ` Eli Zaretskii
  0 siblings, 0 replies; 21+ messages in thread
From: Eli Zaretskii @ 2014-03-17 16:44 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <palves@redhat.com>
> Date: Mon, 17 Mar 2014 15:22:57 +0000
> 
> To be like other native targets.
> 
> Leave to_shortname, to_longname, to_doc as inf-child.c sets them:
> 
>   t->to_shortname = "native";
>   t->to_longname = "Native process";
>   t->to_doc = "Native process (started by the \"run\" command).";
> 
> gdb/
> 2014-03-17  Pedro Alves  <palves@redhat.com>
> 
> 	* go32-nat.c (go32_target): Don't override to_shortname,
> 	to_longname or to_doc.

OK.

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

* Re: [PATCH 7/8] NEWS: Mention native target renames.
  2014-03-17 15:23 ` [PATCH 7/8] NEWS: Mention native target renames Pedro Alves
@ 2014-03-17 16:45   ` Eli Zaretskii
  0 siblings, 0 replies; 21+ messages in thread
From: Eli Zaretskii @ 2014-03-17 16:45 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <palves@redhat.com>
> Date: Mon, 17 Mar 2014 15:23:03 +0000
> 
> gdb/
> 2014-03-17  Pedro Alves  <palves@redhat.com>
> 
> 	* NEWS: Mention that the "child", "GNU, "djgpp", "darwin-child"
> 	and "procfs" targets are now called "native" instead.
> ---
>  gdb/NEWS | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/gdb/NEWS b/gdb/NEWS
> index 2a384ba..5bdda0a 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -76,6 +76,17 @@ maint ada show ignore-descriptive-types
>  
>  * The "catch syscall" command now works on s390*-linux* targets.
>  
> +* All native targets are now consistently called "native".
> +  Consequently, the "target child", "target GNU", "target djgpp",
> +  "target procfs" (Solaris/Irix/OSF/AIX) and "target darwin-child"
> +  commands have been replaced with "target native".  The QNX/NTO port
> +  leaves the "procfs" in place and adds a "native" target for
> +  consistency.  The impact on users should be minimal as these
> +  commands previously either throwed an error, or were no-ops.  The
> +  target's name is visible in the output of the following commands:
> +  "help target", "info target", "info files", "maint print
> +  target-stack".

OK, thanks.

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

* Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
@ 2014-03-17 16:47   ` Eli Zaretskii
  2014-03-18 21:55   ` Stan Shebs
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 21+ messages in thread
From: Eli Zaretskii @ 2014-03-17 16:47 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

> From: Pedro Alves <palves@redhat.com>
> Date: Mon, 17 Mar 2014 15:23:04 +0000
> 
> gdb/doc/
> 2014-03-17  Pedro Alves  <palves@redhat.com>
> 
> 	* gdb.texinfo (Starting): Document "set/show
> 	auto-connect-native-target".
> 	(Target Commands): Document "target native".

OK for this part.

Thanks.

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

* [PATCH 0.5/8] Rename "target child" to "target native".
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (7 preceding siblings ...)
  2014-03-17 15:23 ` [PATCH 5/8] nto-procfs.c: Add " Pedro Alves
@ 2014-03-17 17:01 ` Pedro Alves
  2014-03-17 17:02 ` Pedro Alves
  2014-05-21 17:57 ` [pushed] Re: [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 17:01 UTC (permalink / raw)
  Cc: gdb-patches

Whoop, pilot error...  

On 03/17/2014 03:22 PM, Pedro Alves wrote:

> Pedro Alves (8):
>   Rename "target djgpp" -> "target native"
>   Rename "target darwin-child" -> "target native"
>   Rename "target GNU" -> "target native"
>   Windows: Rename "target child" -> "target native"
>   nto-procfs.c: Add "target native".
>   go32-nat.c: Don't override to_open.
>   NEWS: Mention native target renames.
>   Allow making GDB not automatically connect to the native target.

I missed including in the series the real patch #1, the one that
renames "target child" -> "target native".  Here is is.

---
[PATCH 1/9] Rename "target child" to "target native".

I had been pondering renaming "target child" to something else.
"child" is a little lie in case of "attach", and not exactly very
clear to users, IMO.  By best suggestion is "target native".  If I
were to explain what "target child" is, I'd just start out with "it's
the native target" anyway.  I was worrying a little that "native"
might be a lie too if some port comes up with a default target that
can run but is not really native, but I think that's a very minor
issue - we can consider that "native" really means the default built
in target that GDB supports, instead of saying that's the target that
debugs host native processes, if it turns out necessary.

This change doesn't affect users much, because "target child" results
in error today:

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

Other places "child" is visible:

 (gdb) help target
 ...
 List of target subcommands:

 target child -- Child process (started by the "run" command)
 target core -- Use a core file as a target
 target exec -- Use an executable file as a target
 ...

 (gdb) info target
 Symbols from "/home/pedro/gdb/mygit/build/gdb/gdb".
 Child process:
	 Using the running image of child Thread 0x7ffff7fc9740 (LWP 4818).
	 While running this, GDB does not access memory from...
 ...

These places will say "native" instead.  I think that's a good thing.

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* inf-child.c (inf_child_open): Remove mention of "child".
	(inf_child_target): Rename target to "native" instead of "child".

gdb/testsuite/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* gdb.base/default.exp: Test "target native" instead of "target
	child".
---
 gdb/inf-child.c                    | 8 ++++----
 gdb/testsuite/gdb.base/default.exp | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 282467b..8a99adc 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -112,7 +112,7 @@ inf_child_prepare_to_store (struct target_ops *self,
 static void
 inf_child_open (char *arg, int from_tty)
 {
-  error (_("Use the \"run\" command to start a child process."));
+  error (_("Use the \"run\" command to start a process."));
 }
 
 static void
@@ -406,9 +406,9 @@ inf_child_target (void)
 {
   struct target_ops *t = XCNEW (struct target_ops);
 
-  t->to_shortname = "child";
-  t->to_longname = "Child process";
-  t->to_doc = "Child process (started by the \"run\" command).";
+  t->to_shortname = "native";
+  t->to_longname = "Native process";
+  t->to_doc = "Native process (started by the \"run\" command).";
   t->to_open = inf_child_open;
   t->to_post_attach = inf_child_post_attach;
   t->to_fetch_registers = inf_child_fetch_inferior_registers;
diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp
index a39ffcf..3636fa3 100644
--- a/gdb/testsuite/gdb.base/default.exp
+++ b/gdb/testsuite/gdb.base/default.exp
@@ -746,8 +746,8 @@ gdb_test "step" "The program is not being run." "step #2"
 #test symbol-file
 gdb_test "symbol-file" ".*" "symbol-file"
 
-#test target child
-gdb_test "target child" "Use the \"run\" command to start a child process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target child"
+#test target native
+gdb_test "target native" "Use the \"run\" command to start a process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target native"
 
 #test target core
 send_gdb "target core\n"
-- 
1.7.11.7


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

* [PATCH 0.5/8] Rename "target child" to "target native".
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (8 preceding siblings ...)
  2014-03-17 17:01 ` [PATCH 0.5/8] Rename "target child" to " Pedro Alves
@ 2014-03-17 17:02 ` Pedro Alves
  2014-06-03  5:29   ` [PATCH OBV] Fix regexp pattern in gdb.base/default.exp (Re: [PATCH 0.5/8] Rename "target child" to "target native".) Yao Qi
  2014-05-21 17:57 ` [pushed] Re: [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
  10 siblings, 1 reply; 21+ messages in thread
From: Pedro Alves @ 2014-03-17 17:02 UTC (permalink / raw)
  Cc: gdb-patches

Whoop, pilot error...  

On 03/17/2014 03:22 PM, Pedro Alves wrote:

> Pedro Alves (8):
>   Rename "target djgpp" -> "target native"
>   Rename "target darwin-child" -> "target native"
>   Rename "target GNU" -> "target native"
>   Windows: Rename "target child" -> "target native"
>   nto-procfs.c: Add "target native".
>   go32-nat.c: Don't override to_open.
>   NEWS: Mention native target renames.
>   Allow making GDB not automatically connect to the native target.

I missed including in the series the real patch #1, the one that
renames "target child" -> "target native".  Here is is.

---
[PATCH 1/9] Rename "target child" to "target native".

I had been pondering renaming "target child" to something else.
"child" is a little lie in case of "attach", and not exactly very
clear to users, IMO.  By best suggestion is "target native".  If I
were to explain what "target child" is, I'd just start out with "it's
the native target" anyway.  I was worrying a little that "native"
might be a lie too if some port comes up with a default target that
can run but is not really native, but I think that's a very minor
issue - we can consider that "native" really means the default built
in target that GDB supports, instead of saying that's the target that
debugs host native processes, if it turns out necessary.

This change doesn't affect users much, because "target child" results
in error today:

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

Other places "child" is visible:

 (gdb) help target
 ...
 List of target subcommands:

 target child -- Child process (started by the "run" command)
 target core -- Use a core file as a target
 target exec -- Use an executable file as a target
 ...

 (gdb) info target
 Symbols from "/home/pedro/gdb/mygit/build/gdb/gdb".
 Child process:
	 Using the running image of child Thread 0x7ffff7fc9740 (LWP 4818).
	 While running this, GDB does not access memory from...
 ...

These places will say "native" instead.  I think that's a good thing.

gdb/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* inf-child.c (inf_child_open): Remove mention of "child".
	(inf_child_target): Rename target to "native" instead of "child".

gdb/testsuite/
2014-03-17  Pedro Alves  <palves@redhat.com>

	* gdb.base/default.exp: Test "target native" instead of "target
	child".
---
 gdb/inf-child.c                    | 8 ++++----
 gdb/testsuite/gdb.base/default.exp | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 282467b..8a99adc 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -112,7 +112,7 @@ inf_child_prepare_to_store (struct target_ops *self,
 static void
 inf_child_open (char *arg, int from_tty)
 {
-  error (_("Use the \"run\" command to start a child process."));
+  error (_("Use the \"run\" command to start a process."));
 }
 
 static void
@@ -406,9 +406,9 @@ inf_child_target (void)
 {
   struct target_ops *t = XCNEW (struct target_ops);
 
-  t->to_shortname = "child";
-  t->to_longname = "Child process";
-  t->to_doc = "Child process (started by the \"run\" command).";
+  t->to_shortname = "native";
+  t->to_longname = "Native process";
+  t->to_doc = "Native process (started by the \"run\" command).";
   t->to_open = inf_child_open;
   t->to_post_attach = inf_child_post_attach;
   t->to_fetch_registers = inf_child_fetch_inferior_registers;
diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp
index a39ffcf..3636fa3 100644
--- a/gdb/testsuite/gdb.base/default.exp
+++ b/gdb/testsuite/gdb.base/default.exp
@@ -746,8 +746,8 @@ gdb_test "step" "The program is not being run." "step #2"
 #test symbol-file
 gdb_test "symbol-file" ".*" "symbol-file"
 
-#test target child
-gdb_test "target child" "Use the \"run\" command to start a child process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target child"
+#test target native
+gdb_test "target native" "Use the \"run\" command to start a process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target native"
 
 #test target core
 send_gdb "target core\n"
-- 
1.7.11.7


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

* Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
  2014-03-17 16:47   ` Eli Zaretskii
@ 2014-03-18 21:55   ` Stan Shebs
  2014-03-25 20:06   ` Doug Evans
  2014-06-03  5:35   ` [PATCH OBV] Fix a regexp pattern in gdb.base/auto-connect-native-target.exp (Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.) Yao Qi
  3 siblings, 0 replies; 21+ messages in thread
From: Stan Shebs @ 2014-03-18 21:55 UTC (permalink / raw)
  To: gdb-patches

On 3/17/14 8:23 AM, Pedro Alves wrote:

> gdb/doc/
> 2014-03-17  Pedro Alves  <palves@redhat.com>
> 
> 	* gdb.texinfo (Starting): Document "set/show
> 	auto-connect-native-target".
> 	(Target Commands): Document "target native".

On that note, there are a bunch of random "child process" references in
the manual that should be reworded.

I researched alternative possibilities for naming, but didn't come up
with much - debugger manuals seem to just go with the somewhat-awkward
"process being debugged" when it's necessary to be clear about it.

Stan
stan@codesourcery.com

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

* Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
  2014-03-17 16:47   ` Eli Zaretskii
  2014-03-18 21:55   ` Stan Shebs
@ 2014-03-25 20:06   ` Doug Evans
  2014-05-21 17:53     ` Pedro Alves
  2014-06-03  5:35   ` [PATCH OBV] Fix a regexp pattern in gdb.base/auto-connect-native-target.exp (Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.) Yao Qi
  3 siblings, 1 reply; 21+ messages in thread
From: Doug Evans @ 2014-03-25 20:06 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On Mon, Mar 17, 2014 at 8:23 AM, Pedro Alves <palves@redhat.com> wrote:
> [...]
> It's generic_mourn_inferior that ends up clearing inf->pid, so we need
> to call it before the have_inferiors check.
> [...]

Hi.
I didn't see this dependency documented in the code anywhere
(especially the "why" of it).
Could have missed it of course.

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

* Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.
  2014-03-25 20:06   ` Doug Evans
@ 2014-05-21 17:53     ` Pedro Alves
  0 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-05-21 17:53 UTC (permalink / raw)
  To: Doug Evans; +Cc: gdb-patches

On 03/25/2014 08:06 PM, Doug Evans wrote:
> On Mon, Mar 17, 2014 at 8:23 AM, Pedro Alves <palves@redhat.com> wrote:
>> [...]
>> It's generic_mourn_inferior that ends up clearing inf->pid, so we need
>> to call it before the have_inferiors check.
>> [...]
> 
> Hi.
> I didn't see this dependency documented in the code anywhere
> (especially the "why" of it).
> Could have missed it of course.

Thanks Doug.  I've added it here:

/* Unpush the target if it wasn't explicitly open with "target native"
   and there are no live inferiors left.  Note: if calling this as a
   result of a mourn or detach, the current inferior shall already
   have its PID cleared, so it isn't counted as live.  That's usually
   done by calling either generic_mourn_inferior or
   detach_inferior.  */

extern void inf_child_maybe_unpush_target (struct target_ops *ops);

I've also moved the "set auto-connect-native-target off" from
the native-extended-gdbserver.exp board to gdbserver-base.exp.
Naturally we don't want to spawn the native target even when
testing with "target remote".  Hmm, maybe we should make
gdb.exp itself do that if $use_gdb_stub is set.

Anyway, that uncovered another similar issue:

 105-file-exec-and-symbols /home/pedro/gdb/mygit/build/gdb/testsuite/outputs/gdb.mi/mi-async/mi-async
 105^done
 (gdb)
 start
 &"start\n"
 &"Don't know how to run.  Try \"help target\".\n"
 ^error,msg="Don't know how to run.  Try \"help target\"."
 (gdb)
 FAIL: gdb.mi/mi-async.exp: start: send (MI error)


The test supposedly is skipped against remote, but the predicate is wrong:

 # Because of the way this test is written, using 'start' and not using
 # mi_run_cmd, it ignores whatever target the rest of GDB testsuite is
 # using, and always tries to run natively.  So, don't do anything unless
 # we're actually testing native.
 if { !([isnative] && [istarget *-linux*]) } then {
   return
 }

I don't know why this test uses "start" though.  Seems to me it
it could do without it.

Below's what I pushed (nothing else changed).

---------
From: Pedro Alves <palves@redhat.com>
Subject: [PATCH] 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.
---
 gdb/ChangeLog                                      |  68 +++++++
 gdb/NEWS                                           |  10 +
 gdb/darwin-nat.c                                   |   6 +-
 gdb/doc/ChangeLog                                  |   6 +
 gdb/doc/gdb.texinfo                                |  58 ++++++
 gdb/gnu-nat.c                                      |   4 +-
 gdb/go32-nat.c                                     |   5 +-
 gdb/inf-child.c                                    |  62 +++++-
 gdb/inf-child.h                                    |  15 ++
 gdb/inf-ptrace.c                                   |   6 +-
 gdb/inf-ttrace.c                                   |  10 +-
 gdb/linux-nat.c                                    |   8 +
 gdb/nto-procfs.c                                   |  54 ++++--
 gdb/procfs.c                                       |  13 +-
 gdb/target.c                                       |  48 ++++-
 gdb/testsuite/ChangeLog                            |   7 +
 gdb/testsuite/boards/gdbserver-base.exp            |   2 +
 .../gdb.base/auto-connect-native-target.c          |  23 +++
 .../gdb.base/auto-connect-native-target.exp        | 209 +++++++++++++++++++++
 gdb/windows-nat.c                                  |   7 +-
 20 files changed, 574 insertions(+), 47 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.c
 create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.exp

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 1c74843..ba052db0 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,73 @@
 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.
+
+2014-05-21  Pedro Alves  <palves@redhat.com>
+
 	* NEWS: Mention that the "child", "GNU, "djgpp", "darwin-child"
 	and "procfs" targets are now called "native" instead.

diff --git a/gdb/NEWS b/gdb/NEWS
index e5d4130..4663650 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -61,6 +61,12 @@ maint ada show ignore-descriptive-types
   the user manual for more details on descriptive types and the intended
   usage of this option.

+set auto-connect-native-target
+
+  Control whether GDB is allowed to automatically connect to the
+  native target for the run, attach, etc. commands when not connected
+  to any target yet.  See also "target native" below.
+
 * New features in the GDB remote stub, GDBserver

   ** New option --debug-format=option1[,option2,...] allows one to add
@@ -108,6 +114,10 @@ maint ada show ignore-descriptive-types
   commands: "help target", "info target", "info files", "maint print
   target-stack".

+* The "target native" command now connects to the native target.  This
+  can be used to launch native programs even when "set
+  auto-connect-native-target" is set to off.
+
 * New remote packets

 qXfer:btrace:read's annex
diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c
index 478429c..f3d510d 100644
--- a/gdb/darwin-nat.c
+++ b/gdb/darwin-nat.c
@@ -1214,8 +1214,6 @@ darwin_mourn_inferior (struct target_ops *ops)
   mach_port_t prev;
   int i;

-  unpush_target (darwin_ops);
-
   /* Deallocate threads.  */
   if (inf->private->threads)
     {
@@ -1271,6 +1269,7 @@ darwin_mourn_inferior (struct target_ops *ops)
   inf->private = NULL;

   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 static void
@@ -1506,7 +1505,8 @@ impact on the debugging session."));
 	     "returned: %d"),
 	   kret);

-  push_target (darwin_ops);
+  if (!target_is_pushed (darwin_ops))
+    push_target (darwin_ops);
 }

 static void
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 7d0fdbd..18588f6 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,9 @@
+2014-05-21  Pedro Alves  <palves@redhat.com>
+
+	* gdb.texinfo (Starting): Document "set/show
+	auto-connect-native-target".
+	(Target Commands): Document "target native".
+
 2014-05-20  Pedro Alves  <palves@redhat.com>

 	* gdb.texinfo (Memory) <compare-sections>: Generalize comments to
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 6092ff4..47d4bb7 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -2148,6 +2148,57 @@ initialization file---such as @file{.cshrc} for C-shell,
 $@file{.zshenv} for the Z shell, or the file specified in the
 @samp{BASH_ENV} environment variable for BASH.

+@anchor{set auto-connect-native-target}
+@kindex set auto-connect-native-target
+@item set auto-connect-native-target
+@itemx set auto-connect-native-target on
+@itemx set auto-connect-native-target off
+@itemx show auto-connect-native-target
+
+By default, if not connected to any target yet (e.g., with
+@code{target remote}), the @code{run} command starts your program as a
+native process under @value{GDBN}, on your local machine.  If you're
+sure you don't want to debug programs on your local machine, you can
+tell @value{GDBN} to not connect to the native target automatically
+with the @code{set auto-connect-native-target off} command.
+
+If @code{on}, which is the default, and if @value{GDBN} is not
+connected to a target already, the @code{run} command automaticaly
+connects to the native target, if one is available.
+
+If @code{off}, and if @value{GDBN} is not connected to a target
+already, the @code{run} command fails with an error:
+
+@smallexample
+(@value{GDBP}) run
+Don't know how to run.  Try "help target".
+@end smallexample
+
+If @value{GDBN} is already connected to a target, @value{GDBN} always
+uses it with the @code{run} command.
+
+In any case, you can explicitly connect to the native target with the
+@code{target native} command.  For example,
+
+@smallexample
+(@value{GDBP}) set auto-connect-native-target off
+(@value{GDBP}) run
+Don't know how to run.  Try "help target".
+(@value{GDBP}) target native
+(@value{GDBP}) run
+Starting program: ./a.out
+[Inferior 1 (process 10421) exited normally]
+@end smallexample
+
+In case you connected explicitly to the @code{native} target,
+@value{GDBN} remains connected even if all inferiors exit, ready for
+the next @code{run} command.  Use the @code{disconnect} command to
+disconnect.
+
+Examples of other commands that likewise respect the
+@code{auto-connect-native-target} setting: @code{attach}, @code{info
+proc}, @code{info os}.
+
 @kindex set disable-randomization
 @item set disable-randomization
 @itemx set disable-randomization on
@@ -18079,6 +18130,13 @@ provide these.  For info about any processor-specific simulator details,
 see the appropriate section in @ref{Embedded Processors, ,Embedded
 Processors}.

+@item target native
+@cindex native target
+Setup for local/native process debugging.  Useful to make the
+@code{run} command spawn native processes (likewise @code{attach},
+etc.@:) even when @code{set auto-connect-native-target} is @code{off}
+(@pxref{set auto-connect-native-target}).
+
 @end table

 Different targets are available on different configurations of @value{GDBN};
diff --git a/gdb/gnu-nat.c b/gdb/gnu-nat.c
index 8235811..3317215 100644
--- a/gdb/gnu-nat.c
+++ b/gdb/gnu-nat.c
@@ -2080,8 +2080,8 @@ gnu_mourn_inferior (struct target_ops *ops)
 {
   inf_debug (gnu_current_inf, "rip");
   inf_detach (gnu_current_inf);
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 \f
@@ -2253,7 +2253,7 @@ gnu_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);

-  unpush_target (ops);	/* Pop out of handling an inferior.  */
+  inf_child_maybe_unpush_target (ops);
 }
 \f
 static void
diff --git a/gdb/go32-nat.c b/gdb/go32-nat.c
index e17707a..77843ea 100644
--- a/gdb/go32-nat.c
+++ b/gdb/go32-nat.c
@@ -710,7 +710,8 @@ go32_create_inferior (struct target_ops *ops, char *exec_file,
   inf = current_inferior ();
   inferior_appeared (inf, SOME_PID);

-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);

   add_thread_silent (inferior_ptid);

@@ -744,8 +745,8 @@ go32_mourn_inferior (struct target_ops *ops)
   delete_thread_silent (ptid);
   prog_has_started = 0;

-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 /* Hardware watchpoint support.  */
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 8a99adc..a3c4fe4 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -40,6 +40,11 @@
 #include <fcntl.h>
 #include <unistd.h>

+/* A pointer to what is returned by inf_child_target.  Used by
+   inf_child_open to push the most-derived target in reaction to
+   "target native".  */
+static struct target_ops *inf_child_ops = NULL;
+
 /* Helper function for child_wait and the derivatives of child_wait.
    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
    translation of that in OURSTATUS.  */
@@ -109,10 +114,58 @@ inf_child_prepare_to_store (struct target_ops *self,
 {
 }

+/* True if the user did "target native".  In that case, we won't
+   unpush the child target automatically when the last inferior is
+   gone.  */
+static int inf_child_explicitly_opened;
+
+/* See inf-child.h.  */
+
+void
+inf_child_open_target (struct target_ops *target, char *arg, int from_tty)
+{
+  target_preopen (from_tty);
+  push_target (target);
+  inf_child_explicitly_opened = 1;
+  if (from_tty)
+    printf_filtered ("Done.  Use the \"run\" command to start a process.\n");
+}
+
 static void
 inf_child_open (char *arg, int from_tty)
 {
-  error (_("Use the \"run\" command to start a process."));
+  inf_child_open_target (inf_child_ops, arg, from_tty);
+}
+
+/* Implement the to_disconnect target_ops method.  */
+
+static void
+inf_child_disconnect (struct target_ops *target, char *args, int from_tty)
+{
+  if (args != NULL)
+    error (_("Argument given to \"disconnect\"."));
+
+  /* This offers to detach/kill current inferiors, and then pops all
+     targets.  */
+  target_preopen (from_tty);
+}
+
+/* Implement the to_close target_ops method.  */
+
+static void
+inf_child_close (struct target_ops *target)
+{
+  /* In case we were forcibly closed.  */
+  inf_child_explicitly_opened = 0;
+}
+
+/* See inf-child.h.  */
+
+void
+inf_child_maybe_unpush_target (struct target_ops *ops)
+{
+  if (!inf_child_explicitly_opened && !have_inferiors ())
+    unpush_target (ops);
 }

 static void
@@ -410,6 +463,8 @@ inf_child_target (void)
   t->to_longname = "Native process";
   t->to_doc = "Native process (started by the \"run\" command).";
   t->to_open = inf_child_open;
+  t->to_close = inf_child_close;
+  t->to_disconnect = inf_child_disconnect;
   t->to_post_attach = inf_child_post_attach;
   t->to_fetch_registers = inf_child_fetch_inferior_registers;
   t->to_store_registers = inf_child_store_inferior_registers;
@@ -445,5 +500,10 @@ inf_child_target (void)
   t->to_magic = OPS_MAGIC;
   t->to_use_agent = inf_child_use_agent;
   t->to_can_use_agent = inf_child_can_use_agent;
+
+  /* Store a pointer so we can push the most-derived target from
+     inf_child_open.  */
+  inf_child_ops = t;
+
   return t;
 }
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index 4473cff..3f00ab6 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -30,4 +30,19 @@ extern struct target_ops *inf_child_target (void);
 /* This is for native targets which use a unix/POSIX-style waitstatus.  */
 extern void store_waitstatus (struct target_waitstatus *, int);

+/* This is to be called by the native target's open routine to push
+   the target, in case it need to override to_open.  */
+
+extern void inf_child_open_target (struct target_ops *target,
+				   char *arg, int from_tty);
+
+/* Unpush the target if it wasn't explicitly open with "target native"
+   and there are no live inferiors left.  Note: if calling this as a
+   result of a mourn or detach, the current inferior shall already
+   have its PID cleared, so it isn't counted as live.  That's usually
+   done by calling either generic_mourn_inferior or
+   detach_inferior.  */
+
+extern void inf_child_maybe_unpush_target (struct target_ops *ops);
+
 #endif
diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index bffb4ba..cc4921b 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -176,8 +176,7 @@ inf_ptrace_mourn_inferior (struct target_ops *ops)

   generic_mourn_inferior ();

-  if (!have_inferiors ())
-    unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }

 /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
@@ -297,8 +296,7 @@ inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);

-  if (!have_inferiors ())
-    unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }

 /* Kill the inferior.  */
diff --git a/gdb/inf-ttrace.c b/gdb/inf-ttrace.c
index 802dc85..96105dc 100644
--- a/gdb/inf-ttrace.c
+++ b/gdb/inf-ttrace.c
@@ -644,7 +644,8 @@ inf_ttrace_him (struct target_ops *ops, int pid)

   do_cleanups (old_chain);

-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);

   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);

@@ -695,8 +696,8 @@ inf_ttrace_mourn_inferior (struct target_ops *ops)
     }
   inf_ttrace_page_dict.count = 0;

-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 /* Assuming we just attached the debugger to a new inferior, create
@@ -796,7 +797,8 @@ inf_ttrace_attach (struct target_ops *ops, char *args, int from_tty)
 	      (uintptr_t)&tte, sizeof tte, 0) == -1)
     perror_with_name (("ttrace"));

-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);

   inf_ttrace_create_threads_after_attach (pid);
 }
@@ -837,7 +839,7 @@ inf_ttrace_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);

-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }

 static void
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index e84ee95..f60d54c 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -201,6 +201,10 @@ static int (*linux_nat_siginfo_fixup) (siginfo_t *,
    Called by our to_xfer_partial.  */
 static target_xfer_partial_ftype *super_xfer_partial;

+/* The saved to_close method, inherited from inf-ptrace.c.
+   Called by our to_close.  */
+static void (*super_close) (struct target_ops *);
+
 static unsigned int debug_linux_nat;
 static void
 show_debug_linux_nat (struct ui_file *file, int from_tty,
@@ -4775,6 +4779,8 @@ linux_nat_close (struct target_ops *self)

   if (linux_ops->to_close)
     linux_ops->to_close (linux_ops);
+
+  super_close (self);
 }

 /* When requests are passed down from the linux-nat layer to the
@@ -4856,6 +4862,8 @@ linux_nat_add_target (struct target_ops *t)
   t->to_async = linux_nat_async;
   t->to_terminal_inferior = linux_nat_terminal_inferior;
   t->to_terminal_ours = linux_nat_terminal_ours;
+
+  super_close = t->to_close;
   t->to_close = linux_nat_close;

   /* Methods for non-stop support.  */
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index b328dfa..8a241a8 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -54,8 +54,6 @@ static void (*ofunc) ();

 static procfs_run run;

-static void procfs_open (char *, int);
-
 static ptid_t do_attach (ptid_t ptid);

 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
@@ -71,7 +69,7 @@ static int procfs_remove_hw_watchpoint (struct target_ops *self,

 static int procfs_stopped_by_watchpoint (struct target_ops *ops);

-/* These two globals are only ever set in procfs_open(), but are
+/* These two globals are only ever set in procfs_open_1, but are
    referenced elsewhere.  'nto_procfs_node' is a flag used to say
    whether we are local, or we should get the current node descriptor
    for the remote QNX node.  */
@@ -103,12 +101,12 @@ procfs_is_nto_target (bfd *abfd)
   return GDB_OSABI_QNXNTO;
 }

-/* This is called when we call 'target procfs <arg>' from the (gdb) prompt.
-   For QNX6 (nto), the only valid arg will be a QNX node string,
-   eg: "/net/some_node".  If arg is not a valid QNX node, we will
-   default to local.  */
+/* This is called when we call 'target native' or 'target procfs
+   <arg>' from the (gdb) prompt.  For QNX6 (nto), the only valid arg
+   will be a QNX node string, eg: "/net/some_node".  If arg is not a
+   valid QNX node, we will default to local.  */
 static void
-procfs_open (char *arg, int from_tty)
+procfs_open_1 (struct target_ops *ops, char *arg, int from_tty)
 {
   char *nodestr;
   char *endstr;
@@ -117,6 +115,9 @@ procfs_open (char *arg, int from_tty)
   procfs_sysinfo *sysinfo;
   struct cleanup *cleanups;

+  /* Offer to kill previous inferiors before opening this target.  */
+  target_preopen (from_tty);
+
   nto_is_nto_target = procfs_is_nto_target;

   /* Set the default node used for spawning to this one,
@@ -197,6 +198,8 @@ procfs_open (char *arg, int from_tty)
 	}
     }
   do_cleanups (cleanups);
+
+  inf_child_open_target (ops, arg, from_tty);
   printf_filtered ("Debugging using %s\n", nto_procfs_path);
 }

@@ -628,7 +631,8 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
   inferior_appeared (inf, pid);
   inf->attach_flag = 1;

-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);

   procfs_find_new_threads (ops);
 }
@@ -904,7 +908,7 @@ procfs_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (pid);
   init_thread_list ();
-  unpush_target (ops);	/* Pop out of handling an inferior.  */
+  inf_child_maybe_unpush_target (ops);
 }

 static int
@@ -1023,8 +1027,8 @@ procfs_mourn_inferior (struct target_ops *ops)
     }
   inferior_ptid = null_ptid;
   init_thread_list ();
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 /* This function breaks up an argument string into an argument
@@ -1207,7 +1211,8 @@ procfs_create_inferior (struct target_ops *ops, char *exec_file,
       /* warning( "Failed to set Kill-on-Last-Close flag: errno = %d(%s)\n",
          errn, strerror(errn) ); */
     }
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
   target_terminal_init ();

   if (exec_bfd != NULL
@@ -1385,6 +1390,25 @@ procfs_can_run (struct target_ops *self)
 /* "target procfs".  */
 static struct target_ops nto_procfs_ops;

+/* "target native".  */
+static struct target_ops *nto_native_ops;
+
+/* to_open implementation for "target procfs".  */
+
+static void
+procfs_open (char *arg, int from_tty)
+{
+  procfs_open_1 (&nto_procfs_ops, arg, from_tty);
+}
+
+/* to_open implementation for "target native".  */
+
+static void
+procfs_native_open (char *arg, int from_tty)
+{
+  procfs_open_1 (nto_native_ops, arg, from_tty);
+}
+
 /* Create the "native" and "procfs" targets.  */

 static void
@@ -1395,7 +1419,7 @@ init_procfs_targets (void)
   /* Leave to_shortname as "native".  */
   t->to_longname = "QNX Neutrino local process";
   t->to_doc = "QNX Neutrino local process (started by the \"run\" command).";
-  t->to_open = procfs_open;
+  t->to_open = procfs_native_open;
   t->to_attach = procfs_attach;
   t->to_post_attach = procfs_post_attach;
   t->to_detach = procfs_detach;
@@ -1424,6 +1448,8 @@ init_procfs_targets (void)
   t->to_have_continuable_watchpoint = 1;
   t->to_extra_thread_info = nto_extra_thread_info;

+  nto_native_ops = t;
+
   /* Register "target native".  This is the default run target.  */
   add_target (t);

@@ -1433,6 +1459,8 @@ init_procfs_targets (void)
   nto_procfs_ops.to_can_run = procfs_can_run;
   t->to_longname = "QNX Neutrino local or remote process";
   t->to_doc = "QNX Neutrino process.  target procfs <node>";
+  t->to_open = procfs_open;
+
   add_target (&nto_procfs_ops);
 }

diff --git a/gdb/procfs.c b/gdb/procfs.c
index f0d65d2..80b0a6a 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -3062,7 +3062,8 @@ procfs_attach (struct target_ops *ops, char *args, int from_tty)
       fflush (stdout);
     }
   inferior_ptid = do_attach (pid_to_ptid (pid));
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
 }

 static void
@@ -3091,7 +3092,7 @@ procfs_detach (struct target_ops *ops, const char *args, int from_tty)

   inferior_ptid = null_ptid;
   detach_inferior (pid);
-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }

 static ptid_t
@@ -4340,7 +4341,8 @@ procfs_mourn_inferior (struct target_ops *ops)
       if (pi)
 	destroy_procinfo (pi);
     }
-  unpush_target (ops);
+
+  generic_mourn_inferior ();

   if (dbx_link_bpt != NULL)
     {
@@ -4349,7 +4351,7 @@ procfs_mourn_inferior (struct target_ops *ops)
       dbx_link_bpt = NULL;
     }

-  generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 /* When GDB forks to create a runnable inferior process, this function
@@ -4367,7 +4369,8 @@ procfs_init_inferior (struct target_ops *ops, int pid)

   /* This routine called on the parent side (GDB side)
      after GDB forks the inferior.  */
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);

   if ((pi = create_procinfo (pid, 0)) == NULL)
     perror (_("procfs: out of memory in 'init_inferior'"));
diff --git a/gdb/target.c b/gdb/target.c
index 71292d3..07e3c2b 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2436,6 +2436,20 @@ target_require_runnable (void)
   internal_error (__FILE__, __LINE__, _("No targets found"));
 }

+/* Whether GDB is allowed to fall back to the default run target for
+   "run", "attach", etc. when no target is connected yet.  */
+static int auto_connect_native_target = 1;
+
+static void
+show_auto_connect_native_target (struct ui_file *file, int from_tty,
+				 struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file,
+		    _("Whether GDB may automatically connect to the "
+		      "native target is %s.\n"),
+		    value);
+}
+
 /* Look through the list of possible targets for a target that can
    execute a run or attach command without any other data.  This is
    used to locate the default process stratum.
@@ -2446,23 +2460,28 @@ target_require_runnable (void)
 static struct target_ops *
 find_default_run_target (char *do_mesg)
 {
-  struct target_ops **t;
   struct target_ops *runable = NULL;
-  int count;

-  count = 0;
-
-  for (t = target_structs; t < target_structs + target_struct_size;
-       ++t)
+  if (auto_connect_native_target)
     {
-      if ((*t)->to_can_run != delegate_can_run && target_can_run (*t))
+      struct target_ops **t;
+      int count = 0;
+
+      for (t = target_structs; t < target_structs + target_struct_size;
+	   ++t)
 	{
-	  runable = *t;
-	  ++count;
+	  if ((*t)->to_can_run != delegate_can_run && target_can_run (*t))
+	    {
+	      runable = *t;
+	      ++count;
+	    }
 	}
+
+      if (count != 1)
+	runable = NULL;
     }

-  if (count != 1)
+  if (runable == NULL)
     {
       if (do_mesg)
 	error (_("Don't know how to %s.  Try \"help target\"."), do_mesg);
@@ -4270,4 +4289,13 @@ When this permission is on, GDB may interrupt/stop the target's execution.\n\
 Otherwise, any attempt to interrupt or stop will be ignored."),
 			   set_target_permissions, NULL,
 			   &setlist, &showlist);
+
+  add_setshow_boolean_cmd ("auto-connect-native-target", class_support,
+			   &auto_connect_native_target, _("\
+Set whether GDB may automatically connect to the native target."), _("\
+Show whether GDB may automatically connect to the native target."), _("\
+When on, and GDB is not connected to a target yet, GDB\n\
+attempts \"run\" and other commands with the native target."),
+			   NULL, show_auto_connect_native_target,
+			   &setlist, &showlist);
 }
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 790a82a..4639f3b 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,12 @@
 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.
+
+2014-05-21  Pedro Alves  <palves@redhat.com>
+
 	* gdb.base/default.exp: Test "target native" instead of "target
 	child".

diff --git a/gdb/testsuite/boards/gdbserver-base.exp b/gdb/testsuite/boards/gdbserver-base.exp
index eff6327..f6bdf8c 100644
--- a/gdb/testsuite/boards/gdbserver-base.exp
+++ b/gdb/testsuite/boards/gdbserver-base.exp
@@ -34,6 +34,8 @@ set_board_info gdb,nofileio 1
 # The predefined TSVs in GDBserver.
 set_board_info gdb,predefined_tsv "\\\$trace_timestamp"

+set GDBFLAGS "${GDBFLAGS} -ex \"set auto-connect-native-target off\""
+
 proc ${board}_file { dest op args } {
     if { $op == "delete" } {
 	return 0
diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.c b/gdb/testsuite/gdb.base/auto-connect-native-target.c
new file mode 100644
index 0000000..9677e0e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.c
@@ -0,0 +1,23 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+main ()
+{
+  return 0;
+}
+
diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.exp b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
new file mode 100644
index 0000000..ac8c79f
--- /dev/null
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
@@ -0,0 +1,209 @@
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test "set auto-connect-native-target off" and "target native" on
+# native targets.
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+# Whether this GDB is configured with a "native" target.
+set have_native 0
+
+set test "help target native"
+gdb_test_multiple $test $test {
+    -re "Undefined target command.* $gdb_prompt $" {
+	set have_native 0
+    }
+    -re "Native process.*$gdb_prompt $" {
+	set have_native 1
+    }
+}
+
+if { !$have_native } {
+    unsupported "No \"target native\" support."
+    return
+}
+
+# Returns the topmost target pushed on the target stack.  TEST is used
+# as test message.
+
+proc get_topmost_target {test} {
+    global gdb_prompt
+
+    set topmost "unknown"
+
+    gdb_test_multiple "maint print target-stack" $test {
+	-re "The current target stack is:\r\n  - (\[^ \]+) .*$gdb_prompt $" {
+	    set topmost $expect_out(1,string)
+	    pass $test
+	}
+    }
+
+    return $topmost
+}
+
+set topmost [get_topmost_target "check whether a target is already connected"]
+
+# Testing against the extended-remote board, for example?
+if { $topmost != "exec" } {
+    unsupported "Already connected to target $topmost."
+    return
+}
+
+# Check which target this board connects to.  If testing with a native
+# target board, this should cause the native target to auto connect.
+if ![runto_main] then {
+    fail "Can't run to main"
+    return 0
+}
+
+# Returns true if the native target is pushed on the target stack.
+# TEST is used as test message.
+
+proc check_native_target {test} {
+    global gdb_prompt
+
+    gdb_test_multiple "maint print target-stack" $test {
+	-re " native .*$gdb_prompt $" {
+	    pass $test
+	    return 1
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    return 0
+}
+
+# Testing against a remote board, for example?
+if { ![check_native_target "check whether board tests the native target"] } {
+    unsupported "Not testing the native target."
+    return
+}
+
+# Kill program.  TEST is used as test message.
+
+proc kill_program {test} {
+    global gdb_prompt
+
+    gdb_test_multiple "kill" $test {
+	-re "Kill the program being debugged\\? .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+}
+
+# Kill the program.  This should pop the target.  The "start" test
+# below will fail otherwise.
+kill_program "kill"
+
+# Now prevent the native target from auto connecting.
+gdb_test_no_output "set auto-connect-native-target off"
+
+# Commands that rely on the native target auto-connecting should no longer work.
+gdb_test "start" "Don't know how to run.*" "start no longer works"
+
+# Explicitly connect to the native target.
+gdb_test "target native" \
+    "Done.  Use the \"run\" command to start a process.*" \
+    "explicitly connect to the native target"
+
+proc test_native_target_remains_pushed {} {
+    gdb_test "maint print target-stack"  \
+	"The current target stack is:\r\n  .* native .* exec .*" \
+	"native target remains pushed"
+}
+
+# Test a set of "inferior gone" scenarios, making sure the target
+# remains pushed.
+
+with_test_prefix "kill" {
+    gdb_test "start" "main.*" "start"
+
+    kill_program "kill"
+
+    test_native_target_remains_pushed
+}
+
+with_test_prefix "detach" {
+    gdb_test "start" "main.*"
+
+    set test "detach"
+    gdb_test_multiple $test $test {
+	-re "Detach the program being debugged\\? .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    test_native_target_remains_pushed
+}
+
+with_test_prefix "run to exit" {
+    gdb_test "start" "Temporary breakpoint .* main .*"
+
+    gdb_test "c" "$inferior_exited_re normally.*"
+
+    test_native_target_remains_pushed
+}
+
+# Now test disconnecting.  Commands that rely on the native target
+# auto-connecting should no longer work (again) after this.
+
+with_test_prefix "disconnect" {
+    gdb_test "start" "Temporary breakpoint .* main .*"
+
+    set test "disconnect"
+    gdb_test_multiple $test $test {
+	-re "A program is being debugged already.* .y or n. $" {
+	    send_gdb "y\n"
+	    exp_continue
+	}
+	-re "$gdb_prompt $" {
+	    pass $test
+	}
+    }
+
+    set topmost \
+	[get_topmost_target "check whether the target is no longer connected"]
+
+    set test "no longer connected to a target"
+    if { $topmost == "exec" } {
+	pass $test
+    } else {
+	fail $test
+    }
+
+    gdb_test "start" "Don't know how to run.*" "start no longer works"
+}
+
+# Reenable auto-connecting to the native target.  Plain "start" should
+# start working again.
+gdb_test_no_output "set auto-connect-native-target on"
+
+gdb_test "start" "Temporary breakpoint .* main .*" \
+    "start auto-connects to the native target after reenabling auto-connect"
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index d026a36..fe43c24 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -1726,7 +1726,8 @@ do_initial_windows_stuff (struct target_ops *ops, DWORD pid, int attaching)
 #endif
   current_event.dwProcessId = pid;
   memset (&current_event, 0, sizeof (current_event));
-  push_target (ops);
+  if (!target_is_pushed (ops))
+    push_target (ops);
   disable_breakpoints_in_shlibs ();
   windows_clear_solib ();
   clear_proceed_status ();
@@ -1914,7 +1915,7 @@ windows_detach (struct target_ops *ops, const char *args, int from_tty)
   inferior_ptid = null_ptid;
   detach_inferior (current_event.dwProcessId);

-  unpush_target (ops);
+  inf_child_maybe_unpush_target (ops);
 }

 /* Try to determine the executable filename.
@@ -2367,8 +2368,8 @@ windows_mourn_inferior (struct target_ops *ops)
       CHECK (CloseHandle (current_process_handle));
       open_process_used = 0;
     }
-  unpush_target (ops);
   generic_mourn_inferior ();
+  inf_child_maybe_unpush_target (ops);
 }

 /* Send a SIGINT to the process group.  This acts just like the user typed a
-- 
1.9.0


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

* [pushed] Re: [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native").
  2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
                   ` (9 preceding siblings ...)
  2014-03-17 17:02 ` Pedro Alves
@ 2014-05-21 17:57 ` Pedro Alves
  10 siblings, 0 replies; 21+ messages in thread
From: Pedro Alves @ 2014-05-21 17:57 UTC (permalink / raw)
  To: gdb-patches

I've pushed this in now.

Pedro Alves

On 03/17/2014 03:22 PM, Pedro Alves wrote:
> 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 this series, 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 renamed all native targets
> to "target native", and made "target native" work instead of erroring
> out as today.
> 
> E.g., before, on GNU/Linux and most native targets, the native target
> is called "child", and trying to open it yields:
> 
>  (gdb) target child
>  Use the "run" command to start a child process.
> 
> After the series we get:
> 
>  (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 that board, it's always a bug to launch a
> program with the native target.  Turns out we still have one such case
> this series 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
> 
> For convenience, I've also put this at:
>   git@github.com:palves/gdb.git target_native_v1
> 
> Pedro Alves (8):
>   Rename "target djgpp" -> "target native"
>   Rename "target darwin-child" -> "target native"
>   Rename "target GNU" -> "target native"
>   Windows: Rename "target child" -> "target native"
>   nto-procfs.c: Add "target native".
>   go32-nat.c: Don't override to_open.
>   NEWS: Mention native target renames.
>   Allow making GDB not automatically connect to the native target.
> 
>  gdb/NEWS                                           |  21 +++
>  gdb/darwin-nat.c                                   |  10 +-
>  gdb/doc/gdb.texinfo                                |  58 ++++++
>  gdb/gnu-nat.c                                      |   8 +-
>  gdb/go32-nat.c                                     |  16 +-
>  gdb/inf-child.c                                    |  62 +++++-
>  gdb/inf-child.h                                    |  11 ++
>  gdb/inf-ptrace.c                                   |   6 +-
>  gdb/inf-ttrace.c                                   |  10 +-
>  gdb/linux-nat.c                                    |   8 +
>  gdb/nto-procfs.c                                   |  97 +++++++---
>  gdb/procfs.c                                       |  13 +-
>  gdb/target.c                                       |  48 ++++-
>  gdb/testsuite/boards/native-extended-gdbserver.exp |   2 +
>  .../gdb.base/auto-connect-native-target.c          |  23 +++
>  .../gdb.base/auto-connect-native-target.exp        | 209 +++++++++++++++++++++
>  gdb/windows-nat.c                                  |  10 +-
>  17 files changed, 531 insertions(+), 81 deletions(-)
>  create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.c
>  create mode 100644 gdb/testsuite/gdb.base/auto-connect-native-target.exp
> 


-- 
Pedro Alves

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

* [PATCH OBV] Fix regexp pattern in gdb.base/default.exp (Re: [PATCH 0.5/8] Rename "target child" to "target native".)
  2014-03-17 17:02 ` Pedro Alves
@ 2014-06-03  5:29   ` Yao Qi
  0 siblings, 0 replies; 21+ messages in thread
From: Yao Qi @ 2014-06-03  5:29 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

On 03/18/2014 01:02 AM, Pedro Alves wrote:
> -#test target child
> -gdb_test "target child" "Use the \"run\" command to start a child process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target child"
> +#test target native
> +gdb_test "target native" "Use the \"run\" command to start a process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target native"

Subject: [PATCH] Fix regexp pattern in gdb.base/default.exp

I see this failure on arm-none-eabi gdb testing.

target native^M
Undefined target command: "native".  Try "help target".^M
(gdb) FAIL: gdb.base/default.exp: target native

This patch is to update the regexp pattern to match "native" instead of
"child".  I'll push it in.

gdb/testsuite:

2014-06-03  Yao Qi  <yao@codesourcery.com>

	* gdb.base/default.exp: Replace "child" with "native" in
	regexp pattern.
---
 gdb/testsuite/gdb.base/default.exp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp
index 3636fa3..6674df3 100644
--- a/gdb/testsuite/gdb.base/default.exp
+++ b/gdb/testsuite/gdb.base/default.exp
@@ -747,7 +747,7 @@ gdb_test "step" "The program is not being run." "step #2"
 gdb_test "symbol-file" ".*" "symbol-file"
 
 #test target native
-gdb_test "target native" "Use the \"run\" command to start a process.*|Undefined target command: \"child\".  *Try \"help target\".*" "target native"
+gdb_test "target native" "Use the \"run\" command to start a process.*|Undefined target command: \"native\".  *Try \"help target\".*" "target native"
 
 #test target core
 send_gdb "target core\n"
-- 
1.9.0

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

* [PATCH OBV] Fix a regexp pattern in gdb.base/auto-connect-native-target.exp (Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.)
  2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
                     ` (2 preceding siblings ...)
  2014-03-25 20:06   ` Doug Evans
@ 2014-06-03  5:35   ` Yao Qi
  3 siblings, 0 replies; 21+ messages in thread
From: Yao Qi @ 2014-06-03  5:35 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 03/17/2014 11:23 PM, Pedro Alves wrote:
> +set test "help target native"
> +gdb_test_multiple $test $test {
> +    -re "Undefined target command.* $gdb_prompt $" {

The space before "$gdb_prompt $" looks redundant and this patch
is to remove it from the regexp pattern.

> +	set have_native 0
> +    }
> +    -re "Native process.*$gdb_prompt $" {
> +	set have_native 1
> +    }
> +}


-- 
Yao (齐尧)

Subject: [PATCH] Fix a regexp pattern in gdb.base/auto-connect-native-target.exp

When I test gdb head (for 7.8 release) on arm-none-eabi, I find the
following failure, which is caused by the improper regexp
pattern in the test.

(gdb) help target native^M
Undefined target command: "native".  Try "help target".^M
(gdb) FAIL: gdb.base/auto-connect-native-target.exp: help target native

The space before "$gdb_prompt $" looks redundant, and this patch
is to remove it from the regexp pattern.

gdb/testsuite:

2014-06-03  Yao Qi  <yao@codesourcery.com>

	* gdb.base/auto-connect-native-target.exp: Remove redundant
	space from the regexp pattern.
---
 gdb/testsuite/gdb.base/auto-connect-native-target.exp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/testsuite/gdb.base/auto-connect-native-target.exp b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
index ac8c79f..79febe3 100644
--- a/gdb/testsuite/gdb.base/auto-connect-native-target.exp
+++ b/gdb/testsuite/gdb.base/auto-connect-native-target.exp
@@ -27,7 +27,7 @@ set have_native 0
 
 set test "help target native"
 gdb_test_multiple $test $test {
-    -re "Undefined target command.* $gdb_prompt $" {
+    -re "Undefined target command.*$gdb_prompt $" {
 	set have_native 0
     }
     -re "Native process.*$gdb_prompt $" {
-- 
1.9.0

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

end of thread, other threads:[~2014-06-03  5:35 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-17 15:23 [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves
2014-03-17 15:23 ` [PATCH 3/8] Rename "target GNU" -> "target native" Pedro Alves
2014-03-17 15:23 ` [PATCH 7/8] NEWS: Mention native target renames Pedro Alves
2014-03-17 16:45   ` Eli Zaretskii
2014-03-17 15:23 ` [PATCH 1/8] Rename "target djgpp" -> "target native" Pedro Alves
2014-03-17 16:44   ` Eli Zaretskii
2014-03-17 15:23 ` [PATCH 8/8] Allow making GDB not automatically connect to the native target Pedro Alves
2014-03-17 16:47   ` Eli Zaretskii
2014-03-18 21:55   ` Stan Shebs
2014-03-25 20:06   ` Doug Evans
2014-05-21 17:53     ` Pedro Alves
2014-06-03  5:35   ` [PATCH OBV] Fix a regexp pattern in gdb.base/auto-connect-native-target.exp (Re: [PATCH 8/8] Allow making GDB not automatically connect to the native target.) Yao Qi
2014-03-17 15:23 ` [PATCH 6/8] go32-nat.c: Don't override to_open Pedro Alves
2014-03-17 16:44   ` Eli Zaretskii
2014-03-17 15:23 ` [PATCH 4/8] Windows: Rename "target child" -> "target native" Pedro Alves
2014-03-17 15:23 ` [PATCH 2/8] Rename "target darwin-child" " Pedro Alves
2014-03-17 15:23 ` [PATCH 5/8] nto-procfs.c: Add " Pedro Alves
2014-03-17 17:01 ` [PATCH 0.5/8] Rename "target child" to " Pedro Alves
2014-03-17 17:02 ` Pedro Alves
2014-06-03  5:29   ` [PATCH OBV] Fix regexp pattern in gdb.base/default.exp (Re: [PATCH 0.5/8] Rename "target child" to "target native".) Yao Qi
2014-05-21 17:57 ` [pushed] Re: [PATCH 0/8] Allow making GDB not automatically connect to the native target (and add "target native") Pedro Alves

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).