GDB (xrefs)
Loading...
Searching...
No Matches
charset.c
Go to the documentation of this file.
1/* Character set conversion support for GDB.
2
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "charset.h"
22#include "gdbcmd.h"
23#include "gdbsupport/gdb_obstack.h"
24#include "gdbsupport/gdb_wait.h"
25#include "charset-list.h"
26#include "gdbsupport/environ.h"
27#include "arch-utils.h"
28#include "gdbsupport/gdb_vecs.h"
29#include <ctype.h>
30
31#ifdef USE_WIN32API
32#include <windows.h>
33#endif
34
35/* How GDB's character set support works
36
37 GDB has three global settings:
38
39 - The `current host character set' is the character set GDB should
40 use in talking to the user, and which (hopefully) the user's
41 terminal knows how to display properly. Most users should not
42 change this.
43
44 - The `current target character set' is the character set the
45 program being debugged uses.
46
47 - The `current target wide character set' is the wide character set
48 the program being debugged uses, that is, the encoding used for
49 wchar_t.
50
51 There are commands to set each of these, and mechanisms for
52 choosing reasonable default values. GDB has a global list of
53 character sets that it can use as its host or target character
54 sets.
55
56 The header file `charset.h' declares various functions that
57 different pieces of GDB need to perform tasks like:
58
59 - printing target strings and characters to the user's terminal
60 (mostly target->host conversions),
61
62 - building target-appropriate representations of strings and
63 characters the user enters in expressions (mostly host->target
64 conversions),
65
66 and so on.
67
68 To avoid excessive code duplication and maintenance efforts,
69 GDB simply requires a capable iconv function. Users on platforms
70 without a suitable iconv can use the GNU iconv library. */
71
72
73#ifdef PHONY_ICONV
74
75/* Provide a phony iconv that does as little as possible. Also,
76 arrange for there to be a single available character set. */
77
78#undef GDB_DEFAULT_HOST_CHARSET
79#ifdef USE_WIN32API
80# define GDB_DEFAULT_HOST_CHARSET "CP1252"
81#else
82# define GDB_DEFAULT_HOST_CHARSET "ISO-8859-1"
83#endif
84#define GDB_DEFAULT_TARGET_CHARSET GDB_DEFAULT_HOST_CHARSET
85#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
86#undef DEFAULT_CHARSET_NAMES
87#define DEFAULT_CHARSET_NAMES GDB_DEFAULT_HOST_CHARSET ,
88
89#undef iconv_t
90#define iconv_t int
91#undef iconv_open
92#define iconv_open phony_iconv_open
93#undef iconv
94#define iconv phony_iconv
95#undef iconv_close
96#define iconv_close phony_iconv_close
97
98#undef ICONV_CONST
99#define ICONV_CONST const
100
101/* We allow conversions from UTF-32, wchar_t, and the host charset.
102 We allow conversions to wchar_t and the host charset.
103 Return 1 if we are converting from UTF-32BE, 2 if from UTF32-LE,
104 0 otherwise. This is used as a flag in calls to iconv. */
105
106static iconv_t
107phony_iconv_open (const char *to, const char *from)
108{
109 if (strcmp (to, "wchar_t") && strcmp (to, GDB_DEFAULT_HOST_CHARSET))
110 return -1;
111
112 if (!strcmp (from, "UTF-32BE") || !strcmp (from, "UTF-32"))
113 return 1;
114
115 if (!strcmp (from, "UTF-32LE"))
116 return 2;
117
118 if (strcmp (from, "wchar_t") && strcmp (from, GDB_DEFAULT_HOST_CHARSET))
119 return -1;
120
121 return 0;
122}
123
124static int
126{
127 return 0;
128}
129
130static size_t
131phony_iconv (iconv_t utf_flag, const char **inbuf, size_t *inbytesleft,
132 char **outbuf, size_t *outbytesleft)
133{
134 if (utf_flag)
135 {
136 enum bfd_endian endian
137 = utf_flag == 1 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
138 while (*inbytesleft >= 4)
139 {
140 unsigned long c
141 = extract_unsigned_integer ((const gdb_byte *)*inbuf, 4, endian);
142
143 if (c >= 256)
144 {
145 errno = EILSEQ;
146 return -1;
147 }
148 if (*outbytesleft < 1)
149 {
150 errno = E2BIG;
151 return -1;
152 }
153 **outbuf = c & 0xff;
154 ++*outbuf;
155 --*outbytesleft;
156
157 *inbuf += 4;
158 *inbytesleft -= 4;
159 }
160 if (*inbytesleft)
161 {
162 /* Partial sequence on input. */
163 errno = EINVAL;
164 return -1;
165 }
166 }
167 else
168 {
169 /* In all other cases we simply copy input bytes to the
170 output. */
171 size_t amt = *inbytesleft;
172
173 if (amt > *outbytesleft)
174 amt = *outbytesleft;
175 memcpy (*outbuf, *inbuf, amt);
176 *inbuf += amt;
177 *outbuf += amt;
178 *inbytesleft -= amt;
179 *outbytesleft -= amt;
180 if (*inbytesleft)
181 {
182 errno = E2BIG;
183 return -1;
184 }
185 }
186
187 /* The number of non-reversible conversions -- but they were all
188 reversible. */
189 return 0;
190}
191
192#else /* PHONY_ICONV */
193
194/* On systems that don't have EILSEQ, GNU iconv's iconv.h defines it
195 to ENOENT, while gnulib defines it to a different value. Always
196 map ENOENT to gnulib's EILSEQ, leaving callers agnostic. */
197
198static size_t
199gdb_iconv (iconv_t utf_flag, ICONV_CONST char **inbuf, size_t *inbytesleft,
200 char **outbuf, size_t *outbytesleft)
201{
202 size_t ret;
203
204 ret = iconv (utf_flag, inbuf, inbytesleft, outbuf, outbytesleft);
205 if (errno == ENOENT)
206 errno = EILSEQ;
207 return ret;
208}
209
210#undef iconv
211#define iconv gdb_iconv
212
213#endif /* PHONY_ICONV */
214
215
216/* The global lists of character sets and translations. */
217
218
219#ifndef GDB_DEFAULT_TARGET_CHARSET
220#define GDB_DEFAULT_TARGET_CHARSET "ISO-8859-1"
221#endif
222
223#ifndef GDB_DEFAULT_TARGET_WIDE_CHARSET
224#define GDB_DEFAULT_TARGET_WIDE_CHARSET "UTF-32"
225#endif
226
228static const char *host_charset_name = "auto";
229static void
230show_host_charset_name (struct ui_file *file, int from_tty,
231 struct cmd_list_element *c,
232 const char *value)
233{
234 if (!strcmp (value, "auto"))
235 gdb_printf (file,
236 _("The host character set is \"auto; currently %s\".\n"),
238 else
239 gdb_printf (file, _("The host character set is \"%s\".\n"), value);
240}
241
242static const char *target_charset_name = "auto";
243static void
244show_target_charset_name (struct ui_file *file, int from_tty,
245 struct cmd_list_element *c, const char *value)
246{
247 if (!strcmp (value, "auto"))
248 gdb_printf (file,
249 _("The target character set is \"auto; "
250 "currently %s\".\n"),
252 else
253 gdb_printf (file, _("The target character set is \"%s\".\n"),
254 value);
255}
256
257static const char *target_wide_charset_name = "auto";
258static void
260 int from_tty,
261 struct cmd_list_element *c,
262 const char *value)
263{
264 if (!strcmp (value, "auto"))
265 gdb_printf (file,
266 _("The target wide character set is \"auto; "
267 "currently %s\".\n"),
269 else
270 gdb_printf (file, _("The target wide character set is \"%s\".\n"),
271 value);
272}
273
274static const char * const default_charset_names[] =
275{
277 0
278};
279
280static const char * const *charset_enum;
281
282
283/* If the target wide character set has big- or little-endian
284 variants, these are the corresponding names. */
287
288/* The architecture for which the BE- and LE-names are valid. */
289static struct gdbarch *be_le_arch;
290
291/* A helper function which sets the target wide big- and little-endian
292 character set names, if possible. */
293
294static void
296{
297 if (be_le_arch == gdbarch)
298 return;
300
301#ifdef PHONY_ICONV
302 /* Match the wide charset names recognized by phony_iconv_open. */
303 target_wide_charset_le_name = "UTF-32LE";
304 target_wide_charset_be_name = "UTF-32BE";
305#else
306 int i, len;
307 const char *target_wide;
308
311
312 target_wide = target_wide_charset_name;
313 if (!strcmp (target_wide, "auto"))
314 target_wide = gdbarch_auto_wide_charset (gdbarch);
315
316 len = strlen (target_wide);
317 for (i = 0; charset_enum[i]; ++i)
318 {
319 if (strncmp (target_wide, charset_enum[i], len))
320 continue;
321 if ((charset_enum[i][len] == 'B'
322 || charset_enum[i][len] == 'L')
323 && charset_enum[i][len + 1] == 'E'
324 && charset_enum[i][len + 2] == '\0')
325 {
326 if (charset_enum[i][len] == 'B')
328 else
330 }
331 }
332# endif /* PHONY_ICONV */
333}
334
335/* 'Set charset', 'set host-charset', 'set target-charset', 'set
336 target-wide-charset', 'set charset' sfunc's. */
337
338static void
340{
341 iconv_t desc;
342 const char *host_cset = host_charset ();
343 const char *target_cset = target_charset (gdbarch);
344 const char *target_wide_cset = target_wide_charset_name;
345
346 if (!strcmp (target_wide_cset, "auto"))
347 target_wide_cset = gdbarch_auto_wide_charset (gdbarch);
348
349 desc = iconv_open (target_wide_cset, host_cset);
350 if (desc == (iconv_t) -1)
351 error (_("Cannot convert between character sets `%s' and `%s'"),
352 target_wide_cset, host_cset);
353 iconv_close (desc);
354
355 desc = iconv_open (target_cset, host_cset);
356 if (desc == (iconv_t) -1)
357 error (_("Cannot convert between character sets `%s' and `%s'"),
358 target_cset, host_cset);
359 iconv_close (desc);
360
361 /* Clear the cache. */
362 be_le_arch = NULL;
363}
364
365/* This is the sfunc for the 'set charset' command. */
366static void
367set_charset_sfunc (const char *charset, int from_tty,
368 struct cmd_list_element *c)
369{
370 /* CAREFUL: set the target charset here as well. */
373}
374
375/* 'set host-charset' command sfunc. We need a wrapper here because
376 the function needs to have a specific signature. */
377static void
378set_host_charset_sfunc (const char *charset, int from_tty,
379 struct cmd_list_element *c)
380{
382}
383
384/* Wrapper for the 'set target-charset' command. */
385static void
386set_target_charset_sfunc (const char *charset, int from_tty,
387 struct cmd_list_element *c)
388{
390}
391
392/* Wrapper for the 'set target-wide-charset' command. */
393static void
394set_target_wide_charset_sfunc (const char *charset, int from_tty,
395 struct cmd_list_element *c)
396{
398}
399
400/* sfunc for the 'show charset' command. */
401static void
402show_charset (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *name)
405{
406 show_host_charset_name (file, from_tty, c, host_charset_name);
408 show_target_wide_charset_name (file, from_tty, c,
410}
411
412
413/* Accessor functions. */
414
415const char *
417{
418 if (!strcmp (host_charset_name, "auto"))
420 return host_charset_name;
421}
422
423const char *
425{
426 if (!strcmp (target_charset_name, "auto"))
428 return target_charset_name;
429}
430
431const char *
433{
434 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
435
437 if (byte_order == BFD_ENDIAN_BIG)
438 {
441 }
442 else
443 {
446 }
447
448 if (!strcmp (target_wide_charset_name, "auto"))
450
452}
453
454
455/* Host character set management. For the time being, we assume that
456 the host character set is some superset of ASCII. */
457
458char
460{
461 if (c == '?')
462 return 0177;
463 return c & 0237;
464}
465
466
467/* Public character management functions. */
468
470{
471public:
472
473 iconv_wrapper (const char *to, const char *from)
474 {
475 m_desc = iconv_open (to, from);
476 if (m_desc == (iconv_t) -1)
477 perror_with_name (_("Converting character sets"));
478 }
479
481 {
483 }
484
485 size_t convert (ICONV_CONST char **inp, size_t *inleft, char **outp,
486 size_t *outleft)
487 {
488 return iconv (m_desc, inp, inleft, outp, outleft);
489 }
490
491private:
492
494};
495
496void
497convert_between_encodings (const char *from, const char *to,
498 const gdb_byte *bytes, unsigned int num_bytes,
499 int width, struct obstack *output,
500 enum transliterations translit)
501{
502 size_t inleft;
503 ICONV_CONST char *inp;
504 unsigned int space_request;
505
506 /* Often, the host and target charsets will be the same. */
507 if (!strcmp (from, to))
508 {
509 obstack_grow (output, bytes, num_bytes);
510 return;
511 }
512
513 iconv_wrapper desc (to, from);
514
515 inleft = num_bytes;
516 inp = (ICONV_CONST char *) bytes;
517
518 space_request = num_bytes;
519
520 while (inleft > 0)
521 {
522 char *outp;
523 size_t outleft, r;
524 int old_size;
525
526 old_size = obstack_object_size (output);
527 obstack_blank (output, space_request);
528
529 outp = (char *) obstack_base (output) + old_size;
530 outleft = space_request;
531
532 r = desc.convert (&inp, &inleft, &outp, &outleft);
533
534 /* Now make sure that the object on the obstack only includes
535 bytes we have converted. */
536 obstack_blank_fast (output, -(ssize_t) outleft);
537
538 if (r == (size_t) -1)
539 {
540 switch (errno)
541 {
542 case EILSEQ:
543 {
544 int i;
545
546 /* Invalid input sequence. */
547 if (translit == translit_none)
548 error (_("Could not convert character "
549 "to `%s' character set"), to);
550
551 /* We emit escape sequence for the bytes, skip them,
552 and try again. */
553 for (i = 0; i < width; ++i)
554 {
555 char octal[5];
556
557 xsnprintf (octal, sizeof (octal), "\\%.3o", *inp & 0xff);
558 obstack_grow_str (output, octal);
559
560 ++inp;
561 --inleft;
562 }
563 }
564 break;
565
566 case E2BIG:
567 /* We ran out of space in the output buffer. Make it
568 bigger next time around. */
569 space_request *= 2;
570 break;
571
572 case EINVAL:
573 /* Incomplete input sequence. FIXME: ought to report this
574 to the caller somehow. */
575 inleft = 0;
576 break;
577
578 default:
579 perror_with_name (_("Internal error while "
580 "converting character sets"));
581 }
582 }
583 }
584}
585
586
587
588/* Create a new iterator. */
589wchar_iterator::wchar_iterator (const gdb_byte *input, size_t bytes,
590 const char *charset, size_t width)
591: m_input (input),
592 m_bytes (bytes),
593 m_width (width),
594 m_out (1)
595{
597 if (m_desc == (iconv_t) -1)
598 perror_with_name (_("Converting character sets"));
599}
600
606
607int
609 gdb_wchar_t **out_chars,
610 const gdb_byte **ptr,
611 size_t *len)
612{
613 size_t out_request;
614
615 /* Try to convert some characters. At first we try to convert just
616 a single character. The reason for this is that iconv does not
617 necessarily update its outgoing arguments when it encounters an
618 invalid input sequence -- but we want to reliably report this to
619 our caller so it can emit an escape sequence. */
620 out_request = 1;
621 while (m_bytes > 0)
622 {
623 ICONV_CONST char *inptr = (ICONV_CONST char *) m_input;
624 char *outptr = (char *) m_out.data ();
625 const gdb_byte *orig_inptr = m_input;
626 size_t orig_in = m_bytes;
627 size_t out_avail = out_request * sizeof (gdb_wchar_t);
628 size_t num;
629 size_t r = iconv (m_desc, &inptr, &m_bytes, &outptr, &out_avail);
630
631 m_input = (gdb_byte *) inptr;
632
633 if (r == (size_t) -1)
634 {
635 switch (errno)
636 {
637 case EILSEQ:
638 /* Invalid input sequence. We still might have
639 converted a character; if so, return it. */
640 if (out_avail < out_request * sizeof (gdb_wchar_t))
641 break;
642
643 /* Otherwise skip the first invalid character, and let
644 the caller know about it. */
645 *out_result = wchar_iterate_invalid;
646 *ptr = m_input;
647 *len = m_width;
648 m_input += m_width;
649 m_bytes -= m_width;
650 return 0;
651
652 case E2BIG:
653 /* We ran out of space. We still might have converted a
654 character; if so, return it. Otherwise, grow the
655 buffer and try again. */
656 if (out_avail < out_request * sizeof (gdb_wchar_t))
657 break;
658
659 ++out_request;
660 if (out_request > m_out.size ())
661 m_out.resize (out_request);
662 continue;
663
664 case EINVAL:
665 /* Incomplete input sequence. Let the caller know, and
666 arrange for future calls to see EOF. */
667 *out_result = wchar_iterate_incomplete;
668 *ptr = m_input;
669 *len = m_bytes;
670 m_bytes = 0;
671 return 0;
672
673 default:
674 perror_with_name (_("Internal error while "
675 "converting character sets"));
676 }
677 }
678
679 /* We converted something. */
680 num = out_request - out_avail / sizeof (gdb_wchar_t);
681 *out_result = wchar_iterate_ok;
682 *out_chars = m_out.data ();
683 *ptr = orig_inptr;
684 *len = orig_in - m_bytes;
685 return num;
686 }
687
688 /* Really done. */
689 *out_result = wchar_iterate_eof;
690 return -1;
691}
692
694{
696 {
697 /* Note that we do not call charset_vector::clear, which would also xfree
698 the elements. This destructor is only called after exit, at which point
699 those will be freed anyway on process exit, so not freeing them now is
700 not classified as a memory leak. OTOH, freeing them now might be
701 classified as a data race, because some worker thread might still be
702 accessing them. */
703 charsets.clear ();
704 }
705
706 void clear ()
707 {
708 for (char *c : charsets)
709 xfree (c);
710
711 charsets.clear ();
712 }
713
714 std::vector<char *> charsets;
715};
716
718
719#ifdef PHONY_ICONV
720
721static void
723{
724 charsets.charsets.push_back (xstrdup (GDB_DEFAULT_HOST_CHARSET));
725 charsets.charsets.push_back (NULL);
726}
727
728#else /* PHONY_ICONV */
729
730/* Sometimes, libiconv redefines iconvlist as libiconvlist -- but
731 provides different symbols in the static and dynamic libraries.
732 So, configure may see libiconvlist but not iconvlist. But, calling
733 iconvlist is the right thing to do and will work. Hence we do a
734 check here but unconditionally call iconvlist below. */
735#if defined (HAVE_ICONVLIST) || defined (HAVE_LIBICONVLIST)
736
737/* A helper function that adds some character sets to the vector of
738 all character sets. This is a callback function for iconvlist. */
739
740static int
741add_one (unsigned int count, const char *const *names, void *data)
742{
743 unsigned int i;
744
745 for (i = 0; i < count; ++i)
746 charsets.charsets.push_back (xstrdup (names[i]));
747
748 return 0;
749}
750
751static void
753{
754 iconvlist (add_one, NULL);
755
756 charsets.charsets.push_back (NULL);
757}
758
759#else
760
761/* Return non-zero if LINE (output from iconv) should be ignored.
762 Older iconv programs (e.g. 2.2.2) include the human readable
763 introduction even when stdout is not a tty. Newer versions omit
764 the intro if stdout is not a tty. */
765
766static int
767ignore_line_p (const char *line)
768{
769 /* This table is used to filter the output. If this text appears
770 anywhere in the line, it is ignored (strstr is used). */
771 static const char * const ignore_lines[] =
772 {
773 "The following",
774 "not necessarily",
775 "the FROM and TO",
776 "listed with several",
777 NULL
778 };
779 int i;
780
781 for (i = 0; ignore_lines[i] != NULL; ++i)
782 {
783 if (strstr (line, ignore_lines[i]) != NULL)
784 return 1;
785 }
786
787 return 0;
788}
789
790static void
792{
793 struct pex_obj *child;
794 const char *args[3];
795 int err, status;
796 int fail = 1;
797 int flags;
798 gdb_environ iconv_env = gdb_environ::from_host_environ ();
799 char *iconv_program;
800
801 /* Older iconvs, e.g. 2.2.2, don't omit the intro text if stdout is
802 not a tty. We need to recognize it and ignore it. This text is
803 subject to translation, so force LANGUAGE=C. */
804 iconv_env.set ("LANGUAGE", "C");
805 iconv_env.set ("LC_ALL", "C");
806
807 child = pex_init (PEX_USE_PIPES, "iconv", NULL);
808
809#ifdef ICONV_BIN
810 {
811 std::string iconv_dir = relocate_gdb_directory (ICONV_BIN,
812 ICONV_BIN_RELOCATABLE);
813 iconv_program
814 = concat (iconv_dir.c_str(), SLASH_STRING, "iconv", (char *) NULL);
815 }
816#else
817 iconv_program = xstrdup ("iconv");
818#endif
819 args[0] = iconv_program;
820 args[1] = "-l";
821 args[2] = NULL;
822 flags = PEX_STDERR_TO_STDOUT;
823#ifndef ICONV_BIN
824 flags |= PEX_SEARCH;
825#endif
826 /* Note that we simply ignore errors here. */
827 if (!pex_run_in_environment (child, flags,
828 args[0], const_cast<char **> (args),
829 iconv_env.envp (),
830 NULL, NULL, &err))
831 {
832 FILE *in = pex_read_output (child, 0);
833
834 /* POSIX says that iconv -l uses an unspecified format. We
835 parse the glibc and libiconv formats; feel free to add others
836 as needed. */
837
838 while (in != NULL && !feof (in))
839 {
840 /* The size of buf is chosen arbitrarily. */
841 char buf[1024];
842 char *start, *r;
843 int len;
844
845 r = fgets (buf, sizeof (buf), in);
846 if (!r)
847 break;
848 len = strlen (r);
849 if (len <= 3)
850 continue;
851 if (ignore_line_p (r))
852 continue;
853
854 /* Strip off the newline. */
855 --len;
856 /* Strip off one or two '/'s. glibc will print lines like
857 "8859_7//", but also "10646-1:1993/UCS4/". */
858 if (buf[len - 1] == '/')
859 --len;
860 if (buf[len - 1] == '/')
861 --len;
862 buf[len] = '\0';
863
864 /* libiconv will print multiple entries per line, separated
865 by spaces. Older iconvs will print multiple entries per
866 line, indented by two spaces, and separated by ", "
867 (i.e. the human readable form). */
868 start = buf;
869 while (1)
870 {
871 int keep_going;
872 char *p;
873
874 /* Skip leading blanks. */
875 for (p = start; *p && *p == ' '; ++p)
876 ;
877 start = p;
878 /* Find the next space, comma, or end-of-line. */
879 for ( ; *p && *p != ' ' && *p != ','; ++p)
880 ;
881 /* Ignore an empty result. */
882 if (p == start)
883 break;
884 keep_going = *p;
885 *p = '\0';
886 charsets.charsets.push_back (xstrdup (start));
887 if (!keep_going)
888 break;
889 /* Skip any extra spaces. */
890 for (start = p + 1; *start && *start == ' '; ++start)
891 ;
892 }
893 }
894
895 if (pex_get_status (child, 1, &status)
896 && WIFEXITED (status) && !WEXITSTATUS (status))
897 fail = 0;
898
899 }
900
901 xfree (iconv_program);
902 pex_free (child);
903
904 if (fail)
905 {
906 /* Some error occurred, so drop the vector. */
907 charsets.clear ();
908 }
909 else
910 charsets.charsets.push_back (NULL);
911}
912
913#endif /* HAVE_ICONVLIST || HAVE_LIBICONVLIST */
914#endif /* PHONY_ICONV */
915
916/* The "auto" target charset used by default_auto_charset. */
918
919const char *
924
925const char *
930
931
932#ifdef USE_INTERMEDIATE_ENCODING_FUNCTION
933/* Macro used for UTF or UCS endianness suffix. */
934#if WORDS_BIGENDIAN
935#define ENDIAN_SUFFIX "BE"
936#else
937#define ENDIAN_SUFFIX "LE"
938#endif
939
940/* GDB cannot handle strings correctly if this size is different. */
941
942gdb_static_assert (sizeof (gdb_wchar_t) == 2 || sizeof (gdb_wchar_t) == 4);
943
944/* intermediate_encoding returns the charset used internally by
945 GDB to convert between target and host encodings. As the test above
946 compiled, sizeof (gdb_wchar_t) is either 2 or 4 bytes.
947 UTF-16/32 is tested first, UCS-2/4 is tested as a second option,
948 otherwise an error is generated. */
949
950const char *
951intermediate_encoding (void)
952{
953 iconv_t desc;
954 static const char *stored_result = NULL;
955 gdb::unique_xmalloc_ptr<char> result;
956
957 if (stored_result)
958 return stored_result;
959 result = xstrprintf ("UTF-%d%s", (int) (sizeof (gdb_wchar_t) * 8),
960 ENDIAN_SUFFIX);
961 /* Check that the name is supported by iconv_open. */
962 desc = iconv_open (result.get (), host_charset ());
963 if (desc != (iconv_t) -1)
964 {
965 iconv_close (desc);
966 stored_result = result.release ();
967 return stored_result;
968 }
969 /* Second try, with UCS-2 type. */
970 result = xstrprintf ("UCS-%d%s", (int) sizeof (gdb_wchar_t),
971 ENDIAN_SUFFIX);
972 /* Check that the name is supported by iconv_open. */
973 desc = iconv_open (result.get (), host_charset ());
974 if (desc != (iconv_t) -1)
975 {
976 iconv_close (desc);
977 stored_result = result.release ();
978 return stored_result;
979 }
980 /* No valid charset found, generate error here. */
981 error (_("Unable to find a valid charset for string conversions"));
982}
983
984#endif /* USE_INTERMEDIATE_ENCODING_FUNCTION */
985
986void _initialize_charset ();
987void
989{
990 /* The first element is always "auto". */
991 charsets.charsets.push_back (xstrdup ("auto"));
993
994 if (charsets.charsets.size () > 1)
995 charset_enum = (const char * const *) charsets.charsets.data ();
996 else
998
999#ifndef PHONY_ICONV
1000#ifdef HAVE_LANGINFO_CODESET
1001 /* The result of nl_langinfo may be overwritten later. This may
1002 leak a little memory, if the user later changes the host charset,
1003 but that doesn't matter much. */
1005 /* Solaris will return `646' here -- but the Solaris iconv then does
1006 not accept this. Darwin (and maybe FreeBSD) may return "" here,
1007 which GNU libiconv doesn't like (infinite loop). */
1008 if (!strcmp (auto_host_charset_name, "646") || !*auto_host_charset_name)
1009 auto_host_charset_name = "ASCII";
1011#elif defined (USE_WIN32API)
1012 {
1013 /* "CP" + x<=5 digits + paranoia. */
1014 static char w32_host_default_charset[16];
1015
1016 snprintf (w32_host_default_charset, sizeof w32_host_default_charset,
1017 "CP%d", GetACP());
1018 auto_host_charset_name = w32_host_default_charset;
1020 }
1021#endif
1022#endif
1023
1024 /* Recall that the first element is always "auto". */
1026 gdb_assert (strcmp (host_charset_name, "auto") == 0);
1029Set the host and target character sets."), _("\
1030Show the host and target character sets."), _("\
1031The `host character set' is the one used by the system GDB is running on.\n\
1032The `target character set' is the one used by the program being debugged.\n\
1033You may only use supersets of ASCII for your host character set; GDB does\n\
1034not support any others.\n\
1035To see a list of the character sets GDB supports, type `set charset <TAB>'."),
1036 /* Note that the sfunc below needs to set
1037 target_charset_name, because the 'set
1038 charset' command sets two variables. */
1041 &setlist, &showlist);
1042
1043 add_setshow_enum_cmd ("host-charset", class_support,
1045Set the host character set."), _("\
1046Show the host character set."), _("\
1047The `host character set' is the one used by the system GDB is running on.\n\
1048You may only use supersets of ASCII for your host character set; GDB does\n\
1049not support any others.\n\
1050To see a list of the character sets GDB supports, type `set host-charset <TAB>'."),
1053 &setlist, &showlist);
1054
1055 /* Recall that the first element is always "auto". */
1057 gdb_assert (strcmp (target_charset_name, "auto") == 0);
1058 add_setshow_enum_cmd ("target-charset", class_support,
1060Set the target character set."), _("\
1061Show the target character set."), _("\
1062The `target character set' is the one used by the program being debugged.\n\
1063GDB translates characters and strings between the host and target\n\
1064character sets as needed.\n\
1065To see a list of the character sets GDB supports, type `set target-charset'<TAB>"),
1068 &setlist, &showlist);
1069
1070 /* Recall that the first element is always "auto". */
1072 gdb_assert (strcmp (target_wide_charset_name, "auto") == 0);
1073 add_setshow_enum_cmd ("target-wide-charset", class_support,
1075 _("\
1076Set the target wide character set."), _("\
1077Show the target wide character set."), _("\
1078The `target wide character set' is the one used by the program being debugged.\
1079\nIn particular it is the encoding used by `wchar_t'.\n\
1080GDB translates characters and strings between the host and target\n\
1081character sets as needed.\n\
1082To see a list of the character sets GDB supports, type\n\
1083`set target-wide-charset'<TAB>"),
1086 &setlist, &showlist);
1087}
const char *const name
void xfree(void *)
gdb_static_assert(sizeof(splay_tree_key) >=sizeof(CORE_ADDR *))
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
#define GDB_DEFAULT_TARGET_WIDE_CHARSET
Definition charset.c:85
#define iconv_close
Definition charset.c:96
static const char * auto_host_charset_name
Definition charset.c:227
static const char * auto_target_charset_name
Definition charset.c:917
static const char * target_wide_charset_le_name
Definition charset.c:286
static size_t phony_iconv(iconv_t utf_flag, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition charset.c:131
#define GDB_DEFAULT_TARGET_CHARSET
Definition charset.c:84
#define iconv_t
Definition charset.c:90
const char * default_auto_charset(void)
Definition charset.c:920
void _initialize_charset()
Definition charset.c:988
static const char * target_wide_charset_name
Definition charset.c:257
const char * target_wide_charset(struct gdbarch *gdbarch)
Definition charset.c:432
#define iconv
Definition charset.c:94
static void set_target_charset_sfunc(const char *charset, int from_tty, struct cmd_list_element *c)
Definition charset.c:386
static const char * target_charset_name
Definition charset.c:242
static void show_host_charset_name(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition charset.c:230
#define ICONV_CONST
Definition charset.c:99
const char * host_charset(void)
Definition charset.c:416
static struct gdbarch * be_le_arch
Definition charset.c:289
static void find_charset_names(void)
Definition charset.c:722
static void show_target_charset_name(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition charset.c:244
static int phony_iconv_close(iconv_t arg)
Definition charset.c:125
static void validate(struct gdbarch *gdbarch)
Definition charset.c:339
static void set_be_le_names(struct gdbarch *gdbarch)
Definition charset.c:295
static void set_charset_sfunc(const char *charset, int from_tty, struct cmd_list_element *c)
Definition charset.c:367
#define DEFAULT_CHARSET_NAMES
Definition charset.c:87
static void show_target_wide_charset_name(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition charset.c:259
static iconv_t phony_iconv_open(const char *to, const char *from)
Definition charset.c:107
#define iconv_open
Definition charset.c:92
static void set_target_wide_charset_sfunc(const char *charset, int from_tty, struct cmd_list_element *c)
Definition charset.c:394
static const char *const * charset_enum
Definition charset.c:280
static charset_vector charsets
Definition charset.c:717
static const char *const default_charset_names[]
Definition charset.c:274
static void show_charset(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *name)
Definition charset.c:402
#define GDB_DEFAULT_HOST_CHARSET
Definition charset.c:82
const char * default_auto_wide_charset(void)
Definition charset.c:926
char host_letter_to_control_character(char c)
Definition charset.c:459
static const char * host_charset_name
Definition charset.c:228
static void set_host_charset_sfunc(const char *charset, int from_tty, struct cmd_list_element *c)
Definition charset.c:378
void convert_between_encodings(const char *from, const char *to, const gdb_byte *bytes, unsigned int num_bytes, int width, struct obstack *output, enum transliterations translit)
Definition charset.c:497
const char * target_charset(struct gdbarch *gdbarch)
Definition charset.c:424
static const char * target_wide_charset_be_name
Definition charset.c:285
transliterations
Definition charset.h:44
@ translit_none
Definition charset.h:46
wchar_iterate_result
Definition charset.h:75
@ wchar_iterate_eof
Definition charset.h:83
@ wchar_iterate_invalid
Definition charset.h:79
@ wchar_iterate_incomplete
Definition charset.h:81
@ wchar_iterate_ok
Definition charset.h:77
size_t convert(ICONV_CONST char **inp, size_t *inleft, char **outp, size_t *outleft)
Definition charset.c:485
iconv_wrapper(const char *to, const char *from)
Definition charset.c:473
iconv_t m_desc
Definition charset.c:493
wchar_iterator(const gdb_byte *input, size_t bytes, const char *charset, size_t width)
Definition charset.c:589
const gdb_byte * m_input
Definition charset.h:139
iconv_t m_desc
Definition charset.h:135
size_t m_width
Definition charset.h:144
int iterate(enum wchar_iterate_result *out_result, gdb_wchar_t **out_chars, const gdb_byte **ptr, size_t *len)
Definition charset.c:608
size_t m_bytes
Definition charset.h:141
gdb::def_vector< gdb_wchar_t > m_out
Definition charset.h:147
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * setlist
Definition cli-cmds.c:119
set_show_commands add_setshow_enum_cmd(const char *name, enum command_class theclass, const char *const *enumlist, const char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:688
@ class_support
Definition command.h:58
std::string relocate_gdb_directory(const char *initial, bool relocatable)
Definition main.c:160
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
char gdb_wchar_t
Definition gdb_wchar.h:101
#define INTERMEDIATE_ENCODING
Definition gdb_wchar.h:117
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
const char * gdbarch_auto_charset(struct gdbarch *gdbarch)
Definition gdbarch.c:4939
const char * gdbarch_auto_wide_charset(struct gdbarch *gdbarch)
Definition gdbarch.c:4956
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition gnu-nat.c:1789
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
static void keep_going(struct execution_control_state *ecs)
Definition infrun.c:8568
char * nl_langinfo(nl_item)
Definition go32-nat.c:1003
#define CODESET
Definition langinfo.h:31
std::vector< char * > charsets
Definition charset.c:714
void clear()
Definition charset.c:706
Definition value.h:130
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886