A Simple Guide to Using GDB Debugger

Original: https://blog.csdn.net/fangye945a/article/details/85254627

When developing C/C++ projects on the ARM and Linux platforms, you will inevitably encounter some bugs in your programs. Small projects are manageable, but for large projects with extensive code, it becomes challenging to pinpoint issues. This is where using the gdb debugger can help you easily locate code bugs.

Previously, I shared information about using GDB:

GDB Debugger Is Actually That Simple

A Step-by-Step Guide to Using VSCode + gdb + gdbserver to Debug ARM Programs

These can be read in conjunction.

1. Usage Method

Below is an example code:

A Simple Guide to Using GDB Debugger

When compiling the code, add the debug option -g, as shown below:

gcc -g test.c -o test

Use the gdb debugger to run the program: gdb <executable name>

A Simple Guide to Using GDB Debugger

2. Controlling gdb

1. (gdb) l(L) to view the source code, (gdb) L 1 to start viewing from the first line.

A Simple Guide to Using GDB Debugger

2. (gdb) r(run) to run the code from the beginning, automatically returning to the gdb command line upon hitting set breakpoints, segmentation faults, or program termination.

A Simple Guide to Using GDB Debugger

3. To run code with parameters, add them after r. For example: (gdb) r arg1 arg2

A Simple Guide to Using GDB Debugger

4. (gdb) b n to set a breakpoint at line n (the program will pause at this position).

A Simple Guide to Using GDB Debugger

5. (gdb) n to execute the next line of code (will not enter functions).

A Simple Guide to Using GDB Debugger

6. (gdb) s(step) to execute the next line of code (will enter functions and expand them).

A Simple Guide to Using GDB Debugger

7. (gdb) p(print) <expression> to print the value of an expression, which can be any valid C language expression, such as a variable, number, function call, etc.

A Simple Guide to Using GDB Debugger

8. (gdb) c(continue) to continue running the program, automatically returning to the gdb command line upon hitting set breakpoints, segmentation faults, or program termination.

A Simple Guide to Using GDB Debugger

9. (gdb) bt or where to display the program stack information, generally used when encountering segmentation faults.

A Simple Guide to Using GDB Debugger

10. (gdb) q and then input y to exit gdb debugging.

A Simple Guide to Using GDB Debugger

11. While the program is running, input ctrl+c to return to gdb debugging mode, and input (gdb) signal <signal> to send a signal to the program.

A Simple Guide to Using GDB Debugger

12. (gdb) layout split to display the source code and assembly window, allowing you to see the breakpoint location and debug the code more intuitively.

A Simple Guide to Using GDB Debugger

3. Related Links

Program Compilation:

https://blog.csdn.net/csdn_kou/article/details/81407195

More gdb Usage:

https://blog.csdn.net/yimingsilence/article/details/72153049

http://www.cnblogs.com/qigaohua/p/6077790.html

Debugging Multi-file Programs:

https://blog.csdn.net/niepangu/article/details/52887803

https://blog.csdn.net/timsley/article/details/51000667

Debugging Multi-threaded Programs:

http://www.cnblogs.com/lsgxeva/p/8078670.html

Copyright Statement: This article is sourced from the internet, freely conveying knowledge, and the copyright belongs to the original author. If there are any copyright issues, please contact me for deletion.

You May Also Like:

Practical | How to Remotely Log In to Development Boards?

Practical | Teach You to Build an Embedded Web Server in 10 Minutes

Practical | Teach You to Turn on Lights via Webpage in 10 Minutes

GDB Debugger Is Actually That Simple

A Step-by-Step Guide to Using VSCode + gdb + gdbserver to Debug ARM Programs

Reply with 1024 in the WeChat public account chat interface to get embedded resources; reply m to view the article summary.

Leave a Comment