You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 line
21KB

  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>Compiling and Injecting Code (Debugging with GDB)</title>
  17. <meta name="description" content="Compiling and Injecting Code (Debugging with GDB)">
  18. <meta name="keywords" content="Compiling and Injecting Code (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="Altering.html#Altering" rel="up" title="Altering">
  26. <link href="GDB-Files.html#GDB-Files" rel="next" title="GDB Files">
  27. <link href="Patching.html#Patching" rel="prev" title="Patching">
  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="Compiling-and-Injecting-Code"></a>
  58. <div class="header">
  59. <p>
  60. Previous: <a href="Patching.html#Patching" accesskey="p" rel="prev">Patching</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</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="Compiling-and-injecting-code-in-GDB"></a>
  64. <h3 class="section">17.7 Compiling and injecting code in <small>GDB</small></h3>
  65. <a name="index-injecting-code"></a>
  66. <a name="index-writing-into-executables-1"></a>
  67. <a name="index-compiling-code"></a>
  68. <p><small>GDB</small> supports on-demand compilation and code injection into
  69. programs running under <small>GDB</small>. GCC 5.0 or higher built with
  70. <samp>libcc1.so</samp> must be installed for this functionality to be enabled.
  71. This functionality is implemented with the following commands.
  72. </p>
  73. <dl compact="compact">
  74. <dd><a name="index-compile-code"></a>
  75. </dd>
  76. <dt><code>compile code <var>source-code</var></code></dt>
  77. <dt><code>compile code -raw <var>--</var> <var>source-code</var></code></dt>
  78. <dd><p>Compile <var>source-code</var> with the compiler language found as the current
  79. language in <small>GDB</small> (see <a href="Languages.html#Languages">Languages</a>). If compilation and
  80. injection is not supported with the current language specified in
  81. <small>GDB</small>, or the compiler does not support this feature, an error
  82. message will be printed. If <var>source-code</var> compiles and links
  83. successfully, <small>GDB</small> will load the object-code emitted,
  84. and execute it within the context of the currently selected inferior.
  85. It is important to note that the compiled code is executed immediately.
  86. After execution, the compiled code is removed from <small>GDB</small> and any
  87. new types or variables you have defined will be deleted.
  88. </p>
  89. <p>The command allows you to specify <var>source-code</var> in two ways.
  90. The simplest method is to provide a single line of code to the command.
  91. E.g.:
  92. </p>
  93. <div class="smallexample">
  94. <pre class="smallexample">compile code printf (&quot;hello world\n&quot;);
  95. </pre></div>
  96. <p>If you specify options on the command line as well as source code, they
  97. may conflict. The &lsquo;<samp>--</samp>&rsquo; delimiter can be used to separate options
  98. from actual source code. E.g.:
  99. </p>
  100. <div class="smallexample">
  101. <pre class="smallexample">compile code -r -- printf (&quot;hello world\n&quot;);
  102. </pre></div>
  103. <p>Alternatively you can enter source code as multiple lines of text. To
  104. enter this mode, invoke the &lsquo;<samp>compile code</samp>&rsquo; command without any text
  105. following the command. This will start the multiple-line editor and
  106. allow you to type as many lines of source code as required. When you
  107. have completed typing, enter &lsquo;<samp>end</samp>&rsquo; on its own line to exit the
  108. editor.
  109. </p>
  110. <div class="smallexample">
  111. <pre class="smallexample">compile code
  112. &gt;printf (&quot;hello\n&quot;);
  113. &gt;printf (&quot;world\n&quot;);
  114. &gt;end
  115. </pre></div>
  116. <p>Specifying &lsquo;<samp>-raw</samp>&rsquo;, prohibits <small>GDB</small> from wrapping the
  117. provided <var>source-code</var> in a callable scope. In this case, you must
  118. specify the entry point of the code by defining a function named
  119. <code>_gdb_expr_</code>. The &lsquo;<samp>-raw</samp>&rsquo; code cannot access variables of the
  120. inferior. Using &lsquo;<samp>-raw</samp>&rsquo; option may be needed for example when
  121. <var>source-code</var> requires &lsquo;<samp>#include</samp>&rsquo; lines which may conflict with
  122. inferior symbols otherwise.
  123. </p>
  124. <a name="index-compile-file"></a>
  125. </dd>
  126. <dt><code>compile file <var>filename</var></code></dt>
  127. <dt><code>compile file -raw <var>filename</var></code></dt>
  128. <dd><p>Like <code>compile code</code>, but take the source code from <var>filename</var>.
  129. </p>
  130. <div class="smallexample">
  131. <pre class="smallexample">compile file /home/user/example.c
  132. </pre></div>
  133. </dd>
  134. </dl>
  135. <dl compact="compact">
  136. <dt><code>compile print [[<var>options</var>] --] <var>expr</var></code></dt>
  137. <dt><code>compile print [[<var>options</var>] --] /<var>f</var> <var>expr</var></code></dt>
  138. <dd><p>Compile and execute <var>expr</var> with the compiler language found as the
  139. current language in <small>GDB</small> (see <a href="Languages.html#Languages">Languages</a>). By default the
  140. value of <var>expr</var> is printed in a format appropriate to its data type;
  141. you can choose a different format by specifying &lsquo;<samp>/<var>f</var></samp>&rsquo;, where
  142. <var>f</var> is a letter specifying the format; see <a href="Output-Formats.html#Output-Formats">Output
  143. Formats</a>. The <code>compile print</code> command accepts the same options
  144. as the <code>print</code> command; see <a href="Data.html#print-options">print options</a>.
  145. </p>
  146. </dd>
  147. <dt><code>compile print [[<var>options</var>] --]</code></dt>
  148. <dt><code>compile print [[<var>options</var>] --] /<var>f</var></code></dt>
  149. <dd><a name="index-reprint-the-last-value-1"></a>
  150. <p>Alternatively you can enter the expression (source code producing it) as
  151. multiple lines of text. To enter this mode, invoke the &lsquo;<samp>compile print</samp>&rsquo;
  152. command without any text following the command. This will start the
  153. multiple-line editor.
  154. </p></dd>
  155. </dl>
  156. <p>The process of compiling and injecting the code can be inspected using:
  157. </p>
  158. <dl compact="compact">
  159. <dd><a name="set-debug-compile"></a></dd>
  160. <dt><code>set debug compile</code></dt>
  161. <dd><a name="index-compile-command-debugging-info"></a>
  162. <p>Turns on or off display of <small>GDB</small> process of compiling and
  163. injecting the code. The default is off.
  164. </p>
  165. </dd>
  166. <dt><code>show debug compile</code></dt>
  167. <dd><p>Displays the current state of displaying <small>GDB</small> process of
  168. compiling and injecting the code.
  169. </p>
  170. <a name="set-debug-compile_002dcplus_002dtypes"></a></dd>
  171. <dt><code>set debug compile-cplus-types</code></dt>
  172. <dd><a name="index-compile-C_002b_002b-type-conversion"></a>
  173. <p>Turns on or off the display of C<tt>++</tt> type conversion debugging information.
  174. The default is off.
  175. </p>
  176. </dd>
  177. <dt><code>show debug compile-cplus-types</code></dt>
  178. <dd><p>Displays the current state of displaying debugging information for
  179. C<tt>++</tt> type conversion.
  180. </p></dd>
  181. </dl>
  182. <a name="Compilation-options-for-the-compile-command"></a>
  183. <h4 class="subsection">17.7.1 Compilation options for the <code>compile</code> command</h4>
  184. <p><small>GDB</small> needs to specify the right compilation options for the code
  185. to be injected, in part to make its ABI compatible with the inferior
  186. and in part to make the injected code compatible with <small>GDB</small>&rsquo;s
  187. injecting process.
  188. </p>
  189. <p>The options used, in increasing precedence:
  190. </p>
  191. <dl compact="compact">
  192. <dt>target architecture and OS options (<code>gdbarch</code>)</dt>
  193. <dd><p>These options depend on target processor type and target operating
  194. system, usually they specify at least 32-bit (<code>-m32</code>) or 64-bit
  195. (<code>-m64</code>) compilation option.
  196. </p>
  197. </dd>
  198. <dt>compilation options recorded in the target</dt>
  199. <dd><p><small>GCC</small> (since version 4.7) stores the options used for compilation
  200. into <code>DW_AT_producer</code> part of DWARF debugging information according
  201. to the <small>GCC</small> option <code>-grecord-gcc-switches</code>. One has to
  202. explicitly specify <code>-g</code> during inferior compilation otherwise
  203. <small>GCC</small> produces no DWARF. This feature is only relevant for
  204. platforms where <code>-g</code> produces DWARF by default, otherwise one may
  205. try to enforce DWARF by using <code>-gdwarf-4</code>.
  206. </p>
  207. </dd>
  208. <dt>compilation options set by <code>set compile-args</code></dt>
  209. </dl>
  210. <p>You can override compilation options using the following command:
  211. </p>
  212. <dl compact="compact">
  213. <dt><code>set compile-args</code></dt>
  214. <dd><a name="index-compile-command-options-override"></a>
  215. <p>Set compilation options used for compiling and injecting code with the
  216. <code>compile</code> commands. These options override any conflicting ones
  217. from the target architecture and/or options stored during inferior
  218. compilation.
  219. </p>
  220. </dd>
  221. <dt><code>show compile-args</code></dt>
  222. <dd><p>Displays the current state of compilation options override.
  223. This does not show all the options actually used during compilation,
  224. use <a href="#set-debug-compile">set debug compile</a> for that.
  225. </p></dd>
  226. </dl>
  227. <a name="Caveats-when-using-the-compile-command"></a>
  228. <h4 class="subsection">17.7.2 Caveats when using the <code>compile</code> command</h4>
  229. <p>There are a few caveats to keep in mind when using the <code>compile</code>
  230. command. As the caveats are different per language, the table below
  231. highlights specific issues on a per language basis.
  232. </p>
  233. <dl compact="compact">
  234. <dt>C code examples and caveats</dt>
  235. <dd><p>When the language in <small>GDB</small> is set to &lsquo;<samp>C</samp>&rsquo;, the compiler will
  236. attempt to compile the source code with a &lsquo;<samp>C</samp>&rsquo; compiler. The source
  237. code provided to the <code>compile</code> command will have much the same
  238. access to variables and types as it normally would if it were part of
  239. the program currently being debugged in <small>GDB</small>.
  240. </p>
  241. <p>Below is a sample program that forms the basis of the examples that
  242. follow. This program has been compiled and loaded into <small>GDB</small>,
  243. much like any other normal debugging session.
  244. </p>
  245. <div class="smallexample">
  246. <pre class="smallexample">void function1 (void)
  247. {
  248. int i = 42;
  249. printf (&quot;function 1\n&quot;);
  250. }
  251. void function2 (void)
  252. {
  253. int j = 12;
  254. function1 ();
  255. }
  256. int main(void)
  257. {
  258. int k = 6;
  259. int *p;
  260. function2 ();
  261. return 0;
  262. }
  263. </pre></div>
  264. <p>For the purposes of the examples in this section, the program above has
  265. been compiled, loaded into <small>GDB</small>, stopped at the function
  266. <code>main</code>, and <small>GDB</small> is awaiting input from the user.
  267. </p>
  268. <p>To access variables and types for any program in <small>GDB</small>, the
  269. program must be compiled and packaged with debug information. The
  270. <code>compile</code> command is not an exception to this rule. Without debug
  271. information, you can still use the <code>compile</code> command, but you will
  272. be very limited in what variables and types you can access.
  273. </p>
  274. <p>So with that in mind, the example above has been compiled with debug
  275. information enabled. The <code>compile</code> command will have access to
  276. all variables and types (except those that may have been optimized
  277. out). Currently, as <small>GDB</small> has stopped the program in the
  278. <code>main</code> function, the <code>compile</code> command would have access to
  279. the variable <code>k</code>. You could invoke the <code>compile</code> command
  280. and type some source code to set the value of <code>k</code>. You can also
  281. read it, or do anything with that variable you would normally do in
  282. <code>C</code>. Be aware that changes to inferior variables in the
  283. <code>compile</code> command are persistent. In the following example:
  284. </p>
  285. <div class="smallexample">
  286. <pre class="smallexample">compile code k = 3;
  287. </pre></div>
  288. <p>the variable <code>k</code> is now 3. It will retain that value until
  289. something else in the example program changes it, or another
  290. <code>compile</code> command changes it.
  291. </p>
  292. <p>Normal scope and access rules apply to source code compiled and
  293. injected by the <code>compile</code> command. In the example, the variables
  294. <code>j</code> and <code>k</code> are not accessible yet, because the program is
  295. currently stopped in the <code>main</code> function, where these variables
  296. are not in scope. Therefore, the following command
  297. </p>
  298. <div class="smallexample">
  299. <pre class="smallexample">compile code j = 3;
  300. </pre></div>
  301. <p>will result in a compilation error message.
  302. </p>
  303. <p>Once the program is continued, execution will bring these variables in
  304. scope, and they will become accessible; then the code you specify via
  305. the <code>compile</code> command will be able to access them.
  306. </p>
  307. <p>You can create variables and types with the <code>compile</code> command as
  308. part of your source code. Variables and types that are created as part
  309. of the <code>compile</code> command are not visible to the rest of the program for
  310. the duration of its run. This example is valid:
  311. </p>
  312. <div class="smallexample">
  313. <pre class="smallexample">compile code int ff = 5; printf (&quot;ff is %d\n&quot;, ff);
  314. </pre></div>
  315. <p>However, if you were to type the following into <small>GDB</small> after that
  316. command has completed:
  317. </p>
  318. <div class="smallexample">
  319. <pre class="smallexample">compile code printf (&quot;ff is %d\n'', ff);
  320. </pre></div>
  321. <p>a compiler error would be raised as the variable <code>ff</code> no longer
  322. exists. Object code generated and injected by the <code>compile</code>
  323. command is removed when its execution ends. Caution is advised
  324. when assigning to program variables values of variables created by the
  325. code submitted to the <code>compile</code> command. This example is valid:
  326. </p>
  327. <div class="smallexample">
  328. <pre class="smallexample">compile code int ff = 5; k = ff;
  329. </pre></div>
  330. <p>The value of the variable <code>ff</code> is assigned to <code>k</code>. The variable
  331. <code>k</code> does not require the existence of <code>ff</code> to maintain the value
  332. it has been assigned. However, pointers require particular care in
  333. assignment. If the source code compiled with the <code>compile</code> command
  334. changed the address of a pointer in the example program, perhaps to a
  335. variable created in the <code>compile</code> command, that pointer would point
  336. to an invalid location when the command exits. The following example
  337. would likely cause issues with your debugged program:
  338. </p>
  339. <div class="smallexample">
  340. <pre class="smallexample">compile code int ff = 5; p = &amp;ff;
  341. </pre></div>
  342. <p>In this example, <code>p</code> would point to <code>ff</code> when the
  343. <code>compile</code> command is executing the source code provided to it.
  344. However, as variables in the (example) program persist with their
  345. assigned values, the variable <code>p</code> would point to an invalid
  346. location when the command exists. A general rule should be followed
  347. in that you should either assign <code>NULL</code> to any assigned pointers,
  348. or restore a valid location to the pointer before the command exits.
  349. </p>
  350. <p>Similar caution must be exercised with any structs, unions, and typedefs
  351. defined in <code>compile</code> command. Types defined in the <code>compile</code>
  352. command will no longer be available in the next <code>compile</code> command.
  353. Therefore, if you cast a variable to a type defined in the
  354. <code>compile</code> command, care must be taken to ensure that any future
  355. need to resolve the type can be achieved.
  356. </p>
  357. <div class="smallexample">
  358. <pre class="smallexample">(gdb) compile code static struct a { int a; } v = { 42 }; argv = &amp;v;
  359. (gdb) compile code printf (&quot;%d\n&quot;, ((struct a *) argv)-&gt;a);
  360. gdb command line:1:36: error: dereferencing pointer to incomplete type ‘struct a’
  361. Compilation failed.
  362. (gdb) compile code struct a { int a; }; printf (&quot;%d\n&quot;, ((struct a *) argv)-&gt;a);
  363. 42
  364. </pre></div>
  365. <p>Variables that have been optimized away by the compiler are not
  366. accessible to the code submitted to the <code>compile</code> command.
  367. Access to those variables will generate a compiler error which <small>GDB</small>
  368. will print to the console.
  369. </p></dd>
  370. </dl>
  371. <a name="Compiler-search-for-the-compile-command"></a>
  372. <h4 class="subsection">17.7.3 Compiler search for the <code>compile</code> command</h4>
  373. <p><small>GDB</small> needs to find <small>GCC</small> for the inferior being debugged
  374. which may not be obvious for remote targets of different architecture
  375. than where <small>GDB</small> is running. Environment variable <code>PATH</code> on
  376. <small>GDB</small> host is searched for <small>GCC</small> binary matching the
  377. target architecture and operating system. This search can be overriden
  378. by <code>set compile-gcc</code> <small>GDB</small> command below. <code>PATH</code> is
  379. taken from shell that executed <small>GDB</small>, it is not the value set by
  380. <small>GDB</small> command <code>set environment</code>). See <a href="Environment.html#Environment">Environment</a>.
  381. </p>
  382. <p>Specifically <code>PATH</code> is searched for binaries matching regular expression
  383. <code><var>arch</var>(-[^-]*)?-<var>os</var>-gcc</code> according to the inferior target being
  384. debugged. <var>arch</var> is processor name &mdash; multiarch is supported, so for
  385. example both <code>i386</code> and <code>x86_64</code> targets look for pattern
  386. <code>(x86_64|i.86)</code> and both <code>s390</code> and <code>s390x</code> targets look
  387. for pattern <code>s390x?</code>. <var>os</var> is currently supported only for
  388. pattern <code>linux(-gnu)?</code>.
  389. </p>
  390. <p>On Posix hosts the compiler driver <small>GDB</small> needs to find also
  391. shared library <samp>libcc1.so</samp> from the compiler. It is searched in
  392. default shared library search path (overridable with usual environment
  393. variable <code>LD_LIBRARY_PATH</code>), unrelated to <code>PATH</code> or <code>set
  394. compile-gcc</code> settings. Contrary to it <samp>libcc1plugin.so</samp> is found
  395. according to the installation of the found compiler &mdash; as possibly
  396. specified by the <code>set compile-gcc</code> command.
  397. </p>
  398. <dl compact="compact">
  399. <dt><code>set compile-gcc</code></dt>
  400. <dd><a name="index-compile-command-driver-filename-override"></a>
  401. <p>Set compilation command used for compiling and injecting code with the
  402. <code>compile</code> commands. If this option is not set (it is set to
  403. an empty string), the search described above will occur &mdash; that is the
  404. default.
  405. </p>
  406. </dd>
  407. <dt><code>show compile-gcc</code></dt>
  408. <dd><p>Displays the current compile command <small>GCC</small> driver filename.
  409. If set, it is the main command <code>gcc</code>, found usually for example
  410. under name <samp>x86_64-linux-gnu-gcc</samp>.
  411. </p></dd>
  412. </dl>
  413. <hr>
  414. <div class="header">
  415. <p>
  416. Previous: <a href="Patching.html#Patching" accesskey="p" rel="prev">Patching</a>, Up: <a href="Altering.html#Altering" accesskey="u" rel="up">Altering</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>
  417. </div>
  418. </body>
  419. </html>