How can I get GDB to tell me what address caused a segfault?

前端 未结 2 473
滥情空心
滥情空心 2020-12-07 17:24

I\'d like to know if my program is accessing NULL pointers or stale memory.

The backtrace looks like this:

Program received signal SIGSEGV, Segmentation f         


        
相关标签:
2条回答
  • 2020-12-07 18:23

    With GDB 7 and higher, you can examine the $_siginfo structure that is filled out when the signal occurs, and determine the faulting address:

    (gdb) p $_siginfo._sifields._sigfault.si_addr
    

    If it shows (void *) 0x0 (or a small number) then you have a NULL pointer dereference.

    0 讨论(0)
  • 2020-12-07 18:24

    Run your program under GDB. When the segfault occurs, GDB will inform you of the line and statement of your program, along with the variable and its associated address.

    You can use the "print" (p) command in GDB to inspect variables. If the crash occurred in a library call, you can use the "frame" series of commands to see the stack frame in question.

    0 讨论(0)
提交回复
热议问题