1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2001-2024 Free Software Foundation, Inc.
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.
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.
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/>.
18 # Please email any bugs, comments, and/or additions to this file to:
21 # Test GDB's character set support.
24 standard_testfile .c charset-malloc.c
26 if { [prepare_for_testing "failed to prepare" ${testfile} [list $srcfile $srcfile2]] } {
30 # Parse the output from a `show charset' command. Return the host
31 # and target charset as a two-element list.
32 proc parse_show_charset_output {testname} {
36 -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 $" {
37 set host_charset $expect_out(1,string)
38 set target_charset $expect_out(2,string)
39 set retlist [list $host_charset $target_charset]
42 -re "The host character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
43 set host_charset $expect_out(1,string)
44 set retlist [list $host_charset]
47 -re "The target character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
48 set target_charset $expect_out(1,string)
49 set retlist [list $target_charset]
52 -re ".*$gdb_prompt $" {
56 fail "$testname (timeout)"
64 # Try the various `show charset' commands.
66 send_gdb "show charset\n"
67 set show_charset [parse_show_charset_output "show charset"]
69 send_gdb "show target-charset\n"
70 set show_target_charset \
71 [lindex [parse_show_charset_output "show target-charset"] 0]
73 if {[lsearch -exact $show_charset $show_target_charset] >= 0} {
74 pass "check `show target-charset' against `show charset'"
76 fail "check `show target-charset' against `show charset'"
79 send_gdb "show host-charset\n"
80 set show_host_charset \
81 [lindex [parse_show_charset_output "show host-charset"] 0]
83 if {[lsearch -exact $show_charset $show_host_charset] >= 0} {
84 pass "check `show host-charset' against `show charset'"
86 fail "check `show host-charset' against `show charset'"
89 # Try a malformed `set charset'.
92 gdb_test_multiple $cmd "try malformed `set charset'" {
93 -re "^$cmd\r\nRequires an argument. Valid arguments are " {
101 gdb_assert { $ok } $gdb_test_name
105 # Try using `set host-charset' on an invalid character set.
106 gdb_test "set host-charset my_grandma_bonnie" \
107 "Undefined item: \"my_grandma_bonnie\"." \
108 "try `set host-charset' with invalid charset"
110 # Try using `set target-charset' on an invalid character set.
111 gdb_test "set target-charset my_grandma_bonnie" \
112 "Undefined item: \"my_grandma_bonnie\"." \
113 "try `set target-charset' with invalid charset"
115 # A Tcl array mapping the names of all the character sets we've seen
116 # to "1" if the character set can be used as a host character set, or
117 # "0" otherwise. We can use `array names charsets' just to get a list
118 # of all character sets.
119 array set charsets {}
121 proc all_charset_names {} {
123 return [array names charsets]
126 proc valid_host_charset {charset} {
128 return [expr {[info exists charsets($charset)] && $charsets($charset)}]
131 proc valid_target_charset {charset} {
133 return [info exists charsets($charset)]
136 send_gdb "set host-charset\n"
138 -re "Requires an argument. Valid arguments are (.*)\\.\r\n$gdb_prompt $" {
139 set host_charset_list $expect_out(1,string)
140 regsub -all {, } $host_charset_list {,} host_charset_list
141 foreach host_charset [split $host_charset_list ","] {
142 set charsets($host_charset) 1
144 pass "capture valid host charsets"
147 -re ".*$gdb_prompt $" {
148 fail "capture valid host charsets"
152 fail "(timeout) capture valid host charsets"
156 # If gdb was built with a phony iconv, it will only have two character
157 # sets: "auto" and the default. In this situation, this set of tests
159 if {[llength [array names charsets]] < 3} {
160 untested "fewer than 3 charsets"
164 send_gdb "set target-charset\n"
166 -re "Requires an argument. Valid arguments are (.*)\\.\r\n$gdb_prompt $" {
167 set target_charset_list $expect_out(1,string)
168 regsub -all {, } $target_charset_list {,} target_charset_list
169 foreach target_charset [split $target_charset_list ","] {
170 if {! [info exists charsets($target_charset)]} {
171 set charsets($target_charset) 0
174 pass "capture valid target charsets"
177 -re ".*$gdb_prompt $" {
178 fail "capture valid target charsets"
182 fail "(timeout) capture valid target charsets"
186 # We don't want to test all the charset names here, since that would
187 # be too many combinations. We we pick a subset.
188 set charset_subset {ASCII ISO-8859-1 EBCDIC-US IBM1047}
189 foreach_with_prefix host_charset $charset_subset {
190 if {[valid_host_charset $host_charset]} {
192 set testname "try `set host-charset $host_charset'"
193 send_gdb "set host-charset $host_charset\n"
195 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
196 # How did it get into `charsets' then?
197 fail "$testname (didn't recognize name)"
199 -re "GDB can't use `.*' as its host character set\\.\[\r\n]+${gdb_prompt} $" {
200 # Well, then why does its `charsets' entry say it can?
203 -re "${gdb_prompt} $" {
207 fail "$testname (timeout)"
211 # Check that the command actually had its intended effect:
212 # $host_charset should now be the host character set.
213 send_gdb "show charset\n"
214 set result [parse_show_charset_output "parse `show charset' after `set host-charset $host_charset'"]
215 if {! [string compare [lindex $result 0] $host_charset]} {
216 pass "check effect of `set host-charset $host_charset'"
218 fail "check effect of `set host-charset $host_charset'"
221 # Now try setting every possible target character set,
222 # given that host charset.
223 foreach target_charset $charset_subset {
224 if {![valid_target_charset $target_charset]} {
227 set testname "try `set target-charset $target_charset'"
228 send_gdb "set target-charset $target_charset\n"
230 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
231 fail "$testname (didn't recognize name)"
233 -re "GDB can't convert from the .* character set to .*\\.\[\r\n\]+${gdb_prompt} $" {
234 # This is a serious problem. GDB should be able to convert
235 # between any arbitrary pair of character sets.
236 fail "$testname (can't convert)"
238 -re "${gdb_prompt} $" {
242 fail "$testname (timeout)"
246 # Check that the command actually had its intended effect:
247 # $target_charset should now be the target charset.
248 send_gdb "show charset\n"
249 set result [parse_show_charset_output "parse `show charset' after `set target-charset $target_charset'"]
250 if {! [string compare $result [list $host_charset $target_charset]]} {
251 pass "check effect of `set target-charset $target_charset'"
253 fail "check effect of `set target-charset $target_charset'"
256 # Test handling of characters in the host charset which
257 # can't be translated into the target charset. \xA2 is
258 # `cent' in ISO-8859-1, which has no equivalent in ASCII.
260 # On some systems, the pseudo-tty through which we
261 # communicate with GDB insists on stripping the high bit
262 # from input characters, meaning that `cent' turns into
263 # `"'. Since ISO-8859-1 and ASCII are identical in the
264 # lower 128 characters, it's tough to see how we can test
265 # this behavior on such systems, so we just xfail it.
267 # Note: the \x16 (Control-V) is an escape to allow \xA2 to
269 if {! [string compare $host_charset iso-8859-1] && ! [string compare $target_charset ascii]} {
271 set testname "untranslatable character in character literal"
272 send_gdb "print '\x16\xA2'\n"
274 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
277 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
278 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
280 -re "$gdb_prompt $" {
284 fail "$testname (timeout)"
288 set testname "untranslatable character in string literal"
289 # If the PTTY zeros bit seven, then this turns into
291 # which gets us a syntax error. We don't care.
292 send_gdb "print \"\x16\xA2\"\n"
294 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
297 -re "Unterminated string in expression.\[\r\n\]+$gdb_prompt $" {
298 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
300 -re "$gdb_prompt $" {
304 fail "$testname (timeout)"
308 set testname "untranslatable characters in backslash escape"
309 send_gdb "print '\\\x16\xA2'\n"
311 -re "The escape sequence .* is equivalent to plain .*, which has no equivalent\[\r\n\]+in the .* character set\\.\[\r\n\]+$gdb_prompt $" {
314 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
315 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
317 -re "$gdb_prompt $" {
321 fail "$testname (timeout)"
330 # Set the host character set to plain ASCII, and try actually printing
331 # some strings in various target character sets. We need to run the
332 # test program to the point at which the strings have been
334 gdb_test "break ${srcfile}:[gdb_get_line_number "all strings initialized"]" \
335 ".*Breakpoint.* at .*" \
336 "set breakpoint after all strings have been initialized"
338 gdb_test "" "Breakpoint.*all strings initialized.*" "run until all strings have been initialized"
340 # We only try the wide character tests on machines where the wchar_t
341 # typedef in the test case has the right size.
342 set wchar_size [get_sizeof wchar_t 99]
344 if {$wchar_size == 2} {
345 lappend charset_subset UTF-16
347 } elseif {$wchar_size == 4} {
348 lappend charset_subset UTF-32
352 gdb_test_no_output "set host-charset ASCII"
353 foreach target_charset $charset_subset {
354 if {![valid_target_charset $target_charset]} {
358 if {$target_charset == "UTF-32" || $target_charset == "UTF-16"} {
359 set param target-wide-charset
362 set param target-charset
365 gdb_test_no_output "set $param $target_charset"
367 # Try printing the null character. There seems to be a bug in
368 # gdb_test that requires us to use gdb_expect here.
369 send_gdb "print $L'\\0'\n"
371 -re "\\\$${decimal} = 0 $L'\\\\000'\[\r\n\]+$gdb_prompt $" {
372 pass "print the null character in ${target_charset}"
374 -re "$gdb_prompt $" {
375 fail "print the null character in ${target_charset}"
378 fail "print the null character in ${target_charset} (timeout)"
382 # Compute the name of the variable in the test program that holds
383 # a string in $target_charset. The variable's name is the
384 # character set's name, in lower-case, with all non-identifier
385 # characters replaced with '_', with "_string" stuck on the end.
386 if {$target_charset == "UTF-16"} {
387 # We still use the utf_32_string variable -- but the size is
388 # correct for UTF-16.
389 set var_name utf_32_string
391 set var_name [string tolower "${target_charset}_string"]
392 regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
395 # Compute a regexp matching the results we expect. This is static,
396 # but it's easier than writing it out.
397 regsub -all "." "abfnrtv" "(\\\\&|x)" escapes
398 set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
399 set lowercase "abcdefghijklmnopqrstuvwxyz"
400 set digits "0123456789"
401 set octal_escape "\\\\\[0-9\]+"
403 send_gdb "print $var_name\n"
404 # ${escapes}${uppercase}${lowercase}${digits}${octal}${octal}
406 -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 $" {
407 pass "print string in $target_charset"
409 -re "$gdb_prompt $" {
410 fail "print string in $target_charset"
413 fail "print string in $target_charset (timeout)"
417 # Try entering a character literal, and see if it comes back unchanged.
418 gdb_test "print $L'A'" \
419 " = \[0-9-\]+ $L'A'" \
420 "parse character literal in ${target_charset}"
422 # Check that the character literal was encoded correctly.
423 gdb_test "print /d $L'A' == $var_name\[7\]" \
425 "check value of parsed character literal in ${target_charset}"
427 # Try entering a string literal, and see if it comes back unchanged.
428 gdb_test "print $L\"abcdefABCDEF012345\"" \
429 " = $L\"abcdefABCDEF012345\"" \
430 "parse string literal in ${target_charset}"
432 # Check that the string literal was encoded correctly.
433 gdb_test "print /d $L\"q\"\[0\] == $var_name\[49\]" \
435 "check value of parsed string literal in ${target_charset}"
437 # Test handling of characters in the target charset which
438 # can't be translated into the host charset.
439 if {! [string compare $target_charset iso-8859-1]} {
440 gdb_test "print iso_8859_1_string\[69\]" \
441 " = \[0-9-\]+ '\\\\242'" \
442 "print character with no equivalent in host character set"
443 gdb_test "print iso_8859_1_string + 70" \
444 " = ${hex} \"\\\\242.*\"" \
445 "print string with no equivalent in host character set"
448 # Make sure that we don't apply the ISO-8859-1 `print_literally'
450 if {! [string compare $target_charset ascii]} {
451 gdb_test "print iso_8859_1_string\[69\]" \
452 " = \[0-9-\]+ '\\\\242'" \
453 "print ASCII unprintable character"
454 gdb_test "print iso_8859_1_string + 70" \
455 " = ${hex} \"\\\\242.*\"" \
456 "print ASCII unprintable string"
459 # Try printing characters with backslash escape equivalents.
460 set escapees {a b f n r t v}
461 for {set i 0} {$i < [llength $escapees]} {incr i} {
462 set escape [lindex $escapees $i]
463 send_gdb "print $var_name\[$i\]\n"
466 -re "= \[0-9-\]+ $L'\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
467 pass "try printing '\\${escape}' in ${target_charset}"
469 -re "= \[0-9-\]+ 'x'\[\r\n\]+$gdb_prompt $" {
470 xfail "try printing '\\${escape}' in ${target_charset} (no such escape)"
473 -re "$gdb_prompt $" {
474 fail "try printing '\\${escape}' in ${target_charset}"
477 fail "try printing '\\${escape}' in ${target_charset} (timeout)"
483 # Try parsing a backslash escape in a character literal.
484 gdb_test "print /d $L'\\${escape}' == $var_name\[$i\]" \
486 "check value of '\\${escape}' in ${target_charset}"
488 # Try parsing a backslash escape in a string literal.
489 gdb_test "print /d $L\"\\${escape}\"\[0\] == $var_name\[$i\]" \
491 "check value of \"\\${escape}\" in ${target_charset}"
495 # Try printing a character escape that doesn't exist. We should
496 # get the unescaped character, in the target character set.
497 gdb_test "print $L'\\q'" " = \[0-9-\]+ $L'q'" \
498 "print escape that doesn't exist in $target_charset"
499 gdb_test "print /d $L'\\q' == $var_name\[49\]" " = 1" \
500 "check value of escape that doesn't exist in $target_charset"
503 # Reset the target charset.
504 gdb_test_no_output "set target-charset UTF-8"
506 # \242 is not a valid UTF-8 character.
507 gdb_test "print \"\\242\"" " = \"\\\\242\"" \
508 "non-representable target character"
510 gdb_test "print '\\x'" "\\\\x escape without a following hex digit"
511 gdb_test "print '\\u'" "\\\\u escape without a following hex digit"
512 gdb_test "print '\\9'" " = \[0-9\]+ '9'"
514 # An octal escape can only be 3 digits.
515 gdb_test "print \"\\1011\"" " = \"A1\""
517 # The final digit does not need to be escaped here.
518 foreach val {0 1 2 3 4 5 6 7 8 9 a b c d e f} {
519 gdb_test "print \"\\0\" \"${val}\"" " = \"\\\\000${val}\""
522 # Tests for wide- or unicode- strings. L is the prefix letter to use,
523 # either "L" (for wide strings), "u" (for UTF-16), or "U" (for UTF-32).
524 # NAME is used in the test names and should be related to the prefix
525 # letter in some easy-to-undestand way.
526 proc test_wide_or_unicode {L name} {
527 gdb_test "print $L\"ab\" $L\"c\"" " = $L\"abc\"" \
528 "basic $name string concatenation"
529 gdb_test "print $L\"ab\" \"c\"" " = $L\"abc\"" \
530 "narrow and $name string concatenation"
531 gdb_test "print \"ab\" $L\"c\"" " = $L\"abc\"" \
532 "$name and narrow string concatenation"
533 gdb_test "print $L\"\\xe\" $L\"c\"" " = $L\"\\\\016c\"" \
534 "$name string concatenation with escape"
535 gdb_test "print $L\"\" \"abcdef\" \"g\"" \
537 "concatenate three strings with empty $name string"
538 gdb_test "print $L\"\\xffef\" $L\"f\"" \
539 "$L\"\\\\xffef\\\\146\"" \
540 "test multi-char escape sequence case for $name"
542 gdb_test "print $L'a'" "= \[0-9\]+ $L'a'" \
543 "basic $name character"
547 test_wide_or_unicode L wide
550 set ucs2_ok [expr {[get_sizeof char16_t 99] == 2}]
552 if ![valid_host_charset "UTF-16"] {
553 verbose -log "Disabling UTF-16 tests."
558 test_wide_or_unicode u UTF-16
561 set ucs4_ok [expr {[get_sizeof char32_t 99] == 4}]
563 test_wide_or_unicode U UTF-32
566 # Test an invalid string combination.
567 proc test_combination {L1 name1 L2 name2} {
568 gdb_test "print $L1\"abc\" $L2\"def\"" \
569 "Undefined string concatenation." \
570 "undefined concatenation of $name1 and $name2"
573 if {$wchar_ok && $ucs2_ok} {
574 test_combination L wide u UTF-16
576 if {$wchar_ok && $ucs4_ok} {
577 test_combination L wide U UTF-32
578 # Regression test for a typedef to a typedef.
579 gdb_test "print myvar" "= \[0-9\]+ L'A'" \
582 if {$ucs2_ok && $ucs4_ok} {
583 test_combination u UTF-16 U UTF-32
588 gdb_test_multiple "python print ('hello, world!')" \
589 "verify python support for charset tests" {
590 -re "not supported.*$gdb_prompt $" {
591 unsupported "python support is disabled"
594 -re "$gdb_prompt $" {}
598 gdb_test "print u\"abcdef\"" " = u\"abcdef\"" \
599 "set up for python printing of utf-16 string"
601 gdb_test "python print (gdb.history(0).string())" "abcdef" \
602 "extract utf-16 string using python"
606 # Regression test for a cleanup bug in the charset code.
607 gdb_test "print /d 'a' == 'a' || 'b' == 'b'" \
609 "EVAL_SKIP cleanup handling regression test"
612 proc string_display { var_name set_prefix x_size x_type} {
613 with_test_prefix "set_prefix=$set_prefix" {
614 gdb_test_no_output "set ${var_name} = ${set_prefix}\"Test String\\0with zeroes\""\
615 "assign ${var_name} with prefix ${set_prefix}"
616 gdb_test "x /2${x_size}s ${var_name}" ".*\t${x_type}\"Test String\"\[\r\n\]+.*\t${x_type}\"with zeroes\"" \
617 "display String ${var_name} with x/${x_size}s"
622 string_display String16 u h u
623 if {$wchar_size == 2} {
624 string_display String16 L h u
628 string_display String32 U w U
629 if {$wchar_size == 4} {
630 string_display String32 L w U
634 foreach name {short int long} {
635 # We're really just checking to make sure this doesn't give an
637 gdb_test "print ${name}_array = \"hi\"" \
639 "assign string to $name array"