Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

306 lines
13KB

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