1# Copyright (C) 2018-2021 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# Check that "gdb -batch" exits with appropriate status.
17
18standard_testfile
19
20set good_commands "$srcdir/$subdir/batch-exit-status.good-commands"
21set bad_commands "$srcdir/$subdir/batch-exit-status.bad-commands"
22
23proc _test_exit_status {expect_status cmdline_opts} {
24    global gdb_spawn_id
25
26    gdb_exit
27    if {[gdb_spawn_with_cmdline_opts $cmdline_opts] != 0} {
28	fail "spawn"
29	return
30    }
31
32    gdb_test_multiple "" "run til exit" {
33	eof {
34	    set result [wait -i $gdb_spawn_id]
35	    verbose $result
36
37	    gdb_assert { [lindex $result 2] == 0 }
38	    gdb_assert { [lindex $result 3] == $expect_status }
39
40	    remote_close host
41	    clear_gdb_spawn_id
42	}
43    }
44}
45
46proc test_exit_status {expect_status cmdline_opts prefix} {
47    if { $prefix == "" } {
48	set prefix $cmdline_opts
49    }
50
51    with_test_prefix $prefix {
52	_test_exit_status $expect_status $cmdline_opts
53    }
54}
55
56# gdb -batch with nothing to do should exit 0.
57test_exit_status 0 "-batch" ""
58
59# Bad command-line options should cause exit 1.
60test_exit_status 1 "-batch -jslkflsdjlkfjlksdjf" ""
61
62# gdb -batch with good commands should exit 0.
63test_exit_status 0 "-batch -ex \"info source\"" ""
64test_exit_status 0 "-batch -x $good_commands" "-batch -x good-commands"
65
66# gdb -batch with bad commands should exit 1.
67test_exit_status 1 "-batch -ex \"set not-a-thing 4\"" ""
68test_exit_status 1 "-batch -x $bad_commands" "-batch -x bad-commands"
69
70# Success or failure of the last thing determines the exit code.
71test_exit_status 0 "-batch -ex \"set not-a-thing 4\" -x $good_commands" \
72    "-batch -ex \"set not-a-thing 4\" -x good-commands"
73test_exit_status 0 "-batch -x $bad_commands -ex \"info source\"" \
74    "-batch -x bad-commands -ex \"info source\""
75test_exit_status 1 "-batch -x $good_commands -x $bad_commands" \
76    "-batch -x good-commands -x bad-commands"
77test_exit_status 1 "-batch -x $good_commands -ex \"set not-a-thing 4\"" \
78    "-batch -x good-commands -ex \"set not-a-thing 4\""
79
80set no_such_re ": No such file or directory\\."
81test_exit_status 1 "-batch \"\"" $no_such_re
82test_exit_status 1 "-batch \"\" \"\"" [multi_line $no_such_re $no_such_re]
83