简体   繁体   中英

Using GDB: I crash (segfault). How can I see the lines of code leading up to it?

Forgive me if this is a duplicate - but I can't seem to find any straightforward advice on this.

I have an application that executes a loop a great many times. At some some point, this segfaults. It's written in C, I've been using GDB to debug this. In the past I have been hitting n hundreds of times, and there is something to be said for this. However, I think it would be VASTLY more efficient in the present case if I could actually step backwards to see where the fault happens.

Unfortunately, library code is being cited in the crash (from a precompiled library if I recall correctly) so I can't even attempt to look at code going backwards. Moreover, I trust this library. (For now of course :P)

I would be immensely grateful if someone could provide a few ways to trace backwards to lines of code in my files where this crash happens!

In the past I have been hitting n hundreds of times, and there is something to be said for this.

If your crash is repeatable (eg it always crashes after 1234-th call to foo() ), then here is a useful technique to avoid hitting n hundreds of times:

(gdb) break foo
(gdb) ignore 1 10000
(gdb) run

At this point, your program runs and crashes on N-th call to foo . Use info break to find out what N is. And now:

(gdb) ignore 1 M  # where M == N-1
(gdb) run

Now you are stopped on penultimate call to foo . Step through your code, set breakpoints, etc. until you get to the next call to foo (which will crash).

However, I think it would be VASTLY more efficient in the present case if I could actually step backwards to see where the fault happens.

On Linux you could do that: https://rr-project.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM