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.

223 lines
10KB

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