1 # Copyright 2001 Free Software Foundation, Inc.
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 2 of the License, or
6 # (at your option) any later version.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
20 # Test GDB's character set support.
29 set testfile "charset"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
36 # Start with a fresh gdb.
39 gdb_reinitialize_dir $srcdir/$subdir
42 # Parse the output from a `show charset' command. Return the host
43 # and target charset as a two-element list.
44 proc parse_show_charset_output {testname} {
48 -re "The current host and target character set is `(.*)'\\.\[\r\n\]+$gdb_prompt $" {
49 set host_charset $expect_out(1,string)
50 set target_charset $expect_out(1,string)
53 -re "The current host character set is `(.*)'\\.\[\r\n\]+The current target character set is `(.*)'\\.\[\r\n\]+$gdb_prompt $" {
54 set host_charset $expect_out(1,string)
55 set target_charset $expect_out(2,string)
58 -re ".*$gdb_prompt $" {
62 fail "$testname (timeout)"
66 return [list $host_charset $target_charset]
70 # Try the various `show charset' commands. These are all aliases of each
71 # other; `show target-charset' and `show host-charset' actually print
72 # both the host and target charsets.
74 send_gdb "show charset\n"
75 set show_charset [parse_show_charset_output "show charset"]
77 send_gdb "show target-charset\n"
78 set show_target_charset [parse_show_charset_output "show target-charset"]
80 if {! [string compare $show_charset $show_target_charset]} {
81 pass "check `show target-charset' against `show charset'"
83 fail "check `show target-charset' against `show charset'"
86 send_gdb "show host-charset\n"
87 set show_host_charset [parse_show_charset_output "show host-charset"]
89 if {! [string compare $show_charset $show_host_charset]} {
90 pass "check `show host-charset' against `show charset'"
92 fail "check `show host-charset' against `show charset'"
96 # Get the list of supported charsets.
97 send_gdb "set charset\n"
99 # True iff we've seen the "Valid character sets are:" message.
102 # True iff we've seen the "can be used as a host character set" message.
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 {}
111 proc all_charset_names {} {
113 return [array names charsets]
116 proc charset_exists {charset} {
118 return [info exists charsets($charset)]
121 proc valid_host_charset {charset} {
123 return $charsets($charset)
127 -re "Valid character sets are:\[\r\n\]+" {
128 # There's no ^ at the beginning of the pattern above, so that
129 # expect can skip the echoed `set charset' command.
133 -re "^ (\[^ \t\n\]*) \\*\[\r\n\]+" {
134 set charsets($expect_out(1,string)) 1
137 -re "^ (\[^ \t\n\]*)\[ \t\]*\[\r\n\]+" {
138 set charsets($expect_out(1,string)) 0
141 -re "^\\* - can be used as a host character set\[\r\n\]+" {
145 -re ".*${gdb_prompt} $" {
146 # We don't do an exp_continue here.
149 fail "get valid character sets (timeout)"
154 # Check that we've seen all the right pieces of the output, and that
155 # we can at least use ASCII as a host character set.
156 if {$seen_valid && $seen_can_host && [charset_exists ascii]} {
157 # We can't do the below as part of the test above, since all the
158 # [] substitution takes place before any expression evaluation
159 # takes place; && doesn't really short circuit things the way
160 # you'd like. We'd get an "can't read $charsets(ascii)" error
161 # even when `info exists' had returned zero.
162 if {[valid_host_charset ascii]} {
163 pass "get valid character sets"
165 fail "get valid character sets"
168 fail "get valid character sets (no ascii charset)"
172 # Try using `set host-charset' on an invalid character set.
173 gdb_test "set host-charset my_grandma_bonnie" \
174 "GDB doesn't know of any character set named `my_grandma_bonnie'." \
175 "try `set host-charset' with invalid charset"
178 # Try using `set target-charset' on an invalid character set.
179 gdb_test "set target-charset my_grandma_bonnie" \
180 "GDB doesn't know of any character set named `my_grandma_bonnie'." \
181 "try `set target-charset' with invalid charset"
184 # Make sure that GDB supports every host/target charset combination.
185 foreach host_charset [all_charset_names] {
186 if {[valid_host_charset $host_charset]} {
188 set testname "try `set host-charset $host_charset'"
189 send_gdb "set host-charset $host_charset\n"
191 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
192 # How did it get into `charsets' then?
193 fail "$testname (didn't recognize name)"
195 -re "GDB can't use `.*' as its host character set\\.\[\r\n]+${gdb_prompt} $" {
196 # Well, then why does its `charsets' entry say it can?
199 -re "${gdb_prompt} $" {
203 fail "$testname (timeout)"
207 # Check that the command actually had its intended effect:
208 # $host_charset should now be the host character set.
209 send_gdb "show charset\n"
210 set result [parse_show_charset_output "parse `show charset' after `set host-charset $host_charset'"]
211 if {! [string compare [lindex $result 0] $host_charset]} {
212 pass "check effect of `set host-charset $host_charset'"
214 fail "check effect of `set host-charset $host_charset'"
217 # Now try setting every possible target character set,
218 # given that host charset.
219 foreach target_charset [all_charset_names] {
220 set testname "try `set target-charset $target_charset'"
221 send_gdb "set target-charset $target_charset\n"
223 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
224 fail "$testname (didn't recognize name)"
226 -re "GDB can't convert from the .* character set to .*\\.\[\r\n\]+${gdb_prompt} $" {
227 # This is a serious problem. GDB should be able to convert
228 # between any arbitrary pair of character sets.
229 fail "$testname (can't convert)"
231 -re "${gdb_prompt} $" {
235 fail "$testname (timeout)"
239 # Check that the command actually had its intended effect:
240 # $target_charset should now be the target charset.
241 send_gdb "show charset\n"
242 set result [parse_show_charset_output "parse `show charset' after `set target-charset $target_charset'"]
243 if {! [string compare $result [list $host_charset $target_charset]]} {
244 pass "check effect of `set target-charset $target_charset'"
246 fail "check effect of `set target-charset $target_charset'"
249 # Test handling of characters in the host charset which
250 # can't be translated into the target charset. \xA2 is
251 # `cent' in ISO-8859-1, which has no equivalent in ASCII.
253 # On some systems, the pseudo-tty through which we
254 # communicate with GDB insists on stripping the high bit
255 # from input characters, meaning that `cent' turns into
256 # `"'. Since ISO-8859-1 and ASCII are identical in the
257 # lower 128 characters, it's tough to see how we can test
258 # this behavior on such systems, so we just xfail it.
260 # Note: the \x16 (Control-V) is an escape to allow \xA2 to
262 if {! [string compare $host_charset iso-8859-1] && ! [string compare $target_charset ascii]} {
264 set testname "untranslatable character in character literal"
265 send_gdb "print '\x16\xA2'\n"
267 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
270 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
271 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
273 -re "$gdb_prompt $" {
277 fail "$testname (timeout)"
281 set testname "untranslatable character in string literal"
282 # If the PTTY zeros bit seven, then this turns into
284 # which gets us a syntax error. We don't care.
285 send_gdb "print \"\x16\xA2\"\n"
287 -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
290 -re "Unterminated string in expression.\[\r\n\]+$gdb_prompt $" {
291 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
293 -re "$gdb_prompt $" {
297 fail "$testname (timeout)"
301 set testname "untranslatable characters in backslash escape"
302 send_gdb "print '\\\x16\xA2'\n"
304 -re "The escape sequence .* is equivalent to plain .*, which has no equivalent\[\r\n\]+in the .* character set\\.\[\r\n\]+$gdb_prompt $" {
307 -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
308 xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
310 -re "$gdb_prompt $" {
314 fail "$testname (timeout)"
323 # Set the host character set to plain ASCII, and try actually printing
324 # some strings in various target character sets. We need to run the
325 # test program to the point at which the strings have been
327 gdb_test "break [gdb_get_line_number "all strings initialized"]" \
328 ".*Breakpoint.* at .*" \
329 "set breakpoint after all strings have been initialized"
332 -re "Breakpoint.*all strings initialized.*$gdb_prompt $" {
333 pass "run until all strings have been initialized"
335 -re "$gdb_prompt $" {
336 fail "run until all strings have been initialized"
339 fail "run until all strings have been initialized (timeout)"
344 gdb_test "set host-charset ascii" ""
345 foreach target_charset [all_charset_names] {
346 send_gdb "set target-charset $target_charset\n"
348 -re "$gdb_prompt $" {
349 pass "set target-charset $target_charset"
352 fail "set target-charset $target_charset (timeout)"
356 # Try printing the null character. There seems to be a bug in
357 # gdb_test that requires us to use gdb_expect here.
358 send_gdb "print '\\0'\n"
360 -re "\\\$${decimal} = 0 '\\\\0'\[\r\n\]+$gdb_prompt $" {
361 pass "print the null character in ${target_charset}"
363 -re "$gdb_prompt $" {
364 fail "print the null character in ${target_charset}"
367 fail "print the null character in ${target_charset} (timeout)"
371 # Compute the name of the variable in the test program that holds
372 # a string in $target_charset. The variable's name is the
373 # character set's name, in lower-case, with all non-identifier
374 # characters replaced with '_', with "_string" stuck on the end.
375 set var_name [string tolower "${target_charset}_string"]
376 regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
378 # Compute a regexp matching the results we expect. This is static,
379 # but it's easier than writing it out.
380 regsub -all "." "abefnrtv" "(\\\\&|x)" escapes
381 set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
382 set lowercase "abcdefghijklmnopqrstuvwxyz"
383 set digits "0123456789"
384 set octal_escape "\\\\\[0-9\]\[0-9\]\[0-9\]"
386 send_gdb "print $var_name\n"
387 # ${escapes}${uppercase}${lowercase}${digits}${octal}${octal}
389 -re ".* = \"(\\\\a|x)(\\\\b|x)(\\\\e|x)(\\\\f|x)(\\\\n|x)(\\\\r|x)(\\\\t|x)(\\\\v|x)${uppercase}${lowercase}${digits}(\\\\\[0-9\]\[0-9\]\[0-9\]|x)(\\\\\[0-9\]\[0-9\]\[0-9\]|x).*\"\[\r\n\]+$gdb_prompt $" {
390 pass "print string in $target_charset"
392 -re "$gdb_prompt $" {
393 fail "print string in $target_charset"
396 fail "print string in $target_charset (timeout)"
400 # Try entering a character literal, and see if it comes back unchanged.
401 gdb_test "print 'A'" \
403 "parse character literal in ${target_charset}"
405 # Check that the character literal was encoded correctly.
406 gdb_test "print 'A' == $var_name\[8\]" \
408 "check value of parsed character literal in ${target_charset}"
410 # Try entering a string literal, and see if it comes back unchanged.
411 gdb_test "print \"abcdefABCDEF012345\"" \
412 " = \"abcdefABCDEF012345\"" \
413 "parse string literal in ${target_charset}"
415 # Check that the string literal was encoded correctly.
416 gdb_test "print \"q\"\[0\] == $var_name\[50\]" \
418 "check value of parsed string literal in ${target_charset}"
420 # Test handling of characters in the target charset which
421 # can't be translated into the host charset.
422 if {! [string compare $target_charset iso-8859-1]} {
423 gdb_test "print iso_8859_1_string\[70\]" \
424 " = \[0-9-\]+ '\\\\242'" \
425 "print character with no equivalent in host character set"
426 gdb_test "print iso_8859_1_string + 70" \
427 " = ${hex} \"\\\\242.*\"" \
428 "print string with no equivalent in host character set"
431 # Make sure that we don't apply the ISO-8859-1 `print_literally'
433 if {! [string compare $target_charset ascii]} {
434 gdb_test "print iso_8859_1_string\[70\]" \
435 " = \[0-9-\]+ '\\\\242'" \
436 "print ASCII unprintable character"
437 gdb_test "print iso_8859_1_string + 70" \
438 " = ${hex} \"\\\\242.*\"" \
439 "print ASCII unprintable string"
442 # Try printing characters with backslash escape equivalents.
443 set escapees {a b e f n r t v}
444 for {set i 0} {$i < [llength $escapees]} {incr i} {
445 set escape [lindex $escapees $i]
446 send_gdb "print $var_name\[$i\]\n"
449 -re "= \[0-9-\]+ '\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
450 pass "try printing '\\${escape}' in ${target_charset}"
452 -re "= \[0-9-\]+ 'x'\[\r\n\]+$gdb_prompt $" {
453 xfail "try printing '\\${escape}' in ${target_charset} (no such escape)"
456 -re "$gdb_prompt $" {
457 fail "try printing '\\${escape}' in ${target_charset}"
460 fail "try printing '\\${escape}' in ${target_charset} (timeout)"
466 # Try parsing a backslash escape in a character literal.
467 gdb_test "print '\\${escape}' == $var_name\[$i\]" \
469 "check value of '\\${escape}' in ${target_charset}"
471 # Try parsing a backslash escape in a string literal.
472 gdb_test "print \"\\${escape}\"\[0\] == $var_name\[$i\]" \
474 "check value of \"\\${escape}\" in ${target_charset}"
478 # Try printing a character escape that doesn't exist. We should
479 # get the unescaped character, in the target character set.
480 gdb_test "print '\\q'" " = \[0-9-\]+ 'q'" \
481 "print escape that doesn't exist in $target_charset"
482 gdb_test "print '\\q' == $var_name\[50\]" " = 1" \
483 "check value of escape that doesn't exist in $target_charset"