* [PATCH v2 1/2] gdb: delay the new inferior notification
2026-11-25 13:08 [PATCH v2 0/2] disable pagination for new inferior notification Stephan Rohr
@ 2026-11-25 13:08 ` Stephan Rohr
2026-11-25 13:08 ` [PATCH v2 2/2] gdb: disable pagination while following a fork Stephan Rohr
1 sibling, 0 replies; 3+ messages in thread
From: Stephan Rohr @ 2026-11-25 13:08 UTC (permalink / raw)
To: gdb-patches
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.
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] gdb: disable pagination while following a fork
2026-11-25 13:08 [PATCH v2 0/2] disable pagination for new inferior notification Stephan Rohr
2026-11-25 13:08 ` [PATCH v2 1/2] gdb: delay the " Stephan Rohr
@ 2026-11-25 13:08 ` Stephan Rohr
1 sibling, 0 replies; 3+ messages in thread
From: Stephan Rohr @ 2026-11-25 13:08 UTC (permalink / raw)
To: gdb-patches
With "set detach-on-fork off", continueing from a fork catchpoint
follows the fork. Pagination is enabled. If the new inferior
notification triggers pagination and the user answers "q", the new
inferior is not fully set up. It is missing the target and threads:
(gdb) set height 1
(gdb) set width 10
(gdb) continue
Continuing.
[New infer--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(gdb) inferior 2
Inferior has no threads.
Temporarily disable pagination in "follow_fork ()".
---
gdb/infrun.c | 6 ++
.../gdb.base/fork-inferior-paginate.exp | 83 +++++++++++++++++++
2 files changed, 89 insertions(+)
create mode 100644 gdb/testsuite/gdb.base/fork-inferior-paginate.exp
diff --git a/gdb/infrun.c b/gdb/infrun.c
index dced78a3963..b8864aabd76 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -812,6 +812,12 @@ follow_fork ()
{
INFRUN_SCOPED_DEBUG_ENTER_EXIT;
+ /* Temporarily disable pagination. Otherwise, the user could answer
+ 'q' at the pager prompt and abort following the fork, i.e., the new
+ inferior has no target and threads. */
+ scoped_restore save_pagination
+ = make_scoped_restore (&pagination_enabled, false);
+
bool follow_child = (follow_fork_mode_string == follow_fork_mode_child);
bool should_resume = true;
diff --git a/gdb/testsuite/gdb.base/fork-inferior-paginate.exp b/gdb/testsuite/gdb.base/fork-inferior-paginate.exp
new file mode 100644
index 00000000000..c394e5f8977
--- /dev/null
+++ b/gdb/testsuite/gdb.base/fork-inferior-paginate.exp
@@ -0,0 +1,83 @@
+# 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/>.
+
+# Test that there is no pagination if a new inferior is setup when following
+# a fork. "Continue" may still paginate but should not leave GDB incompletely
+# setup.
+
+require allow_fork_tests
+
+standard_testfile fork-print-inferior-events.c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
+ return
+}
+
+set bp_line [gdb_get_line_number "return 0;"]
+
+proc test_paginate { follow_fork_mode } {
+ global gdb_prompt pagination_prompt
+
+ with_test_prefix "follow-fork-mode=$follow_fork_mode" {
+ clean_restart $::testfile
+
+ if {![runto_main]} {
+ return
+ }
+
+ gdb_test_no_output "set detach-on-fork off"
+ gdb_test_no_output "set follow-fork-mode $follow_fork_mode"
+ gdb_test "catch fork" "Catchpoint $::decimal \\(fork\\)"
+ gdb_test "continue" \
+ "Catchpoint $::decimal \\(forked process $::decimal\\).*"
+ gdb_breakpoint $::srcfile:$::bp_line
+
+ # Avoid unexpected output from readline.
+ gdb_test_no_output "set editing off"
+
+ # Following the fork shall not produce any pagination. "Continue"
+ # still may paginate, though.
+ set saw_fork_pagination 0
+ with_set height 1 {
+ with_set width 10 {
+ gdb_test_multiple "continue" "no pagination in follow fork" {
+ -re "(New infer|Attachin)\[^\r\n\]*$pagination_prompt$" {
+ set saw_fork_pagination 1
+ send_gdb "q\n"
+ exp_continue
+ }
+ -re "$pagination_prompt$" {
+ send_gdb "\n"
+ exp_continue
+ }
+ -re "\r\n$gdb_prompt $" {
+ gdb_assert { !$saw_fork_pagination } $gdb_test_name
+ }
+ }
+ }
+ }
+
+ # Check inferior 2 is properly set up.
+ gdb_test "info inferiors" \
+ "\r\n\\*?\\s+2\\s+process $::decimal\\s+$::decimal \\(\[^\r\n\]*"
+ gdb_test "inferior 2" \
+ "\\\[Switching to thread 2\\.1 \[^\r\n\]*\\\].*" \
+ "switch to inferior 2"
+ }
+}
+
+foreach follow_fork_mode {parent child} {
+ test_paginate $follow_fork_mode
+}
--
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.
^ permalink raw reply [flat|nested] 3+ messages in thread