【跟我一起学gdb】(5)自动化gdb脚本 之 break function if expression

本文介绍了一种利用GDB自动化脚本进行条件断点设置的方法,通过具体示例展示了如何在C语言程序中,当特定函数参数满足一定条件时自动触发断点,从而简化了调试过程。

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

main.c

#include <stdio.h>

typedef struct
{
	int iW;
	int iH;
	int iX;
	int iY;
}BOX_ST;

int sum(const int a, const int b)
{
	int c = a + b;
	
	return c;
}

int main()
{
	BOX_ST stBox;
	
	for (int i = 0; i < 10000; ++i)
	{
		stBox.iW = i;
		stBox.iH = i;
		stBox.iX = i;
		stBox.iY = i;
		
		sum(i, i + 1);
	}
	
	return 0;
}

gdb自动化脚本

当函数sum的入参a的值为77时在函数sum处打断点

tom@ubuntu:~/dvp$ cat -n gdbcmd_breakif.txt
     1	b sum if a == 77
tom@ubuntu:~/dvp$ 

gdb自动化脚本的使用

tom@ubuntu:~/dvp$ gdb ./a.out -x ./gdbcmd_breakif.txt
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 ./a.out...done.
Breakpoint 1 at 0x4004e0: file main.c, line 13.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004e0 in sum at main.c:13
	stop only if a == 77
(gdb) run
Starting program: /home/tom/dvp/a.out 

Breakpoint 1, sum (a=77, b=78) at main.c:13
13		int c = a + b;
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004e0 in sum at main.c:13
	stop only if a == 77
	breakpoint already hit 1 time
(gdb) bt
#0  sum (a=77, b=78) at main.c:13
#1  0x000000000040052b in main () at main.c:29
(gdb) 

《完》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值