[go: up one dir, main page]

0% found this document useful (0 votes)
9 views13 pages

KM GDB

GDB, or GNU Debugger, is a powerful tool for debugging programs written in languages like C and C++, allowing users to inspect program execution and identify errors such as segmentation faults. To use GDB effectively, programs must be compiled with the -g option for debugging support, and users can set breakpoints, run commands, and inspect variable values during execution. Additional commands like 'watch' and 'backtrace' enhance debugging capabilities by monitoring variable changes and tracing function calls.

Uploaded by

20 SNEHA 5011 M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

KM GDB

GDB, or GNU Debugger, is a powerful tool for debugging programs written in languages like C and C++, allowing users to inspect program execution and identify errors such as segmentation faults. To use GDB effectively, programs must be compiled with the -g option for debugging support, and users can set breakpoints, run commands, and inspect variable values during execution. Additional commands like 'watch' and 'backtrace' enhance debugging capabilities by monitoring variable changes and tracing function calls.

Uploaded by

20 SNEHA 5011 M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

GDB

What is GDB?
• “GNU Debugger”
• A debugger for several languages, including C and C++
• It allows you to inspect what the program is doing at a certain point
during execution.
• Errors like segmentation faults may be easier to find with the help
of gdb.
Additional step when compiling program
• Normally, you would compile a program like:
gcc [flags] <source files> -o <output file>
For example:
gcc hello.c -o hello.x
• Now you add a -g option to enable built-in debugging support
(which gdb needs):
gcc [other flags] -g <source files> -o <output file>
For example:
gcc -g hello.c -o hello_debug
Starting up gdb
• Just try “gdb” or “gdb a.out.” You’ll get a prompt that looks like this:
(gdb)
• If you didn’t specify a program to debug, you’ll have to load it in
now:
(gdb) file a.out
Here, a.out is the program you want to load, and “file” is the
command to load it.
gdb help
• gdb has an interactive shell, much like the one you use as soon as
you log into the linux grace machines. It can recall history with the
arrow keys, auto-complete words (most of the time) with the TAB
key, and has other nice features.

(gdb) help [command]


Running the program
• To run the program, just use:
(gdb) run
• This runs the program.
• If it has no serious problems (i.e. the normal program didn’t get a
segmentation fault, etc.), the program should run fine here too.
• If the program did have issues, then you (should) get some useful
information like the line number where it crashed, and parameters to the
function that caused the error:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400524 in sum array region (arr=0x7fffc902a270, r1=2, c1=5,
r2=4, c2=6) at sum-array-region2.c:12
Setting breakpoints
• Breakpoints can be used to stop the program run in the middle, at
a designated point. The simplest way is the command “break.”
• This sets a breakpoint at a specified file-line pair:
(gdb) break file1.c:6
• This sets a breakpoint at line 6, of file1.c. Now, if the program ever
reaches that location when running, the program will pause and
prompt you for another command.
• You can set as many breakpoints as you want, and the program
should stop execution if it reaches any of them.
More fun with breakpoints
• You can also tell gdb to break at a particular function. Suppose you
have a function my func:
int my func(int a, char *b);
• You can break anytime this function is called:
(gdb) break my func
gdb commands …
• Once you’ve set a breakpoint, you can try using the run command again. This
time, it should stop where you tell it to (unless a fatal error occurs before
reaching that point).
• You can proceed onto the next breakpoint by typing “continue” (Typing run
again would restart the program from the beginning, which isn’t very useful.)
(gdb) continue
• You can single-step (execute just the next line of code) by typing “step.” This
gives you really fine-grained control over how the program proceeds. You can
do this a lot...
(gdb) step
gdb commands …
• Similar to “step,” the “next” command single-steps as well, except
this one doesn’t execute each line of a sub-routine, it just treats it
as one instruction.
(gdb) next
• Typing “step” or “next” a lot of times can be tedious. If you just
press ENTER, gdb will repeat the same command you just gave it.
You can do this a bunch of times.
gdb commands …
• So far you’ve learned how to interrupt program flow at fixed, specified
points, and how to continue stepping line-by-line. However, sooner or later
you’re going to want to see things like the values of variables, etc. This might
be useful in debugging.
• The print command prints the value of the variable specified, and print/x
prints the value in hexadecimal:
(gdb) print my var
(gdb) print/x my var
Setting watch points
• Whereas breakpoints interrupt the program at a particular line or
function, watch points act on variables. They pause the program
whenever a watched variable’s value is modified.
For example, the following watch command:
(gdb) watch my var
• Now, whenever my var’s value is modified, the program will
interrupt and print out the old and new values.
Other useful commands
• backtrace - produces a stack trace of the function calls that lead to a seg fault
(should remind you of Java exceptions).
• where - same as backtrace; you can think of this version as working even
when you’re still in the middle of the program
• finish - runs until the current function is finished
• delete - deletes a specified breakpoint
• info breakpoints - shows information about all declared breakpoints.

You might also like