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