bd5b14999d8a1a3b38d2d54a0040054c35be876a
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.guile / scm-value.exp
1 # Copyright (C) 2008-2021 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.
17 # It tests the mechanism exposing values to Guile.
18
19 load_lib gdb-guile.exp
20
21 standard_testfile
22
23 set has_argv0 [gdb_has_argv0]
24
25 # Build inferior to language specification.
26 # LANG is one of "c" or "c++".
27 proc 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
48 proc test_value_in_inferior {} {
49 global gdb_prompt
50 global testfile
51
52 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
53
54 gdb_continue_to_breakpoint "break to inspect struct and union"
55
56 # Just get inferior variable s in the value history, available to guile.
57 gdb_test "print s" "= {a = 3, b = 5}" ""
58
59 gdb_scm_test_silent_cmd "gu (define s (history-ref 0))" "set s"
60
61 gdb_test "gu (print (value-field s \"a\"))" \
62 "= 3" "access element inside struct using string name"
63
64 # Append value in the value history.
65 gdb_scm_test_silent_cmd "gu (define i (history-append! (make-value 42)))" \
66 "append 42"
67
68 gdb_test "gu i" "\[0-9\]+"
69 gdb_test "gu (history-ref i)" "#<gdb:value 42>"
70 gdb_test "p \$" "= 42"
71
72 # Verify the recorded history value survives a gc.
73 gdb_test_no_output "guile (gc)"
74 gdb_test "p \$\$" "= 42"
75
76 # Make sure 'history-append!' rejects non-value objects.
77 gdb_test "gu (history-append! 123)" \
78 "ERROR:.* Wrong type argument.*" "history-append! type error"
79
80 # Test dereferencing the argv pointer.
81
82 # Just get inferior variable argv the value history, available to guile.
83 gdb_test "print argv" "= \\(char \\*\\*\\) 0x.*" ""
84
85 gdb_scm_test_silent_cmd "gu (define argv (history-ref 0))" \
86 "set argv"
87 gdb_scm_test_silent_cmd "gu (define arg0 (value-dereference argv))" \
88 "set arg0"
89
90 # Check that the dereferenced value is sane.
91 global has_argv0
92 set test "verify dereferenced value"
93 if { $has_argv0 } {
94 gdb_test_no_output "set print elements unlimited" ""
95 gdb_test_no_output "set print repeats unlimited" ""
96 gdb_test "gu (print arg0)" "0x.*$testfile\"" $test
97 } else {
98 unsupported $test
99 }
100
101 # Smoke-test value-optimized-out?.
102 gdb_test "gu (print (value-optimized-out? arg0))" \
103 "= #f" "Test value-optimized-out?"
104
105 # Test address attribute.
106 gdb_test "gu (print (value-address arg0))" \
107 "= 0x\[\[:xdigit:\]\]+" "Test address attribute"
108 # Test address attribute is #f in a non-addressable value.
109 gdb_test "gu (print (value-address (make-value 42)))" \
110 "= #f" "Test address attribute in non-addressable value"
111
112 # Test displaying a variable that is temporarily at a bad address.
113 # But if we can examine what's at memory address 0, then we'll also be
114 # able to display it without error. Don't run the test in that case.
115 set can_read_0 [is_address_zero_readable]
116
117 # Test memory error.
118 set test "parse_and_eval with memory error"
119 if {$can_read_0} {
120 untested $test
121 } else {
122 gdb_test "gu (print (parse-and-eval \"*(int*)0\"))" \
123 "ERROR: Cannot access memory at address 0x0.*" $test
124 }
125
126 # Test Guile lazy value handling
127 set test "memory error and lazy values"
128 if {$can_read_0} {
129 untested $test
130 } else {
131 gdb_test_no_output "gu (define inval (parse-and-eval \"*(int*)0\"))"
132 gdb_test "gu (print (value-lazy? inval))" \
133 "#t"
134 gdb_test "gu (define inval2 (value-add inval 1))" \
135 "ERROR: Cannot access memory at address 0x0.*" \
136 "$test, using value in value-add"
137 gdb_test "gu (value-fetch-lazy! inval))" \
138 "ERROR: Cannot access memory at address 0x0.*" \
139 "$test, using value in value-fetch-lazy!"
140 }
141 gdb_test_no_output "gu (define argc-lazy (parse-and-eval \"argc\"))"
142 gdb_test_no_output "gu (define argc-notlazy (parse-and-eval \"argc\"))"
143 gdb_test_no_output "gu (value-fetch-lazy! argc-notlazy)"
144 gdb_test "gu (print (value-lazy? argc-lazy))" "= #t" \
145 "argc-lazy is initially lazy"
146 gdb_test "gu (print (value-lazy? argc-notlazy))" "= #f"
147 gdb_test "print argc" "= 1" "sanity check argc"
148 gdb_test "gu (print (value-lazy? argc-lazy))" "= #t" \
149 "argc-lazy is still lazy after argc is printed"
150 gdb_test_no_output "set argc=2"
151 gdb_test "gu (print argc-notlazy)" "= 1"
152 gdb_test "gu (print argc-lazy)" "= 2"
153 gdb_test "gu (print (value-lazy? argc-lazy))" "= #f" \
154 "argc-lazy is no longer lazy"
155
156 # Test string fetches, both partial and whole.
157 gdb_test "print st" "\"divide et impera\""
158 gdb_scm_test_silent_cmd "gu (define st (history-ref 0))" \
159 "inf: get st value from history"
160 gdb_test "gu (print (value->string st))" \
161 "= divide et impera" "Test string with no length"
162 gdb_test "gu (print (value->string st #:length -1))" \
163 "= divide et impera" "Test string (length = -1) is all of the string"
164 gdb_test "gu (print (value->string st #:length 6))" \
165 "= divide"
166 gdb_test "gu (print (string-append \"---\" (value->string st #:length 0) \"---\"))" \
167 "= ------" "Test string (length = 0) is empty"
168 gdb_test "gu (print (string-length (value->string st #:length 0)))" \
169 "= 0" "Test length is 0"
170
171 # Fetch a string that has embedded nulls.
172 gdb_test "print nullst" "\"divide\\\\000et\\\\000impera\".*"
173 gdb_scm_test_silent_cmd "gu (define nullst (history-ref 0))" \
174 "inf: get nullst value from history"
175 gdb_test "gu (print (value->string nullst))" \
176 "divide" "Test string to first null"
177 gdb_scm_test_silent_cmd "gu (set! nullst (value->string nullst #:length 9))" \
178 "get string beyond null"
179 gdb_test "gu (print nullst)" \
180 "= divide\\\\000et"
181
182 gdb_scm_test_silent_cmd "gu (define argv-ref (value-reference-value argv))" \
183 "test value-reference-value"
184 gdb_test "gu (equal? argv (value-referenced-value argv-ref))" "#t"
185 gdb_test "gu (eqv? (type-code (value-type argv-ref)) TYPE_CODE_REF)" "#t"
186
187 gdb_scm_test_silent_cmd "gu (define argv-rref (value-rvalue-reference-value argv))" \
188 "test value-rvalue-reference-value"
189 gdb_test "gu (equal? argv (value-referenced-value argv-rref))" "#t"
190 gdb_test "gu (eqv? (type-code (value-type argv-rref)) TYPE_CODE_RVALUE_REF)" "#t"
191
192 gdb_test "gu (equal? (value-type (value-const-value argv)) (type-const (value-type argv)))" \
193 "#t"
194 }
195
196 proc test_strings {} {
197 gdb_test "gu (make-value \"test\")" "#<gdb:value \"test\">" "make string"
198
199 # Test string conversion errors.
200 set save_charset [get_target_charset]
201 gdb_test_no_output "set target-charset UTF-8"
202
203 gdb_test_no_output "gu (set-port-conversion-strategy! #f 'error)"
204 gdb_test "gu (print (value->string (make-value (string #\\x1234)) #:encoding \"ASCII\"))" \
205 "ERROR.*decoding-error.*" \
206 "value->string with default #:errors = 'error"
207
208 # There is no 'escape strategy for C->SCM string conversions, but it's
209 # still a legitimate value for %default-port-conversion-strategy.
210 # GDB handles this by, umm, substituting 'substitute.
211 # Use this case to also handle "#:errors #f" which explicitly says
212 # "use %default-port-conversion-strategy".
213 gdb_test_no_output "gu (set-port-conversion-strategy! #f 'escape)"
214 gdb_test "gu (print (value->string (make-value (string #\\x1234)) #:encoding \"ASCII\" #:errors #f))" \
215 "= \[?\]{3}" "value->string with default #:errors = 'escape"
216
217 # This is last in the default conversion tests so that
218 # %default-port-conversion-strategy ends up with the default value.
219 gdb_test_no_output "gu (set-port-conversion-strategy! #f 'substitute)"
220 gdb_test "gu (print (value->string (make-value (string #\\x1234)) #:encoding \"ASCII\"))" \
221 "= \[?\]{3}" "value->string with default #:errors = 'substitute"
222
223 gdb_test "gu (print (value->string (make-value (string #\\x1234)) #:encoding \"ASCII\" #:errors 'error))" \
224 "ERROR.*decoding-error.*" "value->string #:errors 'error"
225 gdb_test "gu (print (value->string (make-value (string #\\x1234)) #:encoding \"ASCII\" #:errors 'substitute))" \
226 "= \[?\]{3}" "value->string #:errors 'substitute"
227 gdb_test "gu (print (value->string (make-value \"abc\") #:errors \"foo\"))" \
228 "ERROR.*invalid error kind.*" "bad value for #:errors"
229
230 gdb_test_no_output "set target-charset $save_charset" \
231 "restore target-charset"
232 }
233
234 proc test_inferior_function_call {} {
235 global gdb_prompt hex decimal
236
237 # Correct inferior call without arguments.
238 gdb_test "p/x fp1" "= $hex.*"
239 gdb_scm_test_silent_cmd "gu (define fp1 (history-ref 0))" \
240 "get fp1 value from history"
241 gdb_scm_test_silent_cmd "gu (set! fp1 (value-dereference fp1))" \
242 "dereference fp1"
243 gdb_test "gu (print (value-call fp1 '()))" \
244 "= void"
245
246 # Correct inferior call with arguments.
247 gdb_test "p/x fp2" "= $hex.*" \
248 "place fp2 into value history, the first time"
249 gdb_scm_test_silent_cmd "gu (define fp2 (history-ref 0))" \
250 "get fp2 value from history"
251 gdb_scm_test_silent_cmd "gu (set! fp2 (value-dereference fp2))" \
252 "dereference fp2"
253 gdb_test "gu (print (value-call fp2 (list 10 20)))" \
254 "= 30"
255
256 # Incorrect to call an int value.
257 gdb_test "p i" "= $decimal.*"
258 gdb_scm_test_silent_cmd "gu (define i (history-ref 0))" \
259 "inf call: get i value from history"
260 gdb_test "gu (print (value-call i '()))" \
261 "ERROR: .*: Wrong type argument in position 1 \\(expecting function \\(value of TYPE_CODE_FUNC\\)\\): .*"
262
263 # Incorrect number of arguments.
264 gdb_test "p/x fp2" "= $hex.*" \
265 "place fp2 into value history, the second time"
266 gdb_scm_test_silent_cmd "gu (define fp3 (history-ref 0))" \
267 "get fp3 value from history"
268 gdb_scm_test_silent_cmd "gu (set! fp3 (value-dereference fp3))" \
269 "dereference fp3"
270 gdb_test "gu (print (value-call fp3 (list 10)))" \
271 "ERROR: Too few arguments in function call.*"
272 }
273
274 proc test_value_after_death {} {
275 # Construct a type while the inferior is still running.
276 gdb_scm_test_silent_cmd "gu (define ptrtype (lookup-type \"PTR\"))" \
277 "create PTR type"
278
279 # Kill the inferior and remove the symbols.
280 gdb_test "kill" "" "kill the inferior" \
281 "Kill the program being debugged. .y or n. $" \
282 "y"
283 gdb_test "file" "" "discard the symbols" \
284 "Discard symbol table from.*y or n. $" \
285 "y"
286
287 # First do a garbage collect to delete anything unused. PR 16612.
288 gdb_scm_test_silent_cmd "gu (gc)" "garbage collect"
289
290 # Now create a value using that type. Relies on arg0, created by
291 # test_value_in_inferior.
292 gdb_scm_test_silent_cmd "gu (define castval (value-cast arg0 (type-pointer ptrtype)))" \
293 "cast arg0 to PTR"
294
295 # Make sure the type is deleted.
296 gdb_scm_test_silent_cmd "gu (set! ptrtype #f)" \
297 "delete PTR type"
298
299 # Now see if the value's type is still valid.
300 gdb_test "gu (print (value-type castval))" \
301 "= PTR ." "print value's type"
302 }
303
304 # Regression test for invalid subscript operations. The bug was that
305 # the type of the value was not being checked before allowing a
306 # subscript operation to proceed.
307
308 proc test_subscript_regression {exefile lang} {
309 # Start with a fresh gdb.
310 clean_restart ${exefile}
311
312 if ![gdb_guile_runto_main ] {
313 fail "can't run to main"
314 return
315 }
316
317 if {$lang == "c++"} {
318 gdb_breakpoint [gdb_get_line_number "break to inspect pointer by reference"]
319 gdb_continue_to_breakpoint "break to inspect pointer by reference"
320
321 gdb_scm_test_silent_cmd "print rptr_int" \
322 "Obtain address"
323 gdb_scm_test_silent_cmd "gu (define rptr (history-ref 0))" \
324 "set rptr"
325 gdb_test "gu (print (value-subscript rptr 0))" \
326 "= 2" "Check pointer passed as reference"
327
328 # Just the most basic test of dynamic_cast -- it is checked in
329 # the C++ tests.
330 gdb_test "gu (print (value->bool (value-dynamic-cast (parse-and-eval \"base\") (type-pointer (lookup-type \"Derived\")))))" \
331 "= #t"
332
333 # Likewise.
334 gdb_test "gu (print (value-dynamic-type (parse-and-eval \"base\")))" \
335 "= Derived \[*\]"
336 gdb_test "gu (print (value-dynamic-type (parse-and-eval \"base_ref\")))" \
337 "= Derived \[&\]"
338 # A static type case.
339 gdb_test "gu (print (value-dynamic-type (parse-and-eval \"5\")))" \
340 "= int"
341 }
342
343 gdb_breakpoint [gdb_get_line_number "break to inspect struct and union"]
344 gdb_continue_to_breakpoint "break to inspect struct and union in $lang"
345
346 gdb_scm_test_silent_cmd "gu (define intv (make-value 1))" \
347 "Create int value for subscript test"
348 gdb_scm_test_silent_cmd "gu (define stringv (make-value \"foo\"))" \
349 "Create string value for subscript test"
350
351 # Try to access an int with a subscript. This should fail.
352 gdb_test "gu (print intv)" \
353 "= 1" "Baseline print of an int Guile value"
354 gdb_test "gu (print (value-subscript intv 0))" \
355 "ERROR: Cannot subscript requested type.*" \
356 "Attempt to access an integer with a subscript"
357
358 # Try to access a string with a subscript. This should pass.
359 gdb_test "gu (print stringv)" \
360 "= \"foo\"" "Baseline print of a string Guile value"
361 gdb_test "gu (print (value-subscript stringv 0))" \
362 "= 102 'f'" "Attempt to access a string with a subscript"
363
364 # Try to access an int array via a pointer with a subscript.
365 # This should pass.
366 gdb_scm_test_silent_cmd "print p" "Build pointer to array"
367 gdb_scm_test_silent_cmd "gu (define pointer (history-ref 0))" "set pointer"
368 gdb_test "gu (print (value-subscript pointer 0))" \
369 "= 1" "Access array via pointer with int subscript"
370 gdb_test "gu (print (value-subscript pointer intv))" \
371 "= 2" "Access array via pointer with value subscript"
372
373 # Try to access a single dimension array with a subscript to the
374 # result. This should fail.
375 gdb_test "gu (print (value-subscript (value-subscript pointer intv) 0))" \
376 "ERROR: Cannot subscript requested type.*" \
377 "Attempt to access an integer with a subscript 2"
378
379 # Lastly, test subscript access to an array with multiple
380 # dimensions. This should pass.
381 gdb_scm_test_silent_cmd "print {\"fu \",\"foo\",\"bar\"}" "Build array"
382 gdb_scm_test_silent_cmd "gu (define marray (history-ref 0))" ""
383 gdb_test "gu (print (value-subscript (value-subscript marray 1) 2))" \
384 "o." "Test multiple subscript"
385 }
386
387 # A few tests of gdb:parse-and-eval.
388
389 proc test_parse_and_eval {} {
390 gdb_test "gu (print (parse-and-eval \"23\"))" \
391 "= 23" "parse-and-eval constant test"
392 gdb_test "gu (print (parse-and-eval \"5 + 7\"))" \
393 "= 12" "parse-and-eval simple expression test"
394 gdb_test "gu (raw-print (parse-and-eval \"5 + 7\"))" \
395 "#<gdb:value 12>" "parse-and-eval type test"
396 }
397
398 # Test that values are hashable.
399 # N.B.: While smobs are hashable, the hash is really non-existent,
400 # they all get hashed to the same value. Guile may provide a hash function
401 # for smobs in a future release. In the meantime one should use a custom
402 # hash table that uses gdb:hash-gsmob.
403
404 proc test_value_hash {} {
405 gdb_test_multiline "Simple Guile value dictionary" \
406 "guile" "" \
407 "(define one (make-value 1))" "" \
408 "(define two (make-value 2))" "" \
409 "(define three (make-value 3))" "" \
410 "(define vdict (make-hash-table 5))" "" \
411 "(hash-set! vdict one \"one str\")" "" \
412 "(hash-set! vdict two \"two str\")" "" \
413 "(hash-set! vdict three \"three str\")" "" \
414 "end"
415 gdb_test "gu (print (hash-ref vdict one))" \
416 "one str" "Test dictionary hash 1"
417 gdb_test "gu (print (hash-ref vdict two))" \
418 "two str" "Test dictionary hash 2"
419 gdb_test "gu (print (hash-ref vdict three))" \
420 "three str" "Test dictionary hash 3"
421 }
422
423 # Build C version of executable. C++ is built later.
424 if { [build_inferior "${binfile}" "c"] < 0 } {
425 return
426 }
427
428 # Start with a fresh gdb.
429 clean_restart ${binfile}
430
431 # Skip all tests if Guile scripting is not enabled.
432 if { [skip_guile_tests] } { continue }
433
434 gdb_install_guile_utils
435 gdb_install_guile_module
436
437 test_parse_and_eval
438 test_value_hash
439
440 # The following tests require execution.
441
442 if ![gdb_guile_runto_main] {
443 fail "can't run to main"
444 return
445 }
446
447 test_value_in_inferior
448 test_inferior_function_call
449 test_strings
450 test_value_after_death
451
452 # Test either C or C++ values.
453
454 test_subscript_regression "${binfile}" "c"
455
456 if ![skip_cplus_tests] {
457 if { [build_inferior "${binfile}-cxx" "c++"] < 0 } {
458 return
459 }
460 with_test_prefix "c++" {
461 test_subscript_regression "${binfile}-cxx" "c++"
462 }
463 }
This page took 0.038382 seconds and 3 git commands to generate.