Update years in copyright notice for the GDB files.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / charset.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2001-2013 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@gnu.org
20
21 # Test GDB's character set support.
22
23
24 set testfile "charset"
25 set srcfile ${testfile}.c
26 set srcmallocfile ${testfile}-malloc.c
27 if { [prepare_for_testing ${testfile}.exp ${testfile} [list $srcfile $srcmallocfile]] } {
28 return -1
29 }
30
31 # Parse the output from a `show charset' command. Return the host
32 # and target charset as a two-element list.
33 proc parse_show_charset_output {testname} {
34 global gdb_prompt
35
36 gdb_expect {
37 -re "The host character set is \"(.*)\"\\.\[\r\n\]+The target character set is \"(.*)\"\\.\[\r\n\]+The target wide character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
38 set host_charset $expect_out(1,string)
39 set target_charset $expect_out(2,string)
40 set retlist [list $host_charset $target_charset]
41 pass $testname
42 }
43 -re "The host character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
44 set host_charset $expect_out(1,string)
45 set retlist [list $host_charset]
46 pass $testname
47 }
48 -re "The target character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
49 set target_charset $expect_out(1,string)
50 set retlist [list $target_charset]
51 pass $testname
52 }
53 -re ".*$gdb_prompt $" {
54 fail $testname
55 }
56 timeout {
57 fail "$testname (timeout)"
58 }
59 }
60
61 return $retlist
62 }
63
64
65 # Try the various `show charset' commands.
66
67 send_gdb "show charset\n"
68 set show_charset [parse_show_charset_output "show charset"]
69
70 send_gdb "show target-charset\n"
71 set show_target_charset \
72 [lindex [parse_show_charset_output "show target-charset"] 0]
73
74 if {[lsearch -exact $show_charset $show_target_charset] >= 0} {
75 pass "check `show target-charset' against `show charset'"
76 } else {
77 fail "check `show target-charset' against `show charset'"
78 }
79
80 send_gdb "show host-charset\n"
81 set show_host_charset \
82 [lindex [parse_show_charset_output "show host-charset"] 0]
83
84 if {[lsearch -exact $show_charset $show_host_charset] >= 0} {
85 pass "check `show host-charset' against `show charset'"
86 } else {
87 fail "check `show host-charset' against `show charset'"
88 }
89
90 # Try a malformed `set charset'.
91 gdb_test "set charset" \
92 "Requires an argument. Valid arguments are.*" \
93 "try malformed `set charset'"
94
95 # Try using `set host-charset' on an invalid character set.
96 gdb_test "set host-charset my_grandma_bonnie" \
97 "Undefined item: \"my_grandma_bonnie\"." \
98 "try `set host-charset' with invalid charset"
99
100 # Try using `set target-charset' on an invalid character set.
101 gdb_test "set target-charset my_grandma_bonnie" \
102 "Undefined item: \"my_grandma_bonnie\"." \
103 "try `set target-charset' with invalid charset"
104
105 # A Tcl array mapping the names of all the character sets we've seen
106 # to "1" if the character set can be used as a host character set, or
107 # "0" otherwise. We can use `array names charsets' just to get a list
108 # of all character sets.
109 array set charsets {}
110
111 proc all_charset_names {} {
112 global charsets
113 return [array names charsets]
114 }
115
116 proc valid_host_charset {charset} {
117 global charsets
118 return [expr {[info exists charsets($charset)] && $charsets($charset)}]
119 }
120
121 proc valid_target_charset {charset} {
122 global charsets
123 return [info exists charsets($charset)]
124 }
125
126 send_gdb "set host-charset\n"
127 gdb_expect {
128 -re "Requires an argument. Valid arguments are (.*)\\.\r\n$gdb_prompt $" {
129 set host_charset_list $expect_out(1,string)
130 regsub -all {, } $host_charset_list {,} host_charset_list
131 foreach host_charset [split $host_charset_list ","] {
132 set charsets($host_charset) 1
133 }
134 pass "capture valid host charsets"
135 }
136
137 -re ".*$gdb_prompt $" {
138 fail "capture valid host charsets"
139 }
140
141 timeout {
142 fail "(timeout) capture valid host charsets"
143 }
144 }
145
146 # If gdb was built with a phony iconv, it will only have two character
147 # sets: "auto" and the default. In this situation, this set of tests
148 # is pointless.
149 if {[llength [array names charsets]] < 3} {
150 untested charset.exp
151 return -1
152 }
153
154 send_gdb "set target-charset\n"
155 gdb_expect {
156 -re "Requires an argument. Valid arguments are (.*)\\.\r\n$gdb_prompt $" {
157 set target_charset_list $expect_out(1,string)
158 regsub -all {, } $target_charset_list {,} target_charset_list
159 foreach target_charset [split $target_charset_list ","] {
160 if {! [info exists charsets($target_charset)]} {
161 set charsets($target_charset) 0
162 }
163 }
164 pass "capture valid target charsets"
165 }
166
167 -re ".*$gdb_prompt $" {
168 fail "capture valid target charsets"
169 }
170
171 timeout {
172 fail "(timeout) capture valid target charsets"
173 }
174 }
175
176 # We don't want to test all the charset names here, since that would
177 # be too many combinations. We we pick a subset.
178 set charset_subset {ASCII ISO-8859-1 EBCDIC-US IBM1047}
179 foreach host_charset $charset_subset {
180 if {[valid_host_charset $host_charset]} {
181
182 set testname "try `set host-charset $host_charset'"
183 send_gdb "set host-charset $host_charset\n"
184 gdb_expect {
185 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
186 # How did it get into `charsets' then?
187 fail "$testname (didn't recognize name)"
188 }
189 -re "GDB can't use `.*' as its host character set\\.\[\r\n]+${gdb_prompt} $" {
190 # Well, then why does its `charsets' entry say it can?
191 fail $testname
192 }
193 -re "${gdb_prompt} $" {
194 pass $testname
195 }
196 timeout {
197 fail "$testname (timeout)"
198 }
199 }
200
201 # Check that the command actually had its intended effect:
202 # $host_charset should now be the host character set.
203 send_gdb "show charset\n"
204 set result [parse_show_charset_output "parse `show charset' after `set host-charset $host_charset'"]
205 if {! [string compare [lindex $result 0] $host_charset]} {
206 pass "check effect of `set host-charset $host_charset'"
207 } else {
208 fail "check effect of `set host-charset $host_charset'"
209 }
210
211 # Now try setting every possible target character set,
212 # given that host charset.
213 foreach target_charset $charset_subset {
214 if {![valid_target_charset $target_charset]} {
215 continue
216 }
217 set testname "try `set target-charset $target_charset'"
218 send_gdb "set target-charset $target_charset\n"
219 gdb_expect {
220 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
221 fail "$testname (didn't recognize name)"
222 }
223 -re "GDB can't convert from the .* character set to .*\\.\[\r\n\]+${gdb_prompt} $" {
224 # This is a serious problem. GDB should be able to convert
225 # between any arbitrary pair of character sets.
226 fail "$testname (can't convert)"
227 }
228 -re "${gdb_prompt} $" {
229 pass $testname
230 }
231 timeout {
232 fail "$testname (timeout)"
233 }
234 }
235
236 # Check that the command actually had its intended effect:
237 # $target_charset should now be the target charset.
238 send_gdb "show charset\n"
239 set result [parse_show_charset_output "parse `show charset' after `set target-charset $target_charset'"]
240 if {! [string compare $result [list $host_charset $target_charset]]} {
241 pass "check effect of `set target-charset $target_charset'"
242 } else {
243 fail "check effect of `set target-charset $target_charset'"
244 }
245
246 # Test handling of characters in the host charset which
247 # can't be translated into the target charset. \xA2 is
248 # `cent' in ISO-8859-1, which has no equivalent in ASCII.
249 #
250 # On some systems, the pseudo-tty through which we
251 # communicate with GDB insists on stripping the high bit
252 # from input characters, meaning that `cent' turns into
253 # `"'. Since ISO-8859-1 and ASCII are identical in the
254 # lower 128 characters, it's tough to see how we can test
255 # this behavior on such systems, so we just xfail it.
256 #
257 # Note: the \x16 (Control-V) is an escape to allow \xA2 to
258 # get past readline.
259 if {! [string compare $host_charset iso-8859-1] && ! [string compare $target_charset ascii]} {
260
261 set testname "untranslatable character in character literal"
262 send_gdb "print '\x16\xA2'\n"
263 gdb_expect {
264 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
265 pass $testname
266 }
267 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
268 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
269 }
270 -re "$gdb_prompt $" {
271 fail $testname
272 }
273 timeout {
274 fail "$testname (timeout)"
275 }
276 }
277
278 set testname "untranslatable character in string literal"
279 # If the PTTY zeros bit seven, then this turns into
280 # print """
281 # which gets us a syntax error. We don't care.
282 send_gdb "print \"\x16\xA2\"\n"
283 gdb_expect {
284 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
285 pass $testname
286 }
287 -re "Unterminated string in expression.\[\r\n\]+$gdb_prompt $" {
288 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
289 }
290 -re "$gdb_prompt $" {
291 fail $testname
292 }
293 timeout {
294 fail "$testname (timeout)"
295 }
296 }
297
298 set testname "untranslatable characters in backslash escape"
299 send_gdb "print '\\\x16\xA2'\n"
300 gdb_expect {
301 -re "The escape sequence .* is equivalent to plain .*, which has no equivalent\[\r\n\]+in the .* character set\\.\[\r\n\]+$gdb_prompt $" {
302 pass $testname
303 }
304 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
305 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
306 }
307 -re "$gdb_prompt $" {
308 fail $testname
309 }
310 timeout {
311 fail "$testname (timeout)"
312 }
313 }
314 }
315 }
316 }
317 }
318
319
320 # Set the host character set to plain ASCII, and try actually printing
321 # some strings in various target character sets. We need to run the
322 # test program to the point at which the strings have been
323 # initialized.
324 gdb_test "break ${srcfile}:[gdb_get_line_number "all strings initialized"]" \
325 ".*Breakpoint.* at .*" \
326 "set breakpoint after all strings have been initialized"
327 gdb_run_cmd
328 gdb_expect {
329 -re "Breakpoint.*all strings initialized.*$gdb_prompt $" {
330 pass "run until all strings have been initialized"
331 }
332 -re "$gdb_prompt $" {
333 fail "run until all strings have been initialized"
334 }
335 timeout {
336 fail "run until all strings have been initialized (timeout)"
337 }
338 }
339
340
341 # We only try the wide character tests on machines where the wchar_t
342 # typedef in the test case has the right size.
343 set wchar_size [get_sizeof wchar_t 99]
344 set wchar_ok 0
345 if {$wchar_size == 2} {
346 lappend charset_subset UTF-16
347 set wchar_ok 1
348 } elseif {$wchar_size == 4} {
349 lappend charset_subset UTF-32
350 set wchar_ok 1
351 }
352
353 gdb_test_no_output "set host-charset ASCII"
354 foreach target_charset $charset_subset {
355 if {![valid_target_charset $target_charset]} {
356 continue
357 }
358
359 if {$target_charset == "UTF-32" || $target_charset == "UTF-16"} {
360 set param target-wide-charset
361 set L L
362 } else {
363 set param target-charset
364 set L ""
365 }
366 gdb_test_no_output "set $param $target_charset"
367
368 # Try printing the null character. There seems to be a bug in
369 # gdb_test that requires us to use gdb_expect here.
370 send_gdb "print $L'\\0'\n"
371 gdb_expect {
372 -re "\\\$${decimal} = 0 $L'\\\\000'\[\r\n\]+$gdb_prompt $" {
373 pass "print the null character in ${target_charset}"
374 }
375 -re "$gdb_prompt $" {
376 fail "print the null character in ${target_charset}"
377 }
378 timeout {
379 fail "print the null character in ${target_charset} (timeout)"
380 }
381 }
382
383 # Compute the name of the variable in the test program that holds
384 # a string in $target_charset. The variable's name is the
385 # character set's name, in lower-case, with all non-identifier
386 # characters replaced with '_', with "_string" stuck on the end.
387 if {$target_charset == "UTF-16"} {
388 # We still use the utf_32_string variable -- but the size is
389 # correct for UTF-16.
390 set var_name utf_32_string
391 } else {
392 set var_name [string tolower "${target_charset}_string"]
393 regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
394 }
395
396 # Compute a regexp matching the results we expect. This is static,
397 # but it's easier than writing it out.
398 regsub -all "." "abfnrtv" "(\\\\&|x)" escapes
399 set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
400 set lowercase "abcdefghijklmnopqrstuvwxyz"
401 set digits "0123456789"
402 set octal_escape "\\\\\[0-9\]+"
403
404 send_gdb "print $var_name\n"
405 # ${escapes}${uppercase}${lowercase}${digits}${octal}${octal}
406 gdb_expect {
407 -re ".* = $L\"(\\\\a|x)(\\\\b|x)(\\\\f|x)(\\\\n|x)(\\\\r|x)(\\\\t|x)(\\\\v|x)${uppercase}${lowercase}${digits}(${octal_escape}|x)+\"\[\r\n\]+$gdb_prompt $" {
408 pass "print string in $target_charset"
409 }
410 -re "$gdb_prompt $" {
411 fail "print string in $target_charset"
412 }
413 timeout {
414 fail "print string in $target_charset (timeout)"
415 }
416 }
417
418 # Try entering a character literal, and see if it comes back unchanged.
419 gdb_test "print $L'A'" \
420 " = \[0-9-\]+ $L'A'" \
421 "parse character literal in ${target_charset}"
422
423 # Check that the character literal was encoded correctly.
424 gdb_test "print $L'A' == $var_name\[7\]" \
425 " = 1" \
426 "check value of parsed character literal in ${target_charset}"
427
428 # Try entering a string literal, and see if it comes back unchanged.
429 gdb_test "print $L\"abcdefABCDEF012345\"" \
430 " = $L\"abcdefABCDEF012345\"" \
431 "parse string literal in ${target_charset}"
432
433 # Check that the string literal was encoded correctly.
434 gdb_test "print $L\"q\"\[0\] == $var_name\[49\]" \
435 " = 1" \
436 "check value of parsed string literal in ${target_charset}"
437
438 # Test handling of characters in the target charset which
439 # can't be translated into the host charset.
440 if {! [string compare $target_charset iso-8859-1]} {
441 gdb_test "print iso_8859_1_string\[69\]" \
442 " = \[0-9-\]+ '\\\\242'" \
443 "print character with no equivalent in host character set"
444 gdb_test "print iso_8859_1_string + 70" \
445 " = ${hex} \"\\\\242.*\"" \
446 "print string with no equivalent in host character set"
447 }
448
449 # Make sure that we don't apply the ISO-8859-1 `print_literally'
450 # function to ASCII.
451 if {! [string compare $target_charset ascii]} {
452 gdb_test "print iso_8859_1_string\[69\]" \
453 " = \[0-9-\]+ '\\\\242'" \
454 "print ASCII unprintable character"
455 gdb_test "print iso_8859_1_string + 70" \
456 " = ${hex} \"\\\\242.*\"" \
457 "print ASCII unprintable string"
458 }
459
460 # Try printing characters with backslash escape equivalents.
461 set escapees {a b f n r t v}
462 for {set i 0} {$i < [llength $escapees]} {incr i} {
463 set escape [lindex $escapees $i]
464 send_gdb "print $var_name\[$i\]\n"
465 set have_escape 1
466 gdb_expect {
467 -re "= \[0-9-\]+ $L'\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
468 pass "try printing '\\${escape}' in ${target_charset}"
469 }
470 -re "= \[0-9-\]+ 'x'\[\r\n\]+$gdb_prompt $" {
471 xfail "try printing '\\${escape}' in ${target_charset} (no such escape)"
472 set have_escape 0
473 }
474 -re "$gdb_prompt $" {
475 fail "try printing '\\${escape}' in ${target_charset}"
476 }
477 timeout {
478 fail "try printing '\\${escape}' in ${target_charset} (timeout)"
479 }
480 }
481
482 if {$have_escape} {
483
484 # Try parsing a backslash escape in a character literal.
485 gdb_test "print $L'\\${escape}' == $var_name\[$i\]" \
486 " = 1" \
487 "check value of '\\${escape}' in ${target_charset}"
488
489 # Try parsing a backslash escape in a string literal.
490 gdb_test "print $L\"\\${escape}\"\[0\] == $var_name\[$i\]" \
491 " = 1" \
492 "check value of \"\\${escape}\" in ${target_charset}"
493 }
494 }
495
496 # Try printing a character escape that doesn't exist. We should
497 # get the unescaped character, in the target character set.
498 gdb_test "print $L'\\q'" " = \[0-9-\]+ $L'q'" \
499 "print escape that doesn't exist in $target_charset"
500 gdb_test "print $L'\\q' == $var_name\[49\]" " = 1" \
501 "check value of escape that doesn't exist in $target_charset"
502 }
503
504 # Reset the target charset.
505 gdb_test_no_output "set target-charset UTF-8"
506
507 # \242 is not a valid UTF-8 character.
508 gdb_test "print \"\\242\"" " = \"\\\\242\"" \
509 "non-representable target character"
510
511 gdb_test "print '\\x'" "\\\\x escape without a following hex digit."
512 gdb_test "print '\\u'" "\\\\u escape without a following hex digit."
513 gdb_test "print '\\9'" " = \[0-9\]+ '9'"
514
515 # An octal escape can only be 3 digits.
516 gdb_test "print \"\\1011\"" " = \"A1\""
517
518 # Tests for wide- or unicode- strings. L is the prefix letter to use,
519 # either "L" (for wide strings), "u" (for UTF-16), or "U" (for UTF-32).
520 # NAME is used in the test names and should be related to the prefix
521 # letter in some easy-to-undestand way.
522 proc test_wide_or_unicode {L name} {
523 gdb_test "print $L\"ab\" $L\"c\"" " = $L\"abc\"" \
524 "basic $name string concatenation"
525 gdb_test "print $L\"ab\" \"c\"" " = $L\"abc\"" \
526 "narrow and $name string concatenation"
527 gdb_test "print \"ab\" $L\"c\"" " = $L\"abc\"" \
528 "$name and narrow string concatenation"
529 gdb_test "print $L\"\\xe\" $L\"c\"" " = $L\"\\\\016c\"" \
530 "$name string concatenation with escape"
531 gdb_test "print $L\"\" \"abcdef\" \"g\"" \
532 "$L\"abcdefg\"" \
533 "concatenate three strings with empty $name string"
534
535 gdb_test "print $L'a'" "= \[0-9\]+ $L'a'" \
536 "basic $name character"
537 }
538
539 if {$wchar_ok} {
540 test_wide_or_unicode L wide
541 }
542
543 set ucs2_ok [expr {[get_sizeof char16_t 99] == 2}]
544
545 if ![valid_host_charset "UTF-16"] {
546 verbose -log "Disabling UTF-16 tests."
547 set ucs2_ok 0
548 }
549
550 if {$ucs2_ok} {
551 test_wide_or_unicode u UTF-16
552 }
553
554 set ucs4_ok [expr {[get_sizeof char32_t 99] == 4}]
555 if {$ucs4_ok} {
556 test_wide_or_unicode U UTF-32
557 }
558
559 # Test an invalid string combination.
560 proc test_combination {L1 name1 L2 name2} {
561 gdb_test "print $L1\"abc\" $L2\"def\"" \
562 "Undefined string concatenation." \
563 "undefined concatenation of $name1 and $name2"
564 }
565
566 if {$wchar_ok && $ucs2_ok} {
567 test_combination L wide u UTF-16
568 }
569 if {$wchar_ok && $ucs4_ok} {
570 test_combination L wide U UTF-32
571 # Regression test for a typedef to a typedef.
572 gdb_test "print myvar" "= \[0-9\]+ L'A'" \
573 "typedef to wchar_t"
574 }
575 if {$ucs2_ok && $ucs4_ok} {
576 test_combination u UTF-16 U UTF-32
577 }
578
579 if {$ucs2_ok} {
580 set go 1
581 gdb_test_multiple "python print ('hello, world!')" \
582 "verify python support for charset tests" {
583 -re "not supported.*$gdb_prompt $" {
584 unsupported "python support is disabled"
585 set go 0
586 }
587 -re "$gdb_prompt $" {}
588 }
589
590 if {$go} {
591 gdb_test "print u\"abcdef\"" " = u\"abcdef\"" \
592 "set up for python printing of utf-16 string"
593
594 gdb_test "python print (gdb.history(0).string())" "abcdef" \
595 "extract utf-16 string using python"
596 }
597 }
598
599 # Regression test for a cleanup bug in the charset code.
600 gdb_test "print 'a' == 'a' || 'b' == 'b'" \
601 ".* = 1" \
602 "EVAL_SKIP cleanup handling regression test"
603
604
605 proc string_display { var_name set_prefix x_size x_type} {
606 gdb_test_no_output "set ${var_name} = ${set_prefix}\"Test String\\0with zeroes\"" "Assign ${var_name} with prefix ${set_prefix}"
607 gdb_test "x /2${x_size}s ${var_name}" ".*\t${x_type}\"Test String\"\[\r\n\]+.*\t${x_type}\"with zeroes\"" "Display String ${var_name} with x/${x_size}s"
608 }
609
610 if {$ucs2_ok} {
611 string_display String16 u h u
612 if {$wchar_size == 2} {
613 string_display String16 L h u
614 }
615 }
616
617 string_display String32 U w U
618 if {$wchar_size == 4} {
619 string_display String32 L w U
620 }
621
622
623 foreach name {short int long} {
624 # We're really just checking to make sure this doesn't give an
625 # error.
626 gdb_test "print ${name}_array = \"hi\"" \
627 " = {.*}" \
628 "assign string to $name array"
629 }
630
631
632 gdb_exit
This page took 0.044032 seconds and 5 git commands to generate.