public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCHv2 2/2] gdb/mi: add --no-connection to MI -add-inferior command
Date: Wed,  2 Mar 2022 11:57:36 +0000	[thread overview]
Message-ID: <2d926ccc029733a21d4b1483f89b87739e570b29.1646221667.git.aburgess@redhat.com> (raw)
In-Reply-To: <cover.1646221667.git.aburgess@redhat.com>

Following on from the previous commit, where the -add-inferior command
now uses the same connection as the current inferior, this commit adds
a --no-connection option to -add-inferior.

This new option matches the existing option of the same name for the
CLI version of add-inferior; the new inferior is created with no
connection.

I've added a new 'connection' field to the MI output of -add-inferior,
which includes the connection number and short name.  I haven't
included the longer description field, this is the MI after all.  My
expectation would be that if the frontend wanted to display all the
connection details then this would be looked up from 'info
connection' (or the MI equivalent if/when such a command is added).

The existing -add-inferior tests are updated, as are the docs.
---
 gdb/doc/gdb.texinfo                      | 39 +++++++++++++++++---
 gdb/mi/mi-main.c                         | 45 +++++++++++++++++++++---
 gdb/testsuite/gdb.mi/mi-add-inferior.exp | 10 +++++-
 3 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 504eb663c14..00824f840eb 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3310,6 +3310,7 @@
 @w{@code{remove-inferiors}} command.
 
 @table @code
+@anchor{add_inferior_cli}
 @kindex add-inferior
 @item add-inferior [ -copies @var{n} ] [ -exec @var{executable} ] [-no-connection ]
 Adds @var{n} inferiors to be run using @var{executable} as the
@@ -37101,15 +37102,45 @@
 @subheading Synopsis
 
 @smallexample
--add-inferior
+-add-inferior [ --no-connection ]
 @end smallexample
 
 Creates a new inferior (@pxref{Inferiors Connections and Programs}).  The created
 inferior is not associated with any executable.  Such association may
 be established with the @samp{-file-exec-and-symbols} command
-(@pxref{GDB/MI File Commands}).  The command response has a single
-field, @samp{inferior}, whose value is the identifier of the
-thread group corresponding to the new inferior.
+(@pxref{GDB/MI File Commands}).
+
+By default, the new inferior begins connected to the same target
+connection as the current inferior.  For example, if the current
+inferior was connected to @code{gdbserver} with @code{target remote},
+then the new inferior will be connected to the same @code{gdbserver}
+instance.  The @samp{--no-connection} option starts the new inferior
+with no connection yet.  You can then for example use the
+@code{-target-select remote} command to connect to some other
+@code{gdbserver} instance, use @code{-exec-run} to spawn a local
+program, etc.
+
+The command response always has a field, @samp{inferior}, whose value
+is the identifier of the thread group corresponding to the new
+inferior.
+
+An additional section field, @samp{connection}, is optional.  This
+field will only exist if the new inferior has a target connection.  If
+this field exists, then its value will be a tuple containing the
+following fields:
+
+@table @samp
+@item number
+The number of the connection used for the new inferior.
+
+@item name
+The name of the connection type used for the new inferior.
+@end table
+
+@subheading @value{GDBN} Command
+
+The corresponding @value{GDBN} command is @samp{add-inferior}
+(@pxref{add_inferior_cli,,@samp{add-inferior}}).
 
 @subheading Example
 
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 2fab592d6fb..b3592964e3d 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -1703,18 +1703,53 @@ mi_cmd_list_target_features (const char *command, char **argv, int argc)
 void
 mi_cmd_add_inferior (const char *command, char **argv, int argc)
 {
-  struct inferior *inf;
+  bool no_connection = false;
 
-  if (argc != 0)
-    error (_("-add-inferior should be passed no arguments"));
+  /* Parse the command options.  */
+  enum opt
+    {
+      NO_CONNECTION_OPT,
+    };
+  static const struct mi_opt opts[] =
+    {
+	{"-no-connection", NO_CONNECTION_OPT, 0},
+	{NULL, 0, 0},
+    };
+
+  int oind = 0;
+  char *oarg;
+
+  while (1)
+    {
+      int opt = mi_getopt ("-add-inferior", argc, argv, opts, &oind, &oarg);
+
+      if (opt < 0)
+	break;
+      switch ((enum opt) opt)
+	{
+	case NO_CONNECTION_OPT:
+	  no_connection = true;
+	  break;
+	}
+    }
 
   scoped_restore_current_pspace_and_thread restore_pspace_thread;
 
-  inf = add_inferior_with_spaces ();
+  inferior *inf = add_inferior_with_spaces ();
 
-  switch_to_inferior_and_push_target (inf, false, current_inferior ());
+  switch_to_inferior_and_push_target (inf, no_connection,
+				      current_inferior ());
 
   current_uiout->field_fmt ("inferior", "i%d", inf->num);
+
+  process_stratum_target *proc_target = inf->process_target ();
+
+  if (proc_target != nullptr)
+    {
+      ui_out_emit_tuple tuple_emitter (current_uiout, "connection");
+      current_uiout->field_unsigned ("number", proc_target->connection_number);
+      current_uiout->field_string ("name", proc_target->shortname ());
+    }
 }
 
 void
