1<html lang="en"> 2<head> 3<title>Emacs - Debugging with GDB</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Debugging with GDB"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="prev" href="TUI.html#TUI" title="TUI"> 9<link rel="next" href="GDB_002fMI.html#GDB_002fMI" title="GDB/MI"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 131998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 14Free Software Foundation, Inc. 15 16Permission is granted to copy, distribute and/or modify this document 17under the terms of the GNU Free Documentation License, Version 1.3 or 18any later version published by the Free Software Foundation; with the 19Invariant Sections being ``Free Software'' and ``Free Software Needs 20Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' 21and with the Back-Cover Texts as in (a) below. 22 23(a) The FSF's Back-Cover Text is: ``You are free to copy and modify 24this GNU Manual. Buying copies from GNU Press supports the FSF in 25developing GNU and promoting software freedom.''--> 26<meta http-equiv="Content-Style-Type" content="text/css"> 27<style type="text/css"><!-- 28 pre.display { font-family:inherit } 29 pre.format { font-family:inherit } 30 pre.smalldisplay { font-family:inherit; font-size:smaller } 31 pre.smallformat { font-family:inherit; font-size:smaller } 32 pre.smallexample { font-size:smaller } 33 pre.smalllisp { font-size:smaller } 34 span.sc { font-variant:small-caps } 35 span.roman { font-family:serif; font-weight:normal; } 36 span.sansserif { font-family:sans-serif; font-weight:normal; } 37--></style> 38<link rel="stylesheet" type="text/css" href="../cs.css"> 39</head> 40<body> 41<div class="node"> 42<a name="Emacs"></a> 43<p> 44Next: <a rel="next" accesskey="n" href="GDB_002fMI.html#GDB_002fMI">GDB/MI</a>, 45Previous: <a rel="previous" accesskey="p" href="TUI.html#TUI">TUI</a>, 46Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> 47<hr> 48</div> 49 50<h2 class="chapter">26 Using <span class="sc">gdb</span> under <span class="sc">gnu</span> Emacs</h2> 51 52<p><a name="index-Emacs-1998"></a><a name="index-g_t_0040sc_007bgnu_007d-Emacs-1999"></a>A special interface allows you to use <span class="sc">gnu</span> Emacs to view (and 53edit) the source files for the program you are debugging with 54<span class="sc">gdb</span>. 55 56 <p>To use this interface, use the command <kbd>M-x gdb</kbd> in Emacs. Give the 57executable file you want to debug as an argument. This command starts 58<span class="sc">gdb</span> as a subprocess of Emacs, with input and output through a newly 59created Emacs buffer. 60<!-- (Do not use the @code{-tui} option to run @value{GDBN} from Emacs.) --> 61 62 <p>Running <span class="sc">gdb</span> under Emacs can be just like running <span class="sc">gdb</span> normally except for two 63things: 64 65 <ul> 66<li>All “terminal” input and output goes through an Emacs buffer, called 67the GUD buffer. 68 69 <p>This applies both to <span class="sc">gdb</span> commands and their output, and to the input 70and output done by the program you are debugging. 71 72 <p>This is useful because it means that you can copy the text of previous 73commands and input them again; you can even use parts of the output 74in this way. 75 76 <p>All the facilities of Emacs' Shell mode are available for interacting 77with your program. In particular, you can send signals the usual 78way—for example, <kbd>C-c C-c</kbd> for an interrupt, <kbd>C-c C-z</kbd> for a 79stop. 80 81 <li><span class="sc">gdb</span> displays source code through Emacs. 82 83 <p>Each time <span class="sc">gdb</span> displays a stack frame, Emacs automatically finds the 84source file for that frame and puts an arrow (‘<samp><span class="samp">=></span></samp>’) at the 85left margin of the current line. Emacs uses a separate buffer for 86source display, and splits the screen to show both your <span class="sc">gdb</span> session 87and the source. 88 89 <p>Explicit <span class="sc">gdb</span> <code>list</code> or search commands still produce output as 90usual, but you probably have no reason to use them from Emacs. 91</ul> 92 93 <p>We call this <dfn>text command mode</dfn>. Emacs 22.1, and later, also uses 94a graphical mode, enabled by default, which provides further buffers 95that can control the execution and describe the state of your program. 96See <a href="../Emacs/GDB-Graphical-Interface.html#GDB-Graphical-Interface">GDB Graphical Interface</a>. 97 98 <p>If you specify an absolute file name when prompted for the <kbd>M-x 99gdb</kbd> argument, then Emacs sets your current working directory to where 100your program resides. If you only specify the file name, then Emacs 101sets your current working directory to to the directory associated 102with the previous buffer. In this case, <span class="sc">gdb</span> may find your 103program by searching your environment's <code>PATH</code> variable, but on 104some operating systems it might not find the source. So, although the 105<span class="sc">gdb</span> input and output session proceeds normally, the auxiliary 106buffer does not display the current source and line of execution. 107 108 <p>The initial working directory of <span class="sc">gdb</span> is printed on the top 109line of the GUD buffer and this serves as a default for the commands 110that specify files for <span class="sc">gdb</span> to operate on. See <a href="Files.html#Files">Commands to Specify Files</a>. 111 112 <p>By default, <kbd>M-x gdb</kbd> calls the program called <samp><span class="file">gdb</span></samp>. If you 113need to call <span class="sc">gdb</span> by a different name (for example, if you 114keep several configurations around, with different names) you can 115customize the Emacs variable <code>gud-gdb-command-name</code> to run the 116one you want. 117 118 <p>In the GUD buffer, you can use these special Emacs commands in 119addition to the standard Shell mode commands: 120 121 <dl> 122<dt><kbd>C-h m</kbd><dd>Describe the features of Emacs' GUD Mode. 123 124 <br><dt><kbd>C-c C-s</kbd><dd>Execute to another source line, like the <span class="sc">gdb</span> <code>step</code> command; also 125update the display window to show the current file and location. 126 127 <br><dt><kbd>C-c C-n</kbd><dd>Execute to next source line in this function, skipping all function 128calls, like the <span class="sc">gdb</span> <code>next</code> command. Then update the display window 129to show the current file and location. 130 131 <br><dt><kbd>C-c C-i</kbd><dd>Execute one instruction, like the <span class="sc">gdb</span> <code>stepi</code> command; update 132display window accordingly. 133 134 <br><dt><kbd>C-c C-f</kbd><dd>Execute until exit from the selected stack frame, like the <span class="sc">gdb</span> 135<code>finish</code> command. 136 137 <br><dt><kbd>C-c C-r</kbd><dd>Continue execution of your program, like the <span class="sc">gdb</span> <code>continue</code> 138command. 139 140 <br><dt><kbd>C-c <</kbd><dd>Go up the number of frames indicated by the numeric argument 141(see <a href="../Emacs/Arguments.html#Arguments">Numeric Arguments</a>), 142like the <span class="sc">gdb</span> <code>up</code> command. 143 144 <br><dt><kbd>C-c ></kbd><dd>Go down the number of frames indicated by the numeric argument, like the 145<span class="sc">gdb</span> <code>down</code> command. 146</dl> 147 148 <p>In any source file, the Emacs command <kbd>C-x <SPC></kbd> (<code>gud-break</code>) 149tells <span class="sc">gdb</span> to set a breakpoint on the source line point is on. 150 151 <p>In text command mode, if you type <kbd>M-x speedbar</kbd>, Emacs displays a 152separate frame which shows a backtrace when the GUD buffer is current. 153Move point to any frame in the stack and type <RET> to make it 154become the current frame and display the associated source in the 155source buffer. Alternatively, click <kbd>Mouse-2</kbd> to make the 156selected frame become the current one. In graphical mode, the 157speedbar displays watch expressions. 158 159 <p>If you accidentally delete the source-display buffer, an easy way to get 160it back is to type the command <code>f</code> in the <span class="sc">gdb</span> buffer, to 161request a frame display; when you run under Emacs, this recreates 162the source buffer if necessary to show you the context of the current 163frame. 164 165 <p>The source files displayed in Emacs are in ordinary Emacs buffers 166which are visiting the source files in the usual way. You can edit 167the files with these buffers if you wish; but keep in mind that <span class="sc">gdb</span> 168communicates with Emacs in terms of line numbers. If you add or 169delete lines from the text, the line numbers that <span class="sc">gdb</span> knows cease 170to correspond properly with the code. 171 172 <p>A more detailed description of Emacs' interaction with <span class="sc">gdb</span> is 173given in the Emacs manual (see <a href="../Emacs/Debuggers.html#Debuggers">Debuggers</a>). 174 175<!-- The following dropped because Epoch is nonstandard. Reactivate --> 176<!-- if/when v19 does something similar. -doc@cygnus.com 19dec1990 --> 177 </body></html> 178 179