| 1 | # Copyright (C) 2008-2013 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 | |
| 19 | load_lib gdb-python.exp |
| 20 | |
| 21 | standard_testfile |
| 22 | |
| 23 | # Build inferior to language specification. |
| 24 | # LANG is one of "c" or "c++". |
| 25 | proc build_inferior {exefile lang} { |
| 26 | global srcdir subdir srcfile testfile hex |
| 27 | |
| 28 | # Use different names for .o files based on the language. |
| 29 | # For Fission, the debug info goes in foo.dwo and we don't want, |
| 30 | # for example, a C++ compile to clobber the dwo of a C compile. |
| 31 | # ref: http://gcc.gnu.org/wiki/DebugFission |
| 32 | switch ${lang} { |
| 33 | "c" { set filename ${testfile}.o } |
| 34 | "c++" { set filename ${testfile}-cxx.o } |
| 35 | } |
| 36 | set objfile [standard_output_file $filename] |
| 37 | |
| 38 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objfile}" object "debug $lang"] != "" |
| 39 | || [gdb_compile "${objfile}" "${exefile}" executable "debug $lang"] != "" } { |
| 40 | untested "Couldn't compile ${srcfile} in $lang mode" |
| 41 | return -1 |
| 42 | } |
| 43 | return 0 |
| 44 | } |
| 45 | |
| 46 | proc test_value_creation {} { |
| 47 | global gdb_prompt |
| 48 | global gdb_py_is_py3k |
| 49 | |
| 50 | gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1 |
| 51 | gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1 |
| 52 | if { $gdb_py_is_py3k == 0 } { |
| 53 | gdb_py_test_silent_cmd "python i = gdb.Value (5L)" "create long value" 1 |
| 54 | } |
| 55 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1 |
| 56 | gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1 |
| 57 | gdb_test "python print (a)" "\"string test\"" "print 8-bit string" |
| 58 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string" |
| 59 | if { $gdb_py_is_py3k == 0 } { |
| 60 | gdb_py_test_silent_cmd "python a = gdb.Value (u'unicode test')" "create unicode value" 1 |
| 61 | gdb_test "python print (a)" "\"unicode test\"" "print Unicode string" |
| 62 | gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of unicode string" |
| 63 | } |
| 64 | |
| 65 | # Test address attribute is None in a non-addressable value |
| 66 | gdb_test "python print ('result = %s' % i.address)" "= None" "Test address attribute in non-addressable value" |
| 67 | } |
| 68 | |
| 69 | proc test_value_numeric_ops {} { |
| 70 | global gdb_prompt |
| 71 | |
| 72 | gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0 |
| 73 | gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0 |
| 74 | gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0 |
| 75 | gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0 |
| 76 | gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values" |
| 77 | gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result" |
| 78 | |
| 79 | gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values" |
| 80 | gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values" |
| 81 | gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values" |
| 82 | gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values" |
| 83 | gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values" |
| 84 | gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values" |
| 85 | gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" |
| 86 | gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" |
| 87 | # Remainder of float is implemented in Python but not in GDB's value system. |
| 88 | |
| 89 | gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value" |
| 90 | gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value" |
| 91 | |
| 92 | gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value" |
| 93 | gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value" |
| 94 | gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value" |
| 95 | gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value" |
| 96 | gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value" |
| 97 | gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value" |
| 98 | |
| 99 | # Test gdb.Value mixed with Python types. |
| 100 | |
| 101 | gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer" |
| 102 | gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result" |
| 103 | gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float" |
| 104 | |
| 105 | gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value" |
| 106 | gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value" |
| 107 | |
| 108 | # Conversion test. |
| 109 | gdb_test "print evalue" " = TWO" |
| 110 | gdb_test_no_output "python evalue = gdb.history (0)" |
| 111 | gdb_test "python print (int (evalue))" "2" |
| 112 | |
| 113 | # Test pointer arithmethic |
| 114 | |
| 115 | # First, obtain the pointers |
| 116 | gdb_test "print (void *) 2" ".*" "" |
| 117 | gdb_test_no_output "python a = gdb.history (0)" "" |
| 118 | gdb_test "print (void *) 5" ".*" "" |
| 119 | gdb_test_no_output "python b = gdb.history (0)" "" |
| 120 | |
| 121 | gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer" |
| 122 | gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value" |
| 123 | gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values" |
| 124 | |
| 125 | # Test some invalid operations. |
| 126 | |
| 127 | gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" { |
| 128 | -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"} |
| 129 | -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} |
| 130 | -re "$gdb_prompt $" {fail "catch error in python type conversion"} |
| 131 | } |
| 132 | |
| 133 | gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" { |
| 134 | -re "Traceback.*$gdb_prompt $" {pass "catch throw of GDB error"} |
| 135 | -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"} |
| 136 | -re "$gdb_prompt $" {fail "catch throw of GDB error"} |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | proc test_value_boolean {} { |
| 141 | # First, define a useful function to test booleans. |
| 142 | gdb_py_test_multiple "define function to test booleans" \ |
| 143 | "python" "" \ |
| 144 | "def test_bool (val):" "" \ |
| 145 | " if val:" "" \ |
| 146 | " print ('yay')" "" \ |
| 147 | " else:" "" \ |
| 148 | " print ('nay')" "" \ |
| 149 | "end" "" |
| 150 | |
| 151 | gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression" |
| 152 | |
| 153 | gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression" |
| 154 | |
| 155 | gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression" |
| 156 | |
| 157 | gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression" |
| 158 | |
| 159 | gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true integer value in expression" |
| 160 | |
| 161 | gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false integer value in expression" |
| 162 | } |
| 163 | |
| 164 | proc test_value_compare {} { |
| 165 | gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal" |
| 166 | gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less" |
| 167 | gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater" |
| 168 | gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None" |
| 169 | |
| 170 | gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal" |
| 171 | gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less" |
| 172 | gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater" |
| 173 | gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None" |
| 174 | |
| 175 | gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values" |
| 176 | gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values" |
| 177 | gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value" |
| 178 | gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value" |
| 179 | gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None" |
| 180 | |
| 181 | gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false" |
| 182 | gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true" |
| 183 | gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None" |
| 184 | |
| 185 | gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal" |
| 186 | gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less" |
| 187 | gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater" |
| 188 | gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None" |
| 189 | |
| 190 | gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal" |
| 191 | gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less" |
| 192 | gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater" |
| 193 | gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None" |
| 194 | } |
| 195 | |
| 196 | proc test_value_in_inferior {} { |
| 197 | global gdb_prompt |
| 198 | global testfile |
| 199 | global gdb_py_is_py3k |
| 200 | |
| 201 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] |
| 202 | |
| 203 | gdb_continue_to_breakpoint "break to inspect struct and union" |
| 204 | |
| 205 | # Just get inferior variable s in the value history, available to python. |
| 206 | gdb_test "print s" " = {a = 3, b = 5}" "" |
| 207 | |
| 208 | gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value from history" 1 |
| 209 | |
| 210 | gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name" |
| 211 | if { $gdb_py_is_py3k == 0 } { |
| 212 | gdb_test "python print ('result = ' + str(s\[u'a'\]))" " = 3" "access element inside struct using unicode name" |
| 213 | } |
| 214 | |
| 215 | # Test dereferencing the argv pointer |
| 216 | |
| 217 | # Just get inferior variable argv the value history, available to python. |
| 218 | gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" "" |
| 219 | |
| 220 | gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0 |
| 221 | gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1 |
| 222 | |
| 223 | # Check that the dereferenced value is sane |
| 224 | if { ! [target_info exists noargs] } { |
| 225 | gdb_test "python print (arg0)" "0x.*$testfile\"" "verify dereferenced value" |
| 226 | } |
| 227 | |
| 228 | # Smoke-test is_optimized_out attribute |
| 229 | gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "Test is_optimized_out attribute" |
| 230 | |
| 231 | # Test address attribute |
| 232 | gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "Test address attribute" |
| 233 | |
| 234 | # Test displaying a variable that is temporarily at a bad address. |
| 235 | # But if we can examine what's at memory address 0, then we'll also be |
| 236 | # able to display it without error. Don't run the test in that case. |
| 237 | set can_read_0 0 |
| 238 | gdb_test_multiple "x 0" "memory at address 0" { |
| 239 | -re "0x0:\[ \t\]*Cannot access memory at address 0x0\r\n$gdb_prompt $" { } |
| 240 | -re "0x0:\[ \t\]*Error accessing memory address 0x0\r\n$gdb_prompt $" { } |
| 241 | -re "\r\n$gdb_prompt $" { |
| 242 | set can_read_0 1 |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | # Test memory error. |
| 247 | set test "parse_and_eval with memory error" |
| 248 | if {$can_read_0} { |
| 249 | untested $test |
| 250 | } else { |
| 251 | gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
| 252 | } |
| 253 | |
| 254 | # Test Python lazy value handling |
| 255 | set test "memory error and lazy values" |
| 256 | if {$can_read_0} { |
| 257 | untested $test |
| 258 | } else { |
| 259 | gdb_test "python inval = gdb.parse_and_eval('*(int*)0')" |
| 260 | gdb_test "python print (inval.is_lazy)" "True" |
| 261 | gdb_test "python inval2 = inval+1" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
| 262 | gdb_test "python inval.fetch_lazy ()" "gdb.MemoryError: Cannot access memory at address 0x0.*" $test |
| 263 | } |
| 264 | gdb_test "python argc_lazy = gdb.parse_and_eval('argc')" |
| 265 | gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')" |
| 266 | gdb_test "python argc_notlazy.fetch_lazy()" |
| 267 | gdb_test "python print (argc_lazy.is_lazy)" "True" |
| 268 | gdb_test "python print (argc_notlazy.is_lazy)" "False" |
| 269 | gdb_test "print argc" " = 1" "sanity check argc" |
| 270 | gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue" |
| 271 | gdb_test_no_output "set argc=2" |
| 272 | gdb_test "python print (argc_notlazy)" "\r\n1" |
| 273 | gdb_test "python print (argc_lazy)" "\r\n2" |
| 274 | gdb_test "python print (argc_lazy.is_lazy)" "False" |
| 275 | |
| 276 | # Test string fetches, both partial and whole. |
| 277 | gdb_test "print st" "\"divide et impera\"" |
| 278 | gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1 |
| 279 | gdb_test "python print (st.string ())" "divide et impera" "Test string with no length" |
| 280 | gdb_test "python print (st.string (length = -1))" "divide et impera" "Test string (length = -1) is all of the string" |
| 281 | gdb_test "python print (st.string (length = 6))" "divide" |
| 282 | gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "Test string (length = 0) is empty" |
| 283 | gdb_test "python print (len(st.string (length = 0)))" "0" "Test length is 0" |
| 284 | |
| 285 | |
| 286 | # Fetch a string that has embedded nulls. |
| 287 | gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*" |
| 288 | gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value from history" 1 |
| 289 | gdb_test "python print (nullst.string ())" "divide" "Test string to first null" |
| 290 | # Python cannot print strings that contain the null (\0) character. |
| 291 | # For the purposes of this test, use repr() |
| 292 | gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1 |
| 293 | gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'" |
| 294 | } |
| 295 | |
| 296 | proc test_lazy_strings {} { |
| 297 | |
| 298 | global hex |
| 299 | |
| 300 | gdb_test "print sptr" "\"pointer\"" |
| 301 | gdb_py_test_silent_cmd "python sptr = gdb.history (0)" "Get value from history" 1 |
| 302 | |
| 303 | gdb_py_test_silent_cmd "python lstr = sptr.lazy_string()" "Aquire lazy string" 1 |
| 304 | gdb_test "python print (lstr.type)" "const char \*." "Test lazy-string type name equality" |
| 305 | gdb_test "python print (sptr.type)" "const char \*." "Test string type name equality" |
| 306 | gdb_test "print sn" "0x0" |
| 307 | gdb_py_test_silent_cmd "python snptr = gdb.history (0)" "Get value from history" 1 |
| 308 | gdb_test "python snstr = snptr.lazy_string(length=5)" ".*Cannot create a lazy string with address.*" "Test lazy string" |
| 309 | gdb_py_test_silent_cmd "python snstr = snptr.lazy_string(length=0)" "Succesfully create a lazy string" 1 |
| 310 | gdb_test "python print (snstr.length)" "0" "Test lazy string length" |
| 311 | gdb_test "python print (snstr.address)" "0" "Test lazy string address" |
| 312 | } |
| 313 | |
| 314 | |
| 315 | proc test_inferior_function_call {} { |
| 316 | global gdb_prompt hex decimal |
| 317 | |
| 318 | # Correct inferior call without arguments. |
| 319 | gdb_test "p/x fp1" " = $hex.*" |
| 320 | gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value from history" 1 |
| 321 | gdb_test "python fp1 = fp1.dereference()" "" |
| 322 | gdb_test "python result = fp1()" "" |
| 323 | gdb_test "python print (result)" "void" |
| 324 | |
| 325 | # Correct inferior call with arguments. |
| 326 | gdb_test "p/x fp2" " = $hex.*" |
| 327 | gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value from history" 1 |
| 328 | gdb_test "python fp2 = fp2.dereference()" "" |
| 329 | gdb_test "python result2 = fp2(10,20)" "" |
| 330 | gdb_test "python print (result2)" "30" |
| 331 | |
| 332 | # Incorrect to call an int value. |
| 333 | gdb_test "p i" " = $decimal.*" |
| 334 | gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value from history" 1 |
| 335 | gdb_test "python result3 = i()" ".*Value is not callable.*" |
| 336 | |
| 337 | # Incorrect number of arguments. |
| 338 | gdb_test "p/x fp2" " = $hex.*" |
| 339 | gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value from history" 1 |
| 340 | gdb_test "python fp3 = fp3.dereference()" "" |
| 341 | gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*" |
| 342 | } |
| 343 | |
| 344 | # A few objfile tests. |
| 345 | proc test_objfiles {} { |
| 346 | gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'py-value' in file.filename:\n ok=True\nprint (ok)\nend" "True" \ |
| 347 | "py-value in file.filename" |
| 348 | |
| 349 | gdb_test "python print (gdb.objfiles()\[0\].pretty_printers)" "\\\[\\\]" |
| 350 | |
| 351 | gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \ |
| 352 | "pretty_printers attribute must be a list.*Error while executing Python code." |
| 353 | } |
| 354 | |
| 355 | proc test_value_after_death {} { |
| 356 | # Construct a type while the inferior is still running. |
| 357 | gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \ |
| 358 | "create PTR type" 1 |
| 359 | |
| 360 | # Kill the inferior and remove the symbols. |
| 361 | gdb_test "kill" "" "kill the inferior" \ |
| 362 | "Kill the program being debugged. .y or n. $" \ |
| 363 | "y" |
| 364 | gdb_test "file" "" "Discard the symbols" \ |
| 365 | "Discard symbol table from.*y or n. $" \ |
| 366 | "y" |
| 367 | |
| 368 | # Now create a value using that type. Relies on arg0, created by |
| 369 | # test_value_in_inferior. |
| 370 | gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \ |
| 371 | "cast arg0 to PTR" 1 |
| 372 | |
| 373 | # Make sure the type is deleted. |
| 374 | gdb_py_test_silent_cmd "python ptrtype = None" \ |
| 375 | "delete PTR type" 1 |
| 376 | |
| 377 | # Now see if the value's type is still valid. |
| 378 | gdb_test "python print (castval.type)" "PTR ." \ |
| 379 | "print value's type" |
| 380 | } |
| 381 | |
| 382 | # Regression test for invalid subscript operations. The bug was that |
| 383 | # the type of the value was not being checked before allowing a |
| 384 | # subscript operation to proceed. |
| 385 | |
| 386 | proc test_subscript_regression {exefile lang} { |
| 387 | # Start with a fresh gdb. |
| 388 | clean_restart ${exefile} |
| 389 | |
| 390 | if ![runto_main ] then { |
| 391 | perror "couldn't run to breakpoint" |
| 392 | return |
| 393 | } |
| 394 | |
| 395 | if {$lang == "c++"} { |
| 396 | gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"] |
| 397 | gdb_continue_to_breakpoint "break to inspect pointer by reference" |
| 398 | |
| 399 | gdb_py_test_silent_cmd "print rptr_int" \ |
| 400 | "Obtain address" 1 |
| 401 | gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \ |
| 402 | "Obtains value from GDB" 1 |
| 403 | gdb_test "python print (rptr\[0\])" "2" "Check pointer passed as reference" |
| 404 | |
| 405 | # Just the most basic test of dynamic_cast -- it is checked in |
| 406 | # the C++ tests. |
| 407 | gdb_test "python print (bool(gdb.parse_and_eval('base').dynamic_cast(gdb.lookup_type('Derived').pointer())))" \ |
| 408 | True |
| 409 | |
| 410 | # Likewise. |
| 411 | gdb_test "python print (gdb.parse_and_eval('base').dynamic_type)" \ |
| 412 | "Derived \[*\]" |
| 413 | # A static type case. |
| 414 | gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \ |
| 415 | "int" |
| 416 | } |
| 417 | |
| 418 | gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] |
| 419 | gdb_continue_to_breakpoint "break to inspect struct and union" |
| 420 | |
| 421 | gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \ |
| 422 | "Create a value for subscript test" 1 |
| 423 | gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \ |
| 424 | "Create a value for subscript test" 1 |
| 425 | |
| 426 | # Try to access an int with a subscript. This should fail. |
| 427 | gdb_test "python print (intv)" "1" "Baseline print of an int Python value" |
| 428 | gdb_test "python print (intv\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
| 429 | "Attempt to access an integer with a subscript" |
| 430 | |
| 431 | # Try to access a string with a subscript. This should pass. |
| 432 | gdb_test "python print (stringv)" "foo." "Baseline print of a string Python value" |
| 433 | gdb_test "python print (stringv\[0\])" "f." "Attempt to access a string with a subscript" |
| 434 | |
| 435 | # Try to access an int array via a pointer with a subscript. This should pass. |
| 436 | gdb_py_test_silent_cmd "print p" "Build pointer to array" 1 |
| 437 | gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "" 1 |
| 438 | gdb_test "python print (pointer\[0\])" "1" "Access array via pointer with int subscript" |
| 439 | gdb_test "python print (pointer\[intv\])" "2" "Access array via pointer with value subscript" |
| 440 | |
| 441 | # Try to access a single dimension array with a subscript to the |
| 442 | # result. This should fail. |
| 443 | gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error: Cannot subscript requested type.*" \ |
| 444 | "Attempt to access an integer with a subscript" |
| 445 | |
| 446 | # Lastly, test subscript access to an array with multiple |
| 447 | # dimensions. This should pass. |
| 448 | gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1 |
| 449 | gdb_py_test_silent_cmd "python marray = gdb.history(0)" "" 1 |
| 450 | gdb_test "python print (marray\[1\]\[2\])" "o." "Test multiple subscript" |
| 451 | } |
| 452 | |
| 453 | # A few tests of gdb.parse_and_eval. |
| 454 | proc test_parse_and_eval {} { |
| 455 | gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \ |
| 456 | "parse_and_eval constant test" |
| 457 | gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \ |
| 458 | "parse_and_eval simple expression test" |
| 459 | gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \ |
| 460 | ".(type|class) 'gdb.Value'."\ |
| 461 | "parse_and_eval type test" |
| 462 | } |
| 463 | |
| 464 | # Test that values are hashable. |
| 465 | proc test_value_hash {} { |
| 466 | gdb_py_test_multiple "Simple Python value dictionary" \ |
| 467 | "python" "" \ |
| 468 | "one = gdb.Value(1)" "" \ |
| 469 | "two = gdb.Value(2)" "" \ |
| 470 | "three = gdb.Value(3)" "" \ |
| 471 | "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \ |
| 472 | "end" |
| 473 | gdb_test "python print (vdict\[one\])" "one str" "Test dictionary hash" |
| 474 | gdb_test "python print (vdict\[two\])" "two str" "Test dictionary hash" |
| 475 | gdb_test "python print (vdict\[three\])" "three str" "Test dictionary hash" |
| 476 | gdb_test "python print (one.__hash__() == hash(one))" "True" "Test inbuilt hash" |
| 477 | } |
| 478 | |
| 479 | # Build C version of executable. C++ is built later. |
| 480 | if { [build_inferior "${binfile}" "c"] < 0 } { |
| 481 | return -1 |
| 482 | } |
| 483 | |
| 484 | # Start with a fresh gdb. |
| 485 | clean_restart ${binfile} |
| 486 | |
| 487 | # Skip all tests if Python scripting is not enabled. |
| 488 | if { [skip_python_tests] } { continue } |
| 489 | |
| 490 | test_value_creation |
| 491 | test_value_numeric_ops |
| 492 | test_value_boolean |
| 493 | test_value_compare |
| 494 | test_objfiles |
| 495 | test_parse_and_eval |
| 496 | test_value_hash |
| 497 | |
| 498 | # The following tests require execution. |
| 499 | |
| 500 | if ![runto_main] then { |
| 501 | fail "Can't run to main" |
| 502 | return 0 |
| 503 | } |
| 504 | |
| 505 | test_value_in_inferior |
| 506 | test_inferior_function_call |
| 507 | test_lazy_strings |
| 508 | test_value_after_death |
| 509 | |
| 510 | # Test either C or C++ values. |
| 511 | |
| 512 | test_subscript_regression "${binfile}" "c" |
| 513 | |
| 514 | if ![skip_cplus_tests] { |
| 515 | if { [build_inferior "${binfile}-cxx" "c++"] < 0 } { |
| 516 | return -1 |
| 517 | } |
| 518 | with_test_prefix "c++" { |
| 519 | test_subscript_regression "${binfile}-cxx" "c++" |
| 520 | } |
| 521 | } |