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