1# Copyright (C) 2008-2024 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 21require allow_python_tests 22 23standard_testfile 24 25set has_argv0 [gdb_has_argv0] 26 27# Build inferior to language specification. 28# LANG is one of "c" or "c++". 29proc build_inferior {exefile lang} { 30 global srcdir subdir srcfile testfile hex 31 32 # Use different names for .o files based on the language. 33 # For Fission, the debug info goes in foo.dwo and we don't want, 34 # for example, a C++ compile to clobber the dwo of a C compile. 35 # ref: http://gcc.gnu.org/wiki/DebugFission 36 switch ${lang} { 37 "c" { set filename ${testfile}.o } 38 "c++" { set filename ${testfile}-cxx.o } 39 } 40 set objfile [standard_output_file $filename] 41 42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${objfile}" object "debug $lang"] != "" 43 || [gdb_compile "${objfile}" "${exefile}" executable "debug $lang"] != "" } { 44 untested "failed to compile in $lang mode" 45 return -1 46 } 47 return 0 48} 49 50proc test_value_creation {} { 51 global gdb_prompt 52 53 gdb_py_test_silent_cmd "python i = gdb.Value (True)" "create boolean value" 1 54 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create integer value" 1 55 gdb_py_test_silent_cmd "python i = gdb.Value (3,None)" "create integer value, with None type" 1 56 57 gdb_py_test_silent_cmd "python l = gdb.Value(0xffffffff12345678)" "create large unsigned 64-bit value" 1 58 gdb_test "python print (int(l))" "18446744069720004216" "large unsigned 64-bit int conversion to python" 59 60 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create double value" 1 61 gdb_py_test_silent_cmd "python a = gdb.Value ('string test')" "create 8-bit string value" 1 62 gdb_test "python print (a)" "\"string test\"" "print 8-bit string" 63 gdb_test "python print (a.__class__)" "<(type|class) 'gdb.Value'>" "verify type of 8-bit string" 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 # Test creating / printing an optimized out value 69 gdb_test "python print(gdb.Value(gdb.Value(5).type.optimized_out()))" \ 70 "<optimized out>" 71} 72 73# Check that we can call gdb.Value.__init__ to change a value. 74proc test_value_reinit {} { 75 gdb_py_test_silent_cmd "python v = gdb.Value (3)" \ 76 "create initial integer value" 1 77 gdb_test "python print(v)" "3" \ 78 "check initial value contents" 79 gdb_py_test_silent_cmd "python v.__init__(5)" \ 80 "call gdb.Value.__init__ manually" 1 81 gdb_test "python print(v)" "5" \ 82 "check new value contents" 83} 84 85proc test_value_numeric_ops {} { 86 global gdb_prompt 87 88 gdb_py_test_silent_cmd "python i = gdb.Value (5)" "create first integer value" 0 89 gdb_py_test_silent_cmd "python j = gdb.Value (2)" "create second integer value" 0 90 gdb_py_test_silent_cmd "python f = gdb.Value (1.25)" "create first double value" 0 91 gdb_py_test_silent_cmd "python g = gdb.Value (2.5)" "create second double value" 0 92 gdb_test "python print ('result = ' + str(i+j))" " = 7" "add two integer values" 93 gdb_test "python print ((i+j).__class__)" "<(type|class) 'gdb.Value'>" "verify type of integer add result" 94 95 gdb_test "python print ('result = ' + str(f+g))" " = 3.75" "add two double values" 96 gdb_test "python print ('result = ' + str(i-j))" " = 3" "subtract two integer values" 97 gdb_test "python print ('result = ' + str(f-g))" " = -1.25" "subtract two double values" 98 gdb_test "python print ('result = ' + str(i*j))" " = 10" "multiply two integer values" 99 gdb_test "python print ('result = ' + str(f*g))" " = 3.125" "multiply two double values" 100 gdb_test "python print ('result = ' + str(i/j))" " = 2" "divide two integer values" 101 gdb_test "python print ('result = ' + str(f/g))" " = 0.5" "divide two double values" 102 gdb_test "python print ('result = ' + str(i%j))" " = 1" "take remainder of two integer values" 103 # Remainder of float is implemented in Python but not in GDB's value system. 104 105 gdb_test "python print ('result = ' + str(i**j))" " = 25" "integer value raised to the power of another integer value" 106 gdb_test "python print ('result = ' + str(g**j))" " = 6.25" "double value raised to the power of integer value" 107 108 gdb_test "python print ('result = ' + str(-i))" " = -5" "negated integer value" 109 gdb_test "python print ('result = ' + str(+i))" " = 5" "positive integer value" 110 gdb_test "python print ('result = ' + str(-f))" " = -1.25" "negated double value" 111 gdb_test "python print ('result = ' + str(+f))" " = 1.25" "positive double value" 112 gdb_test "python print ('result = ' + str(abs(j-i)))" " = 3" "absolute of integer value" 113 gdb_test "python print ('result = ' + str(abs(f-g)))" " = 1.25" "absolute of double value" 114 115 # Test gdb.Value mixed with Python types. 116 117 gdb_test "python print ('result = ' + str(i-1))" " = 4" "subtract integer value from python integer" 118 gdb_test "python print ((i-1).__class__)" "<(type|class) 'gdb.Value'>" "verify type of mixed integer subtraction result" 119 gdb_test "python print ('result = ' + str(f+1.5))" " = 2.75" "add double value with python float" 120 121 gdb_test "python print ('result = ' + str(1-i))" " = -4" "subtract python integer from integer value" 122 gdb_test "python print ('result = ' + str(1.5+f))" " = 2.75" "add python float with double value" 123 124 # Conversion test. 125 gdb_test "print evalue" " = TWO" 126 gdb_test_no_output "python evalue = gdb.history (0)" 127 gdb_test "python print (int (evalue))" "2" 128 129 # Test pointer arithmethic 130 131 # First, obtain the pointers 132 gdb_test "print (void *) 2" ".*" "" 133 gdb_test_no_output "python a = gdb.history (0)" "" 134 gdb_test "print (void *) 5" ".*" "" 135 gdb_test_no_output "python b = gdb.history (0)" "" 136 137 gdb_test "python print(int(b))" "5" "convert pointer to int" 138 139 gdb_test "python print ('result = ' + str(a+5))" " = 0x7( <.*>)?" "add pointer value with python integer" 140 gdb_test "python print ('result = ' + str(b-2))" " = 0x3( <.*>)?" "subtract python integer from pointer value" 141 gdb_test "python print ('result = ' + str(b-a))" " = 3" "subtract two pointer values" 142 143 gdb_test "python print ('result = ' + 'result'\[gdb.Value(0)\])" \ 144 "result = r" "use value as string index" 145 gdb_test "python print ('result = ' + str((1,2,3)\[gdb.Value(0)\]))" \ 146 "result = 1" "use value as tuple index" 147 gdb_test "python print ('result = ' + str(\[1,2,3\]\[gdb.Value(0)\]))" \ 148 "result = 1" "use value as array index" 149 150 gdb_test "python print('%x' % int(gdb.parse_and_eval('-1ull')))" \ 151 "f+" "int conversion respect type sign" 152 153 # Test some invalid operations. 154 155 gdb_test_multiple "python print ('result = ' + str(i+'foo'))" "catch error in python type conversion" { 156 -re "Argument to arithmetic operation not a number or boolean.*$gdb_prompt $" {pass "catch error in python type conversion"} 157 -re "result = .*$gdb_prompt $" {fail "catch error in python type conversion"} 158 -re "$gdb_prompt $" {fail "catch error in python type conversion"} 159 } 160 161 gdb_test_multiple "python print ('result = ' + str(i+gdb.Value('foo')))" "catch throw of GDB error" { 162 -re "Error occurred in Python.*$gdb_prompt $" {pass "catch throw of GDB error"} 163 -re "result = .*$gdb_prompt $" {fail "catch throw of GDB error"} 164 -re "$gdb_prompt $" {fail "catch throw of GDB error"} 165 } 166} 167 168proc test_value_boolean {} { 169 # First, define a useful function to test booleans. 170 gdb_test_multiline "define function to test booleans" \ 171 "python" "" \ 172 "def test_bool (val):" "" \ 173 " if val:" "" \ 174 " print ('yay')" "" \ 175 " else:" "" \ 176 " print ('nay')" "" \ 177 "end" "" 178 179 gdb_test "py test_bool (gdb.Value (True))" "yay" "check evaluation of true boolean value in expression" 180 181 gdb_test "py test_bool (gdb.Value (False))" "nay" "check evaluation of false boolean value in expression" 182 183 gdb_test "py test_bool (gdb.Value (5))" "yay" "check evaluation of true integer value in expression" 184 185 gdb_test "py test_bool (gdb.Value (0))" "nay" "check evaluation of false integer value in expression" 186 187 gdb_test "py test_bool (gdb.Value (5.2))" "yay" "check evaluation of true float value in expression" 188 189 gdb_test "py test_bool (gdb.Value (0.0))" "nay" "check evaluation of false float value in expression" 190} 191 192proc test_value_compare {} { 193 gdb_test "py print (gdb.Value (1) < gdb.Value (1))" "False" "less than, equal" 194 gdb_test "py print (gdb.Value (1) < gdb.Value (2))" "True" "less than, less" 195 gdb_test "py print (gdb.Value (2) < gdb.Value (1))" "False" "less than, greater" 196 gdb_test "py print (gdb.Value (2) < None)" "False" "less than, None" 197 198 gdb_test "py print (gdb.Value (1) <= gdb.Value (1))" "True" "less or equal, equal" 199 gdb_test "py print (gdb.Value (1) <= gdb.Value (2))" "True" "less or equal, less" 200 gdb_test "py print (gdb.Value (2) <= gdb.Value (1))" "False" "less or equal, greater" 201 gdb_test "py print (gdb.Value (2) <= None)" "False" "less or equal, None" 202 203 gdb_test "py print (gdb.Value (1) == gdb.Value (1))" "True" "equality of gdb.Values" 204 gdb_test "py print (gdb.Value (1) == gdb.Value (2))" "False" "inequality of gdb.Values" 205 gdb_test "py print (gdb.Value (1) == 1.0)" "True" "equality of gdb.Value with Python value" 206 gdb_test "py print (gdb.Value (1) == 2)" "False" "inequality of gdb.Value with Python value" 207 gdb_test "py print (gdb.Value (1) == None)" "False" "inequality of gdb.Value with None" 208 209 gdb_test "py print (gdb.Value (1) != gdb.Value (1))" "False" "inequality, false" 210 gdb_test "py print (gdb.Value (1) != gdb.Value (2))" "True" "inequality, true" 211 gdb_test "py print (gdb.Value (1) != None)" "True" "inequality, None" 212 213 gdb_test "py print (gdb.Value (1) > gdb.Value (1))" "False" "greater than, equal" 214 gdb_test "py print (gdb.Value (1) > gdb.Value (2))" "False" "greater than, less" 215 gdb_test "py print (gdb.Value (2) > gdb.Value (1))" "True" "greater than, greater" 216 gdb_test "py print (gdb.Value (2) > None)" "True" "greater than, None" 217 218 gdb_test "py print (gdb.Value (1) >= gdb.Value (1))" "True" "greater or equal, equal" 219 gdb_test "py print (gdb.Value (1) >= gdb.Value (2))" "False" "greater or equal, less" 220 gdb_test "py print (gdb.Value (2) >= gdb.Value (1))" "True" "greater or equal, greater" 221 gdb_test "py print (gdb.Value (2) >= None)" "True" "greater or equal, None" 222} 223 224proc test_value_in_inferior {} { 225 global gdb_prompt 226 global testfile 227 228 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] 229 gdb_continue_to_breakpoint "break to inspect struct and union" 230 231 # Just get inferior variable s in the value history, available to python. 232 gdb_test "print s" " = {a = 3, b = 5}" "" 233 234 gdb_py_test_silent_cmd "python s = gdb.history (0)" "get value s from history" 1 235 236 gdb_test "python print ('result = ' + str(s\['a'\]))" " = 3" "access element inside struct using 8-bit string name" 237 238 # Test dereferencing the argv pointer 239 240 # Just get inferior variable argv the value history, available to python. 241 gdb_test "print argv" " = \\(char \\*\\*\\) 0x.*" "" 242 243 gdb_py_test_silent_cmd "python argv = gdb.history (0)" "" 0 244 gdb_py_test_silent_cmd "python arg0 = argv.dereference ()" "dereference value" 1 245 246 # Check that the dereferenced value is sane 247 global has_argv0 248 set test "verify dereferenced value" 249 if { $has_argv0 } { 250 gdb_test_no_output "set print elements unlimited" "" 251 gdb_test_no_output "set print repeats unlimited" "" 252 gdb_test "python print (arg0)" "0x.*$testfile\"" $test 253 } else { 254 unsupported $test 255 } 256 257 # Smoke-test is_optimized_out attribute 258 gdb_test "python print ('result = %s' % arg0.is_optimized_out)" "= False" "test is_optimized_out attribute" 259 260 # Test address attribute 261 gdb_test "python print ('result = %s' % arg0.address)" "= 0x\[\[:xdigit:\]\]+" "test address attribute" 262 263 # Test displaying a variable that is temporarily at a bad address. 264 # But if we can examine what's at memory address 0, then we'll also be 265 # able to display it without error. Don't run the test in that case. 266 set can_read_0 [is_address_zero_readable] 267 268 # Test memory error. 269 set test "parse_and_eval with memory error" 270 if {$can_read_0} { 271 untested $test 272 } else { 273 gdb_test "python print (gdb.parse_and_eval('*(int*)0'))" "gdb.MemoryError.*: Cannot access memory at address 0x0.*" $test 274 } 275 276 # Test Python lazy value handling 277 set test "memory error and lazy values" 278 if {$can_read_0} { 279 untested $test 280 } else { 281 gdb_test "python inval = gdb.parse_and_eval('*(int*)0')" 282 gdb_test "python print (inval.is_lazy)" "True" 283 gdb_test "python inval2 = inval+1" \ 284 "gdb.MemoryError.*: Cannot access memory at address 0x0.*" \ 285 "$test, first test" 286 gdb_test "python inval.fetch_lazy ()" \ 287 "gdb.MemoryError.*: Cannot access memory at address 0x0.*" \ 288 "$test, second test" 289 } 290 set argc_value [get_integer_valueof "argc" 0] 291 gdb_test "python argc_lazy = gdb.parse_and_eval('argc')" 292 gdb_test "python argc_notlazy = gdb.parse_and_eval('argc')" 293 gdb_test "python argc_notlazy.fetch_lazy()" 294 gdb_test "python print (argc_lazy.is_lazy)" "True" \ 295 "python print (argc_lazy.is_lazy) the first time" 296 gdb_test "python print (argc_notlazy.is_lazy)" "False" 297 gdb_test "print argc" " = $argc_value" "sanity check argc" 298 gdb_test "python print (argc_lazy.is_lazy)" "\r\nTrue" \ 299 "python print (argc_lazy.is_lazy) the second time" 300 gdb_test_no_output "set argc=[expr $argc_value + 1]" "change argc" 301 gdb_test "python print (argc_notlazy)" "\r\n$argc_value" 302 gdb_test "python print (argc_lazy)" "\r\n[expr $argc_value + 1]" 303 gdb_test "python print (argc_lazy.is_lazy)" "False" 304 305 # Test string fetches, both partial and whole. 306 gdb_test "print st" "\"divide et impera\"" 307 gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value st from history" 1 308 gdb_test "python print (st.string ())" "divide et impera" "Test string with no length" 309 gdb_test "python print (st.string (length = -1))" "divide et impera" "test string (length = -1) is all of the string" 310 gdb_test "python print (st.string (length = 6))" "divide" 311 gdb_test "python print (\"---\"+st.string (length = 0)+\"---\")" "------" "test string (length = 0) is empty" 312 gdb_test "python print (len(st.string (length = 0)))" "0" "test length is 0" 313 314 # We choose Ada here to test a language where c_style_arrays is 315 # false. 316 gdb_test "set lang ada" \ 317 "Warning: the current language does not match this frame." 318 gdb_test "python print (st.string ())" "divide et impera" \ 319 "Test string with no length in ada" 320 gdb_test_no_output "set lang auto" 321 322 # Fetch a string that has embedded nulls. 323 gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*" 324 gdb_py_test_silent_cmd "python nullst = gdb.history (0)" "get value nullst from history" 1 325 gdb_test "python print (nullst.string ())" "divide" "test string to first null" 326 # Python cannot print strings that contain the null (\0) character. 327 # For the purposes of this test, use repr() 328 gdb_py_test_silent_cmd "python nullst = nullst.string (length = 9)" "get string beyond null" 1 329 gdb_test "python print (repr(nullst))" "u?'divide\\\\x00et'" 330 331 # Test fetching a string longer than its declared (in C) size. 332 # PR 16286 333 gdb_py_test_silent_cmd "python xstr = gdb.parse_and_eval('xstr')" "get xstr" 1 334 gdb_test "python print(xstr\['text'\].string (length = xstr\['length'\]))" "x{100}" \ 335 "read string beyond declared size" 336 337 # However it shouldn't be possible to fetch past the end of a 338 # non-memory value. 339 gdb_py_test_silent_cmd "python str = '\"str\"'" "set up str variable" 1 340 gdb_test "python print (gdb.parse_and_eval (str).string (length = 10))" \ 341 "gdb.error.*: Attempt to take address of value not located in memory.\r\nError occurred in Python.*" 342 343 gdb_test "python print (gdb.parse_and_eval ('shadowed'))" \ 344 97 "shadowed local value" 345 gdb_test "python print (gdb.parse_and_eval ('shadowed', global_context=True))" \ 346 23 "shadowed global value" 347} 348 349proc test_inferior_function_call {} { 350 global gdb_prompt hex decimal 351 352 # Correct inferior call without arguments. 353 gdb_test "p/x fp1" " = $hex.*" 354 gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value fp1 from history" 1 355 gdb_test "python fp1 = fp1.dereference()" "" 356 gdb_test "python result = fp1()" "" 357 gdb_test "python print (result)" "void" 358 359 # Correct inferior call with arguments. 360 gdb_test "p/x fp2" " = $hex.*" \ 361 "print fp2 to place it into history" 362 gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value fp2 from history" 1 363 gdb_test "python fp2 = fp2.dereference()" "" 364 gdb_test "python result2 = fp2(10,20)" "" 365 gdb_test "python print (result2)" "30" 366 367 # Incorrect to call an int value. 368 gdb_test "p i" " = $decimal.*" 369 gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value i from history" 1 370 gdb_test "python result3 = i()" ".*Value is not callable.*" 371 372 # Incorrect number of arguments. 373 gdb_test "p/x fp2" " = $hex.*" \ 374 "print fp2 again to place it into history" 375 gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value fp3 from history" 1 376 gdb_test "python fp3 = fp3.dereference()" "" 377 gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*" 378} 379 380# A few objfile tests. 381proc test_objfiles {} { 382 gdb_test "python\nok=False\nfor file in gdb.objfiles():\n if 'py-value' in file.filename:\n ok=True\nprint (ok)\nend" "True" \ 383 "py-value in file.filename" 384 385 gdb_test "python print (gdb.objfiles()\[0\].pretty_printers)" "\\\[\\\]" 386 387 gdb_test "python gdb.objfiles()\[0\].pretty_printers = 0" \ 388 "pretty_printers attribute must be a list.*Error occurred in Python.*" 389} 390 391proc test_value_after_death {} { 392 # Construct a type while the inferior is still running. 393 gdb_py_test_silent_cmd "python ptrtype = gdb.lookup_type('PTR')" \ 394 "create PTR type" 1 395 396 # Kill the inferior and remove the symbols. 397 gdb_test "kill" "" "kill the inferior" \ 398 "Kill the program being debugged. .y or n. $" \ 399 "y" 400 gdb_test "file" "" "discard the symbols" \ 401 "Discard symbol table from.*y or n. $" \ 402 "y" 403 404 # Now create a value using that type. Relies on arg0, created by 405 # test_value_in_inferior. 406 gdb_py_test_silent_cmd "python castval = arg0.cast(ptrtype.pointer())" \ 407 "cast arg0 to PTR" 1 408 409 # Make sure the type is deleted. 410 gdb_py_test_silent_cmd "python ptrtype = None" \ 411 "delete PTR type" 1 412 413 # Now see if the value's type is still valid. 414 gdb_test "python print (castval.type)" "PTR ." \ 415 "print value's type" 416} 417 418# Regression test for invalid subscript operations. The bug was that 419# the type of the value was not being checked before allowing a 420# subscript operation to proceed. 421 422proc test_subscript_regression {exefile lang} { 423 # Start with a fresh gdb. 424 clean_restart ${exefile} 425 426 if {![runto_main]} { 427 return 428 } 429 430 if {$lang == "c++"} { 431 gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"] 432 gdb_continue_to_breakpoint "break to inspect pointer by reference" 433 434 gdb_py_test_silent_cmd "print rptr_int" \ 435 "Obtain address" 1 436 gdb_py_test_silent_cmd "python rptr = gdb.history(0)" \ 437 "Obtains value from GDB" 1 438 gdb_test "python print (rptr\[0\])" "2" "check pointer passed as reference" 439 440 # Just the most basic test of dynamic_cast -- it is checked in 441 # the C++ tests. 442 gdb_test "python print (bool(gdb.parse_and_eval('base').dynamic_cast(gdb.lookup_type('Derived').pointer())))" \ 443 True 444 445 # Likewise. 446 gdb_test "python print (gdb.parse_and_eval('base').dynamic_type)" \ 447 "Derived \[*\]" 448 gdb_test "python print (gdb.parse_and_eval('base_ref').dynamic_type)" \ 449 "Derived \[&\]" 450 # A static type case. 451 gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \ 452 "int" 453 } 454 455 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"] 456 gdb_continue_to_breakpoint \ 457 "break to inspect struct and union for subscript regression test" 458 459 gdb_py_test_silent_cmd "python intv = gdb.Value(1)" \ 460 "Create value intv for subscript test" 1 461 gdb_py_test_silent_cmd "python stringv = gdb.Value(\"foo\")" \ 462 "Create value stringv for subscript test" 1 463 464 # Try to access an int with a subscript. This should fail. 465 gdb_test "python print (intv)" "1" "baseline print of an int Python value" 466 gdb_test "python print (intv\[0\])" "gdb.error.*: Cannot subscript requested type.*" \ 467 "Attempt to access an integer with a subscript" 468 469 # Try to access a string with a subscript. This should pass. 470 gdb_test "python print (stringv)" "foo." "baseline print of a string Python value" 471 gdb_test "python print (stringv\[0\])" "f." "attempt to access a string with a subscript" 472 473 # Try to access an int array via a pointer with a subscript. This should pass. 474 gdb_py_test_silent_cmd "print p" "Build pointer to array" 1 475 gdb_py_test_silent_cmd "python pointer = gdb.history(0)" "fetch pointer" 0 476 gdb_test "python print (pointer\[0\])" "1" "access array via pointer with int subscript" 477 gdb_test "python print (pointer\[intv\])" "2" "access array via pointer with value subscript" 478 479 # Try to access a single dimension array with a subscript to the 480 # result. This should fail. 481 gdb_test "python print (pointer\[intv\]\[0\])" "gdb.error.*: Cannot subscript requested type.*" \ 482 "Attempt to access a single dimension array with a two subscripts" 483 484 # Lastly, test subscript access to an array with multiple 485 # dimensions. This should pass. 486 gdb_py_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array" 1 487 gdb_py_test_silent_cmd "python marray = gdb.history(0)" "fetch marray" 0 488 gdb_test "python print (marray\[1\]\[2\])" "o." "test multiple subscript" 489} 490 491# A few tests of gdb.parse_and_eval. 492proc test_parse_and_eval {} { 493 gdb_test "python print (gdb.parse_and_eval ('23'))" "23" \ 494 "parse_and_eval constant test" 495 gdb_test "python print (gdb.parse_and_eval ('5 + 7'))" "12" \ 496 "parse_and_eval simple expression test" 497 gdb_test "python print (type(gdb.parse_and_eval ('5 + 7')))" \ 498 ".(type|class) 'gdb.Value'."\ 499 "parse_and_eval type test" 500} 501 502# Test that values are hashable. 503proc test_value_hash {} { 504 gdb_test_multiline "Simple Python value dictionary" \ 505 "python" "" \ 506 "one = gdb.Value(1)" "" \ 507 "two = gdb.Value(2)" "" \ 508 "three = gdb.Value(3)" "" \ 509 "vdict = {one:\"one str\",two:\"two str\",three:\"three str\"}" "" \ 510 "end" 511 gdb_test "python print (vdict\[one\])" "one str" "test dictionary hash for one" 512 gdb_test "python print (vdict\[two\])" "two str" "test dictionary hash for two" 513 gdb_test "python print (vdict\[three\])" "three str" "test dictionary hash for three" 514 gdb_test "python print (one.__hash__() == hash(one))" "True" "test inbuilt hash" 515} 516 517proc test_float_conversion {} { 518 gdb_test "python print(int(gdb.Value(0)))" "0" 519 gdb_test "python print(int(gdb.Value(2.5)))" "2" 520 gdb_test "python print(float(gdb.Value(2.5)))" "2\\.5" 521 gdb_test "python print(float(gdb.Value(0)))" "0\\.0" 522} 523 524# Setup some Python variables: 525# tp : a gdb.Type for 'int', 526# size_a : the size of array 'a' from the inferior, 527# size_a0 : the size of array element 'a[0] from the inferior, 528# addr : the address of 'a[0]' from the inferior, 529# b : a buffer containing the full contents of array 'a' from the 530# inferior. 531proc prepare_type_and_buffer {} { 532 gdb_py_test_silent_cmd "python tp=gdb.lookup_type('int')" "look up int type" 0 533 gdb_py_test_silent_cmd "python size_a=gdb.parse_and_eval('sizeof(a)')" \ 534 "find size of a" 0 535 gdb_py_test_silent_cmd "python size_a0=gdb.parse_and_eval('sizeof(a\[0\])')" \ 536 "find size of element of a" 0 537 gdb_py_test_silent_cmd "python addr=gdb.parse_and_eval('&a')" \ 538 "find address of a" 0 539 gdb_py_test_silent_cmd "python b=gdb.selected_inferior().read_memory(addr,size_a)" \ 540 "read buffer from memory" 0 541} 542 543proc test_value_from_buffer {} { 544 global gdb_prompt 545 546 # A Python helper function. Create a bytes object from inferior 547 # memory LEN bytes starting at ADDR, and compare this to the bytes 548 # obtained from VAL.bytes. Assert that the two bytes object match. 549 gdb_test_multiline "Create another function to check Value.bytes" \ 550 "python" "" \ 551 "def compare_value_bytes_to_mem(val, addr, len):" "" \ 552 " mem = gdb.selected_inferior().read_memory(addr, len)" "" \ 553 " mem_bytes = mem.tobytes()" "" \ 554 " val_bytes = val.bytes" "" \ 555 " assert mem_bytes == val_bytes" "" \ 556 "end" "" 557 558 prepare_type_and_buffer 559 gdb_test "python v=gdb.Value(b,tp); print(v)" "1" \ 560 "construct value from buffer" 561 gdb_test_no_output { python compare_value_bytes_to_mem(v, addr, size_a0) } 562 gdb_test "python v=gdb.Value(b\[size_a0:\],tp); print(v)" "2" \ 563 "convert 2nd elem of buffer to value" 564 gdb_test_no_output \ 565 { python compare_value_bytes_to_mem(v, (int(addr) + size_a0), size_a0) } 566 gdb_test "python v=gdb.Value(b\[2*size_a0:\],tp); print(v)" "3" \ 567 "convert 3rd elem of buffer to value" 568 gdb_test_no_output \ 569 { python compare_value_bytes_to_mem(v, (int(addr) + (2 * size_a0)), size_a0) } 570 gdb_test "python v=gdb.Value(b\[2*size_a0+1:\],tp); print(v)" \ 571 "ValueError.*: Size of type is larger than that of buffer object\..*" \ 572 "attempt to convert smaller buffer than size of type" 573 gdb_py_test_silent_cmd "python atp=tp.array(2) ; print(atp)" \ 574 "make array type" 0 575 gdb_py_test_silent_cmd "python va=gdb.Value(b,atp)" \ 576 "construct array value from buffer" 0 577 gdb_test_no_output \ 578 { python compare_value_bytes_to_mem(va, addr, size_a0 * 3) } 579 gdb_test "python print(va)" "\\{1, 2, 3\\}" "print array value" 580 gdb_test "python print(va\[0\])" "1" "print first array element" 581 gdb_test "python print(va\[1\])" "2" "print second array element" 582 gdb_test "python print(va\[2\])" "3" "print third array element" 583 gdb_test "python print(va\[3\])" "gdb\.error.*: no such vector element.*" \ 584 "print out of bounds array element" 585 gdb_py_test_silent_cmd "python atpbig=tp.array(3)" "make bigger array type" 0 586 gdb_test "python vabig=gdb.Value(b,atpbig)" \ 587 "ValueError.*: Size of type is larger than that of buffer object\..*" \ 588 "attempt to construct large value with small buffer" 589 gdb_test "python v=gdb.Value(2048,tp)" \ 590 "TypeError.*: Object must support the python buffer protocol\..*" \ 591 "attempt to construct value from buffer with non-buffer object" 592 gdb_test "python v=gdb.Value(b,'int'); print(v)" \ 593 "TypeError.*: type argument must be a gdb\.Type\..*" \ 594 "attempt to construct value with string as type" 595} 596 597# Test the gdb.add_history API. 598proc test_add_to_history {} { 599 # Add a gdb.Value to the value history list. 600 gdb_test_no_output "python idx = gdb.add_history(gdb.Value(42))" \ 601 "add value 42 to the history list" 602 gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \ 603 " = 42" "print value 42 from the history list" 604 set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 42"] 605 gdb_test "print \$${idx}" " = 42" 606 607 # Add something to the history list that can be converted into a 608 # gdb.Value. 609 gdb_test_no_output "python idx = gdb.add_history(84)" \ 610 "add value to 84 to the history list" 611 gdb_test "python print (\"$%d = %s\" % (idx, gdb.history (idx)))" \ 612 " = 84" "print value 84 from the history list" 613 set idx [get_python_valueof "idx" "**DEFAULT**" "get idx for value 84"] 614 gdb_test "print \$${idx}" " = 84" 615 616 # Try adding something that can't be converted to a gdb.Value, 617 # this should give an error. 618 gdb_test "python idx = gdb.add_history(gdb.GdbError(\"an error\"))" \ 619 "TypeError.*: Could not convert Python object: .*" 620} 621 622# Check we can create sub-classes of gdb.Value. 623proc test_value_sub_classes {} { 624 prepare_type_and_buffer 625 626 gdb_test_multiline "Create sub-class of gdb.Value" \ 627 "python" "" \ 628 "class MyValue(gdb.Value):" "" \ 629 " def __init__(self,val,type=None):" "" \ 630 " gdb.Value.__init__(self,val,type)" "" \ 631 " print(\"In MyValue.__init__\")" "" \ 632 "end" 633 634 gdb_test "python obj = MyValue (123)" "In MyValue.__init__" \ 635 "create instance of MyValue" 636 gdb_test "python print(obj)" "123" \ 637 "check printing of MyValue" 638 639 gdb_test "python obj = MyValue(b\[size_a0:\],tp)" "In MyValue.__init__" \ 640 "convert 2nd elem of buffer to a MyValue" 641 gdb_test "python print(obj)" "2" \ 642 "check printing of MyValue when initiaized with a type" 643} 644 645# Test the history count. This must be the first thing called after 646# starting GDB as it depends on there being nothing in the value 647# history. 648proc test_history_count {} { 649 for { set i 0 } { $i < 5 } { incr i } { 650 gdb_test "python print('history count is %d' % gdb.history_count())" \ 651 "history count is $i" "history count is $i" 652 gdb_test "print $i" " = $i" 653 } 654} 655 656# Test the gdb.Value.bytes API. 657proc_with_prefix test_value_bytes { } { 658 # Test accessing the bytes of an optimised out value. 659 gdb_test "python print(gdb.Value(gdb.Value(5).type.optimized_out()).bytes)" \ 660 [multi_line \ 661 "gdb\\.error.*: value has been optimized out" \ 662 "Error occurred in Python.*"] 663 664 # A Python helper function. Fetch VAR_NAME from the inferior as a 665 # gdb.Value. Read the bytes of the value based on its address, and 666 # the size of its type. The compare these bytes to the value 667 # obtained from gdb.Value.bytes. Assert that the two bytes objects 668 # match. 669 gdb_test_multiline "Create a function to check Value.bytes" \ 670 "python" "" \ 671 "def check_value_bytes(var_name):" "" \ 672 " val = gdb.parse_and_eval(var_name)" "" \ 673 " addr = val.address" "" \ 674 " len = val.type.sizeof" "" \ 675 " mem = gdb.selected_inferior().read_memory(addr, len)" "" \ 676 " mem_bytes = mem.tobytes()" "" \ 677 " val_bytes = val.bytes" "" \ 678 " assert mem_bytes == val_bytes" "" \ 679 "end" "" 680 681 gdb_test_no_output { python check_value_bytes("a") } 682 gdb_test_no_output { python check_value_bytes("p") } 683 gdb_test_no_output { python check_value_bytes("i") } 684 gdb_test_no_output { python check_value_bytes("ptr_i") } 685 gdb_test_no_output { python check_value_bytes("embed") } 686 gdb_test_no_output { python check_value_bytes("fp1") } 687 gdb_test_no_output { python check_value_bytes("nullst") } 688 gdb_test_no_output { python check_value_bytes("st") } 689 gdb_test_no_output { python check_value_bytes("s") } 690 gdb_test_no_output { python check_value_bytes("u") } 691 692 # Check that gdb.Value.bytes changes after calling 693 # gdb.Value.assign(). The bytes value is cached within the Value 694 # object, so calling assign should clear the cache. 695 with_test_prefix "assign clears bytes cache" { 696 gdb_test_no_output "python v = gdb.parse_and_eval(\"i\")" 697 gdb_test_no_output "python bytes_before = v.bytes" 698 gdb_test_no_output "python v.assign(9)" 699 gdb_test_no_output "python bytes_after = v.bytes" 700 gdb_test_no_output "python assert(bytes_after != bytes_before)" 701 } 702 703 # Check that if we re-init a gdb.Value object the cached bytes for 704 # the Value are cleared. 705 with_test_prefix "re-init clears bytes cache" { 706 gdb_test_no_output "python w = gdb.Value(1)" 707 gdb_test_no_output "python bytes_before = w.bytes" 708 gdb_test_no_output "python w.__init__(3)" 709 gdb_test_no_output "python bytes_after = w.bytes" 710 gdb_test_no_output "python assert(bytes_after != bytes_before)" 711 } 712 713 # Check that we can assign to the Value.bytes field. 714 gdb_test_no_output "python i_value = gdb.parse_and_eval('i')" \ 715 "evaluate i" 716 gdb_test_no_output "python i_bytes = i_value.bytes" 717 gdb_test_no_output "python i_bytes = bytes(\[b if b != 9 else 5 for b in i_bytes\])" 718 gdb_test_no_output "python i_value.bytes = i_bytes" 719 gdb_test "print i" " = 5" 720 721 # Check we get an exception if attempting to assign a buffer that is 722 # too big, or too small. 723 gdb_test_no_output "python bytes_as_int = \[x for x in i_bytes\]" 724 gdb_test_no_output "python bytes_as_int.append(0)" 725 gdb_test_no_output "python too_many_bytes = bytes(bytes_as_int)" 726 gdb_test "python i_value.bytes = too_many_bytes" \ 727 "ValueError.*: Size of type is not equal to that of buffer object\\..*" 728 gdb_test_no_output "python bytes_as_int = bytes_as_int\[0:-2\]" 729 gdb_test_no_output "python too_few_bytes = bytes(bytes_as_int)" 730 gdb_test "python i_value.bytes = too_few_bytes" \ 731 "ValueError.*: Size of type is not equal to that of buffer object\\..*" 732 733 # Check we get an exception writing to a not_lval. 734 gdb_test_no_output "python i_value = gdb.Value(9)" \ 735 "reset i_value" 736 gdb_test_no_output "python i_bytes = i_value.bytes" \ 737 "grab new value bytes" 738 gdb_test "python i_value.bytes = i_bytes" "not an lvalue.*" \ 739 "cannot assign to not_lval value" 740} 741 742# Test Value.assign. 743proc test_assign {} { 744 gdb_test_no_output "python i_value = gdb.parse_and_eval('i')" \ 745 "evaluate i" 746 gdb_test_no_output "python i_value.assign(27)" \ 747 "set i to 27" 748 gdb_test "print i" " = 27" 749 gdb_test_no_output "python i_value = gdb.Value(27)" \ 750 "reset i_value" 751 gdb_test "python i_value.assign(89)" "not an lvalue.*" \ 752 "cannot assign to integer" 753} 754 755# Build C version of executable. C++ is built later. 756if { [build_inferior "${binfile}" "c"] < 0 } { 757 return -1 758} 759 760# Start with a fresh gdb. 761clean_restart ${binfile} 762 763test_history_count 764test_value_creation 765test_value_reinit 766test_value_numeric_ops 767test_value_boolean 768test_value_compare 769test_objfiles 770test_parse_and_eval 771test_value_hash 772test_float_conversion 773test_add_to_history 774 775# The following tests require execution. 776 777if {![runto_main]} { 778 return 0 779} 780 781test_value_in_inferior 782test_value_from_buffer 783test_value_sub_classes 784test_inferior_function_call 785test_assign 786test_value_bytes 787test_value_after_death 788 789# Test either C or C++ values. 790 791test_subscript_regression "${binfile}" "c" 792 793if {[allow_cplus_tests]} { 794 if { [build_inferior "${binfile}-cxx" "c++"] < 0 } { 795 return -1 796 } 797 with_test_prefix "c++" { 798 test_subscript_regression "${binfile}-cxx" "c++" 799 } 800} 801