1# Copyright (C) 2008-2023 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_test_multiline "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_test_multiline "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_test_multiline "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_test_multiline "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" 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 122gdb_test "python gdb.execute('echo 2\\necho 3\\\\n\\n')" "23" \ 123 "multi-line execute" 124gdb_test " " "23" "gdb.execute does not affect repeat history" 125 126# Test post_event. 127gdb_test_multiline "post event insertion" \ 128 "python" "" \ 129 "someVal = 0" "" \ 130 "class Foo(object):" "" \ 131 " def __call__(self):" "" \ 132 " global someVal" "" \ 133 " someVal += 1" "" \ 134 "gdb.post_event(Foo())" "" \ 135 "end" "" 136 137gdb_test "python print (someVal)" "1" "test post event execution" 138gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" \ 139 "test non callable class" 140 141send_gdb "python gdb.post_event(lambda: invalid())\n" 142gdb_expect { 143 -re "name 'invalid' is not defined" { 144 pass "test post_event error on receipt" 145 } 146 default { 147 fail "test post_event error on receipt" 148 } 149} 150 151# Test (no) pagination of the executed command. 152gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.} 153set lines 10 154gdb_test_no_output "set height $lines" 155 156set test "verify pagination beforehand" 157gdb_test_multiple "python print (\"\\n\" * $lines)" $test { 158 -re "--Type <RET>" { 159 exp_continue 160 } 161 -re " for more, q to quit" { 162 exp_continue 163 } 164 -re ", c to continue without paging--$" { 165 pass $test 166 } 167} 168gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination beforehand: q" 169 170gdb_test "python if gdb.execute('python print (\"\\\\n\" * $lines)', to_string=True) == \"\\n\" * [expr $lines + 1]: print (\"yes\")" "yes" "gdb.execute does not page" 171 172set test "verify pagination afterwards" 173gdb_test_multiple "python print (\"\\n\" * $lines)" $test { 174 -re "--Type <RET>" { 175 exp_continue 176 } 177 -re " for more, q to quit" { 178 exp_continue 179 } 180 -re ", c to continue without paging--$" { 181 pass $test 182 } 183} 184gdb_test "q" "Quit.*Error while executing Python.*" "verify pagination afterwards: q" 185 186gdb_test_no_output "set height 0" 187 188gdb_test_no_output "python a = gdb.execute('help', to_string=True)" "collect help from uiout" 189 190gdb_test "python print (a)" ".*aliases -- User-defined aliases of other commands.*" "verify help to uiout" 191 192# Test PR 12212, using InfThread.selected_thread() when no inferior is 193# loaded. 194gdb_py_test_silent_cmd "python nothread = gdb.selected_thread()" "Attempt to aquire thread with no inferior" 1 195gdb_test "python print (nothread is None)" "True" "ensure that no threads are returned" 196 197gdb_test_multiline "register atexit function" \ 198 "python" "" \ 199 "import atexit" "" \ 200 "def printit(arg):" "" \ 201 " print (arg)" "" \ 202 "atexit.register(printit, 'good bye world')" "" \ 203 "end" "" 204 205send_gdb "quit\n" 206gdb_expect { 207 -re "good bye world" { 208 pass "atexit handling" 209 } 210 default { 211 fail "atexit handling" 212 } 213} 214 215# Start with a fresh gdb. 216clean_restart ${testfile} 217 218# The following tests require execution. 219 220if {![runto_main]} { 221 return 0 222} 223 224set lineno [gdb_get_line_number "Break to end."] 225runto $lineno 226 227# Test gdb.decode_line. 228gdb_test "python gdb.decode_line(\"main.c:43\")" \ 229 "gdb.error: No source file named main.c.*" "test decode_line no source named main" 230 231with_test_prefix "test decode_line current location" { 232 gdb_py_test_silent_cmd "python symtab = gdb.decode_line()" "decode current line" 1 233 gdb_test "python print (len(symtab))" "2" "length of result" 234 gdb_test "python print (symtab\[0\])" "None" "no unparsed text" 235 gdb_test "python print (len(symtab\[1\]))" "1" "length of result locations" 236} 237 238# Test that decode_line with an empty string argument does not crash. 239gdb_py_test_silent_cmd "python symtab2 = gdb.decode_line('')" \ 240 "test decode_line with empty string" 1 241 242if { [is_remote host] } { 243 set python_c [string_to_regexp "python.c"] 244} else { 245 set python_c [string_to_regexp "gdb.python/python.c"] 246} 247with_test_prefix "test decode_line" { 248 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "current location filename" 249 gdb_test "python print (symtab\[1\]\[0\].line)" "$lineno" "current location line number" 250 251 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"python.c:26 if foo\")" "python.c:26 decode" 1 252 gdb_test "python print (len(symtab))" "2" "python.c:26 length 2" 253 gdb_test "python print (symtab\[0\])" "if foo" "expression parse" 254 gdb_test "python print (len(symtab\[1\]))" "1" "python.c:26 length 1" 255 gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_c}" "python.c:26 filename" 256 gdb_test "python print (symtab\[1\]\[0\].line)" "26" "python.c:26 line number" 257 258 gdb_test "python gdb.decode_line(\"randomfunc\")" \ 259 "gdb.error: Function \"randomfunc\" not defined.*" "randomfunc" 260 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"func1\")" "func1()" 1 261 gdb_test "python print (len(symtab))" "2" "func1 length 2" 262 gdb_test "python print (len(symtab\[1\]))" "1" "func1 length 1" 263} 264 265if { [is_remote host] } { 266 set python_1_c [string_to_regexp "python-1.c"] 267} else { 268 set python_1_c [string_to_regexp "gdb.python/python-1.c"] 269} 270gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*${python_1_c}" "test decode_line func1 filename" 271 272# Set a default value for func1_lineno in case we fail to fetch the line number 273# below. 274set func1_lineno "noline" 275 276# Fetch the line GDB thinks func1 starts at. This may change depending 277# on the architecture and on how GDB handles the prologue of the function. 278gdb_test_multiple "info line func1" "info line func1" { 279 -re "Line ($decimal) of .* starts at address $hex <func1> and ends at $hex <func1\\+$decimal>\.\[\r\n\]+$gdb_prompt $" { 280 # Fetch the line number. 281 set func1_lineno $expect_out(1,string) 282 } 283} 284 285gdb_test "python print (symtab\[1\]\[0\].line)" "$func1_lineno" "test decode_line func1 line number" 286gdb_py_test_silent_cmd {python symtab = gdb.decode_line ("func1,func2")} \ 287 "test decode_line func1,func2" 1 288gdb_test {python print (symtab[0])} ",func2" "stop at comma in linespec" 289 290with_test_prefix "test decode_line" { 291 gdb_py_test_silent_cmd "python symtab = gdb.decode_line(\"*0\")" "*0" 1 292 gdb_test "python print (len(symtab))" "2" "*0 result length" 293 gdb_test "python print (symtab\[0\])" "None" "*0 unparsed" 294 gdb_test "python print (len(symtab\[1\]))" "1" "*0 locations length" 295 gdb_test "python print (symtab\[1\]\[0\].symtab)" "None" "*0 filename" 296 gdb_test "python print (symtab\[1\]\[0\].pc)" "0" "*0 pc" 297} 298 299# gdb.write 300gdb_test "python print (sys.stderr)" ".*gdb._GdbFile (instance|object) at.*" "test stderr location" 301gdb_test "python print (sys.stdout)" ".*gdb._GdbFile (instance|object) at.*" "test stdout location" 302gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "test default write" 303gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "test stderr write" 304gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "test stdout write" 305 306if ![gdb_debug_enabled] { 307 gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "test stdlog write" 308} 309 310# Turn on full stack printing for subsequent tests. 311gdb_py_test_silent_cmd "set python print-stack full" \ 312 "Set print-stack full for prompt tests" 1 313 314# Test prompt substituion 315 316gdb_test_multiline "prompt substitution" \ 317 "python" "" \ 318 "someCounter = 0" "" \ 319 "def prompt(current):" "" \ 320 " global someCounter" "" \ 321 " if (current == \"testfake \"):" "" \ 322 " return None" "" \ 323 " someCounter = someCounter + 1" "" \ 324 " return \"py prompt \" + str (someCounter) + \" \"" "" \ 325 "end" "" 326 327gdb_test_multiline "prompt substitution readline" \ 328 "python" "" \ 329 "pCounter = 0" "" \ 330 "def program_prompt(current):" "" \ 331 " global pCounter" "" \ 332 " if (current == \">\"):" "" \ 333 " pCounter = pCounter + 1" "" \ 334 " return \"python line \" + str (pCounter) + \": \"" "" \ 335 " return None" "" \ 336 "end" "" 337 338set newprompt "py prompt 1" 339set newprompt2 "py prompt 2" 340set testfake "testfake" 341 342gdb_test_multiple "python gdb.prompt_hook = prompt" "set the hook = prompt" { 343 -re "\[\r\n\]$newprompt $" { 344 pass $gdb_test_name 345 } 346} 347 348gdb_test_multiple "set prompt testfake " "set testfake prompt in GDB" { 349 -re "\[\r\n\]$testfake $" { 350 pass $gdb_test_name 351 } 352} 353 354gdb_test_multiple "show prompt" "show testfake prompt" { 355 -re "Gdb's prompt is \"$testfake \"..* $" { 356 pass $gdb_test_name 357 } 358} 359 360gdb_test_multiple "set prompt blah " "set blah in GDB" { 361 -re "\[\r\n\]$newprompt2 $" { 362 pass $gdb_test_name 363 } 364} 365 366gdb_test_multiple "python gdb.prompt_hook = None" "delete hook" { 367 -re "\[\r\n\]$newprompt2 $" { 368 pass $gdb_test_name 369 } 370} 371 372gdb_test_multiple "set prompt $gdb_prompt " "set default prompt" { 373 -re "\[\r\n\]$gdb_prompt $" { 374 pass $gdb_test_name 375 } 376} 377 378set working_dir "" 379gdb_test_multiple "pwd" "pwd" { 380 -re "Working directory (.*)\\.\[\r\n\]+$gdb_prompt $" { 381 set working_dir $expect_out(1,string) 382 } 383} 384 385gdb_test_multiple "python gdb.prompt_hook = program_prompt" "set the programming hook" { 386 -re "\[\r\n\]$gdb_prompt $" { 387 pass $gdb_test_name 388 } 389} 390 391gdb_test_multiple "python" "test we ignore substitution for seconday prompts" { 392 -re "\r\n>$" { 393 pass $gdb_test_name 394 } 395} 396 397gdb_test_multiple "end" "end programming" { 398 -re "\[\r\n\]$gdb_prompt $" { 399 pass $gdb_test_name 400 } 401} 402 403gdb_test_multiline "prompt substitution readline import" \ 404 "python" "" \ 405 "import gdb.command.prompt" "" \ 406 "end" "" 407 408gdb_test_multiple "set extended-prompt one two three " \ 409 "set basic extended prompt" { 410 -re "\[\r\n\]one two three $" { 411 pass $gdb_test_name 412 } 413} 414 415gdb_test_multiple "set extended-prompt \\w " \ 416 "set extended prompt working directory" { 417 -re "\[\r\n\][string_to_regexp $working_dir] $" { 418 pass $gdb_test_name 419 } 420} 421 422gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \ 423 "set extended prompt parameter" { 424 -re "\[\r\n\]some param full $" { 425 pass $gdb_test_name 426 } 427} 428 429# Start with a fresh gdb. 430clean_restart ${testfile} 431 432# The following tests require execution. 433 434if {![runto_main]} { 435 return 0 436} 437 438# print-stack settings 439gdb_test "show python print-stack" \ 440 "The mode of Python stack printing on error is \"message\".*" \ 441 "Test print-stack show setting. Default is message." 442gdb_py_test_silent_cmd "set python print-stack full" \ 443 "Test print-stack set setting to full" 1 444gdb_test "show python print-stack" \ 445 "The mode of Python stack printing on error is \"full\".*" \ 446 "Test print-stack show setting to full" 447gdb_py_test_silent_cmd "set python print-stack none" \ 448 "Test print-stack set setting to none" 1 449gdb_test "show python print-stack" \ 450 "The mode of Python stack printing on error is \"none\".*" \ 451 "test print-stack show setting to none" 452 453gdb_py_test_silent_cmd "set python print-stack message" \ 454 "Test print-stack set setting to message" 1 455 456gdb_test_multiline "prompt substitution readline error_prompt" \ 457 "python" "" \ 458 "pCounter = 0" "" \ 459 "def error_prompt(current):" "" \ 460 " raise RuntimeError(\"Python exception called\")" "" \ 461 "end" "" 462 463gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook error_prompt" { 464 -re "Python Exception (exceptions.RuntimeError|<(type 'exceptions.|class ')RuntimeError'>): Python exception called\r\n$gdb_prompt $" { 465 pass $gdb_test_name 466 } 467} 468 469gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ 470 "set the hook to default 1" 1 471 472gdb_py_test_silent_cmd "set python print-stack full" \ 473 "set print-stack full for prompt error test" 1 474 475gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook error_prompt traceback" { 476 -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*$gdb_prompt $" { 477 pass $gdb_test_name 478 } 479} 480 481gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ 482 "set the hook to default 2" 1 483 484# Start with a fresh gdb. 485clean_restart ${testfile} 486 487# The following tests require execution. 488 489if {![runto_main]} { 490 return 0 491} 492 493runto [gdb_get_line_number "Break at func2 call site."] 494 495gdb_py_test_silent_cmd "python line = gdb.selected_frame().find_sal().line" "Get line number of func2 call site" 1 496 497gdb_py_test_silent_cmd "python pc_call = gdb.selected_frame().pc()" \ 498 "Get pc of func2 call site" 1 499 500gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line == line)" "True" "test find_pc_line at func2 call site" 501 502gdb_py_test_silent_cmd "step" "Step into func2" 1 503gdb_py_test_silent_cmd "up" "Step out of func2" 1 504 505# The point of the following test is to see if gdb has advanced past the 506# location where the branch to a function was made. 507set test_name "test find_pc_line with resume address" 508 509gdb_py_test_silent_cmd "python pc_rtn = gdb.selected_frame().pc()" \ 510 "Get pc at func2 return site" 1 511 512gdb_test "python print (pc_rtn > pc_call)" "True" \ 513 "test resume address greater then call address" 514 515gdb_test "python print (gdb.find_pc_line(pc_rtn).line >= line)" "True" \ 516 "test find_pc_line with resume address" 517gdb_test "python print (gdb.find_pc_line(pc_rtn).line == gdb.find_pc_line(gdb.Value(pc_rtn)).line)" \ 518 "True" \ 519 "test find_pc_line using Value" 520 521gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable" 522gdb_test "python print(gdb.convenience_variable('cvar1'))" "23" 523gdb_test "python print(gdb.convenience_variable('cvar2'))" "None" 524gdb_test_no_output "python gdb.set_convenience_variable('cvar1', 89)" \ 525 "change convenience variable from python" 526gdb_test "python print(gdb.convenience_variable('cvar1'))" "89" \ 527 "print new value of convenience variable from python" 528gdb_test "print \$cvar1" " = 89" \ 529 "print new value of convenience variable from CLI" 530gdb_test_no_output "python gdb.set_convenience_variable('cvar3', -5)" \ 531 "make convenience variable from python" 532gdb_test "python print(gdb.convenience_variable('cvar3'))" "-5" \ 533 "print value of new convenience variable from python" 534gdb_test_no_output "python gdb.set_convenience_variable('cvar3', None)" \ 535 "reset convenience variable from python" 536gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \ 537 "print reset convenience variable from python" 538gdb_test "print \$cvar3" "= void" \ 539 "print reset convenience variable from CLI" 540 541# Test PR 23669, the following would invoke the "commands" command instead of 542# "show commands". 543gdb_test "python gdb.execute(\"show commands\")" "$decimal print \\\$cvar3.*" 544 545# Check if starti command is supported. 546if { [use_gdb_stub] == 0 } { 547 # Test that the from_tty argument to gdb.execute is effective. If 548 # False, the user is not prompted for decisions such as restarting the 549 # program, and "yes" is assumed. If True, the user is prompted. 550 # Case 1, from_tty=False. 551 gdb_test "python gdb.execute('starti', from_tty=False)" \ 552 "Program stopped.*" \ 553 "starti via gdb.execute, not from tty" 554 555 # Case 2, from_tty=True. 556 set test "starti via gdb.execute, from tty" 557 set question \ 558 [multi_line \ 559 {The program being debugged has been started already\.} \ 560 {Start it from the beginning\? \(y or n\) $}] 561 gdb_test_multiple "python gdb.execute('starti', from_tty=True)" $test { 562 -re $question { 563 gdb_test "y" "Starting program:.*" $gdb_test_name 564 } 565 } 566} 567