1# Copyright 2017-2024 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# When debugging with "target remote |", the inferior's output is
17# connected to a pipe, and if GDB doesn't flush the pipe while the
18# inferior is running and the pipe becomes full, then the inferior
19# deadlocks:
20#
21#  1. User sets breakpoint, and types "continue"
22#
23#  2. Inferior prints to stdout/stderr before reaching breakpoint
24#     location.
25#
26#  3. The output pipe becomes full, so the inferior blocks forever in
27#     the printf/write call.
28#
29#  4. The breakpoint is never reached.
30
31require {!target_info exists gdb,noinferiorio}
32
33standard_testfile
34
35if [prepare_for_testing "failed to prepare" $testfile {} {debug}] {
36    return -1
37}
38
39if {![runto_main]} {
40    return
41}
42
43set printing_done_line [gdb_get_line_number "printing done"]
44gdb_test "break $printing_done_line" ".*" "set breakpoint after printing"
45
46send_gdb "continue\n"
47
48set expected_lines 3000
49set more 1
50set i 0
51while {$more} {
52    set more 0
53    gdb_expect {
54	-i $inferior_spawn_id
55	-ex "this is line number $i" {
56	    incr i
57           if {$i != $expected_lines} {
58		set more 1
59	    }
60	}
61    }
62}
63
64gdb_assert {$i == $expected_lines} "saw all lines"
65
66set test "breakpoint reached"
67gdb_test_multiple "" $test {
68    -re "Breakpoint .* main .*$srcfile:$printing_done_line.*$gdb_prompt $" {
69	pass $test
70    }
71}
72