From: Stephan Rohr <stephan.rohr@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v2 1/2] gdb: delay the new inferior notification
Date: Wed, 25 Nov 2026 13:08:01 +0000 [thread overview]
Message-ID: <20261125130802.117043-2-stephan.rohr@intel.com> (raw)
In-Reply-To: <20261125130802.117043-1-stephan.rohr@intel.com>
Adding or cloning an inferior causes GDB to print the new inferior
notification. If this causes pagination and the user answers "q", this
leaves the inferior's program space uninitialized:
(gdb) set width 10
(gdb) set height 1
(gdb) add-inferior
[New infer--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(gdb) set height unlimited
(gdb) info inferiors
Num Description Connection Executable
* 1 <null>
2 .../gdb/progspace.c:329: internal-error: set_current_program_space:
Assertion `pspace != NULL' failed.
Remove "add_inferior ()" and replace with "add_inferior_silend ()" and
"print_new_inferior ()" to delay the notification until the program
space is set up. Still, answering "q" in certain scenarios may leave
the inferior partly initialized, e.g., "clone-inferior" may not have
copied the arguments and environment yet.
---
gdb/inferior.c | 26 ++++----
gdb/inferior.h | 14 ++---
gdb/infrun.c | 12 +++-
gdb/remote.c | 6 +-
.../gdb.base/add-inferior-paginate.exp | 59 +++++++++++++++++++
5 files changed, 96 insertions(+), 21 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/add-inferior-paginate.exp
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 6f63c602050..d89553a2393 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -221,22 +221,20 @@ add_inferior_silent (int pid)
return inf;
}
-struct inferior *
-add_inferior (int pid)
-{
- struct inferior *inf = add_inferior_silent (pid);
+/* See inferior.h. */
+void
+print_new_inferior (inferior *inf)
+{
if (print_inferior_events)
{
- if (pid != 0)
+ if (inf->pid != 0)
gdb_printf (_("[New inferior %d (%s)]\n"),
inf->num,
- target_pid_to_str (ptid_t (pid)).c_str ());
+ target_pid_to_str (ptid_t (inf->pid)).c_str ());
else
gdb_printf (_("[New inferior %d]\n"), inf->num);
}
-
- return inf;
}
/* See inferior.h. */
@@ -906,7 +904,7 @@ add_inferior_with_spaces (void)
doesn't really return a new address space; otherwise, it
really does. */
pspace = new program_space (maybe_new_address_space ());
- inf = add_inferior (0);
+ inf = add_inferior_silent (0);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
@@ -918,6 +916,10 @@ add_inferior_with_spaces (void)
always have a valid arch by now. */
gdb_assert (inf->arch () != nullptr);
+ /* Print the new inferior notification once the inferior is properly
+ set up. */
+ print_new_inferior (inf);
+
return inf;
}
@@ -1168,11 +1170,15 @@ clone_inferior_command (const char *args, int from_tty)
doesn't really return a new address space; otherwise, it
really does. */
pspace = new program_space (maybe_new_address_space ());
- inf = add_inferior (0);
+ inf = add_inferior_silent (0);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
inf->set_arch (orginf->arch ());
+ /* Print the new inferior notification once the program space and
+ address space are set up. */
+ print_new_inferior (inf);
+
switch_to_inferior_and_push_target (inf, opts.no_connection, orginf);
/* If the original inferior had a user specified target
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 3d0befc0735..ca2c269a123 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -739,16 +739,14 @@ class inferior : public refcounted_object,
gdbarch *m_gdbarch = nullptr;
};
-/* Add an inferior to the inferior list, print a message that a new
- inferior is found, and return the pointer to the new inferior.
- Caller may use this pointer to initialize the private inferior
- data. */
-extern struct inferior *add_inferior (int pid);
-
-/* Same as add_inferior, but don't print new inferior notifications to
- the CLI. */
+/* Add an inferior to the inferior list and return the pointer to the
+ new inferior. Caller may use this pointer to initialize the private
+ inferior data. No new inferior notification is printed to the CLI. */
extern struct inferior *add_inferior_silent (int pid);
+/* Print the new inferior notification for INF to the CLI, if enabled. */
+extern void print_new_inferior (inferior *inf);
+
extern void delete_inferior (struct inferior *todel);
/* Delete an existing inferior list entry, due to inferior detaching. */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 8f02c49cbcb..dced78a3963 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -560,7 +560,7 @@ holding the child stopped. Try \"set %ps\" or \"%ps\".\n"),
else
{
/* Add process to GDB's tables. */
- child_inf = add_inferior (child_ptid.pid ());
+ child_inf = add_inferior_silent (child_ptid.pid ());
child_inf->attach_flag = parent_inf->attach_flag;
copy_terminal_info (child_inf, parent_inf);
@@ -594,6 +594,10 @@ holding the child stopped. Try \"set %ps\" or \"%ps\".\n"),
child_inf->removable = true;
clone_program_space (child_inf->pspace, parent_inf->pspace);
}
+
+ /* Print the new inferior notification once the inferior is properly
+ setup. */
+ print_new_inferior (child_inf);
}
if (has_vforked)
@@ -636,7 +640,7 @@ holding the child stopped. Try \"set %ps\" or \"%ps\".\n"),
/* Add the new inferior first, so that the target_detach below
doesn't unpush the target. */
- child_inf = add_inferior (child_ptid.pid ());
+ child_inf = add_inferior_silent (child_ptid.pid ());
child_inf->attach_flag = parent_inf->attach_flag;
copy_terminal_info (child_inf, parent_inf);
@@ -685,6 +689,10 @@ holding the child stopped. Try \"set %ps\" or \"%ps\".\n"),
child_inf->symfile_flags = SYMFILE_NO_READ;
clone_program_space (child_inf->pspace, parent_inf->pspace);
}
+
+ /* Print the new inferior notification once the inferior is properly
+ set up. */
+ print_new_inferior (child_inf);
}
gdb_assert (current_inferior () == parent_inf);
diff --git a/gdb/remote.c b/gdb/remote.c
index abe6cf6d4be..d1b8c1486b2 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3092,13 +3092,17 @@ remote_target::remote_add_inferior (bool fake_pid_p, int pid, int attached,
{
/* If the target shares code across all inferiors, then every
attach adds a new inferior. */
- inf = add_inferior (pid);
+ inf = add_inferior_silent (pid);
/* ... and every inferior is bound to the same program space.
However, each inferior may still have its own address
space. */
inf->aspace = maybe_new_address_space ();
inf->pspace = current_program_space;
+
+ /* Print the new inferior notification once the inferior is properly
+ set up. */
+ print_new_inferior (inf);
}
else
{
diff --git a/gdb/testsuite/gdb.base/add-inferior-paginate.exp b/gdb/testsuite/gdb.base/add-inferior-paginate.exp
new file mode 100644
index 00000000000..d00aaf8d3d2
--- /dev/null
+++ b/gdb/testsuite/gdb.base/add-inferior-paginate.exp
@@ -0,0 +1,59 @@
+# Copyright 2026 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/>.
+
+standard_testfile main.c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
+ return
+}
+
+# Run CMD and enforce pagination. Ensure the inferior is properly set up
+# if the user answers "quit".
+
+proc test_paginate { name cmd } {
+ global testfile gdb_prompt pagination_prompt
+
+ with_test_prefix $name {
+ clean_restart $testfile
+
+ # Avoid unexpected output from readline.
+ gdb_test_no_output "set editing off"
+
+ set saw_pagination 0
+ with_set height 1 {
+ with_set width 10 {
+ gdb_test_multiple $cmd "quit at pagination prompt" {
+ -re "$pagination_prompt$" {
+ set saw_pagination 1
+ send_gdb "q\n"
+ exp_continue
+ }
+ -re "Quit\r\n$gdb_prompt $" {
+ gdb_assert { $saw_pagination } $gdb_test_name
+ }
+ }
+ }
+ }
+
+ # Check inferior 2 is properly set up. The quit happens before
+ # any executable is attached to inferior 2, so it has none.
+ gdb_test "info inferiors" "\r\n\\s+2\\s+<null>\[^\r\n\]*"
+ gdb_test "inferior 2" "\\\[Switching to inferior 2 \[^\r\n\]*\\\]" \
+ "switch to inferior 2"
+ }
+}
+
+test_paginate "add-inferior" "add-inferior -exec $binfile"
+test_paginate "clone-inferior" "clone-inferior"
--
2.43.0
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
next prev parent reply other threads:[~2026-11-25 13:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 13:08 [PATCH v2 0/2] disable pagination for " Stephan Rohr
2026-11-25 13:08 ` Stephan Rohr [this message]
2026-11-25 13:08 ` [PATCH v2 2/2] gdb: disable pagination while following a fork Stephan Rohr
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=20261125130802.117043-2-stephan.rohr@intel.com \
--to=stephan.rohr@intel.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