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