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-2021 Free Software Foundation, Inc. 4 5Permission is granted to copy, distribute and/or modify this document 6under the terms of the GNU Free Documentation License, Version 1.3 or 7any later version published by the Free Software Foundation; with the 8Invariant Sections being "Free Software" and "Free Software Needs 9Free Documentation", with the Front-Cover Texts being "A GNU Manual," 10and with the Back-Cover Texts as in (a) below. 11 12(a) The FSF's Back-Cover Text is: "You are free to copy and modify 13this GNU Manual. Buying copies from GNU Press supports the FSF in 14developing GNU and promoting software freedom." --> 15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> 16<head> 17<title>Debugging with GDB: Emacs</title> 18 19<meta name="description" content="Debugging with GDB: Emacs"> 20<meta name="keywords" content="Debugging with GDB: Emacs"> 21<meta name="resource-type" content="document"> 22<meta name="distribution" content="global"> 23<meta name="Generator" content="makeinfo"> 24<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 25<link href="index.html#Top" rel="start" title="Top"> 26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> 27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> 28<link href="index.html#Top" rel="up" title="Top"> 29<link href="GDB_002fMI.html#GDB_002fMI" rel="next" title="GDB/MI"> 30<link href="TUI-Configuration.html#TUI-Configuration" rel="previous" title="TUI Configuration"> 31<style type="text/css"> 32<!-- 33a.summary-letter {text-decoration: none} 34blockquote.smallquotation {font-size: smaller} 35div.display {margin-left: 3.2em} 36div.example {margin-left: 3.2em} 37div.indentedblock {margin-left: 3.2em} 38div.lisp {margin-left: 3.2em} 39div.smalldisplay {margin-left: 3.2em} 40div.smallexample {margin-left: 3.2em} 41div.smallindentedblock {margin-left: 3.2em; font-size: smaller} 42div.smalllisp {margin-left: 3.2em} 43kbd {font-style:oblique} 44pre.display {font-family: inherit} 45pre.format {font-family: inherit} 46pre.menu-comment {font-family: serif} 47pre.menu-preformatted {font-family: serif} 48pre.smalldisplay {font-family: inherit; font-size: smaller} 49pre.smallexample {font-size: smaller} 50pre.smallformat {font-family: inherit; font-size: smaller} 51pre.smalllisp {font-size: smaller} 52span.nocodebreak {white-space:nowrap} 53span.nolinebreak {white-space:nowrap} 54span.roman {font-family:serif; font-weight:normal} 55span.sansserif {font-family:sans-serif; font-weight:normal} 56ul.no-bullet {list-style: none} 57--> 58</style> 59 60 61</head> 62 63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> 64<a name="Emacs"></a> 65<div class="header"> 66<p> 67Next: <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> [<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> 68</div> 69<hr> 70<a name="Using-GDB-under-GNU-Emacs"></a> 71<h2 class="chapter">26 Using <small>GDB</small> under <small>GNU</small> Emacs</h2> 72 73<a name="index-Emacs"></a> 74<a name="index-GNU-Emacs"></a> 75<p>A special interface allows you to use <small>GNU</small> Emacs to view (and 76edit) the source files for the program you are debugging with 77<small>GDB</small>. 78</p> 79<p>To use this interface, use the command <kbd>M-x gdb</kbd> in Emacs. Give the 80executable file you want to debug as an argument. This command starts 81<small>GDB</small> as a subprocess of Emacs, with input and output through a newly 82created Emacs buffer. 83</p> 84<p>Running <small>GDB</small> under Emacs can be just like running <small>GDB</small> normally except for two 85things: 86</p> 87<ul> 88<li> All “terminal” input and output goes through an Emacs buffer, called 89the GUD buffer. 90 91<p>This applies both to <small>GDB</small> commands and their output, and to the input 92and output done by the program you are debugging. 93</p> 94<p>This is useful because it means that you can copy the text of previous 95commands and input them again; you can even use parts of the output 96in this way. 97</p> 98<p>All the facilities of Emacs’ Shell mode are available for interacting 99with your program. In particular, you can send signals the usual 100way—for example, <kbd>C-c C-c</kbd> for an interrupt, <kbd>C-c C-z</kbd> for a 101stop. 102</p> 103</li><li> <small>GDB</small> displays source code through Emacs. 104 105<p>Each time <small>GDB</small> displays a stack frame, Emacs automatically finds the 106source file for that frame and puts an arrow (‘<samp>=></samp>’) at the 107left margin of the current line. Emacs uses a separate buffer for 108source display, and splits the screen to show both your <small>GDB</small> session 109and the source. 110</p> 111<p>Explicit <small>GDB</small> <code>list</code> or search commands still produce output as 112usual, but you probably have no reason to use them from Emacs. 113</p></li></ul> 114 115<p>We call this <em>text command mode</em>. Emacs 22.1, and later, also uses 116a graphical mode, enabled by default, which provides further buffers 117that can control the execution and describe the state of your program. 118See <a href="../Emacs/GDB-Graphical-Interface.html#GDB-Graphical-Interface">GDB Graphical Interface</a> in <cite>The <small>GNU</small> Emacs Manual</cite>. 119</p> 120<p>If you specify an absolute file name when prompted for the <kbd>M-x 121gdb</kbd> argument, then Emacs sets your current working directory to where 122your program resides. If you only specify the file name, then Emacs 123sets your current working directory to the directory associated 124with the previous buffer. In this case, <small>GDB</small> may find your 125program by searching your environment’s <code>PATH</code> variable, but on 126some operating systems it might not find the source. So, although the 127<small>GDB</small> input and output session proceeds normally, the auxiliary 128buffer does not display the current source and line of execution. 129</p> 130<p>The initial working directory of <small>GDB</small> is printed on the top 131line of the GUD buffer and this serves as a default for the commands 132that specify files for <small>GDB</small> to operate on. See <a href="Files.html#Files">Commands to Specify Files</a>. 133</p> 134<p>By default, <kbd>M-x gdb</kbd> calls the program called <samp>gdb</samp>. If you 135need to call <small>GDB</small> by a different name (for example, if you 136keep several configurations around, with different names) you can 137customize the Emacs variable <code>gud-gdb-command-name</code> to run the 138one you want. 139</p> 140<p>In the GUD buffer, you can use these special Emacs commands in 141addition to the standard Shell mode commands: 142</p> 143<dl compact="compact"> 144<dt><kbd>C-h m</kbd></dt> 145<dd><p>Describe the features of Emacs’ GUD Mode. 146</p> 147</dd> 148<dt><kbd>C-c C-s</kbd></dt> 149<dd><p>Execute to another source line, like the <small>GDB</small> <code>step</code> command; also 150update the display window to show the current file and location. 151</p> 152</dd> 153<dt><kbd>C-c C-n</kbd></dt> 154<dd><p>Execute to next source line in this function, skipping all function 155calls, like the <small>GDB</small> <code>next</code> command. Then update the display window 156to show the current file and location. 157</p> 158</dd> 159<dt><kbd>C-c C-i</kbd></dt> 160<dd><p>Execute one instruction, like the <small>GDB</small> <code>stepi</code> command; update 161display window accordingly. 162</p> 163</dd> 164<dt><kbd>C-c C-f</kbd></dt> 165<dd><p>Execute until exit from the selected stack frame, like the <small>GDB</small> 166<code>finish</code> command. 167</p> 168</dd> 169<dt><kbd>C-c C-r</kbd></dt> 170<dd><p>Continue execution of your program, like the <small>GDB</small> <code>continue</code> 171command. 172</p> 173</dd> 174<dt><kbd>C-c <</kbd></dt> 175<dd><p>Go up the number of frames indicated by the numeric argument 176(see <a href="../Emacs/Arguments.html#Arguments">Numeric Arguments</a> in <cite>The <small>GNU</small> Emacs Manual</cite>), 177like the <small>GDB</small> <code>up</code> command. 178</p> 179</dd> 180<dt><kbd>C-c ></kbd></dt> 181<dd><p>Go down the number of frames indicated by the numeric argument, like the 182<small>GDB</small> <code>down</code> command. 183</p></dd> 184</dl> 185 186<p>In any source file, the Emacs command <kbd>C-x <span class="key">SPC</span></kbd> (<code>gud-break</code>) 187tells <small>GDB</small> to set a breakpoint on the source line point is on. 188</p> 189<p>In text command mode, if you type <kbd>M-x speedbar</kbd>, Emacs displays a 190separate frame which shows a backtrace when the GUD buffer is current. 191Move point to any frame in the stack and type <tt class="key">RET</tt> to make it 192become the current frame and display the associated source in the 193source buffer. Alternatively, click <kbd>Mouse-2</kbd> to make the 194selected frame become the current one. In graphical mode, the 195speedbar displays watch expressions. 196</p> 197<p>If you accidentally delete the source-display buffer, an easy way to get 198it back is to type the command <code>f</code> in the <small>GDB</small> buffer, to 199request a frame display; when you run under Emacs, this recreates 200the source buffer if necessary to show you the context of the current 201frame. 202</p> 203<p>The source files displayed in Emacs are in ordinary Emacs buffers 204which are visiting the source files in the usual way. You can edit 205the files with these buffers if you wish; but keep in mind that <small>GDB</small> 206communicates with Emacs in terms of line numbers. If you add or 207delete lines from the text, the line numbers that <small>GDB</small> knows cease 208to correspond properly with the code. 209</p> 210<p>A more detailed description of Emacs’ interaction with <small>GDB</small> is 211given in the Emacs manual (see <a href="../Emacs/Debuggers.html#Debuggers">Debuggers</a> in <cite>The <small>GNU</small> 212Emacs Manual</cite>). 213</p> 214<hr> 215<div class="header"> 216<p> 217Next: <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> [<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> 218</div> 219 220 221 222</body> 223</html> 224