Linux系统中GDB + gcc 使用方法

本文详细介绍GDB的使用方法,包括gcc编译、断点设置、变量查看等基本操作,以及如何处理编译警告。同时,文章还介绍了GDBServer的远程调试原理,适用于资源受限的嵌入式系统。

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

简单使用

被调试程序

// filename testgdb.c

#include <stdio.h>
	
int func(int n);

int main() {
	int i;
	long result = 0;
	
	for (i = 1; i < 100; ++i)
		result += i;

	printf("result[1-99] = %d \n", result);
	printf("result[1-49] = %d \n", func(50));

	return 0;
}

int 
func(int n) {
	int sum = 0, i;
	
	for (i = 0; i < n; ++i)
		sum += i;
		
	return sum;
}

gcc编译文件

gcc -g testgdb.c -o testgdb

上述编译过程可能会出现一个warning, 如下:

testgdb.c: In function ‘main’:
testgdb.c:13:9: warning: format ‘%d’ expects argument of type ‘int’, 
but argument 2 has type ‘long int’ [-Wformat=]
  printf("result[1-99] = %d \n", result);

不过无伤大雅, 直接掠过.

GDB简单调试

输入: gdb testgdb

lius@pix:~/study/gdb$ gdb testgdb 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testgdb...done.
(gdb) 

输入:

	l		显示程序内容
break n		在第n行设置断点
info break	显示所有断点行数
	r		运行程序到断点
	n		逐步运行
	c		运行下一个断点
	p i		显示变量i的内容
	q		离开

运行结果如下:

lius@pix:~/study/gdb$ gdb testgdb 
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testgdb...done.
(gdb) l
1	#include <stdio.h>
2	
3	
4	int func(int n);
5	
6	int main() {
7		int i;
8		long result = 0;
9		
10		for (i = 1; i < 100; ++i)
(gdb) break func
Breakpoint 1 at 0x400591: file testgdb.c, line 21.
(gdb) break 10
Breakpoint 2 at 0x400536: file testgdb.c, line 10.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400591 in func at testgdb.c:21
2       breakpoint     keep y   0x0000000000400536 in main at testgdb.c:10
(gdb) r
Starting program: /home/lius/study/gdb/testgdb 

Breakpoint 2, main () at testgdb.c:10
10		for (i = 1; i < 100; ++i)
(gdb) n
11			result += i;
(gdb) c
Continuing.
result[1-99] = 4950 

Breakpoint 1, func (n=50) at testgdb.c:21
21		int sum = 0, i;
(gdb) p i
$1 = 32767
(gdb) q
A debugging session is active.

	Inferior 1 [process 5140] will be killed.

Quit anyway? (y or n) 

GDBServer

因为嵌入式资源有限, GDB无法安装到开发板上, 所以可以利用GDBServer进行远程调试.

  • 宿主机GDB 与 目标机stub (插窗) 组成;
  • 两者通过串口或者TCP连接 (网线);
  • 对目标机上的系统内核, 上层应用进行监控与调试;

后续补充…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值