In this article we will discuss how to use breakpoints in gdb and check backtrace etc.

How to add breakpoints in gdb

Breakpoints can be added only at “gdb” prompt i.e.

(gdb)

To add a breakpoint in running application just press the “Ctrl-C” command to get the “gdb prompt back.
Otherwise you can also add breakpoints before executing the run command.

Adding break point on a line number i.e.

(gdb) br Sample.cpp:18

(gdb) break Sample.cpp:18

(gdb) b Sample.cpp:18

“break”, “br” ,”b” all are same.

Adding break point with a function name

For global functions,

(gdb) br secondFunction

For member functions,
(gdb) br Dummy::firstFunction

Now if you have added the break point before executing “run” command just press do the run command.
(gdb) run

Otherwise if you have added the breakpoint after stopping the running program using “Ctrl-C” , then press “c” to continue the application i.e.
(gdb) c

How to check btacktrace / stacktrace using gdb

You can check the backtrace when a running code hits a breakpoint or when you stop the running application using “Ctrl-C”. But generally we check the backtrace when some break point is hit.

 

When a breakpoint is hit “gdb” prompt will come like this,

Breakpoint 1, Dummy::firstFunction (this=0x7fffffffdeb0) at Sample.cpp:18
18 mValue = mValue * 2;
(gdb)

To check the backtrace use “bt” command
(gdb) bt

It will show backtrace as follows,`
#0 Dummy::firstFunction (this=0x7fffffffdeb0) at Sample.cpp:18
#1 0x0000000000400a55 in Dummy::displayValue (this=0x7fffffffdeb0) at Sample.cpp:24
#2 0x0000000000400982 in main (argc=1, argv=0x7fffffffdfa8) at Sample.cpp:33
(gdb)


It shows the frames in stack current frame is at top i.e. #0, to go to any other frame in stack use command “f <number of frame>” i.e
(gdb) f 2


At this point you can print variables value , execute some code , also go forward and backward.

Checking Values while debugging

Use the command “print” or “p” to print the values when some break point is hit i.e.

Breakpoint 1, Dummy::firstFunction (this=0x7fffffffdeb0) at Sample.cpp:18
18 mValue = mValue * 2;
(gdb) p mValue
$1 = 3
(gdb)

Changing Values while debugging

Use the command “set” to change the values when some break point is hit i.e.

Breakpoint 1, Dummy::firstFunction (this=0x7fffffffdeb0) at Sample.cpp:18
18 mValue = mValue * 2;
(gdb) p mValue
$1 = 3
(gdb) print mValue
$2 = 3
(gdb) set mValue=9
(gdb) p mValue
$3 = 9
(gdb)

Execute Code while debugging

you can execute some code like some function when a break point is hit i.e. execute a global function ehen breakpoint is hit i.e.

(gdb) p secondFunction()

It will execute the secondFunction().

Traversal When breakpoint is hit

To traverse over the code when a break point is hit use following commands,

  • n — Runs the next line in step over mode i.e. will not go inside any function just run the next line in current function.

(gdb) n

  • s — Runs the next line in step in mode i.e. if next line is a function call then it will go inside that function.

(gdb) s

 

To list out all breakpoints use command,

(gdb) info break

To delete a breakpoint use command

d <breakpoint number>

for example,

(gdb) d 2

Thanks.