gdb的條件斷點可以讓程式在滿足一定條件時停下
break ... if cond
但有時程式邏輯複雜無比,或者條件複雜無比,讓gdb的被動斷點很難設。這時可以在程式中加中斷語句來進行主動的中斷。畢竟大多調試器也是靠在斷點處插異常指令實現的,原理一樣。
例:
test.c
#include <stdio.h>#include <stdbool.h>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 <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".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
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)