aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.python/python.exp
blob: a0b80e0935eb45319311e42234c190d2ce888dd1 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Copyright (C) 2008-2015 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
# exposing values to Python.

load_lib gdb-python.exp

standard_testfile python.c python-1.c

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

# Start with a fresh gdb.
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir

set remote_source2_py [gdb_remote_download host \
			   ${srcdir}/${subdir}/source2.py]

# Do this instead of the skip_python_check.
# We want to do some tests when Python is not present.
gdb_test_multiple "python print (23)" "verify python support" {
    -re "not supported.*$gdb_prompt $"	{
      unsupported "python support is disabled"

      # If Python is not supported, verify that sourcing a python script
      # causes an error.
      gdb_test "source $remote_source2_py" \
	  "Error in sourced command file:.*" \
	  "source source2.py when python disabled"

      # Verify multi-line python commands cause an error.
      gdb_py_test_multiple "multi-line python command" \
	  "python" "" \
	  "print (23)" "" \
	  "end" "not supported.*"

      return -1
    }
    -re "$gdb_prompt $"	{}
}

gdb_py_test_multiple "multi-line python command" \
  "python" "" \
  "print (23)" "" \
  "end" "23"

# Spawn interactive Python help from a multi-line command, thus, after
# a secondary prompt.

with_test_prefix "python interactive help" {
    set test "python; help(); end"
    gdb_test_multiple "python\nhelp()\nend" $test {
	-re ".*help utility.*help> $" {
	    pass $test

	    # The "quit" must be seen on the output.  A buggy GDB
	    # would not display it.
	    gdb_test "quit" "^quit.*leaving help.*" "quit help"
	}
    }
}

gdb_py_test_multiple "show python command" \
  "define zzq" "Type commands for definition of .* just \"end\"\\.*" \
  "python" "" \
  "print (23)" "" \
  "end" "" \
  "end" "" \
  "show user zzq" "User command \"zzq\":.*  python.*print \\(23\\).*  end"

gdb_py_test_multiple "indented multi-line python command" \
  "python" "" \
  "def foo ():" "" \
  "  print ('hello, world!')" "" \
  "foo ()" "" \
  "end" "hello, world!"

gdb_test "source $remote_source2_py" "yes" "source source2.py"

gdb_test "source -s source2.py" "yes" "source -s source2.py"

gdb_test "python print (gdb.current_objfile())" "None"
gdb_test "python print (gdb.objfiles())" "\\\[\\\]"

# Test http://bugs.python.org/issue4434 workaround in configure.ac
gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so"

gdb_test_no_output \
    "python x = gdb.execute('printf \"%d\", 23', to_string = True)"
gdb_test "python print (x)" "23"

# Test post_event.
gdb_py_test_multiple "post event insertion" \
  "python" "" \
  "someVal = 0" "" \
  "class Foo(object):" "" \
  "  def __call__(self):" "" \
  "    global someVal" "" \
  "    someVal += 1" "" \
  "gdb.post_event(Foo())" "" \
  "end" ""

gdb_test "python print (someVal)" "1" "test post event execution"
gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class"

# Test (no) pagination of the executed command.
gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
set lines 10
gdb_test_no_output "set height $lines"

set test "verify pagination beforehand"
gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
    -re "---Type <return>" {
	exp_continue
    }
    -re " to continue, or q <return>" {
	exp_continue
    }
    -re " to quit---$" {
	pass $test
    }
}
gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q"

gdb_test "python if gdb.execute('python print (\"\\\\n\" * $lines)', to_string=True) == \"\\n\" * [expr $lines + 1]: print (\"yes\")" "yes" "gdb.execute does not page"

set test "verify pagination afterwards"
gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
    -re "---Type <return>" {
	exp_continue
    }
    -re " to continue, or q <return>" {
	exp_continue
    }
    -re " to quit---$" {
	pass $test
    }
}
gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q"

gdb_test_no_output "set height 0"

gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout"

gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout"

# Test PR 12212, using InfThread.selected_thread() when no inferior is
# loaded.
gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1
gdb_test "python print (nothread == None)" "True" "Ensure that no threads are returned"

gdb_py_test_multiple "register atexit function" \
    "python" "" \
    "import atexit" "" \
    "def printit(arg):" "" \
    "  print (arg)" "" \
    "atexit.register(printit, 'good bye world')" "" \
    "end" ""

send_gdb "quit\n"
gdb_expect {
    -re "good bye world" {
	pass "atexit handling"
    }
    default {
	fail "atexit handling"
    }
}

# Start with a fresh gdb.
clean_restart ${testfile}

# The following tests require execution.

if ![runto_main] then {
    fail "Can't run to main"
    return 0
}

set lineno [gdb_get_line_number "Break to end."]
runto $lineno

# Test gdb.decode_line.
gdb_test "python gdb.decode_line(\"main.c:43\")" \
    "gdb.error: No source file named main.c.*" "test decode_line no source named main"

gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1
gdb_test "python print (len(symtab))" "2" "Test decode_line current location"
gdb_test "python print (symtab\[0\])" "None" "Test decode_line expression parse"
gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line current location"

if { [is_remote host] } {
    set python_c [string_to_regexp "python.c"]
} else {
    set python_c [string_to_regexp "gdb.python/python.c"]
}
gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line current location filename"
gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "Test decode_line current location line number"

gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1
gdb_test "python print (len(symtab))" "2" "Test decode_line python.c:26 length"
gdb_test "python print (symtab\[0\])" "if foo" "Test decode_line expression parse"
gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line python.c:26 length"
gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line python.c:26 filename"
gdb_test "python print (symtab\[1\]\[0\].line)" "26" "Test decode_line python.c:26 line number"

