-# Copyright (C) 2008-2012 Free Software Foundation, Inc.
+# Copyright (C) 2008-2018 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
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
-# Skip all tests if Python scripting is not enabled.
-if { [skip_python_tests] } { continue }
+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 $srcdir/$subdir/source2.py" \
+ 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 $" {}
"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" "" \
"foo ()" "" \
"end" "hello, world!"
-gdb_test "source $srcdir/$subdir/source2.py" "yes" "source source2.py"
+gdb_test "source $remote_source2_py" "yes" "source source2.py"
gdb_test "source -s source2.py" "yes" "source -s source2.py"
+set remote_source2_symlink_notpy \
+ [gdb_remote_download host ${srcdir}/${subdir}/source2.py \
+ [standard_output_file "source2-symlink.notpy"]]
+set remote_source2_symlink_py [standard_output_file "source2-symlink.py"]
+remote_file host delete $remote_source2_symlink_py
+set status [remote_exec host "ln -sf $remote_source2_symlink_notpy $remote_source2_symlink_py"]
+set test "source -s source2-symlink.py"
+if {[lindex $status 0] == 0} {
+ gdb_test "source -s $remote_source2_symlink_py" "yes" $test
+} else {
+ unsupported "$test (host does not support symbolic links)"
+}
+
gdb_test "python print (gdb.current_objfile())" "None"
gdb_test "python print (gdb.objfiles())" "\\\[\\\]"
"python x = gdb.execute('printf \"%d\", 23', to_string = True)"
gdb_test "python print (x)" "23"
+gdb_test "python gdb.execute('echo 2\\necho 3\\\\n\\n')" "23" \
+ "multi-line execute"
+
# Test post_event.
gdb_py_test_multiple "post event insertion" \
"python" "" \
"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"
+gdb_test "python gdb.post_event(str(1))" "RuntimeError: Posted event is not callable.*" \
+ "test non callable class"
+
+send_gdb "python gdb.post_event(lambda: invalid())\n"
+gdb_expect {
+ -re "name 'invalid' is not defined" {
+ pass "test post_event error on receipt"
+ }
+ default {
+ fail "test post_event error on receipt"
+ }
+}
# Test (no) pagination of the executed command.
gdb_test "show height" {Number of lines gdb thinks are in a page is unlimited\.}
set test "verify pagination beforehand"
gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
- -re "---Type <return>" {
+ -re "--Type <RET>" {
exp_continue
}
- -re " to continue, or q <return>" {
+ -re " for more, q to quit" {
exp_continue
}
- -re " to quit---$" {
+ -re ", c to continue without paging--$" {
pass $test
}
}
set test "verify pagination afterwards"
gdb_test_multiple "python print (\"\\n\" * $lines)" $test {
- -re "---Type <return>" {
+ -re "--Type <RET>" {
exp_continue
}
- -re " to continue, or q <return>" {
+ -re " for more, q to quit" {
exp_continue
}
- -re " to quit---$" {
+ -re ", c to continue without paging--$" {
pass $test
}
}
# 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_test "python print (nothread == None)" "True" "ensure that no threads are returned"
gdb_py_test_multiple "register atexit function" \
"python" "" \
# The following tests require execution.
if ![runto_main] then {
- fail "Can't run to main"
+ fail "can't run to main"
return 0
}
-runto [gdb_get_line_number "Break to end."]
+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"
-gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python.c.*" "Test decode_line current locationn filename"
-gdb_test "python print (symtab\[1\]\[0\].line)" "22" "Test decode_line current location line number"
+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)" ".*gdb.python/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 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"
-gdb_test "python print (symtab\[1\]\[0\].symtab)" ".*gdb.python/python-1.c.*" "Test decode_line func1 filename"
-gdb_test "python print (symtab\[1\]\[0\].line)" "19" "Test decode_line func1 line number"
+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"
+
+# Set a default value for func1_lineno in case we fail to fetch the line number
+# below.
+set func1_lineno "noline"
+
+# Fetch the line GDB thinks func1 starts at. This may change depending
+# on the architecture and on how GDB handles the prologue of the function.
+gdb_test_multiple "info line func1" "info line func1" {
+ -re "Line ($decimal) of .* starts at address $hex <func1> and ends at $hex <func1\\+$decimal>\.\[\r\n\]+$gdb_prompt $" {
+ # Fetch the line number.
+ set func1_lineno $expect_out(1,string)
+ }
+}
+
+gdb_test "python print (symtab\[1\]\[0\].line)" "$func1_lineno" "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_py_test_silent_cmd "python symtab = gdb.decode_line(\"*0\")" "Test decode_line *0" 1
+gdb_test "python print (len(symtab))" "2" "test decode_line *0 result length"
+gdb_test "python print (symtab\[0\])" "None" "test decode_line *0 unparsed"
+gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line *0 locations length"
+gdb_test "python print (symtab\[1\]\[0\].symtab)" "None" "test decode_line *0 filename"
+gdb_test "python print (symtab\[1\]\[0\].pc)" "0" "test decode_line *0 pc"
+
# 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"
-
-# print-stack
-gdb_test "show python print-stack" \
- "The mode of Python stack printing on error is \"message\".*" \
- "Test print-backtrace show setting. Default is message."
+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" \
- "Test print-backtrace set setting" 1
-gdb_test "show python print-stack" \
- "The mode of Python stack printing on error is \"full\".*" \
- "Test print-backtrace show setting to full."
+ "Set print-stack full for prompt tests" 1
# Test prompt substituion
}
}
-gdb_test_multiple "python gdb.prompt_hook = None" "Delete hook" {
+gdb_test_multiple "python gdb.prompt_hook = None" "delete hook" {
-re "\[\r\n\]$newprompt2 $" {
- pass "Delete old hook"
+ pass "delete old hook"
}
}
}
}
+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 "set extended-prompt \\w " \
"set extended prompt working directory" {
- -re "\[\r\n\].*gdb.*testsuite.* $" {
+ -re "\[\r\n\][string_to_regexp $working_dir] $" {
pass "set extended prompt working directory"
}
}
# The following tests require execution.
if ![runto_main] then {
- fail "Can't run to main"
+ 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-backtrace show setting. Default is message."
+ "Test print-stack show setting. Default is message."
gdb_py_test_silent_cmd "set python print-stack full" \
- "Test print-backtrace set setting" 1
+ "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-backtrace show setting to full."
+ "Test print-stack show setting to full"
gdb_py_test_silent_cmd "set python print-stack none" \
- "Test print-backtrace set setting" 1
+ "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-backtrace show setting to none."
+ "test print-stack show setting to none"
gdb_py_test_silent_cmd "set python print-stack message" \
- "Test print-backtrace set setting" 1
+ "Test print-stack set setting to message" 1
gdb_py_test_multiple "prompt substitution readline" \
"python" "" \
"set the hook to default" 1
gdb_py_test_silent_cmd "set python print-stack full" \
- "Test print-backtrace set setting" 1
+ "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.*" {
# The following tests require execution.
if ![runto_main] then {
- fail "Can't run to main"
+ 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_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"
+gdb_test "python print (gdb.find_pc_line(gdb.selected_frame().pc()).line > line)" "True" "test find_pc_line with resume address"
+
+gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable"
+gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"
+gdb_test "python print(gdb.convenience_variable('cvar2'))" "None"
+gdb_test_no_output "python gdb.set_convenience_variable('cvar1', 89)" \
+ "change convenience variable from python"
+gdb_test "python print(gdb.convenience_variable('cvar1'))" "89" \
+ "print new value of convenience variable from python"
+gdb_test "print \$cvar1" " = 89" \
+ "print new value of convenience variable from CLI"
+gdb_test_no_output "python gdb.set_convenience_variable('cvar3', -5)" \
+ "make convenience variable from python"
+gdb_test "python print(gdb.convenience_variable('cvar3'))" "-5" \
+ "print value of new convenience variable from python"
+gdb_test_no_output "python gdb.set_convenience_variable('cvar3', None)" \
+ "reset convenience variable from python"
+gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \
+ "print reset convenience variable from python"
+gdb_test "print \$cvar3" "= void" \
+ "print reset convenience variable from CLI"
+
+# Test PR 23669, the following would invoke the "commands" command instead of
+# "show commands".
+gdb_test "python gdb.execute(\"show commands\")" "$decimal print \\\$cvar3.*"