1# Copyright (C) 2008-2016 Free Software Foundation, Inc. 2 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. 7# 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. 12# 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/>. 15 16# This file is part of the GDB testsuite. It tests the mechanism 17# exposing values to Python. 18 19load_lib gdb-python.exp 20 21standard_testfile python.c python-1.c 22 23if {[build_executable $testfile.exp $testfile \ 24 [list $srcfile $srcfile2] debug] == -1} { 25 return -1 26} 27 28# Start with a fresh gdb. 29gdb_exit 30gdb_start 31gdb_reinitialize_dir $srcdir/$subdir 32 33set remote_source2_py [gdb_remote_download host \ 34 ${srcdir}/${subdir}/source2.py] 35 36# Do this instead of the skip_python_check. 37# We want to do some tests when Python is not present. 38gdb_test_multiple "python print (23)" "verify python support" { 39 -re "not supported.*$gdb_prompt $" { 40 unsupported "python support is disabled" 41 42 # If Python is not supported, verify that sourcing a python script 43 # causes an error. 44 gdb_test "source $remote_source2_py" \ 45 "Error in sourced command file:.*" \ 46 "source source2.py when python disabled" 47 48 # Verify multi-line python commands cause an error. 49 gdb_py_test_multiple "multi-line python command" \ 50 "python" "" \ 51 "print (23)" "" \ 52 "end" "not supported.*" 53 54 return -1 55 } 56 -re "$gdb_prompt $" {} 57} 58 59gdb_py_test_multiple "multi-line python command" \ 60 "python" "" \ 61 "print (23)" "" \ 62 "end" "23" 63 64# Spawn interactive Python help from a multi-line command, thus, after 65# a secondary prompt. 66 67with_test_prefix "python interactive help" { 68 set test "python; help(); end" 69 gdb_test_multiple "python\nhelp()\nend" $test { 70 -re ".*help utility.*help> $" { 71 pass $test 72 73 # The "quit" must be seen on the output. A buggy GDB 74 # would not display it. 75 gdb_test "quit" "^quit.*leaving help.*" "quit help" 76 } 77 } 78} 79 80gdb_py_test_multiple "show python command" \ 81 "define zzq" "Type commands for definition of .* just \"end\"\\.*" \ 82 "python" "" \ 83 "print (23)" "" \ 84 "end" "" \ 85 "end" "" \ 86 "show user zzq" "User command \"zzq\":.* python.*print \\(23\\).* end" 87 88gdb_py_test_multiple "indented multi-line python command" \ 89 "python" "" \ 90 "def foo ():" "" \ 91 " print ('hello, world!')" "" \ 92 "foo ()" "" \ 93 "end" "hello, world!" 94 95gdb_test "source $remote_source2_py" "yes" "source source2.py" 96 97gdb_test "source -s source2.py" "yes" "source -s source2.py" 98 99set remote_source2_symlink_notpy \ 100 [gdb_remote_download host ${srcdir}/${subdir}/source2.py \ 101 [standard_output_file "source2-symlink.notpy"]] 102set remote_source2_symlink_py [standard_output_file "source2-symlink.py"] 103remote_file host delete $remote_source2_symlink_py 104set status [remote_exec host "ln -sf $remote_source2_symlink_notpy $remote_source2_symlink_py"] 105set test "source -s source2-symlink.py" 106if {[lindex $status 0] == 0} { 107 gdb_test "source -s $remote_source2_symlink_py" "yes" $test 108} else { 109 unsupported "$test (host does not support symbolic links)" 110} 111 112gdb_test "python print (gdb.current_objfile())" "None" 113gdb_test "python print (gdb.objfiles())" "\\\[\\\]" 114 115# Test http://bugs.python.org/issue4434 workaround in configure.ac 116gdb_test "python import itertools; print ('IMPOR'+'TED')" "IMPORTED" "pythonX.Y/lib-dynload/*.so" 117 118gdb_test_no_output \ 119 "python x = gdb.execute('printf \"%d\", 23', to_string = True)" 120gdb_test "python print (x)" "23" 121 122# Test post_event. 123gdb_py_test_multiple "post event insertion" \ 124 "python" "" \ 125 "someVal = 0" "" \ 126 "class Foo(object):" "" \ 127 " def __call__(self):" "" \ 128 " global someVal" "" \ 129 " someVal += 1" "" \ 130 "gdb.post_event(Foo())" "" \ 131 "end" "" 132 133gdb_test "python print (someVal)" "1" "test post event execution" 134gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" "Test non callable class" 135 136# Test (no) pagination of the executed command. 137gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.} 138set lines 10 139gdb_test_no_output "set height $lines" 140 141set test "verify pagination beforehand" 142gdb_test_multiple "python print (\"\\n\" * $lines)" $test { 143 -re "---Type <return>" { 144 exp_continue 145 } 146 -re " to continue, or q <return>" { 147 exp_continue 148 } 149 -re " to quit---$" { 150 pass $test 151 } 152} 153gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q" 154 155gdb_test "python if gdb.execute('python print (\"\\\\n\" * $lines)', to_string=True) == \"\\n\" * [expr $lines + 1]: print (\"yes\")" "yes" "gdb.execute does not page" 156 157set test "verify pagination afterwards" 158gdb_test_multiple "python print (\"\\n\" * $lines)" $test { 159 -re "---Type <return>" { 160 exp_continue 161 } 162 -re " to continue, or q <return>" { 163 exp_continue 164 } 165 -re " to quit---$" { 166 pass $test 167 } 168} 169gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q" 170 171gdb_test_no_output "set height 0" 172 173gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout" 174 175gdb_test "python print (a)" ".*aliases -- Aliases of other commands.*" "verify help to uiout" 176 177# Test PR 12212, using InfThread.selected_thread() when no inferior is 178# loaded. 179gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1 180gdb_test "python print (nothread == None)" "True" "Ensure that no threads are returned" 181 182gdb_py_test_multiple "register atexit function" \ 183 "python" "" \ 184 "import atexit" "" \ 185 "def printit(arg):" "" \ 186 " print (arg)" "" \ 187 "atexit.register(printit, 'good bye world')" "" \ 188 "end" "" 189 190send_gdb "quit\n" 191gdb_expect { 192 -re "good bye world" { 193 pass "atexit handling" 194 } 195 default { 196 fail "atexit handling" 197 } 198} 199 200# Start with a fresh gdb. 201clean_restart ${testfile} 202 203# The following tests require execution. 204 205if ![runto_main] then { 206 fail "Can't run to main" 207 return 0 208} 209 210set lineno [gdb_get_line_number "Break to end."] 211runto $lineno 212 213# Test gdb.decode_line. 214gdb_test "python gdb.decode_line(\"main.c:43\")" \ 215 "gdb.error: No source file named main.c.*" "test decode_line no source named main" 216 217gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "test decode_line current location" 1 218gdb_test "python print (len(symtab))" "2" "Test decode_line current location" 219gdb_test "python print (symtab\[0\])" "None" "Test decode_line expression parse" 220gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line current location" 221 222if { [is_remote host] } { 223 set python_c [string_to_regexp "python.c"] 224} else { 225 set python_c [string_to_regexp "gdb.python/python.c"] 226} 227gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line current location filename" 228gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "Test decode_line current location line number" 229 230gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "test decode_line python.c:26" 1 231gdb_test "python print (len(symtab))" "2" "Test decode_line python.c:26 length" 232gdb_test "python print (symtab\[0\])" "if foo" "Test decode_line expression parse" 233gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line python.c:26 length" 234gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "Test decode_line python.c:26 filename" 235gdb_test "python print (symtab\[1\]\[0\].line)" "26" "Test decode_line python.c:26 line number" 236 237gdb_test "python gdb.decode_line(\"randomfunc\")" \ 238 "gdb.error: Function \"randomfunc\" not defined.*" "test decode_line randomfunc" 239gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "test decode_line func1()" 1 240gdb_test "python print (len(symtab))" "2" "Test decode_line func1 length" 241gdb_test "python print (len(symtab\[1\]))" "1" "Test decode_line func1 length" 242 243if { [is_remote host] } { 244 set python_1_c [string_to_regexp "python-1.c"] 245} else { 246 set python_1_c [string_to_regexp "gdb.python/python-1.c"] 247} 248gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "Test decode_line func1 filename" 249gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number" 250gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \ 251 "test decode_line func1,func2" 1 252gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec" 253 254# gdb.write 255gdb_test "python print (sys.stderr)" ".*gdb.GdbOutputErrorFile (instance|object) at.*" "Test stderr location" 256gdb_test "python print (sys.stdout)" ".*gdb.GdbOutputFile (instance|object) at.*" "Test stdout location" 257gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "Test default write" 258gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "Test stderr write" 259gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write" 260gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write" 261 262# Turn on full stack printing for subsequent tests. 263gdb_py_test_silent_cmd "set python print-stack full" \ 264 "Set print-stack full for prompt tests" 1 265 266# Test prompt substituion 267 268gdb_py_test_multiple "prompt substitution" \ 269 "python" "" \ 270 "someCounter = 0" "" \ 271 "def prompt(current):" "" \ 272 " global someCounter" "" \ 273 " if (current == \"testfake \"):" "" \ 274 " return None" "" \ 275 " someCounter = someCounter + 1" "" \ 276 " return \"py prompt \" + str (someCounter) + \" \"" "" \ 277 "end" "" 278 279gdb_py_test_multiple "prompt substitution readline" \ 280 "python" "" \ 281 "pCounter = 0" "" \ 282 "def program_prompt(current):" "" \ 283 " global pCounter" "" \ 284 " if (current == \">\"):" "" \ 285 " pCounter = pCounter + 1" "" \ 286 " return \"python line \" + str (pCounter) + \": \"" "" \ 287 " return None" "" \ 288 "end" "" 289 290set newprompt "py prompt 1" 291set newprompt2 "py prompt 2" 292set testfake "testfake" 293 294gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook" { 295 -re "\[\r\n\]$newprompt $" { 296 pass "set hook" 297 } 298} 299 300gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" { 301 -re "\[\r\n\]$testfake $" { 302 pass "set prompt testfake" 303 } 304} 305 306gdb_test_multiple "show prompt" "show testfake prompt" { 307 -re "Gdb's prompt is \"$testfake \"..* $" { 308 pass "show prompt shows guarded prompt" 309 } 310} 311 312gdb_test_multiple "set prompt blah " "set blah in GDB" { 313 -re "\[\r\n\]$newprompt2 $" { 314 pass "set prompt blah overriden" 315 } 316} 317 318gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" { 319 -re "\[\r\n\]$newprompt2 $" { 320 pass "Delete old hook" 321 } 322} 323 324gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" { 325 -re "\[\r\n\]$gdb_prompt $" { 326 pass "set default prompt" 327 } 328} 329 330set working_dir "" 331gdb_test_multiple "pwd" "pwd" { 332 -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" { 333 set working_dir $expect_out(1,string) 334 } 335} 336 337gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the hook" { 338 -re "\[\r\n\]$gdb_prompt $" { 339 pass "set programming hook" 340 } 341} 342 343gdb_test_multiple "python" "test we ignore substituion for seconday prompts" { 344 -re "\r\n>$" { 345 pass "readline secondary are not substituted" 346 } 347} 348 349gdb_test_multiple "end" "end programming" { 350 -re "\[\r\n\]$gdb_prompt $" { 351 pass "end programming" 352 } 353} 354 355gdb_py_test_multiple "prompt substitution readline" \ 356 "python" "" \ 357 "import gdb.command.prompt" "" \ 358 "end" "" 359 360gdb_test_multiple "set extended-prompt one two three " \ 361 "set basic extended prompt" { 362 -re "\[\r\n\]one two three $" { 363 pass "set basic extended prompt" 364 } 365} 366 367gdb_test_multiple "set extended-prompt \\w " \ 368 "set extended prompt working directory" { 369 -re "\[\r\n\][string_to_regexp $working_dir] $" { 370 pass "set extended prompt working directory" 371 } 372} 373 374gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \ 375 "set extended prompt parameter" { 376 -re "\[\r\n\]some param full $" { 377 pass "set extended prompt parameter" 378 } 379} 380 381# Start with a fresh gdb. 382clean_restart ${testfile} 383 384# The following tests require execution. 385 386if ![runto_main] then { 387 fail "Can't run to main" 388 return 0 389} 390 391# print-stack settings 392gdb_test "show python print-stack" \ 393 "The mode of Python stack printing on error is \"message\".*" \ 394 "Test print-stack show setting. Default is message." 395gdb_py_test_silent_cmd "set python print-stack full" \ 396 "Test print-stack set setting to full" 1 397gdb_test "show python print-stack" \ 398 "The mode of Python stack printing on error is \"full\".*" \ 399 "Test print-stack show setting to full" 400gdb_py_test_silent_cmd "set python print-stack none" \ 401 "Test print-stack set setting to none" 1 402gdb_test "show python print-stack" \ 403 "The mode of Python stack printing on error is \"none\".*" \ 404 "Test print-stack show setting to none" 405 406gdb_py_test_silent_cmd "set python print-stack message" \ 407 "Test print-stack set setting to message" 1 408 409gdb_py_test_multiple "prompt substitution readline" \ 410 "python" "" \ 411 "pCounter = 0" "" \ 412 "def error_prompt(current):" "" \ 413 " raise RuntimeError(\"Python exception called\")" "" \ 414 "end" "" 415 416gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { 417 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>) Python exception called.*" { 418 pass "set hook" 419 } 420} 421 422gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ 423 "set the hook to default" 1 424 425gdb_py_test_silent_cmd "set python print-stack full" \ 426 "set print-stack full for prompt error test" 1 427 428gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { 429 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" { 430 pass "set hook" 431 } 432} 433 434gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ 435 "set the hook to default" 1 436 437# Start with a fresh gdb. 438clean_restart ${testfile} 439 440# The following tests require execution. 441 442if ![runto_main] then { 443 fail "Can't run to main" 444 return 0 445} 446 447runto [gdb_get_line_number "Break at func2 call site."] 448 449gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1 450gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "Test find_pc_line at func2 call site" 451 452gdb_py_test_silent_cmd "step" "Step into func2" 1 453gdb_py_test_silent_cmd "up" "Step out of func2" 1 454 455gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "Test find_pc_line with resume address" 456