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