user1833028
user1833028

Reputation: 943

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!

Upvotes: 1

Views: 714

Answers (1)

Employed Russian
Employed Russian

Reputation: 213646

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 (e.g. 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/

Upvotes: 1

Related Questions