]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/mi: fix regression in mi -add-inferior command
authorUmair Sair <umair_sair@hotmail.com>
Thu, 24 Feb 2022 17:25:51 +0000 (22:25 +0500)
committerAndrew Burgess <aburgess@redhat.com>
Mon, 7 Mar 2022 19:38:53 +0000 (19:38 +0000)
Prior to the multi-target support commit:

  commit 5b6d1e4fa4fc6827c7b3f0e99ff120dfa14d65d2
  Date:   Fri Jan 10 20:06:08 2020 +0000

      Multi-target support

When a new inferior was added using the MI -add-inferior command, the
new inferior would be using the same target as all the other
inferiors.  This makes sense, GDB only supported a single target stack
at a time.

After the above commit, each inferior has its own target stack.

To maintain backward compatibility, for the CLI add-inferior command,
when a new inferior is added the above commit has the new inferior
inherit a copy of the target stack from the current inferior.

Unfortunately, this same backward compatibility is missing for the MI.

This commit fixes this oversight.

Now, when the -add-inferior MI command is used, the new inferior will
inherit a copy of the target stack from the current inferior.

gdb/NEWS
gdb/inferior.c
gdb/inferior.h
gdb/mi/mi-main.c
gdb/testsuite/gdb.mi/mi-add-inferior.exp [new file with mode: 0644]

index ee9eaad63a0a15f9bf4e6f2621d237c9a6b3d050..4e12ebfb0bdac338acb5008cf25df57d0b415121 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -152,6 +152,12 @@ info win
   of the floating-point type, and the "f" is the marker for floating
   point.
 
+* MI changes
+
+ ** The '-add-inferior' with no option flags now inherits the
+    connection of the current inferior, this restores the behaviour of
+    GDB as it was prior to GDB 10.
+
 * New targets
 
 GNU/Linux/LoongArch    loongarch*-*-linux*
index 965ae65287ef665ce0f3ce5017a67ec31bc7e1f4..48d5c8bfd1545759fe94a54fc82c6e94583ad55d 100644 (file)
@@ -759,11 +759,9 @@ add_inferior_with_spaces (void)
   return inf;
 }
 
-/* Switch to inferior NEW_INF, a new inferior, and unless
-   NO_CONNECTION is true, push the process_stratum_target of ORG_INF
-   to NEW_INF.  */
+/* See inferior.h.  */
 
-static void
+void
 switch_to_inferior_and_push_target (inferior *new_inf,
                                    bool no_connection, inferior *org_inf)
 {
index 45de3c2d9c8b1bd6421b3f152433d2bcbfb92161..caabc2ee8d8b0df2fd382891edbc8ee1a8f79f70 100644 (file)
@@ -740,4 +740,11 @@ extern struct inferior *add_inferior_with_spaces (void);
 /* Print the current selected inferior.  */
 extern void print_selected_inferior (struct ui_out *uiout);
 
+/* Switch to inferior NEW_INF, a new inferior, and unless
+   NO_CONNECTION is true, push the process_stratum_target of ORG_INF
+   to NEW_INF.  */
+
+extern void switch_to_inferior_and_push_target
+  (inferior *new_inf, bool no_connection, inferior *org_inf);
+
 #endif /* !defined (INFERIOR_H) */
index 4860da7536a99e6e50f5cc95ab9774735b4125f1..2fab592d6fbeed4ae7e5c354fe869727bbe6d98f 100644 (file)
@@ -1708,8 +1708,12 @@ mi_cmd_add_inferior (const char *command, char **argv, int argc)
   if (argc != 0)
     error (_("-add-inferior should be passed no arguments"));
 
+  scoped_restore_current_pspace_and_thread restore_pspace_thread;
+
   inf = add_inferior_with_spaces ();
 
+  switch_to_inferior_and_push_target (inf, false, current_inferior ());
+
   current_uiout->field_fmt ("inferior", "i%d", inf->num);
 }
 
diff --git a/gdb/testsuite/gdb.mi/mi-add-inferior.exp b/gdb/testsuite/gdb.mi/mi-add-inferior.exp
new file mode 100644 (file)
index 0000000..3f0cd7c
--- /dev/null
@@ -0,0 +1,122 @@
+# Copyright 2022 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 MI '-add-inferior'.
+
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+standard_testfile basics.c
+
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
+          executable {debug}] != "" } {
+    untested "failed to compile"
+    return -1
+}
+
+mi_clean_restart ${binfile}
+
+# Start execution to establish a connection.
+mi_runto_main
+
+# Use 'info inferiors' to find the details of the current connection.
+set header_line ""
+set inf_line ""
+gdb_test_multiple "info inferiors" "" {
+
+    -re "^info inferiors\r\n" {
+       exp_continue
+    }
+
+    -re "^&\[^\r\n\]+\r\n" {
+       exp_continue
+    }
+
+    -re "~\"(  Num\\s+Description\\s+Connection\[^\r\n\]+)\r\n" {
+       set header_line $expect_out(1,string)
+       exp_continue
+    }
+
+    -re "^~\"(\\*\\s+1\\s+\[^\r\n\]+)\r\n" {
+       set inf_line $expect_out(1,string)
+       exp_continue
+    }
+
+    -re "^\\^done\r\n" {
+       exp_continue
+    }
+
+    -re "^$mi_gdb_prompt$" {
+       gdb_assert { [string length "$header_line"] > 0 }
+       gdb_assert { [string length "$inf_line"] > 0 }
+       pass $gdb_test_name
+    }
+}
+
+# Now extract the string that represents the connection, and convert
+# it into a regexp.
+set idx [string first "Connection" "${header_line}"]
+gdb_assert { $idx > -1 }
+set inf_line [string range "${inf_line}" $idx end]
+regexp "^(${decimal} \\(\[^)\]+\\))" $inf_line conn_info
+set conn_pattern [string_to_regexp "${conn_info}"]
+
+# Now add a new inferior, this should use the connection of the
+# current inferior.
+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=\"\[^\"\]+\"" ] \
+    "mi add inferior"
+
+# Now run 'info inferiors' again to check that the currently selected
+# inferior has not changed.
+set saw_current_inferior false
+set saw_new_inferior false
+gdb_test_multiple "info inferiors" \
+    "info inferiors, after new inferior was created" {
+
+       -re "^info inferiors\r\n" {
+           exp_continue
+       }
+
+       -re "^&\[^\r\n\]+\r\n" {
+           exp_continue
+       }
+
+       -re "~\"\\s+Num\\s+Description\\s+Connection\[^\r\n\]+\r\n" {
+           exp_continue
+       }
+
+       -re "^~\"\\*\\s+1\\s+\[^\r\n\]+\\s+${conn_pattern}\\s+\[^\r\n\]+\r\n" {
+           set saw_current_inferior true
+           exp_continue
+       }
+
+       -re "^~\"\\s+2\\s+\[^\r\n\]+\\s+${conn_pattern}\\s+\[^\r\n\]+\r\n" {
+           set saw_new_inferior true
+           exp_continue
+       }
+
+       -re "^\\^done\r\n" {
+           exp_continue
+       }
+
+       -re "^$mi_gdb_prompt$" {
+           gdb_assert { $saw_current_inferior && $saw_new_inferior }
+           pass $gdb_test_name
+       }
+    }