COMP1411. A Tutorial on GDB
1. What is GDB
Copyright By PowCoder代写 加微信 powcoder
2. how to install GDB
3. how to use GDB
What is GDB?
GDB, the GNU Project debugger, allows you to see what is going on `inside’ another program while it executes — or what another program was doing at the moment it crashed.
When to use a debugger
Debugging is something that can’t be avoided. Every programmer will at one point in their programming career have to debug a section of code. There are many ways to go about debugging, from printing out messages to the screen, using a debugger, or just thinking about what the program is doing and making an educated guess as to what the problem is.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
Start your program, specifying anything that might affect its behavior.
Make your program stop on specified conditions.
Examine what has happened, when your program has stopped.
Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
What Languages does GDB Support?
Objective-C
Install GDB
1. directly use the command
2. download the source code of GDB and install it
Tutorial link
http://www.gdbtutorial.com/tutorial/how-install-gdb
1. Install pre-built gdb binaries from verified distribution resources
You can install gdb on Debian-based linux distro (e.g. Ubuntu, Mint, etc) by following command.
$ sudo apt-get update
$ sudo apt-get install gdb
2. Download source code of GDB, compile it and install.
$ wget http://ftp.gnu.org/gnu/gdb/gdb-7.11.tar.gz
$ tar -xvzf gdb-7.11.tar.gz
$ cd gdb-7.11
gdb-7.11$ ./configure
gdb-7.11$ make
$ make install
gdb –version
How to use
In order to run a c program with GDB,
we must compile it with the -g option which tells the compiler to embed debugging information for the debugger to use.
gcc –g c_source_code.c –o output_file
Basic commands
list [line#]
Prints lines from the source code around line#.
If we give it a function name as the argument function, it prints lines from the beginning of that function.
If we give it no argument, it prints lines around the break point
The “run” command starts the program. If we do not set up any “breakpoints” (we’ll see how to use this command later) the program will run until it terminates or core dumps
Set breakpoint
b function name
We can retrieve the values of all variables we’re interested in. To do this we use the “print” command.
n (for “next”)
This executes the current command, and moves to the next command in the program.
s (for “step”)
This steps through the next command. There are differences between step and next. If you are at a function call, and you hit next, then the function will execute and return. But if you hit step, then you will go to the first line of that function.
delete [n]
With no argument, deletes all breakpoints that we have set.
Deletes break point number n.
clear function_name
Deletes the breakpoint set in that function.
Command Description
r Start running program until a breakpoint or end of program
b fun Set a breakpoint at the begining of function “fun”
b N Set a breakpoint at line number N of source file currently executing
b file.c:N Set a breakpoint at line number N of file “file.c”
d N Remove breakpoint number N
info break List all breakpoints
c Continues/Resumes running the program until the next breakpoint or end of program
f Runs until the current function is finished
s Runs the next line of the program
s N Runs the next N lines of program
n Like s, but it does not step into functions
p var Prints the current value of the variable “var”
set var=val Assign “val” value to the variable “var”
bt Prints a stack trace
q Quit from gdb
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com