aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/py-prompt.exp
blob: 262f3885e19ea0a9b81a3de7f2a3a15ecd8ade4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2011-2013 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the GDB testsuite.  It tests the mechanism
# for defining the prompt in Python.

standard_testfile

# We need to use TCL's exec to get the pid.
if [is_remote target] then {
    return 0
}

load_lib gdb-python.exp
load_lib prompt.exp

# Start with a fresh gdb.

gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir

# Skip all tests if Python scripting is not enabled.
if { [skip_python_tests] } { continue }
gdb_exit

if {[build_executable $testfile.exp $testfile $srcfile debug] == -1} {
    return -1
}

global GDBFLAGS
set saved_gdbflags $GDBFLAGS
set GDBFLAGS [concat $GDBFLAGS " -ex \"set height 0\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"set width 0\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"python p = list()\""]
set prompt_func "python def foo(x): global p; p.append(x);  return \'(Foo) \'"
set GDBFLAGS [concat $GDBFLAGS " -ex \"$prompt_func\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"python gdb.prompt_hook=foo\""]

set tmp_gdbflags $GDBFLAGS
set gdb_prompt_fail $gdb_prompt

global gdb_prompt
# Does not include the space at the end of the prompt.
# gdb_test expects it not to be there.
set gdb_prompt "\[(\]Foo\[)\]"

set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing on\""]
prompt_gdb_start
gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
	 ".*prompt is \"$gdb_prompt \".*" \
	 "show prompt gets the correct result"
gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
	 "retrieving the prompt causes no extra prompt_hook calls"
gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
	 "prompt_hook argument is default prompt."
gdb_exit


set GDBFLAGS [concat $tmp_gdbflags " -ex \"set editing off\""]
prompt_gdb_start
gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
	 ".*prompt is \"$gdb_prompt \".*" \
	 "show prompt gets the correct result 2"
gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
	 "retrieving the prompt causes no extra prompt_hook calls 2"
gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
	 "prompt_hook argument is default prompt. 2"
gdb_exit

# Start the program running and then wait for a bit, to be sure
# that it can be attached to.
set testpid [eval exec $binfile &]
exec sleep 2
if { [istarget "*-*-cygwin*"] } {
    # testpid is the Cygwin PID, GDB uses the Windows PID, which might be
    # different due to the way fork/exec works.
    set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
}

set GDBFLAGS [concat $tmp_gdbflags " -ex \"set target-async on\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"set pagination off\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"continue&\""]

# sync_execution = 1 is_running = 1
prompt_gdb_start
gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
	 ".*prompt is \"$gdb_prompt \".*" \
	 "show prompt gets the correct result 3"
gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
	 "retrieving the prompt causes no extra prompt_hook calls 3"
gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
	 "prompt_hook argument is default prompt. 3"
gdb_exit

set GDBFLAGS [concat $tmp_gdbflags " -ex \"set target-async on\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"set pagination off\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"set editing on\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"attach $testpid\""]
set GDBFLAGS [concat $GDBFLAGS " -ex \"interrupt\""]

# sync_execution = 1 is_running = 0
prompt_gdb_start
gdb_test "python x = len(p); print (gdb.execute(\"show prompt\", to_string = True))" \
	 ".*prompt is \"$gdb_prompt \".*" \
	 "show prompt gets the correct result 4"
gdb_test "python print ('%d %d' % (x, len(p)))" "1 2" \
	 "retrieving the prompt causes no extra prompt_hook calls 4"
gdb_test "python print (\"'\" + str(p\[0\]) + \"'\")" "'$gdb_prompt_fail '" \
	 "prompt_hook argument is default prompt. 4"
gdb_exit

set GDBFLAGS $saved_gdbflags
return 0