Commonly used gdb commands

1. Starting up gdb: gdb <Executable name with debugging symbols>
If you run only gdb, you can still specify file name using the 'file' command
$gdb
(gdb) file </path/to/executable>

2. Help of command: To get help of a particular command use "help" command
(gdb) help [command]

3. Running the program: 
(gdb) run

4. Setting breakpoints: Breakpoints are used to stop the program run in the middle, at a designated point.
(gdb) break file1.c:10
The above command puts breakpoint at line 10 of file1.c
(gdb) break <func_name>
The above command puts breakpoint at the starting of this function

5. Continue the Program: After breakpoint, if you type 'run' it will start executing from beginning, instead of that to continue from the breakpoint, use "continue". Program will run until the next breakpoint or program termination
(gdb) continue

6. Single Stepping: After breakpoint, if you want to single step to next line, instead of continuing the program until it reaches the next breakpoint use 'step'
(gdb) step

There is also one more command "next". The difference between "step" and "next" is next will not jump into a sub-routine/function and start executing inside the function. It will treat the sub function as a single instruction.

7. Repeat previous command: Using 'Enter' key you can repeat the previous command you have executed instead of typing the whole command again

8. Printing the values of variables: Whenever at breakpoints, if you want to look at values of variables use 'print' command
(gdb) print ch
(gdb) print/x ch

The print/x will print the value in hexadecimal

9. Setting Watchpoints: Breakpoints interrupt the program at a particular line or function, whereas watchpoints act on variable. Whenever the value of the watched variable is changed the program is paused.

(gdb) watch ch

Whenever the value of ch changes, the program will stop and print out the old and new values.

10. Backtrace: Produces a stack trace of the function calls
(gdb) backtrace

11. finish: Runs until the current function is finished
(gdb) finish

12. Delete breakpoints: Use 'delete' command to delete breakpoints
(gdb) delete 1
The above commands deletes the first breakpoint

13. Show all breakpoints: Use 'info breakpoints' command to show about all declared breakpoints
(gdb) info breakpoints

14. Conditional Breakpoint: Used when you want to break the execution of a program when a particular condition occurs. This helps in avoiding all the unnecessary stepping etc
(gdb) break file1.c:6 if i>=5
The above command sets a breakpoint at line 6 of file1.c which will break the program only when the value of i is greater than or equal to 5

15. Printing Structure Variables: You can also print structure variables using 'print' command
(gdb) print test_struct->name
The above command will print the name variable of the test_struct which is a pointer

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto