1# Copyright 2014-2023 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]} { 25 return 0 26} 27 28standard_testfile 29set escapedbinfile [string_to_regexp ${binfile}] 30 31if [build_executable "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 return -1 50 } 51 52 # Get PID of test program. 53 set inferior_pid -1 54 set test "get inferior process ID" 55 gdb_test_multiple "call ((int (*) (void)) getpid) ()" $test { 56 -re ".* = ($decimal).*$gdb_prompt $" { 57 set inferior_pid $expect_out(1,string) 58 pass $test 59 } 60 } 61 62 if {$inferior_pid == -1} { 63 return 64 } 65 66 # Add a dprintf and detach. 67 gdb_test "dprintf function, \"hello\"" "Dprintf .*" "dprintf insertion" 68 gdb_test "detach" "Detaching from program: .*$escapedbinfile, .*" "detach program" 69 70 gdb_exit 71 72 # Check that the process still exists by attaching a new gdb to it. 73 clean_restart ${binfile} 74 set test "re-attach to inferior" 75 set is_gdbserver [target_is_gdbserver] 76 77 if { $is_gdbserver == 1 } { 78 setup_kfail "*-*-*" "server/17302" 79 } else { 80 # Give some time for the ex-inferior to run and hopefully not crash. 81 sleep 1 82 } 83 84 gdb_test "attach $inferior_pid" "Attaching to program: $escapedbinfile, process $inferior_pid.*Reading symbols from.*" "$test" 85 } 86} 87 88foreach breakpoint_always_inserted { "on" "off" } { 89 foreach dprintf_style { "gdb" "call" "agent" } { 90 foreach disconnected_dprintf { "on" "off" } { 91 dprintf_detach_test $breakpoint_always_inserted $dprintf_style $disconnected_dprintf 92 } 93 } 94} 95