手動插int 3實現程式主動斷點

來源:互聯網
上載者:User

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)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.