diff --git a/gdb/testsuite/gdb.mi/mi-add-inferior.exp b/gdb/testsuite/gdb.mi/mi-add-inferior.exp
index 3f0cd7cc06c..85cd6a5271d 100644
--- a/gdb/testsuite/gdb.mi/mi-add-inferior.exp
+++ b/gdb/testsuite/gdb.mi/mi-add-inferior.exp
@@ -79,7 +79,7 @@ mi_gdb_test "-add-inferior" \
     [multi_line "=thread-group-added,id=\"\[^\"\]+\"" \
 	 "~\"\\\[New inferior 2\\\]\\\\n\"" \
 	 "\~\"Added inferior 2 on connection ${conn_pattern}\\\\n\"" \
-	 "\\^done,inferior=\"\[^\"\]+\"" ] \
+	 "\\^done,inferior=\"\[^\"\]+\",connection=\{number=\"$decimal\",name=\"\[^\"\]+\"\}" ] \
     "mi add inferior"
 
 # Now run 'info inferiors' again to check that the currently selected
@@ -120,3 +120,11 @@ gdb_test_multiple "info inferiors" \
 	    pass $gdb_test_name
 	}
     }
+
+# Add a third inferior, but this time, use --no-connection.
+mi_gdb_test "-add-inferior --no-connection" \
+    [multi_line "=thread-group-added,id=\"\[^\"\]+\"" \
+	 "~\"\\\[New inferior 3\\\]\\\\n\"" \
+	 "\~\"Added inferior 3\\\\n\"" \
+	 "\\^done,inferior=\"\[^\"\]+\"" ] \
+    "mi add inferior with no connection"
-- 
2.25.4


  parent reply	other threads:[~2022-03-02 11:57 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-24 17:25 [PATCH] Fixing regression in mi " Muhammad Umair Sair
2022-03-02 11:57 ` [PATCHv2 0/2] " Andrew Burgess
2022-03-02 11:57   ` [PATCHv2 1/2] gdb/mi: fix " Andrew Burgess
2022-03-02 11:57   ` Andrew Burgess [this message]
2022-03-02 20:01     ` [PATCHv2 2/2] gdb/mi: add --no-connection to MI " Eli Zaretskii
2022-03-03 12:44       ` Andrew Burgess
2022-03-03 14:02         ` Eli Zaretskii
2022-03-03 14:58         ` Pedro Alves
2022-03-07 19:40           ` Andrew Burgess
2022-03-07 20:37             ` Pedro Alves
2022-03-08 12:55               ` Andrew Burgess
2022-03-08 20:41                 ` Pedro Alves
2022-03-08 12:58               ` [PUSHED 0/2] Fixing regression in mi " Andrew Burgess
2022-03-08 12:58                 ` [PUSHED 1/2] gdb/mi: fix " Andrew Burgess
2022-03-08 12:58                 ` [PUSHED 2/2] gdb/mi: add --no-connection to MI " Andrew Burgess
2022-03-03 14:59   ` [PATCHv2 0/2] Fixing regression in mi " Pedro Alves

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=2d926ccc029733a21d4b1483f89b87739e570b29.1646221667.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

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

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