0% found this document useful (0 votes)
28 views

GDB Guide

Uploaded by

mir.inzamam200
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

GDB Guide

Uploaded by

mir.inzamam200
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

GDB Guide

1 Introduction
This document serves as a guide on how to use several gdb commands. The commands listed in this document
are the ones we believe you will use the most frequently in CSCI 0300. There are many other gdb commands
not included in this guide which you can read about online or by typing help inside gdb.

2 Notation
The notation we will use for commands in this document is [c]ommand (args), where “command” is the com-
mand name and the abbreviated version of commands is listed in brackets (in this case “c”).

3 Getting Started
Say you have the program basic.c, which you want to run in gdb. First you must compile basic.c into an
executable. To run it in gdb, use the following command in your terminal:
1 gdb b a s i c

Now you should be inside gdb! You can use the command layout src to view the source file while debug-
ging.

4 Breakpoints
Breakpoints are used in gdb to stop a program whenever a certain point in the program (the breakpoint) is
reached. Breakpoints are particularly useful for debugging.
1 ( gdb ) [ b ] r e a k main ( s e t b r e a k p o i n t a t f u n c t i o n main )
2 ( gdb ) [ b ] r e a k 101 ( s e t b r e a k p o i n t a t l i n e 101 w i t h i n f i l e )
3 ( gdb ) [ b ] r e a k h e l p e r . c :101 ( s e t b r e a k p o i n t a t f i l e h e l p e r . c , l i n e 101)

You can use the info command to get information about the breakpoints you have set and the breakpoint
numbers they correspond to. You can also delete breakpoints with the delete command.
1 ( gdb ) [ i ] nfo b r e a k p o i n t s ( show a l l b r e a k p o i n t s )
2 ( gdb ) [ d ] e l e t e 1 ( d e l e t e b r e a k p o i n t 1)
3 ( gdb ) [ d ] e l e t e ( delete a l l breakpoints )

5 Running and Stopping a Program


To commence execution, you can use the command run followed by any arguments that might be required to
run the program.
1 ( gdb ) [ r ] un a r g s ( s t a r t running program )

The program will pause execution at the first breakpoint encountered. You could step through the lines in
your program using next or step or you can continue running the program until the next breakpoint using
continue.
1 ( gdb ) [ c ] o n t i n u e ( continue to the next breakpoint )
2 ( gdb ) [ n ] e x t ( c o n t i n u e t o n e x t l i n e , s t e p s over f u n c t i o n c a l l s )
3 ( gdb ) [ s ] t e p ( continue to next li n e , s t e p s i n t o f u n c t i o n c a l l s )

You can stop your program execution using CTRL+C and you can quit gdb using the quit command.
1 ( gdb ) CTRL+C ( s t o p program e x e c u t i n g )
2 ( gdb ) [ q ] u i t ( q u i t s GDB)

1
6 More Debugging
A backtrace is a summary of how your program got where it is. It prints a stack trace starting from the current
frame of execution.
1 ( gdb ) [ b ] ack [ t ] r a c e ( display stack backtrace )

The information command is useful to learn more about the frame. (Note: it can also be used to list
information about several other things, such as functions, registers, etc.)
1 ( gdb ) [ i ] nfo frame ( p r o v i d e i n f o r m a t i o n about c u r r e n t frame )

You can use the frame command to inspect a frame with a given number.
1 ( gdb ) frame i ( i n s p e c t frame i )

7 Viewing Data
The print command is useful to display variable names (from the current scope), memory addresses, registers,
and constants.
1 ( gdb ) [ p ] r i n t <e x p r e s s i o n > ( p r i n t s the value that expression evaluates to )
2 ( gdb ) [ p ] r i n t / x <e x p r e s s i o n > ( same as above , but p r i n t s i n hexadecimal )

8 Multiple Threads and Processes


Debugging multi-process programs requires a few other commands. The default behavior of GDB is to not
follow the execution of child processes after they are started with fork. As such, if you set a breakpoint on a
line that executes within a child process, GDB won’t break on that line. However, you can change this behavior
by running the GDB command set-follow-fork-mode child.
1 ( gdb ) s e t −f o l l o w −f o r k−mode c h i l d ( follows execution of child process )

If your program involves multiple threads you can gain information on the current state of your threads
running the command info threads. You can use the command thread [number] to inspect the current state
of that thread and follow its execution.
1 ( gdb ) i n f o t h r e a d s ( a numbered l i s t o f e v e r y c u r r e n t l y running t h r e a d )
2 ( gdb ) t h r e a d i ( i n s p e c t c u r r e n t s t a t e o f t h r e a d i and f o l l o w i t s e x e c u t i o n )

To execute commands across multiple threads you can use thread apply all <command> to apply a given
command to all threads. For example:
1 ( gdb ) t h r e a d a p p l y a l l b t ( g e t a s t a c k t r a c e o f e v e r y running t h r e a d )

9 FAQ
Q1. GDB gives me the error message: “not in executable format: File format not recognized”
You want to make sure that you have compiled your program into an executable, and then run gdb on
that executable.
Q2. When should I set a breakpoint?
If you know that one particular function is causing a segmentation fault, then you can set a breakpoint
on that particular function. If you don’t know where to start setting breakpoints, the best place to set it
would be on main, or if you are working with a test suite, you can set it on the function that is testing
your code.
Q3. Why does layout src not work?
Make sure that you have run the program using the r command and that you have set breakpoints. If
layout src does not work, it might mean that the breakpoints weren’t reached and you have exited or the
program terminated due to a segmentation fault.

Q4. When I run gdb, my interface doesn’t look like it should, what should I do?
Type Ctr-L.

2
Q5. Why does running the program just exit the program?
Make sure that you have set the breakpoint properly! Or the breakpoint that you set is reachable. If you’re
running it and the program just exits normally, it could mean that you fixed the bug!
Q6. When should I use the command “bt”?
You might want to use bt to learn about where a segmentation fault was caused.

You might also like