hc
2023-11-06 15ade055295d13f95d49e3d99b09f3bbfb4a43e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
 
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
 
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual.  Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Character Sets</title>
 
<meta name="description" content="Debugging with GDB: Character Sets">
<meta name="keywords" content="Debugging with GDB: Character Sets">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Data.html#Data" rel="up" title="Data">
<link href="Caching-Target-Data.html#Caching-Target-Data" rel="next" title="Caching Target Data">
<link href="Core-File-Generation.html#Core-File-Generation" rel="previous" title="Core File Generation">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
 
 
</head>
 
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Character-Sets"></a>
<div class="header">
<p>
Next: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="n" rel="next">Caching Target Data</a>, Previous: <a href="Core-File-Generation.html#Core-File-Generation" accesskey="p" rel="previous">Core File Generation</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Character-Sets-1"></a>
<h3 class="section">10.20 Character Sets</h3>
<a name="index-character-sets"></a>
<a name="index-charset"></a>
<a name="index-translating-between-character-sets"></a>
<a name="index-host-character-set"></a>
<a name="index-target-character-set"></a>
 
<p>If the program you are debugging uses a different character set to
represent characters and strings than the one <small>GDB</small> uses itself,
<small>GDB</small> can automatically translate between the character sets for
you.  The character set <small>GDB</small> uses we call the <em>host
character set</em>; the one the inferior program uses we call the
<em>target character set</em>.
</p>
<p>For example, if you are running <small>GDB</small> on a <small>GNU</small>/Linux system, which
uses the ISO Latin 1 character set, but you are using <small>GDB</small>&rsquo;s
remote protocol (see <a href="Remote-Debugging.html#Remote-Debugging">Remote Debugging</a>) to debug a program
running on an IBM mainframe, which uses the <small>EBCDIC</small> character set,
then the host character set is Latin-1, and the target character set is
<small>EBCDIC</small>.  If you give <small>GDB</small> the command <code>set
target-charset EBCDIC-US</code>, then <small>GDB</small> translates between
<small>EBCDIC</small> and Latin 1 as you print character or string values, or use
character and string literals in expressions.
</p>
<p><small>GDB</small> has no way to automatically recognize which character set
the inferior program uses; you must tell it, using the <code>set
target-charset</code> command, described below.
</p>
<p>Here are the commands for controlling <small>GDB</small>&rsquo;s character set
support:
</p>
<dl compact="compact">
<dt><code>set target-charset <var>charset</var></code></dt>
<dd><a name="index-set-target_002dcharset"></a>
<p>Set the current target character set to <var>charset</var>.  To display the
list of supported target character sets, type
<kbd>set&nbsp;<span class="nolinebreak">target-charset</span>&nbsp;<span class="key">TAB</span><span class="key">TAB</span><!-- /@w --></kbd>.
</p>
</dd>
<dt><code>set host-charset <var>charset</var></code></dt>
<dd><a name="index-set-host_002dcharset"></a>
<p>Set the current host character set to <var>charset</var>.
</p>
<p>By default, <small>GDB</small> uses a host character set appropriate to the
system it is running on; you can override that default using the
<code>set host-charset</code> command.  On some systems, <small>GDB</small> cannot
automatically determine the appropriate host character set.  In this
case, <small>GDB</small> uses &lsquo;<samp>UTF-8</samp>&rsquo;.
</p>
<p><small>GDB</small> can only use certain character sets as its host character
set.  If you type <kbd>set&nbsp;<span class="nolinebreak">host-charset</span>&nbsp;<span class="key">TAB</span><span class="key">TAB</span><!-- /@w --></kbd>,
<small>GDB</small> will list the host character sets it supports.
</p>
</dd>
<dt><code>set charset <var>charset</var></code></dt>
<dd><a name="index-set-charset"></a>
<p>Set the current host and target character sets to <var>charset</var>.  As
above, if you type <kbd>set&nbsp;charset&nbsp;<span class="key">TAB</span><span class="key">TAB</span><!-- /@w --></kbd>,
<small>GDB</small> will list the names of the character sets that can be used
for both host and target.
</p>
</dd>
<dt><code>show charset</code></dt>
<dd><a name="index-show-charset"></a>
<p>Show the names of the current host and target character sets.
</p>
</dd>
<dt><code>show host-charset</code></dt>
<dd><a name="index-show-host_002dcharset"></a>
<p>Show the name of the current host character set.
</p>
</dd>
<dt><code>show target-charset</code></dt>
<dd><a name="index-show-target_002dcharset"></a>
<p>Show the name of the current target character set.
</p>
</dd>
<dt><code>set target-wide-charset <var>charset</var></code></dt>
<dd><a name="index-set-target_002dwide_002dcharset"></a>
<p>Set the current target&rsquo;s wide character set to <var>charset</var>.  This is
the character set used by the target&rsquo;s <code>wchar_t</code> type.  To
display the list of supported wide character sets, type
<kbd>set&nbsp;<span class="nolinebreak">target-wide-charset</span>&nbsp;<span class="key">TAB</span><span class="key">TAB</span><!-- /@w --></kbd>.
</p>
</dd>
<dt><code>show target-wide-charset</code></dt>
<dd><a name="index-show-target_002dwide_002dcharset"></a>
<p>Show the name of the current target&rsquo;s wide character set.
</p></dd>
</dl>
 
