Compiling-and-Injecting-Code.html 21 KB

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