gdb_test "python gdb.decode_line(\"randomfunc\")" \
    "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc"
gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1
gdb_test "python print (len(symtab))" "2" "Test decode_line func1 length"
gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line func1 length"

if { [is_remote host] } {
    set python_1_c [string_to_regexp "python-1.c"]
} else {
    set python_1_c [string_to_regexp "gdb.python/python-1.c"]
}
gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "Test decode_line func1 filename"
gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number"
gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \
    "test decode_line func1,func2" 1
gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec"

# gdb.write
gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "Test stderr location"
gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "Test stdout location"
gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write"
gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write"
gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write"
gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write"

# Turn on full stack printing for subsequent tests.
gdb_py_test_silent_cmd "set python print-stack full" \
    "Set print-stack full for prompt tests" 1

# Test prompt substituion

gdb_py_test_multiple "prompt substitution" \
  "python" "" \
  "someCounter = 0" "" \
  "def prompt(current):" "" \
  "   global someCounter" "" \
  "   if (current == \"testfake \"):" "" \
  "      return None" "" \
  "   someCounter = someCounter + 1" "" \
  "   return \"py prompt \" + str (someCounter) + \" \"" "" \
  "end" ""

gdb_py_test_multiple "prompt substitution readline" \
  "python" "" \
  "pCounter = 0" "" \
  "def program_prompt(current):" "" \
  "   global pCounter" "" \
  "   if (current == \">\"):" "" \
  "      pCounter = pCounter + 1" "" \
  "      return \"python line \" + str (pCounter) + \": \"" "" \
  "   return None" "" \
  "end" ""

set newprompt "py prompt 1"
set newprompt2 "py prompt 2"
set testfake "testfake"

gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" {
    -re "\[\r\n\]$newprompt $" {
	pass "set hook"
    }
}

gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" {
    -re "\[\r\n\]$testfake $" {
	pass "set prompt testfake"
    }
}

gdb_test_multiple "show prompt" "show testfake prompt" {
    -re "Gdb's prompt is \"$testfake \"..* $" {
	pass "show prompt shows guarded prompt"
    }
}

gdb_test_multiple "set prompt blah " "set blah in GDB" {
    -re "\[\r\n\]$newprompt2 $" {
	pass "set prompt blah overriden"
    }
}

gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
    -re "\[\r\n\]$newprompt2 $" {
	pass "Delete old hook"
    }
}

gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" {
    -re "\[\r\n\]$gdb_prompt $" {
	pass "set default prompt"
    }
}

set working_dir ""
gdb_test_multiple "pwd" "pwd" {
    -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" {
	set working_dir $expect_out(1,string)
    }
}

gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" {
    -re "\[\r\n\]$gdb_prompt $" {
	pass "set programming hook"
    }
}

gdb_test_multiple "python" "test we ignore substituion for seconday prompts" {
    -re "\r\n>$" {
	pass "readline secondary are not substituted"
    }
}

gdb_test_multiple "end" "end programming" {
    -re "\[\r\n\]$gdb_prompt $" {
	pass "end programming"
    }
}

gdb_py_test_multiple "prompt substitution readline" \
  "python" "" \
  "import gdb.command.prompt" "" \
  "end" ""

gdb_test_multiple "set extended-prompt one two three " \
    "set basic extended prompt" {
    -re "\[\r\n\]one two three $" {
	pass "set basic extended prompt"
    }
}

gdb_test_multiple "set extended-prompt \\w " \
    "set extended prompt working directory" {
    -re "\[\r\n\][string_to_regexp $working_dir] $" {
	pass "set extended prompt working directory"
    }
}

gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
    "set extended prompt parameter" {
    -re "\[\r\n\]some param full $" {
	pass "set extended prompt parameter"
    }
}

# Start with a fresh gdb.
clean_restart ${testfile}

# The following tests require execution.

if ![runto_main] then {
    fail "Can't run to main"
    return 0
}

# print-stack settings
gdb_test "show python print-stack" \
    "The mode of Python stack printing on error is \"message\".*" \
    "Test print-stack show setting. Default is message."
gdb_py_test_silent_cmd "set python print-stack full" \
    "Test print-stack set setting to full" 1
gdb_test "show python print-stack" \
    "The mode of Python stack printing on error is \"full\".*" \
    "Test print-stack show setting to full"
gdb_py_test_silent_cmd "set python print-stack none" \
    "Test print-stack set setting to none" 1
gdb_test "show python print-stack" \
    "The mode of Python stack printing on error is \"none\".*" \
    "Test print-stack show setting to none"

gdb_py_test_silent_cmd "set python print-stack message" \
    "Test print-stack set setting to message" 1

gdb_py_test_multiple "prompt substitution readline" \
  "python" "" \
  "pCounter = 0" "" \
  "def error_prompt(current):" "" \
  "   raise RuntimeError(\"Python exception called\")" "" \
  "end" ""

gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
    -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" {
	pass "set hook"
    }
}

gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
    "set the hook to default" 1

gdb_py_test_silent_cmd "set python print-stack full" \
    "set print-stack full for prompt error test" 1

gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" {
    -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" {
	pass "set hook"
    }
}

gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \
    "set the hook to default" 1

# Start with a fresh gdb.
clean_restart ${testfile}

# The following tests require execution.

if ![runto_main] then {
    fail "Can't run to main"
    return 0
}

runto [gdb_get_line_number "Break at func2 call site."]

gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1
gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "Test find_pc_line at func2 call site"

gdb_py_test_silent_cmd "step" "Step into func2" 1
gdb_py_test_silent_cmd "up" "Step out of func2" 1

gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "Test find_pc_line with resume address"