<p>Here is an example of <small>GDB</small>&rsquo;s character set support in action.
Assume that the following source code has been placed in the file
<samp>charset-test.c</samp>:
</p>
<div class="smallexample">
<pre class="smallexample">#include &lt;stdio.h&gt;
 
char ascii_hello[]
  = {72, 101, 108, 108, 111, 44, 32, 119,
     111, 114, 108, 100, 33, 10, 0};
char ibm1047_hello[]
  = {200, 133, 147, 147, 150, 107, 64, 166,
     150, 153, 147, 132, 90, 37, 0};
 
main ()
{
  printf (&quot;Hello, world!\n&quot;);
}
</pre></div>
 
<p>In this program, <code>ascii_hello</code> and <code>ibm1047_hello</code> are arrays
containing the string &lsquo;<samp>Hello, world!</samp>&rsquo; followed by a newline,
encoded in the <small>ASCII</small> and <small>IBM1047</small> character sets.
</p>
<p>We compile the program, and invoke the debugger on it:
</p>
<div class="smallexample">
<pre class="smallexample">$ gcc -g charset-test.c -o charset-test
$ gdb -nw charset-test
GNU gdb 2001-12-19-cvs
Copyright 2001 Free Software Foundation, Inc.
&hellip;
(gdb)
</pre></div>
 
<p>We can use the <code>show charset</code> command to see what character sets
<small>GDB</small> is currently using to interpret and display characters and
strings:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) show charset
The current host and target character set is `ISO-8859-1'.
(gdb)
</pre></div>
 
<p>For the sake of printing this manual, let&rsquo;s use <small>ASCII</small> as our
initial character set:
</p><div class="smallexample">
<pre class="smallexample">(gdb) set charset ASCII
(gdb) show charset
The current host and target character set is `ASCII'.
(gdb)
</pre></div>
 
<p>Let&rsquo;s assume that <small>ASCII</small> is indeed the correct character set for our
host system &mdash; in other words, let&rsquo;s assume that if <small>GDB</small> prints
characters using the <small>ASCII</small> character set, our terminal will display
them properly.  Since our current target character set is also
<small>ASCII</small>, the contents of <code>ascii_hello</code> print legibly:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print ascii_hello
$1 = 0x401698 &quot;Hello, world!\n&quot;
(gdb) print ascii_hello[0]
$2 = 72 'H'
(gdb)
</pre></div>
 
<p><small>GDB</small> uses the target character set for character and string
literals you use in expressions:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print '+'
$3 = 43 '+'
(gdb)
</pre></div>
 
<p>The <small>ASCII</small> character set uses the number 43 to encode the &lsquo;<samp>+</samp>&rsquo;
character.
</p>
<p><small>GDB</small> relies on the user to tell it which character set the
target program uses.  If we print <code>ibm1047_hello</code> while our target
character set is still <small>ASCII</small>, we get jibberish:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print ibm1047_hello
$4 = 0x4016a8 &quot;\310\205\223\223\226k@\246\226\231\223\204Z%&quot;
(gdb) print ibm1047_hello[0]
$5 = 200 '\310'
(gdb)
</pre></div>
 
<p>If we invoke the <code>set target-charset</code> followed by <tt class="key">TAB</tt><tt class="key">TAB</tt>,
<small>GDB</small> tells us the character sets it supports:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) set target-charset
ASCII       EBCDIC-US   IBM1047     ISO-8859-1
(gdb) set target-charset
</pre></div>
 
<p>We can select <small>IBM1047</small> as our target character set, and examine the
program&rsquo;s strings again.  Now the <small>ASCII</small> string is wrong, but
<small>GDB</small> translates the contents of <code>ibm1047_hello</code> from the
target character set, <small>IBM1047</small>, to the host character set,
<small>ASCII</small>, and they display correctly:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) set target-charset IBM1047
(gdb) show charset
The current host character set is `ASCII'.
The current target character set is `IBM1047'.
(gdb) print ascii_hello
$6 = 0x401698 &quot;\110\145%%?\054\040\167?\162%\144\041\012&quot;
(gdb) print ascii_hello[0]
$7 = 72 '\110'
(gdb) print ibm1047_hello
$8 = 0x4016a8 &quot;Hello, world!\n&quot;
(gdb) print ibm1047_hello[0]
$9 = 200 'H'
(gdb)
</pre></div>
 
<p>As above, <small>GDB</small> uses the target character set for character and
string literals you use in expressions:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) print '+'
$10 = 78 '+'
(gdb)
</pre></div>
 
<p>The <small>IBM1047</small> character set uses the number 78 to encode the &lsquo;<samp>+</samp>&rsquo;
character.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Caching-Target-Data.html#Caching-Target-Data" accesskey="n" rel="next">Caching Target Data</a>, Previous: <a href="Core-File-Generation.html#Core-File-Generation" accesskey="p" rel="previous">Core File Generation</a>, Up: <a href="Data.html#Data" accesskey="u" rel="up">Data</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
 
 
 
</body>
</html>