Difference between revisions of "Using gdb"

From LSDevLinux
Jump to: navigation, search
m (Reverted edits by Okopacare (Talk); changed back to last version by Mayhewn)
 
Line 1: Line 1:
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://exytebuc.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
 
----
 
=[http://exytebuc.co.cc CLICK HERE]=
 
----
 
</div>
 
 
'''gdb''' is the GNU debugger and can be invaluble in dissecting C/C++ programs
 
'''gdb''' is the GNU debugger and can be invaluble in dissecting C/C++ programs
  
 
== Running gdb ==
 
== Running gdb ==
To start &lt;code&gt;gdb&lt;/code&gt;, use this at the command line:
+
To start <code>gdb</code>, use this at the command line:
 
  gdb ''program-name''
 
  gdb ''program-name''
To run the program, use this inside &lt;code&gt;gdb&lt;/code&gt;:
+
To run the program, use this inside <code>gdb</code>:
 
  r ''args''
 
  r ''args''
 
It's also possible to specify all the arguments on the command line (giving the benefit of tab completion and filename globbing):
 
It's also possible to specify all the arguments on the command line (giving the benefit of tab completion and filename globbing):
 
  gdb --args ''program-name arg1 arg2 ...''
 
  gdb --args ''program-name arg1 arg2 ...''
After starting &lt;code&gt;gdb&lt;/code&gt; like this, use the &lt;code&gt;r&lt;/code&gt; command with no arguments:
+
After starting <code>gdb</code> like this, use the <code>r</code> command with no arguments:
 
  r
 
  r
&lt;code&gt;gdb&lt;/code&gt; will then use the arguments passed on the command line.
+
<code>gdb</code> will then use the arguments passed on the command line.
  
If you have an existing test command (in a script or &lt;code&gt;Makefile&lt;/code&gt;) you can run it under &lt;code&gt;gdb&lt;/code&gt; control by prepending &lt;code&gt;gdb --args &lt;/code&gt; to it. In a &lt;code&gt;Makefile&lt;/code&gt;, you can create an empty variable called &lt;code&gt;GDB&lt;/code&gt; and use it in front of the test command:
+
If you have an existing test command (in a script or <code>Makefile</code>) you can run it under <code>gdb</code> control by prepending <code>gdb --args </code> to it. In a <code>Makefile</code>, you can create an empty variable called <code>GDB</code> and use it in front of the test command:
  
 
  GDB =
 
  GDB =
Line 29: Line 21:
 
Running
 
Running
  
  make GDB=&quot;gdb --args&quot;
+
  make GDB="gdb --args"
  
will now test the program under the control of &lt;code&gt;gdb&lt;/code&gt;.
+
will now test the program under the control of <code>gdb</code>.
  
 
== Favorite commands ==
 
== Favorite commands ==
There are many commands available within gdb. These can be discovered by using &lt;code&gt;help&lt;/code&gt; within the program, the manual page in linux, or a reference card.
+
There are many commands available within gdb. These can be discovered by using <code>help</code> within the program, the manual page in linux, or a reference card.
 
The following are my favorites:
 
The following are my favorites:
 
  c Continue running
 
  c Continue running

Latest revision as of 10:29, 24 November 2010

gdb is the GNU debugger and can be invaluble in dissecting C/C++ programs

Running gdb

To start gdb, use this at the command line:

gdb program-name

To run the program, use this inside gdb:

r args

It's also possible to specify all the arguments on the command line (giving the benefit of tab completion and filename globbing):

gdb --args program-name arg1 arg2 ...

After starting gdb like this, use the r command with no arguments:

r

gdb will then use the arguments passed on the command line.

If you have an existing test command (in a script or Makefile) you can run it under gdb control by prepending gdb --args to it. In a Makefile, you can create an empty variable called GDB and use it in front of the test command:

GDB =

check:
    $(GDB) program-name arg1 arg2

Running

make GDB="gdb --args"

will now test the program under the control of gdb.

Favorite commands

There are many commands available within gdb. These can be discovered by using help within the program, the manual page in linux, or a reference card. The following are my favorites:

c Continue running
b file:line Set breakpoint
catch throw Stop when exception is thrown
catch __cxa_throw Same again, can be more reliable
clear Clear current breakpoint
bt Show stack
up n Go up stack trace
info local
info args
p expr
q Quit!