Update copyright year range in all GDB files
[binutils-gdb.git] / gdb / testsuite / gdb.python / py-prompt.exp
blob6eed7159b37b1c363548c10b7868657b91435af7
1 # Copyright (C) 2011-2021 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 # This file is part of the GDB testsuite.  It tests the mechanism
17 # for defining the prompt in Python.
19 standard_testfile
21 load_lib gdb-python.exp
22 load_lib prompt.exp
24 # Start with a fresh gdb.
26 gdb_exit
27 gdb_start
28 gdb_reinitialize_dir $srcdir/$subdir
30 # Skip all tests if Python scripting is not enabled.
31 if { [skip_python_tests] } { continue }
32 gdb_exit
34 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
35     return -1
38 global GDBFLAGS
39 set saved_gdbflags $GDBFLAGS
40 set GDBFLAGS [concat $GDBFLAGS " -ex \"set height 0\""]
41 set GDBFLAGS [concat $GDBFLAGS " -ex \"set width 0\""]
42 set GDBFLAGS [concat $GDBFLAGS " -ex \"python p = list()\""]
43 set prompt_func "python def foo(x): global p; p.append(x);  return \'(Foo) \'"
44 set GDBFLAGS [concat $GDBFLAGS " -ex \"$prompt_func\""]
45 set GDBFLAGS [concat $GDBFLAGS " -ex \"python gdb.prompt_hook=foo\""]
47 set tmp_gdbflags $GDBFLAGS
48 set gdb_prompt_fail $gdb_prompt
50 global gdb_prompt
51 # Does not include the space at the end of the prompt.
52 # gdb_test expects it not to be there.
53 set gdb_prompt "\[(\]Foo\[)\]"
55 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing on\""]
56 prompt_gdb_start
57 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
58          ".*prompt is \"$gdb_prompt \".*" \
59          "show prompt gets the correct result"
60 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
61          "retrieving the prompt causes no extra prompt_hook calls"
62 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
63          "prompt_hook argument is default prompt."
64 gdb_exit
67 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing off\""]
68 prompt_gdb_start
69 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
70          ".*prompt is \"$gdb_prompt \".*" \
71          "show prompt gets the correct result 2"
72 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
73          "retrieving the prompt causes no extra prompt_hook calls 2"
74 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
75          "prompt_hook argument is default prompt. 2"
76 gdb_exit
78 if {![can_spawn_for_attach]} {
79     set GDBFLAGS $saved_gdbflags
80     return 0
83 set test_spawn_id [spawn_wait_for_attach $binfile]
84 set testpid [spawn_id_get_pid $test_spawn_id]
86 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set pagination off\""]
87 set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
88 set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
89 set GDBFLAGS [concat $GDBFLAGS " -ex \"continue&\""]
91 # sync_execution = 1 is_running = 1
92 prompt_gdb_start
93 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
94          ".*prompt is \"$gdb_prompt \".*" \
95          "show prompt gets the correct result 3"
96 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
97          "retrieving the prompt causes no extra prompt_hook calls 3"
98 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
99          "prompt_hook argument is default prompt. 3"
100 gdb_exit
102 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set pagination off\""]
103 set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
104 set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
105 set GDBFLAGS [concat $GDBFLAGS " -ex \"interrupt\""]
107 # sync_execution = 1 is_running = 0
108 prompt_gdb_start
109 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
110          ".*prompt is \"$gdb_prompt \".*" \
111          "show prompt gets the correct result 4"
112 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
113          "retrieving the prompt causes no extra prompt_hook calls 4"
114 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
115          "prompt_hook argument is default prompt. 4"
116 gdb_exit
118 set GDBFLAGS $saved_gdbflags
119 kill_wait_spawned_process $test_spawn_id
120 return 0