1 # Copyright (C) 2011-2015 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.
21 # We need to use TCL's exec to get the pid.
22 if [is_remote target] then {
26 load_lib gdb-python.exp
29 # Start with a fresh gdb.
33 gdb_reinitialize_dir $srcdir/$subdir
35 # Skip all tests if Python scripting is not enabled.
36 if { [skip_python_tests] } { continue }
39 if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
44 set saved_gdbflags $GDBFLAGS
45 set GDBFLAGS [concat $GDBFLAGS " -ex \"set height 0\""]
46 set GDBFLAGS [concat $GDBFLAGS " -ex \"set width 0\""]
47 set GDBFLAGS [concat $GDBFLAGS " -ex \"python p = list()\""]
48 set prompt_func "python def foo(x): global p; p.append(x); return \'(Foo) \'"
49 set GDBFLAGS [concat $GDBFLAGS " -ex \"$prompt_func\""]
50 set GDBFLAGS [concat $GDBFLAGS " -ex \"python gdb.prompt_hook=foo\""]
52 set tmp_gdbflags $GDBFLAGS
53 set gdb_prompt_fail $gdb_prompt
56 # Does not include the space at the end of the prompt.
57 # gdb_test expects it not to be there.
58 set gdb_prompt "\[(\]Foo\[)\]"
60 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing on\""]
62 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
63 ".*prompt is \"$gdb_prompt \".*" \
64 "show prompt gets the correct result"
65 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
66 "retrieving the prompt causes no extra prompt_hook calls"
67 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
68 "prompt_hook argument is default prompt."
72 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing off\""]
74 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
75 ".*prompt is \"$gdb_prompt \".*" \
76 "show prompt gets the correct result 2"
77 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
78 "retrieving the prompt causes no extra prompt_hook calls 2"
79 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
80 "prompt_hook argument is default prompt. 2"
83 set testpid [spawn_wait_for_attach $binfile]
85 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set pagination off\""]
86 set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
87 set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
88 set GDBFLAGS [concat $GDBFLAGS " -ex \"continue&\""]
90 # sync_execution = 1 is_running = 1
92 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
93 ".*prompt is \"$gdb_prompt \".*" \
94 "show prompt gets the correct result 3"
95 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
96 "retrieving the prompt causes no extra prompt_hook calls 3"
97 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
98 "prompt_hook argument is default prompt. 3"
101 set GDBFLAGS [concat $tmp_gdbflags " -ex \"set pagination off\""]
102 set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
103 set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
104 set GDBFLAGS [concat $GDBFLAGS " -ex \"interrupt\""]
106 # sync_execution = 1 is_running = 0
108 gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
109 ".*prompt is \"$gdb_prompt \".*" \
110 "show prompt gets the correct result 4"
111 gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
112 "retrieving the prompt causes no extra prompt_hook calls 4"
113 gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
114 "prompt_hook argument is default prompt. 4"
117 set GDBFLAGS $saved_gdbflags
118 exec kill -9 ${testpid}