1 /* Character set conversion support for GDB.
3 Copyright (C) 2001, 2003, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_assert.h"
25 #include "gdb_obstack.h"
27 #include "charset-list.h"
30 #include "arch-utils.h"
33 #include "gdb_string.h"
40 /* How GDB's character set support works
42 GDB has three global settings:
44 - The `current host character set' is the character set GDB should
45 use in talking to the user, and which (hopefully) the user's
46 terminal knows how to display properly. Most users should not
49 - The `current target character set' is the character set the
50 program being debugged uses.
52 - The `current target wide character set' is the wide character set
53 the program being debugged uses, that is, the encoding used for
56 There are commands to set each of these, and mechanisms for
57 choosing reasonable default values. GDB has a global list of
58 character sets that it can use as its host or target character
61 The header file `charset.h' declares various functions that
62 different pieces of GDB need to perform tasks like:
64 - printing target strings and characters to the user's terminal
65 (mostly target->host conversions),
67 - building target-appropriate representations of strings and
68 characters the user enters in expressions (mostly host->target
73 To avoid excessive code duplication and maintenance efforts,
74 GDB simply requires a capable iconv function. Users on platforms
75 without a suitable iconv can use the GNU iconv library. */
80 /* Provide a phony iconv that does as little as possible. Also,
81 arrange for there to be a single available character set. */
83 #undef GDB_DEFAULT_HOST_CHARSET
84 #define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
85 #define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
86 #define GDB_DEFAULT_TARGET_WIDE_CHARSET "ISO-8859-1"
87 #undef DEFAULT_CHARSET_NAMES
88 #define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
97 #define ICONV_CONST const
99 /* Some systems don't have EILSEQ, so we define it here, but not as
100 EINVAL, because callers of `iconv' want to distinguish EINVAL and
101 EILSEQ. This is what iconv.h from libiconv does as well. Note
102 that wchar.h may also define EILSEQ, so this needs to be after we
103 include wchar.h, which happens in defs.h through gdb_wchar.h. */
105 #define EILSEQ ENOENT
109 iconv_open (const char *to, const char *from)
111 /* We allow conversions from UTF-32BE, wchar_t, and the host charset.
112 We allow conversions to wchar_t and the host charset. */
113 if (strcmp (from, "UTF-32BE") && strcmp (from, "wchar_t")
114 && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
116 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
119 /* Return 1 if we are converting from UTF-32BE, 0 otherwise. This is
120 used as a flag in calls to iconv. */
121 return !strcmp (from, "UTF-32BE");
125 iconv_close (iconv_t arg)
131 iconv (iconv_t utf_flag, const char **inbuf, size_t *inbytesleft,
132 char **outbuf, size_t *outbytesleft)
136 while (*inbytesleft >= 4)
141 for (j = 0; j < 4; ++j)
144 c += (*inbuf)[j] & 0xff;
159 if (*inbytesleft < 4)
167 /* In all other cases we simply copy input bytes to the
169 size_t amt = *inbytesleft;
170 if (amt > *outbytesleft)
172 memcpy (*outbuf, *inbuf, amt);
176 *outbytesleft -= amt;
185 /* The number of non-reversible conversions -- but they were all
194 /* The global lists of character sets and translations. */
197 #ifndef GDB_DEFAULT_TARGET_CHARSET
198 #define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
201 #ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
202 #define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
205 static const char *auto_host_charset_name = GDB_DEFAULT_HOST_CHARSET;
206 static const char *host_charset_name = "auto";
208 show_host_charset_name (struct ui_file *file, int from_tty,
209 struct cmd_list_element *c,
212 if (!strcmp (value, "auto"))
213 fprintf_filtered (file,
214 _("The host character set is \"auto; currently %s\".\n"),
215 auto_host_charset_name);
217 fprintf_filtered (file, _("The host character set is \"%s\".\n"), value);
220 static const char *target_charset_name = "auto";
222 show_target_charset_name (struct ui_file *file, int from_tty,
223 struct cmd_list_element *c, const char *value)
225 if (!strcmp (value, "auto"))
226 fprintf_filtered (file,
227 _("The target character set is \"auto; "
228 "currently %s\".\n"),
229 gdbarch_auto_charset (get_current_arch ()));
231 fprintf_filtered (file, _("The target character set is \"%s\".\n"),
235 static const char *target_wide_charset_name = "auto";
237 show_target_wide_charset_name (struct ui_file *file, int from_tty,
238 struct cmd_list_element *c, const char *value)
240 if (!strcmp (value, "auto"))
241 fprintf_filtered (file,
242 _("The target wide character set is \"auto; "
243 "currently %s\".\n"),
244 gdbarch_auto_wide_charset (get_current_arch ()));
246 fprintf_filtered (file, _("The target wide character set is \"%s\".\n"),
250 static const char *default_charset_names[] =
252 DEFAULT_CHARSET_NAMES
256 static const char **charset_enum;
259 /* If the target wide character set has big- or little-endian
260 variants, these are the corresponding names. */
261 static const char *target_wide_charset_be_name;
262 static const char *target_wide_charset_le_name;
264 /* The architecture for which the BE- and LE-names are valid. */
265 static struct gdbarch *be_le_arch;
267 /* A helper function which sets the target wide big- and little-endian
268 character set names, if possible. */
271 set_be_le_names (struct gdbarch *gdbarch)
274 const char *target_wide;
276 if (be_le_arch == gdbarch)
278 be_le_arch = gdbarch;
280 target_wide_charset_le_name = NULL;
281 target_wide_charset_be_name = NULL;
283 target_wide = target_wide_charset_name;
284 if (!strcmp (target_wide, "auto"))
285 target_wide = gdbarch_auto_wide_charset (gdbarch);
287 len = strlen (target_wide);
288 for (i = 0; charset_enum[i]; ++i)
290 if (strncmp (target_wide, charset_enum[i], len))
292 if ((charset_enum[i][len] == 'B'
293 || charset_enum[i][len] == 'L')
294 && charset_enum[i][len + 1] == 'E'
295 && charset_enum[i][len + 2] == '\0')
297 if (charset_enum[i][len] == 'B')
298 target_wide_charset_be_name = charset_enum[i];
300 target_wide_charset_le_name = charset_enum[i];
305 /* 'Set charset', 'set host-charset', 'set target-charset', 'set
306 target-wide-charset', 'set charset' sfunc's. */
309 validate (struct gdbarch *gdbarch)
312 const char *host_cset = host_charset ();
313 const char *target_cset = target_charset (gdbarch);
314 const char *target_wide_cset = target_wide_charset_name;
315 if (!strcmp (target_wide_cset, "auto"))
316 target_wide_cset = gdbarch_auto_wide_charset (gdbarch);
318 desc = iconv_open (target_wide_cset, host_cset);
319 if (desc == (iconv_t) -1)
320 error ("Cannot convert between character sets `%s' and `%s'",
321 target_wide_cset, host_cset);
324 desc = iconv_open (target_cset, host_cset);
325 if (desc == (iconv_t) -1)
326 error ("Cannot convert between character sets `%s' and `%s'",
327 target_cset, host_cset);
330 /* Clear the cache. */
334 /* This is the sfunc for the 'set charset' command. */
336 set_charset_sfunc (char *charset, int from_tty, struct cmd_list_element *c)
338 /* CAREFUL: set the target charset here as well. */
339 target_charset_name = host_charset_name;
340 validate (get_current_arch ());
343 /* 'set host-charset' command sfunc. We need a wrapper here because
344 the function needs to have a specific signature. */
346 set_host_charset_sfunc (char *charset, int from_tty,
347 struct cmd_list_element *c)
349 validate (get_current_arch ());
352 /* Wrapper for the 'set target-charset' command. */
354 set_target_charset_sfunc (char *charset, int from_tty,
355 struct cmd_list_element *c)
357 validate (get_current_arch ());
360 /* Wrapper for the 'set target-wide-charset' command. */
362 set_target_wide_charset_sfunc (char *charset, int from_tty,
363 struct cmd_list_element *c)
365 validate (get_current_arch ());
368 /* sfunc for the 'show charset' command. */
370 show_charset (struct ui_file *file, int from_tty, struct cmd_list_element *c,
373 show_host_charset_name (file, from_tty, c, host_charset_name);
374 show_target_charset_name (file, from_tty, c, target_charset_name);
375 show_target_wide_charset_name (file, from_tty, c, target_wide_charset_name);
379 /* Accessor functions. */
384 if (!strcmp (host_charset_name, "auto"))
385 return auto_host_charset_name;
386 return host_charset_name;
390 target_charset (struct gdbarch *gdbarch)
392 if (!strcmp (target_charset_name, "auto"))
393 return gdbarch_auto_charset (gdbarch);
394 return target_charset_name;
398 target_wide_charset (struct gdbarch *gdbarch)
400 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
402 set_be_le_names (gdbarch);
403 if (byte_order == BFD_ENDIAN_BIG)
405 if (target_wide_charset_be_name)
406 return target_wide_charset_be_name;
410 if (target_wide_charset_le_name)
411 return target_wide_charset_le_name;
414 if (!strcmp (target_wide_charset_name, "auto"))
415 return gdbarch_auto_wide_charset (gdbarch);
417 return target_wide_charset_name;
421 /* Host character set management. For the time being, we assume that
422 the host character set is some superset of ASCII. */
425 host_letter_to_control_character (char c)
432 /* Convert a host character, C, to its hex value. C must already have
433 been validated using isxdigit. */
436 host_hex_value (char c)
440 if (c >= 'a' && c <= 'f')
442 gdb_assert (c >= 'A' && c <= 'F');
447 /* Public character management functions. */
449 /* A cleanup function which is run to close an iconv descriptor. */
452 cleanup_iconv (void *p)
455 iconv_close (*descp);
459 convert_between_encodings (const char *from, const char *to,
460 const gdb_byte *bytes, unsigned int num_bytes,
461 int width, struct obstack *output,
462 enum transliterations translit)
465 struct cleanup *cleanups;
468 unsigned int space_request;
470 /* Often, the host and target charsets will be the same. */
471 if (!strcmp (from, to))
473 obstack_grow (output, bytes, num_bytes);
477 desc = iconv_open (to, from);
478 if (desc == (iconv_t) -1)
479 perror_with_name ("Converting character sets");
480 cleanups = make_cleanup (cleanup_iconv, &desc);
483 inp = (char *) bytes;
485 space_request = num_bytes;
493 old_size = obstack_object_size (output);
494 obstack_blank (output, space_request);
496 outp = obstack_base (output) + old_size;
497 outleft = space_request;
499 r = iconv (desc, (ICONV_CONST char **) &inp, &inleft, &outp, &outleft);
501 /* Now make sure that the object on the obstack only includes
502 bytes we have converted. */
503 obstack_blank (output, - (int) outleft);
505 if (r == (size_t) -1)
513 /* Invalid input sequence. */
514 if (translit == translit_none)
515 error (_("Could not convert character to `%s' character set"),
518 /* We emit escape sequence for the bytes, skip them,
520 for (i = 0; i < width; ++i)
524 sprintf (octal, "\\%.3o", *inp & 0xff);
525 obstack_grow_str (output, octal);
534 /* We ran out of space in the output buffer. Make it
535 bigger next time around. */
540 /* Incomplete input sequence. FIXME: ought to report this
541 to the caller somehow. */
546 perror_with_name ("Internal error while converting character sets");
551 do_cleanups (cleanups);
556 /* An iterator that returns host wchar_t's from a target string. */
557 struct wchar_iterator
559 /* The underlying iconv descriptor. */
562 /* The input string. This is updated as convert characters. */
564 /* The number of bytes remaining in the input. */
567 /* The width of an input character. */
570 /* The output buffer and its size. */
575 /* Create a new iterator. */
576 struct wchar_iterator *
577 make_wchar_iterator (const gdb_byte *input, size_t bytes, const char *charset,
580 struct wchar_iterator *result;
583 desc = iconv_open (INTERMEDIATE_ENCODING, charset);
584 if (desc == (iconv_t) -1)
585 perror_with_name ("Converting character sets");
587 result = XNEW (struct wchar_iterator);
589 result->input = (char *) input;
590 result->bytes = bytes;
591 result->width = width;
593 result->out = XNEW (gdb_wchar_t);
594 result->out_size = 1;
600 do_cleanup_iterator (void *p)
602 struct wchar_iterator *iter = p;
604 iconv_close (iter->desc);
610 make_cleanup_wchar_iterator (struct wchar_iterator *iter)
612 return make_cleanup (do_cleanup_iterator, iter);
616 wchar_iterate (struct wchar_iterator *iter,
617 enum wchar_iterate_result *out_result,
618 gdb_wchar_t **out_chars,
619 const gdb_byte **ptr,
624 /* Try to convert some characters. At first we try to convert just
625 a single character. The reason for this is that iconv does not
626 necessarily update its outgoing arguments when it encounters an
627 invalid input sequence -- but we want to reliably report this to
628 our caller so it can emit an escape sequence. */
630 while (iter->bytes > 0)
632 char *outptr = (char *) &iter->out[0];
633 char *orig_inptr = iter->input;
634 size_t orig_in = iter->bytes;
635 size_t out_avail = out_request * sizeof (gdb_wchar_t);
639 size_t r = iconv (iter->desc,
640 (ICONV_CONST char **) &iter->input, &iter->bytes,
641 &outptr, &out_avail);
642 if (r == (size_t) -1)
647 /* Invalid input sequence. Skip it, and let the caller
649 *out_result = wchar_iterate_invalid;
652 iter->input += iter->width;
653 iter->bytes -= iter->width;
657 /* We ran out of space. We still might have converted a
658 character; if so, return it. Otherwise, grow the
659 buffer and try again. */
660 if (out_avail < out_request * sizeof (gdb_wchar_t))
664 if (out_request > iter->out_size)
666 iter->out_size = out_request;
667 iter->out = xrealloc (iter->out,
668 out_request * sizeof (gdb_wchar_t));
673 /* Incomplete input sequence. Let the caller know, and
674 arrange for future calls to see EOF. */
675 *out_result = wchar_iterate_incomplete;
682 perror_with_name ("Internal error while converting character sets");
686 /* We converted something. */
687 num = out_request - out_avail / sizeof (gdb_wchar_t);
688 *out_result = wchar_iterate_ok;
689 *out_chars = iter->out;
691 *len = orig_in - iter->bytes;
696 *out_result = wchar_iterate_eof;
701 /* The charset.c module initialization function. */
703 extern initialize_file_ftype _initialize_charset; /* -Wmissing-prototype */
705 typedef char *char_ptr;
706 DEF_VEC_P (char_ptr);
708 static VEC (char_ptr) *charsets;
713 find_charset_names (void)
715 VEC_safe_push (char_ptr, charsets, GDB_DEFAULT_HOST_CHARSET);
716 VEC_safe_push (char_ptr, charsets, NULL);
719 #else /* PHONY_ICONV */
721 /* Sometimes, libiconv redefines iconvlist as libiconvlist -- but
722 provides different symbols in the static and dynamic libraries.
723 So, configure may see libiconvlist but not iconvlist. But, calling
724 iconvlist is the right thing to do and will work. Hence we do a
725 check here but unconditionally call iconvlist below. */
726 #if defined (HAVE_ICONVLIST) || defined (HAVE_LIBICONVLIST)
728 /* A helper function that adds some character sets to the vector of
729 all character sets. This is a callback function for iconvlist. */
732 add_one (unsigned int count, const char *const *names, void *data)
736 for (i = 0; i < count; ++i)
737 VEC_safe_push (char_ptr, charsets, xstrdup (names[i]));
743 find_charset_names (void)
745 iconvlist (add_one, NULL);
746 VEC_safe_push (char_ptr, charsets, NULL);
751 /* Return non-zero if LINE (output from iconv) should be ignored.
752 Older iconv programs (e.g. 2.2.2) include the human readable
753 introduction even when stdout is not a tty. Newer versions omit
754 the intro if stdout is not a tty. */
757 ignore_line_p (const char *line)
759 /* This table is used to filter the output. If this text appears
760 anywhere in the line, it is ignored (strstr is used). */
761 static const char * const ignore_lines[] =
766 "listed with several",
771 for (i = 0; ignore_lines[i] != NULL; ++i)
773 if (strstr (line, ignore_lines[i]) != NULL)
781 find_charset_names (void)
783 struct pex_obj *child;
787 struct gdb_environ *iconv_env;
789 /* Older iconvs, e.g. 2.2.2, don't omit the intro text if stdout is not
790 a tty. We need to recognize it and ignore it. This text is subject
791 to translation, so force LANGUAGE=C. */
792 iconv_env = make_environ ();
793 init_environ (iconv_env);
794 set_in_environ (iconv_env, "LANGUAGE", "C");
795 set_in_environ (iconv_env, "LC_ALL", "C");
797 child = pex_init (0, "iconv", NULL);
802 /* Note that we simply ignore errors here. */
803 if (!pex_run_in_environment (child, PEX_SEARCH | PEX_STDERR_TO_STDOUT,
804 "iconv", args, environ_vector (iconv_env),
807 FILE *in = pex_read_output (child, 0);
809 /* POSIX says that iconv -l uses an unspecified format. We
810 parse the glibc and libiconv formats; feel free to add others
815 /* The size of buf is chosen arbitrarily. */
820 r = fgets (buf, sizeof (buf), in);
826 if (ignore_line_p (r))
829 /* Strip off the newline. */
831 /* Strip off one or two '/'s. glibc will print lines like
832 "8859_7//", but also "10646-1:1993/UCS4/". */
833 if (buf[len - 1] == '/')
835 if (buf[len - 1] == '/')
839 /* libiconv will print multiple entries per line, separated
840 by spaces. Older iconvs will print multiple entries per line,
841 indented by two spaces, and separated by ", "
842 (i.e. the human readable form). */
849 /* Skip leading blanks. */
850 for (p = start; *p && *p == ' '; ++p)
853 /* Find the next space, comma, or end-of-line. */
854 for ( ; *p && *p != ' ' && *p != ','; ++p)
856 /* Ignore an empty result. */
861 VEC_safe_push (char_ptr, charsets, xstrdup (start));
864 /* Skip any extra spaces. */
865 for (start = p + 1; *start && *start == ' '; ++start)
870 if (pex_get_status (child, 1, &status)
871 && WIFEXITED (status) && !WEXITSTATUS (status))
877 free_environ (iconv_env);
881 /* Some error occurred, so drop the vector. */
884 for (ix = 0; VEC_iterate (char_ptr, charsets, ix, elt); ++ix)
886 VEC_truncate (char_ptr, charsets, 0);
889 VEC_safe_push (char_ptr, charsets, NULL);
892 #endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
893 #endif /* PHONY_ICONV */
895 /* The "auto" target charset used by default_auto_charset. */
896 static const char *auto_target_charset_name = GDB_DEFAULT_TARGET_CHARSET;
899 default_auto_charset (void)
901 return auto_target_charset_name;
905 default_auto_wide_charset (void)
907 return GDB_DEFAULT_TARGET_WIDE_CHARSET;
911 _initialize_charset (void)
913 struct cmd_list_element *new_cmd;
915 /* The first element is always "auto". */
916 VEC_safe_push (char_ptr, charsets, xstrdup ("auto"));
917 find_charset_names ();
919 if (VEC_length (char_ptr, charsets) > 1)
920 charset_enum = (const char **) VEC_address (char_ptr, charsets);
922 charset_enum = default_charset_names;
925 #ifdef HAVE_LANGINFO_CODESET
926 /* The result of nl_langinfo may be overwritten later. This may
927 leak a little memory, if the user later changes the host charset,
928 but that doesn't matter much. */
929 auto_host_charset_name = xstrdup (nl_langinfo (CODESET));
930 /* Solaris will return `646' here -- but the Solaris iconv then
931 does not accept this. Darwin (and maybe FreeBSD) may return "" here,
932 which GNU libiconv doesn't like (infinite loop). */
933 if (!strcmp (auto_host_charset_name, "646") || !*auto_host_charset_name)
934 auto_host_charset_name = "ASCII";
935 auto_target_charset_name = auto_host_charset_name;
936 #elif defined (USE_WIN32API)
938 static char w32_host_default_charset[16]; /* "CP" + x<=5 digits + paranoia. */
940 snprintf (w32_host_default_charset, sizeof w32_host_default_charset,
942 auto_host_charset_name = w32_host_default_charset;
943 auto_target_charset_name = auto_host_charset_name;
948 add_setshow_enum_cmd ("charset", class_support,
949 charset_enum, &host_charset_name, _("\
950 Set the host and target character sets."), _("\
951 Show the host and target character sets."), _("\
952 The `host character set' is the one used by the system GDB is running on.\n\
953 The `target character set' is the one used by the program being debugged.\n\
954 You may only use supersets of ASCII for your host character set; GDB does\n\
955 not support any others.\n\
956 To see a list of the character sets GDB supports, type `set charset <TAB>'."),
957 /* Note that the sfunc below needs to set
958 target_charset_name, because the 'set
959 charset' command sets two variables. */
962 &setlist, &showlist);
964 add_setshow_enum_cmd ("host-charset", class_support,
965 charset_enum, &host_charset_name, _("\
966 Set the host character set."), _("\
967 Show the host character set."), _("\
968 The `host character set' is the one used by the system GDB is running on.\n\
969 You may only use supersets of ASCII for your host character set; GDB does\n\
970 not support any others.\n\
971 To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
972 set_host_charset_sfunc,
973 show_host_charset_name,
974 &setlist, &showlist);
976 add_setshow_enum_cmd ("target-charset", class_support,
977 charset_enum, &target_charset_name, _("\
978 Set the target character set."), _("\
979 Show the target character set."), _("\
980 The `target character set' is the one used by the program being debugged.\n\
981 GDB translates characters and strings between the host and target\n\
982 character sets as needed.\n\
983 To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
984 set_target_charset_sfunc,
985 show_target_charset_name,
986 &setlist, &showlist);
988 add_setshow_enum_cmd ("target-wide-charset", class_support,
989 charset_enum, &target_wide_charset_name,
991 Set the target wide character set."), _("\
992 Show the target wide character set."), _("\
993 The `target wide character set' is the one used by the program being debugged.\n\
994 In particular it is the encoding used by `wchar_t'.\n\
995 GDB translates characters and strings between the host and target\n\
996 character sets as needed.\n\
997 To see a list of the character sets GDB supports, type\n\
998 `set target-wide-charset'<TAB>"),
999 set_target_wide_charset_sfunc,
1000 show_target_wide_charset_name,
1001 &setlist, &showlist);