1# This testcase is part of GDB, the GNU debugger.
2#
3# Copyright 2013-2020 Free Software Foundation, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Check that GDB handles GDBserver disconnecting abruptly, in several
19# scenarios.
20
21load_lib gdbserver-support.exp
22
23standard_testfile
24
25if {[skip_gdbserver_tests]} {
26    return 0
27}
28
29if { [build_executable "failed to prepare" ${testfile}] } {
30    return -1
31}
32
33# Spawn GDBserver, run to main, extract GDBserver's PID and save it in
34# the SERVER_PID global.
35
36proc prepare {} {
37    global binfile gdb_prompt srcfile decimal
38    global server_pid
39
40    clean_restart $binfile
41
42    # Make sure we're disconnected, in case we're testing with an
43    # extended-remote board, therefore already connected.
44    gdb_test "disconnect" ".*"
45
46    gdbserver_run ""
47
48    # Continue past server_pid assignment.
49    gdb_breakpoint ${srcfile}:[gdb_get_line_number "i = 0;"]
50    gdb_continue_to_breakpoint "after server_pid assignment"
51
52    # Get the pid of GDBServer.
53    set test "p server_pid"
54    set server_pid 0
55    gdb_test_multiple $test $test {
56	-re " = ($decimal)\r\n$gdb_prompt $" {
57	    set server_pid $expect_out(1,string)
58	    pass $test
59	}
60    }
61
62    if {$server_pid == 0} {
63	return 0
64    }
65
66    return 1
67}
68
69# Kill GDBserver using the PID saved by prepare.
70
71proc kill_server {} {
72    global server_pid
73
74    remote_exec target "kill -9 $server_pid"
75}
76
77# Test issuing "tstatus" right after the connection is dropped.
78
79proc_with_prefix test_tstatus {} {
80    if ![prepare] {
81	return
82    }
83
84    kill_server
85
86    # Enable trace status packet which is disabled after the
87    # connection if the remote target doesn't support tracepoint at
88    # all.  Otherwise, no RSP packet is sent out.
89    gdb_test_no_output "set remote trace-status-packet on"
90
91    # Force GDB to talk with GDBserver, so that we can get the
92    # "connection closed" error.
93    gdb_test "tstatus" {Remote connection closed|Remote communication error\.  Target disconnected\.: Connection reset by peer\.}
94}
95
96# Test unwinding with no debug/unwind info, right after the connection
97# is dropped.
98
99proc_with_prefix test_unwind_nosyms {} {
100    if ![prepare] {
101	return
102    }
103
104    # Remove symbols, so that we try to unwind with one of the
105    # heuristic unwinders, and read memory from within its sniffer.
106    gdb_unload
107
108    kill_server
109
110    gdb_test "bt" "(Target disconnected|Remote connection closed|Remote communication error).*"
111}
112
113# Test unwinding with debug/unwind info, right after the connection is
114# dropped.
115
116proc_with_prefix test_unwind_syms {} {
117    if ![prepare] {
118	return
119    }
120
121    kill_server
122
123    gdb_test "bt" "(Target disconnected|Remote connection closed|Remote communication error).*"
124}
125
126test_tstatus
127test_unwind_nosyms
128test_unwind_syms
129