1# Copyright 2021-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# Test that commands in a GDB script file run via GDB's -x flag work
17# as expected.  Specifically, the script creates a dprintf breakpoint
18# as well as a normal breakpoint that has "continue" in its command
19# list, and then does "run".  Correct output from GDB is checked as
20# part of this test.
21
22# Bail out if the target can't use the 'run' command.
23if ![target_can_use_run_cmd] {
24    return 0
25}
26
27standard_testfile
28
29if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
30    return -1
31}
32
33# This is the name of the GDB script to load.
34set x_file ${srcdir}/${subdir}/$testfile.gdb
35
36# Create context in which the global, GDBFLAGS, will be restored at
37# the end of the block.  All commands run within the block are
38# actually run in the outer context.  (This is why 'res' is available
39# outside of the save_vars block.)
40save_vars { GDBFLAGS } {
41    # Set flags with which to start GDB.
42    append GDBFLAGS " -ex \"set height unlimited\""
43    append GDBFLAGS " -x \"$x_file\""
44    append GDBFLAGS " --args \"$binfile\""
45
46    # Start GDB with above flags.
47    set res [gdb_spawn]
48}
49
50set test "load and run script with -x"
51if { $res != 0} {
52    fail $test
53    return -1
54}
55
56# The script loaded via -x contains a run command; while running, GDB
57# is expected to print three messages from dprintf breakpoints along
58# with three interspersed messages from an ordinary breakpoint (which
59# was set up with a continue command).  Set up pattern D to match
60# output from hitting the dprintf breakpoint and B for the ordinary
61# breakpoint.  Then set PAT to contain the entire pattern of expected
62# output from the interspersed dprintf and ordinary breakpoints along
63# with some (additional) expected output from the dprintf breakpoints,
64# i.e. 0, 1, and 2.
65set d "dprintf in increment.., vi="
66set b "Breakpoint ., inc_vi"
67set pat "${d}0.*?$b.*?${d}1.*?$b.*?${d}2.*?$b.*?"
68
69proc do_test {cmd test} {
70    gdb_test_multiple $cmd $test {
71	-re "$::pat$::inferior_exited_re normally.*$::gdb_prompt $" {
72	    pass $test
73	}
74	-re "Don't know how to run.*$::gdb_prompt $" {
75	    # Even though we bailed out at the beginning of this test case
76	    # for targets which can't use the "run" command, there are
77	    # still targets, e.g. native-extended-gdbserver, which can
78	    # run, but will still print the "Don't know how to run"
79	    # message.  In the case of native-extended-gdbserver, it would
80	    # first need to connect to the target in order to run.  For
81	    # that particular target, the very first test which attempts
82	    # to use the "run" command from a command line script is
83	    # the one that is unsupported.  The other two tests will
84	    # pass because, after reaching the (gdb) prompt, a gdbserver
85	    # is spawned and then connected to.  (The command line which
86	    # spawns GDB for this target has a "-iex set
87	    # auto-connect-native-target off" which prevents it from
88	    # attempting to "run" using the native target.)
89	    unsupported $test
90	}
91    }
92}
93
94# Check output from running script with -x
95do_test "" $test
96
97# Restart GDB and 'source' the script; this will (still) run the program
98# due to the 'run' command in the script.
99clean_restart $binfile
100do_test "source $x_file" "load and run script using source command"
101
102# This should leave us at the gdb prompt; Run program again using
103# already established breakpoints, i.e. those loaded from the
104# script.  Prior to fixing PR 28308, this was the only test that
105# would pass.
106do_test "run" "run again"
107