When you press enter in the gdb command prompt
It will execute the previous command, it will be helpful during the step input of the function.
If you have not loaded the file during invoking the gdb, use file command
$ gdb (enter)
(gdb) file add.c
Break at particular function
If you have the function name my_func(), then you can break at the my_func() using
(gdb) break my_func
Compiling
Compiling the C program with debugging information
gcc -g hello.c –o hello
-g flag is used for inclusion of debugging information.
Execution
In order to debug the code, use the following
gdb hello
Commands
b 🡪 break point
b main 🡪 break at main
q 🡪 quit
r 🡪 run the program
c 🡪 continue the program execution to the next breakpoint
bt 🡪 print the stack trace
u 🡪 go up a level in stack
d 🡪 go down a level in stack
p 🡪 print the variable value
print/x my-var 🡪 print the variable value in hexa decimal
Breakpoints & Watchpoint
b main
put breakpoint at main
break 45
insert break point at 45th line
break myfunction
insert break point to myfunction
delete N
delete Nth breakpoint
watch x
It pauses the program, whenever watched variable value is changed.
watch x==3
wait till x variable’s value become 3
Stepping through Code
| 🡪 list 10 lines of source
|50 🡪 display the 50th line
n, next 🡪 step in
s, step 🡪 step into function
f, finish 🡪 stop out function
Variables
print the value of variable 🡪 print x
Set the variable 🡪 set x=3
Command line Arguments
In order to give the command line arguments through gdb, use the following
Gdb filename
b main // put breakpoint in main
No comments:
Post a Comment