Emacs.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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: Emacs</title>
  16. <meta name="description" content="Debugging with GDB: Emacs">
  17. <meta name="keywords" content="Debugging with GDB: Emacs">
  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="index.html#Top" rel="up" title="Top">
  26. <link href="GDB_002fMI.html#GDB_002fMI" rel="next" title="GDB/MI">
  27. <link href="TUI-Configuration.html#TUI-Configuration" rel="previous" title="TUI Configuration">
  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="Emacs"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="GDB_002fMI.html#GDB_002fMI" accesskey="n" rel="next">GDB/MI</a>, Previous: <a href="TUI.html#TUI" accesskey="p" rel="previous">TUI</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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="Using-GDB-under-GNU-Emacs"></a>
  65. <h2 class="chapter">26 Using <small>GDB</small> under <small>GNU</small> Emacs</h2>
  66. <a name="index-Emacs"></a>
  67. <a name="index-GNU-Emacs"></a>
  68. <p>A special interface allows you to use <small>GNU</small> Emacs to view (and
  69. edit) the source files for the program you are debugging with
  70. <small>GDB</small>.
  71. </p>
  72. <p>To use this interface, use the command <kbd>M-x gdb</kbd> in Emacs. Give the
  73. executable file you want to debug as an argument. This command starts
  74. <small>GDB</small> as a subprocess of Emacs, with input and output through a newly
  75. created Emacs buffer.
  76. </p>
  77. <p>Running <small>GDB</small> under Emacs can be just like running <small>GDB</small> normally except for two
  78. things:
  79. </p>
  80. <ul>
  81. <li> All &ldquo;terminal&rdquo; input and output goes through an Emacs buffer, called
  82. the GUD buffer.
  83. <p>This applies both to <small>GDB</small> commands and their output, and to the input
  84. and output done by the program you are debugging.
  85. </p>
  86. <p>This is useful because it means that you can copy the text of previous
  87. commands and input them again; you can even use parts of the output
  88. in this way.
  89. </p>
  90. <p>All the facilities of Emacs&rsquo; Shell mode are available for interacting
  91. with your program. In particular, you can send signals the usual
  92. way&mdash;for example, <kbd>C-c C-c</kbd> for an interrupt, <kbd>C-c C-z</kbd> for a
  93. stop.
  94. </p>
  95. </li><li> <small>GDB</small> displays source code through Emacs.
  96. <p>Each time <small>GDB</small> displays a stack frame, Emacs automatically finds the
  97. source file for that frame and puts an arrow (&lsquo;<samp>=&gt;</samp>&rsquo;) at the
  98. left margin of the current line. Emacs uses a separate buffer for
  99. source display, and splits the screen to show both your <small>GDB</small> session
  100. and the source.
  101. </p>
  102. <p>Explicit <small>GDB</small> <code>list</code> or search commands still produce output as
  103. usual, but you probably have no reason to use them from Emacs.
  104. </p></li></ul>
  105. <p>We call this <em>text command mode</em>. Emacs 22.1, and later, also uses
  106. a graphical mode, enabled by default, which provides further buffers
  107. that can control the execution and describe the state of your program.
  108. See <a href="../Emacs/GDB-Graphical-Interface.html#GDB-Graphical-Interface">GDB Graphical Interface</a> in <cite>The <small>GNU</small> Emacs Manual</cite>.
  109. </p>
  110. <p>If you specify an absolute file name when prompted for the <kbd>M-x
  111. gdb</kbd> argument, then Emacs sets your current working directory to where
  112. your program resides. If you only specify the file name, then Emacs
  113. sets your current working directory to the directory associated
  114. with the previous buffer. In this case, <small>GDB</small> may find your
  115. program by searching your environment&rsquo;s <code>PATH</code> variable, but on
  116. some operating systems it might not find the source. So, although the
  117. <small>GDB</small> input and output session proceeds normally, the auxiliary
  118. buffer does not display the current source and line of execution.
  119. </p>
  120. <p>The initial working directory of <small>GDB</small> is printed on the top
  121. line of the GUD buffer and this serves as a default for the commands
  122. that specify files for <small>GDB</small> to operate on. See <a href="Files.html#Files">Commands to Specify Files</a>.
  123. </p>
  124. <p>By default, <kbd>M-x gdb</kbd> calls the program called <samp>gdb</samp>. If you
  125. need to call <small>GDB</small> by a different name (for example, if you
  126. keep several configurations around, with different names) you can
  127. customize the Emacs variable <code>gud-gdb-command-name</code> to run the
  128. one you want.
  129. </p>
  130. <p>In the GUD buffer, you can use these special Emacs commands in
  131. addition to the standard Shell mode commands:
  132. </p>
  133. <dl compact="compact">
  134. <dt><kbd>C-h m</kbd></dt>
  135. <dd><p>Describe the features of Emacs&rsquo; GUD Mode.
  136. </p>
  137. </dd>
  138. <dt><kbd>C-c C-s</kbd></dt>
  139. <dd><p>Execute to another source line, like the <small>GDB</small> <code>step</code> command; also
  140. update the display window to show the current file and location.
  141. </p>
  142. </dd>
  143. <dt><kbd>C-c C-n</kbd></dt>
  144. <dd><p>Execute to next source line in this function, skipping all function
  145. calls, like the <small>GDB</small> <code>next</code> command. Then update the display window
  146. to show the current file and location.
  147. </p>
  148. </dd>
  149. <dt><kbd>C-c C-i</kbd></dt>
  150. <dd><p>Execute one instruction, like the <small>GDB</small> <code>stepi</code> command; update
  151. display window accordingly.
  152. </p>
  153. </dd>
  154. <dt><kbd>C-c C-f</kbd></dt>
  155. <dd><p>Execute until exit from the selected stack frame, like the <small>GDB</small>
  156. <code>finish</code> command.
  157. </p>
  158. </dd>
  159. <dt><kbd>C-c C-r</kbd></dt>
  160. <dd><p>Continue execution of your program, like the <small>GDB</small> <code>continue</code>
  161. command.
  162. </p>
  163. </dd>
  164. <dt><kbd>C-c &lt;</kbd></dt>
  165. <dd><p>Go up the number of frames indicated by the numeric argument
  166. (see <a href="../Emacs/Arguments.html#Arguments">Numeric Arguments</a> in <cite>The <small>GNU</small> Emacs Manual</cite>),
  167. like the <small>GDB</small> <code>up</code> command.
  168. </p>
  169. </dd>
  170. <dt><kbd>C-c &gt;</kbd></dt>
  171. <dd><p>Go down the number of frames indicated by the numeric argument, like the
  172. <small>GDB</small> <code>down</code> command.
  173. </p></dd>
  174. </dl>
  175. <p>In any source file, the Emacs command <kbd>C-x <span class="key">SPC</span></kbd> (<code>gud-break</code>)
  176. tells <small>GDB</small> to set a breakpoint on the source line point is on.
  177. </p>
  178. <p>In text command mode, if you type <kbd>M-x speedbar</kbd>, Emacs displays a
  179. separate frame which shows a backtrace when the GUD buffer is current.
  180. Move point to any frame in the stack and type <tt class="key">RET</tt> to make it
  181. become the current frame and display the associated source in the
  182. source buffer. Alternatively, click <kbd>Mouse-2</kbd> to make the
  183. selected frame become the current one. In graphical mode, the
  184. speedbar displays watch expressions.
  185. </p>
  186. <p>If you accidentally delete the source-display buffer, an easy way to get
  187. it back is to type the command <code>f</code> in the <small>GDB</small> buffer, to
  188. request a frame display; when you run under Emacs, this recreates
  189. the source buffer if necessary to show you the context of the current
  190. frame.
  191. </p>
  192. <p>The source files displayed in Emacs are in ordinary Emacs buffers
  193. which are visiting the source files in the usual way. You can edit
  194. the files with these buffers if you wish; but keep in mind that <small>GDB</small>
  195. communicates with Emacs in terms of line numbers. If you add or
  196. delete lines from the text, the line numbers that <small>GDB</small> knows cease
  197. to correspond properly with the code.
  198. </p>
  199. <p>A more detailed description of Emacs&rsquo; interaction with <small>GDB</small> is
  200. given in the Emacs manual (see <a href="../Emacs/Debuggers.html#Debuggers">Debuggers</a> in <cite>The <small>GNU</small>
  201. Emacs Manual</cite>).
  202. </p>
  203. <hr>
  204. <div class="header">
  205. <p>
  206. Next: <a href="GDB_002fMI.html#GDB_002fMI" accesskey="n" rel="next">GDB/MI</a>, Previous: <a href="TUI.html#TUI" accesskey="p" rel="previous">TUI</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
  207. </div>
  208. </body>
  209. </html>