[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / killed-outside.exp
1 # Copyright 2015-2018 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 # Test that GDB doesn't get badly wedged if the inferior is killed
17 # from outside GDB (with SIGKILL) while the program is stopped.
18
19 standard_testfile
20
21 # Get the value of variable VAR in the inferior.  MSG is used as the
22 # test message.
23
24 proc get_value {var msg} {
25     global expect_out
26     global gdb_prompt
27     global decimal
28
29     set value -1
30     gdb_test_multiple "print $var" "$msg" {
31         -re ".*= ($decimal).*\r\n$gdb_prompt $" {
32             set value $expect_out(1,string)
33             pass "$msg"
34         }
35     }
36     return ${value}
37 }
38
39 # Runs the program until a breakpoint, deletes all breakpoints, and
40 # then kills the inferior from _outside_ GDB, with SIGKILL.  Runs CMDS
41 # afterwards, to make sure GDB copes with the inferior disappearing,
42 # and then quits GDB.
43
44 proc test {cmds_after_kill} {
45     global binfile
46     global gdb_prompt
47     global decimal
48
49     clean_restart ${binfile}
50
51     if ![runto done] {
52         return
53     }
54
55     # So that "continue" doesn't try a step over, etc.
56     delete_breakpoints
57
58     set testpid [get_value "pid" "get pid of inferior"]
59     if { $testpid == -1 } {
60         return -1
61     }
62
63     remote_exec target "kill -9 ${testpid}"
64
65     # Give it some time to die.
66     sleep 2
67
68     uplevel 1 $cmds_after_kill
69
70     # Make sure we can quit.
71     set msg "quit GDB"
72     gdb_test_multiple "quit" $msg {
73         -re "Quit anyway\\? \\(y or n\\) $" {
74             send_gdb "y\n"
75             exp_continue
76         }
77         eof {
78             pass $msg
79         }
80     }
81 }
82
83 if {[prepare_for_testing "failed to prepare" $testfile $srcfile] == -1} {
84     return -1
85 }
86
87 # The actual output GDB prints in response to commands after the
88 # inferior is gone isn't very well defined, and will depend on target.
89 # What we're trying to make sure is that GDB doesn't internal error or
90 # get wedged.
91
92 # Try simply continuing.
93 with_test_prefix "continue" {
94     test {
95         # Try stepping the program.  Stepping may need to read/write
96         # registers, unlike continue.
97         gdb_test "continue" ".*"
98
99         # Try listing threads afterwards.  It's probably what the user
100         # will do after an error.
101         gdb_test "info threads" ".*"
102     }
103 }
104
105 # Try stepping the program.  Stepping may go through diferent code
106 # paths in the target backends.
107 with_test_prefix "stepi" {
108     test {
109         gdb_test "si" ".*"
110         gdb_test "info threads" ".*"
111     }
112 }
113
114 # Try fetching registers explicitly, which should cover the error many
115 # other commands would trigger.
116 with_test_prefix "registers" {
117     test {
118         gdb_test "flushregs" ".*"
119         gdb_test "info threads" ".*"
120     }
121 }
122
123 # Try only listing threads explicitly, first thing, which is another
124 # operation GDB may or not decide to do itself and is likely to be
125 # what a user would try after error too.
126 with_test_prefix "info threads" {
127     test {
128         gdb_test "info threads" ".*"
129     }
130 }