1# Copyright (C) 2008-2019 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 Python-based 17# pretty-printing for the CLI. 18 19load_lib gdb-python.exp 20 21standard_testfile 22 23# Start with a fresh gdb. 24gdb_exit 25gdb_start 26 27# Skip all tests if Python scripting is not enabled. 28if { [skip_python_tests] } { continue } 29 30proc run_lang_tests {exefile lang} { 31 global srcdir subdir srcfile testfile hex 32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } { 33 untested "failed to compile in $lang mode" 34 return -1 35 } 36 37 set nl "\[\r\n\]+" 38 39 # Start with a fresh gdb. 40 gdb_exit 41 gdb_start 42 gdb_reinitialize_dir $srcdir/$subdir 43 gdb_load ${exefile} 44 45 if ![runto_main ] then { 46 perror "couldn't run to breakpoint" 47 return 48 } 49 50 gdb_test_no_output "set print pretty on" 51 52 gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \ 53 ".*Breakpoint.*" 54 gdb_test "continue" ".*Breakpoint.*" 55 56 set remote_python_file [gdb_remote_download host \ 57 ${srcdir}/${subdir}/${testfile}.py] 58 59 gdb_test_no_output "source ${remote_python_file}" "load python file" 60 61 gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" 62 gdb_test "print ssa\[1\]" " = a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>" 63 gdb_test "print ssa" " = {a=< a=<3> b=<$hex>> b=< a=<4> b=<$hex>>, a=< a=<5> b=<$hex>> b=< a=<6> b=<$hex>>}" 64 65 gdb_test "print arraystruct" " = {$nl *y = 7, *$nl *x = { a=<23> b=<$hex>, a=<24> b=<$hex>} *$nl *}" 66 67 # Test that when a pretty-printer returns a gdb.Value in its to_string, we 68 # call the pretty-printer of that value too. 69 gdb_test "print tsrvw" " = Inner to_string 1989" 70 71 if {$lang == "c++"} { 72 gdb_test "print cps" "= a=<8> b=<$hex>" 73 gdb_test "print cpss" " = {$nl *zss = 9, *$nl *s = a=<10> b=<$hex>$nl}" 74 gdb_test "print cpssa\[0\]" " = {$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl}" 75 gdb_test "print cpssa\[1\]" " = {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl}" 76 gdb_test "print cpssa" " = {{$nl *zss = 11, *$nl *s = a=<12> b=<$hex>$nl *}, {$nl *zss = 13, *$nl *s = a=<14> b=<$hex>$nl *}}" 77 gdb_test "print sss" "= a=<15> b=< a=<8> b=<$hex>>" 78 gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>" 79 gdb_test "print derived" \ 80 " = \{.*<Vbase1> = pp class name: Vbase1.*<Vbase2> = \{.*<VirtualTest> = pp value variable is: 1,.*members of Vbase2:.*_vptr.Vbase2 = $hex.*<Vbase3> = \{.*members of Vbase3.*members of Derived:.*value = 2.*" 81 gdb_test "print ns " "\"embedded\\\\000null\\\\000string\"" 82 gdb_py_test_silent_cmd "set print elements 3" "" 1 83 gdb_test "print ns" "emb\.\.\.." 84 gdb_py_test_silent_cmd "set print elements 10" "" 1 85 gdb_test "print ns" "embedded\\\\000n\.\.\.." 86 gdb_py_test_silent_cmd "set print elements 200" "" 1 87 } 88 89 if { ![is_address_zero_readable] } { 90 gdb_test "print ns2" \ 91 ".error reading variable: Cannot access memory at address 0x0." 92 } 93 94 gdb_test "print x" " = \"this is x\"" 95 gdb_test "print cstring" " = \"const string\"" 96 97 gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\"" 98 if { ![is_address_zero_readable] } { 99 gdb_test "print estring3" \ 100 " = <error reading variable: Cannot create a lazy string with address 0x0, and a non-zero length.>" 101 } 102 103 gdb_test_no_output "python pp_ls_encoding = 'UTF-8'" 104 gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>" 105 106 gdb_test_no_output "set python print-stack full" 107 gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val" 108 109 gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}" 110 111 gdb_test "print nstype" " = {$nl *.0. = 7,$nl *.1. = 42$nl}" 112 113 gdb_test_no_output "set print pretty off" 114 gdb_test "print nstype" " = {.0. = 7, .1. = 42}" \ 115 "print nstype on one line" 116 117 # Check that GDB doesn't lose typedefs when looking for a printer. 118 gdb_test "print an_int" " = -1" 119 gdb_test "print (int) an_int" " = -1" 120 gdb_test "print (int_type) an_int" " = type=int_type, val=-1" 121 122 gdb_test "print an_int_type" " = type=int_type, val=1" 123 gdb_test "print (int_type) an_int_type" " = type=int_type, val=1" 124 125 gdb_test "print an_int_type2" " = type=int_type2, val=2" 126 gdb_test "print (int) an_int_type2" " = 2" 127 gdb_test "print (int_type) an_int_type2" " = type=int_type, val=2" 128 gdb_test "print (int_type2) an_int_type2" " = type=int_type2, val=2" 129 130 # PR python/16047: it is ok for a pretty printer not to have a 131 # to_string method. 132 gdb_test "print (int_type3) an_int_type2" " = {s = 27}" 133 134 gdb_continue_to_end 135} 136 137with_test_prefix c { 138 if { [run_lang_tests "${binfile}" "c"] == -1 } { 139 return 140 } 141} 142with_test_prefix c++ { 143 if { [run_lang_tests "${binfile}-cxx" "c++"] == -1 } { 144 return 145 } 146} 147 148# Run various other tests. 149 150# Start with a fresh gdb. 151gdb_exit 152gdb_start 153gdb_reinitialize_dir $srcdir/$subdir 154gdb_load ${binfile} 155 156if ![runto_main ] then { 157 perror "couldn't run to breakpoint" 158 return 159} 160 161set remote_python_file [gdb_remote_download host \ 162 ${srcdir}/${subdir}/${testfile}.py] 163 164gdb_test_no_output "source ${remote_python_file}" "load python file" 165 166gdb_breakpoint [gdb_get_line_number "eval-break"] 167gdb_continue_to_breakpoint "eval-break" ".* eval-break .*" 168 169gdb_test "info locals" "eval9 = eval=<123456789>" 170 171gdb_test "b [gdb_get_line_number {break to inspect} ${testfile}.c ]" \ 172 ".*Breakpoint.*" 173gdb_test "continue" ".*Breakpoint.*" 174 175gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \ 176 "print ss enabled #1" 177 178gdb_test_no_output "python disable_lookup_function ()" 179 180gdb_test "print ss" " = {a = {a = 1, b = $hex}, b = {a = 2, b = $hex}}" \ 181 "print ss disabled" 182 183gdb_test_no_output "python enable_lookup_function ()" 184 185gdb_test "print ss" " = a=< a=<1> b=<$hex>> b=< a=<2> b=<$hex>>" \ 186 "print ss enabled #2" 187