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