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