[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / quit-live.exp
blob3199d5f706ce9a3bcd0a92515990e5d51715e77d
1 # Copyright (C) 2017-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test quitting GDB with live inferiors.
18 # Exercises combinations of:
20 # - quitting with "quit" command, or with SIGTERM/SIGHUP signals.
22 # - quitting with live inferior selected, or file_stratum inferior
23 #   selected.
25 # - quitting after "run", or after "attach".
27 # - quitting with local executable, or executable loaded from target
28 #   directly (via default "target:/" sysroot), or with no executable
29 #   loaded.
31 # Note: sending an asynchronous SIGHUP with kill is not the exact same
32 # as closing GDB's input, and that resulting in SIGHUP.  However, it's
33 # still a good approximation, and it has the advantage that because
34 # GDB still has a terminal, internal errors (if any) are visible in
35 # gdb.sum/gdb.log.
37 standard_testfile
39 if {[build_executable "failed to build" $testfile $srcfile debug]} {
40     return
43 # Send signal SIG to GDB, and expect GDB to exit.
45 proc test_quit_with_sig {sig} {
46     set gdb_pid [exp_pid -i [board_info host fileid]]
47     remote_exec host "kill -$sig ${gdb_pid}"
49     set test "quit with SIG$sig"
50     # If GDB mishandles the signal and doesn't exit, this should FAIL
51     # with timeout.  We don't expect a GDB prompt, so if we see one,
52     # we'll FAIL too (without having to wait for timeout).
53     gdb_test_multiple "" $test {
54         eof {
55             pass $test
56         }
57     }
60 # Call the "quit" command with an inferior live.
62 # APPEAR_HOW specifies how the running inferior appears in GDB.  Can
63 # be either:
65 # - "run"
67 #    Appear via the "run" command.
69 # - "attach"
71 #    Appear via the "attach" command.
73 # - "attach-nofile"
75 #    Appear via the "attach" command, but with no program preloaded in
76 #    GDB so that GDB reads the program directly from the target when
77 #    remote debugging (i.e., from the target:/ sysroot).  This makes
78 #    sure that GDB doesn't misbehave if it decides to close the
79 #    'target:/.../program' exec_file after closing the remote
80 #    connection.
82 # EXTRA_INFERIOR is a boolean that specifies whether we try to quit
83 # GDB with an extra executable-only (before "run") inferior selected
84 # or whether we try to quit GDB when the live inferior is selected,
85 # with no extra inferior.
87 # QUIT_HOW specifies how to tell GDB to quit.  It can be either "quit"
88 # (for "quit" command), "sighup" or "sigterm" (for quitting with
89 # SIGHUP and SIGTERM signals, respectively).
91 proc quit_with_live_inferior {appear_how extra_inferior quit_how} {
92     global srcfile testfile binfile
93     global gdb_spawn_id gdb_prompt
95     set test_spawn_id ""
97     if {$appear_how != "attach-nofile"} {
98         clean_restart $binfile
99     } else {
100         clean_restart
101     }
103     if {$appear_how == "run"} {
104         if {![runto_main]} {
105             return
106         }
107     } elseif {$appear_how == "attach" || $appear_how == "attach-nofile"} {
108         set test_spawn_id [spawn_wait_for_attach $binfile]
109         set testpid [spawn_id_get_pid $test_spawn_id]
111         if {[gdb_test "attach $testpid" \
112                  "Attaching to .*process $testpid.*Reading symbols from.*" \
113                  "attach"] != 0} {
114             kill_wait_spawned_process $test_spawn_id
115             return
116         }
117     } else {
118         error "unhandled '\$appear_how': $appear_how"
119     }
121     if {$extra_inferior} {
122         gdb_test "add-inferior" "Added inferior 2 on connection .*" \
123             "add empty inferior 2"
124         gdb_test "inferior 2" "Switching to inferior 2.*" \
125             "switch to inferior 2"
126     }
128     if {$quit_how == "quit"} {
129         # Make regexp that matches the "quit" command's output.
130         proc make_re {how} {
131             multi_line \
132                 "A debugging session is active.\[ \t\r\n\]*Inferior 1\[^\r\n\]* will be $how\." \
133                 "" \
134                 "Quit anyway\\? \\(y or n\\) $"
135         }
137         if {$appear_how == "run"} {
138             set quit_anyway_re [make_re "killed"]
139         } else {
140             set quit_anyway_re [make_re "detached"]
141         }
143         set test "quit with \"quit\""
144         gdb_test_multiple "quit" $test {
145             -re $quit_anyway_re {
146                 send_gdb "y\n"
147                 gdb_test_multiple "" $test {
148                     eof {
149                         pass $test
150                     }
151                 }
152             }
153         }
154     } elseif {$quit_how == "sighup"} {
155         test_quit_with_sig HUP
156     } elseif {$quit_how == "sigterm"} {
157         test_quit_with_sig TERM
158     } else {
159         error "unhandled '\$quit_how': $quit_how"
160     }
162     if {$test_spawn_id != ""} {
163         kill_wait_spawned_process $test_spawn_id
164     }
167 foreach_with_prefix appear_how {"run" "attach" "attach-nofile"} {
168     if {$appear_how != "run" && ![can_spawn_for_attach]} {
169         continue
170     }
172     foreach_with_prefix extra_inferior {0 1} {
173         foreach_with_prefix quit_how {"quit" "sigterm" "sighup"} {
174             quit_with_live_inferior $appear_how $extra_inferior $quit_how
175         }
176     }