手动插int 3实现程序主动断点

gdb的条件断点可以让程序在满足一定条件时停下

break ... if cond

但有时程序逻辑复杂无比,或者条件复杂无比,让gdb的被动断点很难设。这时可以在程序中加中断语句来进行主动的中断。毕竟大多调试器也是靠在断点处插异常指令实现的,原理一样。

 

例:

test.c

#include 
#include 

int main()
{
    printf("hello\n");
    int very_complex_condition = true;
    if (very_complex_condition)
        __asm__ __volatile__("int $3");
    printf("world\n");
    return 0 ;
}

 

$ gcc test.c -g -o test


$ gdb ./test
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
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".
For bug reporting instructions, please see:
...
Reading symbols from /home/zjin/code/c/a.out...done.
(gdb) run
Starting program: /home/zjin/code/c/a.out
hello

Program received signal SIGTRAP, Trace/breakpoint trap.
main () at debug.c:10
10     printf("world\n");
(gdb)

 

你可能感兴趣的:(Develop)