欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【跟我一起学gdb】(4) break-if 以及脚本的第二种使用方法#gdb ./a.out -x gdbcmd.txt

程序员文章站 2024-03-26 12:28:29
...

main.c

#include <stdio.h>

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

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

gdb脚本(break-if)

功能:当if后面的表达式成立时 执行break打断点命令

示例:

b 20 if i == 66

gdb脚本使用

[email protected]:~/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 0x4004f5: file main.c, line 20.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004f5 in main at main.c:20
	stop only if i == 66
(gdb) run
Starting program: /home/tom/dvp/a.out 

Breakpoint 1, main () at main.c:20
20			stBox.iY = i;
(gdb) p i
$1 = 66
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004f5 in main at main.c:20
	stop only if i == 66
	breakpoint already hit 1 time
(gdb) 

《完》