<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://cs.indstate.edu/web/index.php?action=history&amp;feed=atom&amp;title=GNU_Debugger</id>
	<title>GNU Debugger - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://cs.indstate.edu/web/index.php?action=history&amp;feed=atom&amp;title=GNU_Debugger"/>
	<link rel="alternate" type="text/html" href="https://cs.indstate.edu/web/index.php?title=GNU_Debugger&amp;action=history"/>
	<updated>2026-04-14T19:11:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://cs.indstate.edu/web/index.php?title=GNU_Debugger&amp;diff=235&amp;oldid=prev</id>
		<title>Jkinne: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://cs.indstate.edu/web/index.php?title=GNU_Debugger&amp;diff=235&amp;oldid=prev"/>
		<updated>2025-08-17T13:22:16Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 13:22, 17 August 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key wiki2:diff:1.41:old-234:rev-235 --&gt;
&lt;/table&gt;</summary>
		<author><name>Jkinne</name></author>
	</entry>
	<entry>
		<id>https://cs.indstate.edu/web/index.php?title=GNU_Debugger&amp;diff=234&amp;oldid=prev</id>
		<title>wiki_previous&gt;Jkinne: /* gdb Commands */</title>
		<link rel="alternate" type="text/html" href="https://cs.indstate.edu/web/index.php?title=GNU_Debugger&amp;diff=234&amp;oldid=prev"/>
		<updated>2024-01-31T13:21:03Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;gdb Commands&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This page has information to get you started with the GNU Debugger, which is the &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt; command on the server.&lt;br /&gt;
