xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/dprintf-detach.exp (revision a45db23f655e22f0c2354600d3b3c2cb98abf2dc)
1#   Copyright 2014-2020 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# This test checks that inserting a dprintf and detaching does not crash
17# the program.
18#
19# Related bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17012
20
21load_lib gdbserver-support.exp
22
23# The test relies on "detach/attach".
24if { [use_gdb_stub] } then {
25    return 0
26}
27
28standard_testfile
29set escapedbinfile [string_to_regexp ${binfile}]
30
31if [prepare_for_testing "failed to prepare for dprintf-detach" \
32    ${testfile} ${srcfile} {debug}] {
33    return -1
34}
35
36proc dprintf_detach_test { breakpoint_always_inserted dprintf_style disconnected_dprintf } {
37    set test_prefix "bai=${breakpoint_always_inserted} ds=${dprintf_style} dd=${disconnected_dprintf}"
38    global binfile decimal gdb_prompt escapedbinfile
39
40    with_test_prefix "$test_prefix" {
41	# Start with a clean gdb
42	clean_restart ${binfile}
43
44	gdb_test_no_output "set breakpoint always-inserted ${breakpoint_always_inserted}"
45	gdb_test_no_output "set dprintf-style ${dprintf_style}"
46	gdb_test_no_output "set disconnected-dprintf ${disconnected_dprintf}"
47
48	if ![runto_main] {
49	    fail "can't run to main"
50	    return -1
51	}
52
53	# Get PID of test program.
54	set inferior_pid -1
55	set test "get inferior process ID"
56	gdb_test_multiple "call (int) getpid ()" $test {
57	    -re ".* = ($decimal).*$gdb_prompt $" {
58		set inferior_pid $expect_out(1,string)
59		pass $test
60	    }
61	}
62
63	if {$inferior_pid == -1} {
64	    return
65	}
66
67	# Add a dprintf and detach.
68	gdb_test "dprintf function, \"hello\"" "Dprintf .*" "dprintf insertion"
69	gdb_test "detach" "Detaching from program: .*$escapedbinfile, .*" "detach program"
70
71	gdb_exit
72
73	# Check that the process still exists by attaching a new gdb to it.
74	clean_restart ${binfile}
75	set test "re-attach to inferior"
76	set is_gdbserver [target_is_gdbserver]
77
78	if { $is_gdbserver == 1 } {
79		setup_kfail "*-*-*" "server/17302"
80	} else {
81		# Give some time for the ex-inferior to run and hopefully not crash.
82		sleep 1
83	}
84
85	gdb_test "attach $inferior_pid" "Attaching to program: $escapedbinfile, process $inferior_pid.*Reading symbols from.*" "$test"
86    }
87}
88
89foreach breakpoint_always_inserted { "on" "off" } {
90    foreach dprintf_style { "gdb" "call" "agent" } {
91	foreach disconnected_dprintf { "on" "off" } {
92	    dprintf_detach_test $breakpoint_always_inserted $dprintf_style $disconnected_dprintf
93	}
94    }
95}
96