@@ -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
@@ -76,6 +82,10 @@ maint ada show ignore-descriptive-types
* The "catch syscall" command now works on s390*-linux* targets.
+* The "target native" command now connects to the native instead of
+ erroring out. This can be used to launch native programs when "set
+ auto-connect-native-target" is set to off.
+
* New remote packets
qXfer:btrace:read's annex
@@ -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 fail 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}, @code{attach}, etc.@: commands spawn native processes 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};
@@ -40,6 +40,10 @@
#include <fcntl.h>
#include <unistd.h>
+/* A pointer to what is returned by inf_child_target. Used to be able
+ to push the most-derived target in reaction to "target child". */
+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 +113,41 @@ inf_child_prepare_to_store (struct target_ops *self,
{
}
+/* True if the user did "target child". In that case, we won't unpush
+ the child target automatically when the last inferior is gone. */
+static int inf_child_explicitly_opened;
+
static void
inf_child_open (char *arg, int from_tty)
{
- error (_("Use the \"run\" command to start a process."));
+ target_preopen (from_tty);
+ push_target (inf_child_ops);
+ inf_child_explicitly_opened = 1;
+}
+
+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);
+}
+
+static void
+inf_child_close (struct target_ops *target)
+{
+ /* In case we were forcibly closed. */
+ inf_child_explicitly_opened = 0;
+}
+
+void
+inf_child_maybe_unpush (struct target_ops *ops)
+{
+ if (!inf_child_explicitly_opened && !have_inferiors ())
+ unpush_target (ops);
}
static void
@@ -410,6 +445,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 +482,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 target from
+ inf_child_open. */
+ inf_child_ops = t;
+
return t;
}
@@ -25,6 +25,8 @@
extern struct target_ops *inf_child_target (void);
+extern void inf_child_maybe_unpush (struct target_ops *ops);
+
/* Functions for helping to write a native target. */
/* This is for native targets which use a unix/POSIX-style waitstatus. */
@@ -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 (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 (ops);
}
/* Kill the inferior. */
@@ -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. */
@@ -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);
}
@@ -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