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