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