e17bb14bbce4a8323349338fa7afbf8ad3cc3912
[platform/upstream/binutils.git] / gdb / testsuite / gdb.base / charset.exp
1 # Copyright 2001 Free Software Foundation, Inc.
2
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.
7
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.
12
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.  
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # Test GDB's character set support.
21
22 if $tracelevel then {
23         strace $tracelevel
24 }
25
26 set prms_id 0
27 set bug_id 0
28
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."
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 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)
51             set retlist [list $host_charset $target_charset]
52             pass $testname
53         }
54         -re "The current host character set is `(.*)'\\.\[\r\n\]+The current target character set is `(.*)'\\.\[\r\n\]+$gdb_prompt $" {
55             set host_charset $expect_out(1,string)
56             set target_charset $expect_out(2,string)
57             set retlist [list $host_charset $target_charset]
58             pass $testname
59         }
60         -re "The host character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
61             set host_charset $expect_out(1,string)
62             set retlist [list $host_charset]
63             pass $testname
64         }
65         -re "The target character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
66             set target_charset $expect_out(1,string)
67             set retlist [list $target_charset]
68             pass $testname
69         }
70         -re ".*$gdb_prompt $" {
71             fail $testname
72         }
73         timeout {
74             fail "$testname (timeout)"
75         }
76     }
77
78     return $retlist
79 }
80
81
82 # Try the various `show charset' commands.  These are all aliases of each
83 # other; `show target-charset' and `show host-charset' actually print
84 # both the host and target charsets.
85
86 send_gdb "show charset\n"
87 set show_charset [parse_show_charset_output "show charset"]
88
89 send_gdb "show target-charset\n"
90 set show_target_charset [parse_show_charset_output "show target-charset"]
91
92 if {[lsearch $show_charset $show_target_charset] >= 0} {
93     pass "check `show target-charset' against `show charset'"
94 } else {
95     fail "check `show target-charset' against `show charset'"
96 }
97
98 send_gdb "show host-charset\n"
99 set show_host_charset [parse_show_charset_output "show host-charset"]
100
101 if {[lsearch $show_charset $show_host_charset] >= 0} {
102     pass "check `show host-charset' against `show charset'"
103 } else {
104     fail "check `show host-charset' against `show charset'"
105 }
106
107
108 # Get the list of supported (host) charsets as possible completions.
109 send_gdb "set charset \t\t"
110
111 # Check that we can at least use ASCII as a host character set.
112 sleep 1
113 gdb_expect {
114     -re "^set charset .*\r\nASCII.*\r\n$gdb_prompt set charset " {
115         # We got the output that we wanted, including ASCII as possible
116         # charset. Send a newline to get us back to the prompt. This will
117         # also generate an error message. Let's not check here that the error
118         # message makes sense, we do that below, as a separate testcase.
119         send_gdb "\n"
120         gdb_expect {
121             -re ".*Requires an argument.*$gdb_prompt $" {
122                 pass "get valid character sets"
123             }
124             -re ".*$gdb_prompt $" {
125                 send_gdb "\n"
126                 gdb_expect {
127                     -re ".*$gdb_prompt $" {
128                         fail "get valid character sets"
129                     }
130                 }
131             }
132             timeout {
133                 fail "(timeout) get valid character sets"
134             }
135         }
136     }
137     -re ".*$gdb_prompt $" {
138         # We got some output that ended with a regular prompt
139         fail "get valid character sets"
140     }
141     -re "^set charset.*$" {
142         # We got some other output, send a cntrl-c to gdb to get us back
143         # to the prompt.
144         send_gdb "\003"
145         fail "get valid character sets"
146     }
147     timeout {
148         fail "get valid character sets (timeout)"
149     }
150 }
151
152 # Try a malformed `set charset'.
153 gdb_test "set charset" \
154          "Requires an argument. Valid arguments are.*" \
155          "try malformed `set charset'"
156
157 # Try using `set host-charset' on an invalid character set.
158 gdb_test "set host-charset my_grandma_bonnie" \
159          "Undefined item: \"my_grandma_bonnie\"." \
160          "try `set host-charset' with invalid charset"
161
162 # Try using `set target-charset' on an invalid character set.
163 gdb_test "set target-charset my_grandma_bonnie" \
164          "Undefined item: \"my_grandma_bonnie\"." \
165          "try `set target-charset' with invalid charset"
166
167 # A Tcl array mapping the names of all the character sets we've seen
168 # to "1" if the character set can be used as a host character set, or
169 # "0" otherwise.  We can use `array names charsets' just to get a list
170 # of all character sets.
171 array set charsets {}
172
173 proc all_charset_names {} {
174     global charsets
175     return [array names charsets]
176 }
177
178 proc valid_host_charset {charset} {
179     global charsets
180     return $charsets($charset)
181 }
182
183 send_gdb "set host-charset\n"
184 gdb_expect {
185     -re "Requires an argument. Valid arguments are (\[^ \t\n\r,.\]*)" {
186         #set host_charset_list $expect_out(1,string)
187         set charsets($expect_out(1,string)) 1
188         exp_continue
189         #pass "capture valid host charsets"
190     }
191
192     -re ", (\[^ \t\n\r,.\]*)" {
193         #set host_charset_list $expect_out(1,string)
194         set charsets($expect_out(1,string)) 1
195         exp_continue
196         #pass "capture valid host charsets"
197     }
198
199     -re "\\.\r\n$gdb_prompt $" {
200         #set host_charset_list $expect_out(1,string)
201         set charsets($expect_out(1,string)) 1
202         pass "capture valid host charsets"
203     }
204
205     -re ".*$gdb_prompt $" {
206         fail "capture valid host charsets"
207     }
208     timeout {
209         fail "(timeout) capture valid host charsets"
210     }
211 }
212
213
214 send_gdb "set target-charset\n"
215 gdb_expect {
216     -re "Requires an argument. Valid arguments are (\[^ \t\n\r,.\]*)" {
217         set target_charset $expect_out(1,string)
218         if {! [info exists charsets($target_charset)]} {
219             set charsets($target_charset) 0
220         }
221         exp_continue
222     }
223
224     -re ", (\[^ \t\n\r,.\]*)" {
225         set target_charset $expect_out(1,string)
226         if {! [info exists charsets($target_charset)]} {
227             set charsets($target_charset) 0
228         }
229         exp_continue
230     }
231
232     -re "\\.\r\n$gdb_prompt $" {
233         pass "capture valid target charsets"
234
235     }
236
237     -re ".*$gdb_prompt $" {
238         fail "capture valid target charsets"
239     }
240
241     timeout {
242         fail "(timeout) capture valid target charsets"
243     }
244 }
245
246 # Make sure that GDB supports every host/target charset combination.
247 foreach host_charset [all_charset_names] {
248     if {[valid_host_charset $host_charset]} {
249
250         set testname "try `set host-charset $host_charset'"
251         send_gdb "set host-charset $host_charset\n"
252         gdb_expect {
253             -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
254                 # How did it get into `charsets' then?
255                 fail "$testname (didn't recognize name)"
256             }
257             -re "GDB can't use `.*' as its host character set\\.\[\r\n]+${gdb_prompt} $" {
258                 # Well, then why does its `charsets' entry say it can?
259                 fail $testname
260             }
261             -re "${gdb_prompt} $" {
262                 pass $testname
263             }
264             timeout {
265                 fail "$testname (timeout)"
266             }
267         }
268
269         # Check that the command actually had its intended effect:
270         # $host_charset should now be the host character set.
271         send_gdb "show charset\n"
272         set result [parse_show_charset_output "parse `show charset' after `set host-charset $host_charset'"]
273         if {! [string compare [lindex $result 0] $host_charset]} {
274             pass "check effect of `set host-charset $host_charset'"
275         } else {
276             fail "check effect of `set host-charset $host_charset'"
277         }
278
279         # Now try setting every possible target character set,
280         # given that host charset.
281         foreach target_charset [all_charset_names] {
282             set testname "try `set target-charset $target_charset'"
283             send_gdb "set target-charset $target_charset\n"
284             gdb_expect {
285                 -re "GDB doesn't know of any character set named.*\[\r\n]+${gdb_prompt} $" {
286                     fail "$testname (didn't recognize name)"
287                 }
288                 -re "GDB can't convert from the .* character set to .*\\.\[\r\n\]+${gdb_prompt} $" {
289                     # This is a serious problem.  GDB should be able to convert
290                     # between any arbitrary pair of character sets.
291                     fail "$testname (can't convert)"
292                 }
293                 -re "${gdb_prompt} $" {
294                     pass $testname
295                 }
296                 timeout {
297                     fail "$testname (timeout)"
298                 }
299             }
300
301             # Check that the command actually had its intended effect:
302             # $target_charset should now be the target charset.
303             send_gdb "show charset\n"
304             set result [parse_show_charset_output "parse `show charset' after `set target-charset $target_charset'"]
305             if {! [string compare $result [list $host_charset $target_charset]]} {
306                 pass "check effect of `set target-charset $target_charset'"
307             } else {
308                 fail "check effect of `set target-charset $target_charset'"
309             }
310
311             # Test handling of characters in the host charset which
312             # can't be translated into the target charset.  \xA2 is
313             # `cent' in ISO-8859-1, which has no equivalent in ASCII.
314             #
315             # On some systems, the pseudo-tty through which we
316             # communicate with GDB insists on stripping the high bit
317             # from input characters, meaning that `cent' turns into
318             # `"'.  Since ISO-8859-1 and ASCII are identical in the
319             # lower 128 characters, it's tough to see how we can test
320             # this behavior on such systems, so we just xfail it.
321             #
322             # Note: the \x16 (Control-V) is an escape to allow \xA2 to
323             # get past readline.
324             if {! [string compare $host_charset iso-8859-1] && ! [string compare $target_charset ascii]} {
325
326                 set testname "untranslatable character in character literal"
327                 send_gdb "print '\x16\xA2'\n"
328                 gdb_expect {
329                     -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
330                         pass $testname
331                     }
332                     -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
333                         xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
334                     }
335                     -re "$gdb_prompt $" {
336                         fail $testname
337                     }
338                     timeout {
339                         fail "$testname (timeout)"
340                     }
341                 }
342
343                 set testname "untranslatable character in string literal"
344                 # If the PTTY zeros bit seven, then this turns into
345                 #   print """
346                 # which gets us a syntax error.  We don't care.
347                 send_gdb "print \"\x16\xA2\"\n"
348                 gdb_expect {
349                     -re "There is no character corresponding to .* in the target character set .*\\.\[\r\n\]+$gdb_prompt $" {
350                         pass $testname
351                     }
352                     -re "Unterminated string in expression.\[\r\n\]+$gdb_prompt $" {
353                         xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
354                     }
355                     -re "$gdb_prompt $" {
356                         fail $testname
357                     }
358                     timeout {
359                         fail "$testname (timeout)"
360                     }
361                 }
362
363                 set testname "untranslatable characters in backslash escape"
364                 send_gdb "print '\\\x16\xA2'\n"
365                 gdb_expect {
366                     -re "The escape sequence .* is equivalent to plain .*, which has no equivalent\[\r\n\]+in the .* character set\\.\[\r\n\]+$gdb_prompt $" {
367                         pass $testname
368                     }
369                     -re " = 34 '\"'\[\r\n\]+$gdb_prompt $" {
370                         xfail "$testname (DejaGNU's pseudo-tty strips eighth bit)"
371                     }
372                     -re "$gdb_prompt $" {
373                         fail $testname
374                     }
375                     timeout {
376                         fail "$testname (timeout)"
377                     }
378                 }
379             }
380         }
381     }
382 }
383
384
385 # Set the host character set to plain ASCII, and try actually printing
386 # some strings in various target character sets.  We need to run the
387 # test program to the point at which the strings have been
388 # initialized.
389 gdb_test "break [gdb_get_line_number "all strings initialized"]" \
390          ".*Breakpoint.* at .*" \
391          "set breakpoint after all strings have been initialized"
392 gdb_run_cmd
393 gdb_expect {
394     -re "Breakpoint.*all strings initialized.*$gdb_prompt $" {
395         pass "run until all strings have been initialized"
396     }
397     -re "$gdb_prompt $" {
398         fail "run until all strings have been initialized"
399     }
400     timeout {
401         fail "run until all strings have been initialized (timeout)"
402     }
403 }
404
405
406 gdb_test "set host-charset ASCII" ""
407 foreach target_charset [all_charset_names] {
408     send_gdb "set target-charset $target_charset\n" 
409     gdb_expect {
410         -re "$gdb_prompt $" {
411             pass "set target-charset $target_charset"
412         }
413         timeout {
414             fail "set target-charset $target_charset (timeout)"
415         }
416     }
417
418     # Try printing the null character.  There seems to be a bug in
419     # gdb_test that requires us to use gdb_expect here.
420     send_gdb "print '\\0'\n"
421     gdb_expect {
422         -re "\\\$${decimal} = 0 '\\\\0'\[\r\n\]+$gdb_prompt $" {
423             pass "print the null character in ${target_charset}"
424         }
425         -re "$gdb_prompt $" {
426             fail "print the null character in ${target_charset}"
427         }
428         timeout {
429             fail "print the null character in ${target_charset} (timeout)"
430         }
431     }
432
433     # Compute the name of the variable in the test program that holds
434     # a string in $target_charset.  The variable's name is the
435     # character set's name, in lower-case, with all non-identifier
436     # characters replaced with '_', with "_string" stuck on the end.
437     set var_name [string tolower "${target_charset}_string"]
438     regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
439     
440     # Compute a regexp matching the results we expect.  This is static,
441     # but it's easier than writing it out.
442     regsub -all "." "abefnrtv" "(\\\\&|x)" escapes
443     set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
444     set lowercase "abcdefghijklmnopqrstuvwxyz"
445     set digits "0123456789"
446     set octal_escape "\\\\\[0-9\]\[0-9\]\[0-9\]"
447
448     send_gdb "print $var_name\n"
449     # ${escapes}${uppercase}${lowercase}${digits}${octal}${octal}
450     gdb_expect {
451         -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 $" {
452             pass "print string in $target_charset"
453         }
454         -re "$gdb_prompt $" {
455             fail "print string in $target_charset"
456         }
457         timeout {
458             fail "print string in $target_charset (timeout)"
459         }
460     }
461
462     # Try entering a character literal, and see if it comes back unchanged.
463     gdb_test "print 'A'" \
464              " = \[0-9-\]+ 'A'" \
465              "parse character literal in ${target_charset}"
466
467     # Check that the character literal was encoded correctly.
468     gdb_test "print 'A' == $var_name\[8\]" \
469              " = 1" \
470              "check value of parsed character literal in ${target_charset}"
471
472     # Try entering a string literal, and see if it comes back unchanged.
473     gdb_test "print \"abcdefABCDEF012345\"" \
474              " = \"abcdefABCDEF012345\"" \
475              "parse string literal in ${target_charset}"
476
477     # Check that the string literal was encoded correctly.
478     gdb_test "print \"q\"\[0\] == $var_name\[50\]" \
479              " = 1" \
480              "check value of parsed string literal in ${target_charset}"
481
482     # Test handling of characters in the target charset which
483     # can't be translated into the host charset.
484     if {! [string compare $target_charset iso-8859-1]} {
485         gdb_test "print iso_8859_1_string\[70\]" \
486                  " = \[0-9-\]+ '\\\\242'" \
487                  "print character with no equivalent in host character set"
488         gdb_test "print iso_8859_1_string + 70" \
489                  " = ${hex} \"\\\\242.*\"" \
490                  "print string with no equivalent in host character set"
491     }
492
493     # Make sure that we don't apply the ISO-8859-1 `print_literally'
494     # function to ASCII.
495     if {! [string compare $target_charset ascii]} {
496         gdb_test "print iso_8859_1_string\[70\]" \
497                  " = \[0-9-\]+ '\\\\242'" \
498                  "print ASCII unprintable character"
499         gdb_test "print iso_8859_1_string + 70" \
500                  " = ${hex} \"\\\\242.*\"" \
501                  "print ASCII unprintable string"
502     }
503
504     # Try printing characters with backslash escape equivalents.
505     set escapees {a b e f n r t v}
506     for {set i 0} {$i < [llength $escapees]} {incr i} {
507         set escape [lindex $escapees $i]
508         send_gdb "print $var_name\[$i\]\n"
509         set have_escape 1
510         gdb_expect {
511             -re "= \[0-9-\]+ '\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
512                 pass "try printing '\\${escape}' in ${target_charset}"
513             }
514             -re "= \[0-9-\]+ 'x'\[\r\n\]+$gdb_prompt $" {
515                 xfail "try printing '\\${escape}' in ${target_charset} (no such escape)"
516                 set have_escape 0
517             }
518             -re "$gdb_prompt $" {
519                 fail "try printing '\\${escape}' in ${target_charset}"
520             }
521             timeout {
522                 fail "try printing '\\${escape}' in ${target_charset} (timeout)"
523             }
524         }
525
526         if {$have_escape} {
527
528             # Try parsing a backslash escape in a character literal.
529             gdb_test "print '\\${escape}' == $var_name\[$i\]" \
530                      " = 1" \
531                      "check value of '\\${escape}' in ${target_charset}"
532
533             # Try parsing a backslash escape in a string literal.
534             gdb_test "print \"\\${escape}\"\[0\] == $var_name\[$i\]" \
535                      " = 1" \
536                      "check value of \"\\${escape}\" in ${target_charset}"
537         }
538     }
539
540     # Try printing a character escape that doesn't exist.  We should 
541     # get the unescaped character, in the target character set.
542     gdb_test "print '\\q'" " = \[0-9-\]+ 'q'" \
543              "print escape that doesn't exist in $target_charset"
544     gdb_test "print '\\q' == $var_name\[50\]" " = 1" \
545              "check value of escape that doesn't exist in $target_charset"
546 }
547
548 gdb_exit