&lt;br /&gt;
=Compiling and Starting gdb=&lt;br /&gt;
To run a C program in the GNU Debugger, you use the &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt; command.  First you need to compile the file with the &amp;lt;code&amp;gt;-g&amp;lt;/code&amp;gt; flag so that it contains debugging information. We use the file [https://cs.indstate.edu/learn/C/fix_errors/args_add4.c args_add4.c] as an example.  You can download this file, or you can copy/paste the code into a text editor and save it as args_add4.c. When debugging and testing code, it is also a good idea to turn on all warnings (with the &amp;lt;code&amp;gt;-Wall&amp;lt;/code&amp;gt; option) and turn off compiler optimization (with the &amp;lt;code&amp;gt;-O0&amp;lt;/code&amp;gt; option).&lt;br /&gt;
&lt;br /&gt;
To compile with debugging information, you run: &lt;br /&gt;
&amp;lt;pre&amp;gt;gcc -g -Wall -O0 args_add4.c -o args_add4.o&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To run the program in &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt;, you run &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt; giving the .o file name as a parameter:&lt;br /&gt;
&amp;lt;pre&amp;gt;gdb args_add4.o&amp;lt;/pre&amp;gt;&lt;br /&gt;
This has loaded the program into the debugger.&lt;br /&gt;
&lt;br /&gt;
=Commands in gdb=&lt;br /&gt;
The rest of this article is assuming you have started &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt; as indicated above.  You interact with &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt; by typing commands after you have started the program.&lt;br /&gt;
&lt;br /&gt;
To run the program you use the &amp;#039;&amp;#039;run&amp;#039;&amp;#039; command and give any command-line arguments after run.  For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;run 1 2 3&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you type enter, the program will run.  If it crashes, the debugger stops the program from crashing so you can take a look at what is happening.  When we run the above, we see the following.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) run 1 2 3&lt;br /&gt;
Starting program: /net/web/learn/C/fix_errors/args_add4.o 1 2 3&lt;br /&gt;
&lt;br /&gt;
Program received signal SIGSEGV, Segmentation fault.&lt;br /&gt;
0x00007ffff7de5633 in ____strtod_l_internal () from /lib64/libc.so.6&lt;br /&gt;
(gdb)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There has been a segmentation fault.  We can see information about the status of the program by typing the &amp;#039;&amp;#039;backtrace&amp;#039;&amp;#039; command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) backtrace&lt;br /&gt;
#0  0x00007ffff7de5633 in ____strtod_l_internal () from /lib64/libc.so.6&lt;br /&gt;
#1  0x0000000000401174 in main (argc=4, argv=0x7fffffffdfa8) at args_add4.c:15&lt;br /&gt;
(gdb) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The problem is at line 15 in the program.   We can see what is at line 15 either by looking at the file in our text editor, or using the &amp;#039;&amp;#039;list&amp;#039;&amp;#039; command in &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) list 15&lt;br /&gt;
10      #include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
11&lt;br /&gt;
12      int main(int argc, char *argv[]) {&lt;br /&gt;
13        float total = 0;&lt;br /&gt;
14        for(int i=1; i &amp;lt; argc; i--) {&lt;br /&gt;
15          total += atof(argv[i]);&lt;br /&gt;
16        }&lt;br /&gt;
17        printf(&amp;quot;%f\n&amp;quot;, total);&lt;br /&gt;
18        &lt;br /&gt;
19        return 0;&lt;br /&gt;
(gdb)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Probably the segmentation fault is the &amp;lt;code&amp;gt;argv[i]&amp;lt;/code&amp;gt; on line 15.  We can see what the value of &amp;#039;&amp;#039;i&amp;#039;&amp;#039; is by setting the debugger to be using the &amp;quot;stack frame&amp;quot; that has this value (frame #1 as indicated on the output after we ran the &amp;#039;&amp;#039;backtrace&amp;#039;&amp;#039; command) and then using the &amp;#039;&amp;#039;print&amp;#039;&amp;#039; command.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(gdb) frame 1&lt;br /&gt;
#1  0x0000000000401174 in main (argc=4, argv=0x7fffffffdfa8) at args_add4.c:15&lt;br /&gt;
15          total += atof(argv[i]);&lt;br /&gt;
(gdb) print i&lt;br /&gt;
$1 = -1&lt;br /&gt;
(gdb) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is definitely a problem - the index &amp;#039;&amp;#039;i&amp;#039;&amp;#039; should not be negative if we are using it as an array index.  Looking back at the printout of the code, the mistake is in line 14: rather than &amp;lt;code&amp;gt;i--&amp;lt;/code&amp;gt;, the loop increment should be &amp;lt;code&amp;gt;i++&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=gdb Commands=&lt;br /&gt;
The following are a few key commands to get you started in &amp;lt;code&amp;gt;gdb&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &amp;lt;code&amp;gt;run arg1 arg2 ...&amp;lt;/code&amp;gt; (can be abbreviated as &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;) - run the program that is being debugged, giving it command-line arguments &amp;lt;code&amp;gt;arg1 arg2 ...&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;backtrace&amp;lt;/code&amp;gt; (can be abbreviated &amp;lt;code&amp;gt;bt&amp;lt;/code&amp;gt;) - print a trace of the stack frame, which is a history of which functions have been called and still haven&amp;#039;t completed&lt;br /&gt;
* &amp;lt;code&amp;gt;frame i&amp;lt;/code&amp;gt; (can be abbreviated &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;) - set the debugger to be using the given (&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;) stack frame&lt;br /&gt;
* &amp;lt;code&amp;gt;print expr&amp;lt;/code&amp;gt; (can be abbreviated &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt;) - evaluate the given expression and print its value (e.g., can be used to print the value of a variable)&lt;br /&gt;
* &amp;lt;code&amp;gt;quit&amp;lt;/code&amp;gt; (can be abbreviated &amp;lt;code&amp;gt;q&amp;lt;/code&amp;gt;) - quit the debugger&lt;br /&gt;
* &amp;lt;code&amp;gt;(s)tep&amp;lt;/code&amp;gt; - step one line at a time (going into a function if the next line is a function call).&lt;br /&gt;
* &amp;lt;code&amp;gt;(c)ontinue&amp;lt;/code&amp;gt; - continue execution.&lt;br /&gt;
* &amp;lt;code&amp;gt;break file_name.c:100&amp;lt;/code&amp;gt; - set a break point at line 100 in file_name.c&lt;br /&gt;
* &amp;lt;code&amp;gt;break function_name&amp;lt;/code&amp;gt; - set a break point on function_name.&lt;br /&gt;
&lt;br /&gt;
For more information, search online for GDB tutorials, reference sheets, and documentation.  The official documentation is here - https://www.sourceware.org/gdb/documentation/ A good quick start is https://web.eecs.umich.edu/~sugih/pointers/gdbQS.html&lt;/div&gt;</summary>
		<author><name>wiki_previous&gt;Jkinne</name></author>
	</entry>
</feed>