gdb教程:1. How do I use gdb?

本文介绍了如何使用gdb进行程序调试,包括添加调试符号、启动调试器、运行及重启程序、退出调试器等基本操作,并提供了获取帮助的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. How do I use gdb?

When you compile your program, you must tell the compiler to produce a program that is compatible with the debugger. The debugger needs special information to run properly. To do this, you must compile your program with the debugger flag, -g. This step is critical. Without it, the debugger won't have the program symbol information. That means it won't know what your functions and variables are called, and it won't understand when you ask it about them.

 

1.1 How do I compile with debugging symbols?

Pass the -g flag to your compiler:

prompt > gcc -g program.c -o programname

NOTE: If you have a larger program with several files, each must be compiled with the -g flag, and it must also be set when you link.


1.2 How do I run programs with the debugger?

First start the debugger with your program name as the first argument.

prompt> gdb programname

Next use the run command in gdb to start execution. Pass your arguments to this command.

(gdb) run arg1 "arg2" ...


1.3 How do I restart a program running in the debugger?

Use the kill command in gdb to stop execution. The you can use the run command as shown above to start it again.

(gdb) kill
Kill the program being debugged? (y or n) y
(gdb) run ...


1.4 How do I exit the debugger?

Use the quit command.

(gdb) quit

NOTE: You may be asked if you want to kill the program. Answer yes.

(gdb) quit
The program is running. Exit anyway? (y or n) y
prompt >


1.5 How do I get help on debugger commands?

Use the help command. Gdb has a description for every command it understand, and there are many, many more then this tutorial covers. The argument to help is the command you want information about. If you just type "help" with no arguments, you will get a list of help topics similar to the following:

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值