简体   繁体   English

使用 GDB:我崩溃了(段错误)。 我怎样才能看到通向它的代码行?

[英]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.它是用 C 编写的,我一直在使用 GDB 来调试它。 In the past I have been hitting n hundreds of times, and there is something to be said for this.过去我已经打了n次数百次,对此有话要说。 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) (当然现在: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.过去我已经打了n次数百次,对此有话要说。

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:如果您的崩溃是可重复的(例如,它总是在第 1234 次调用foo()之后崩溃),那么这里有一个有用的技术来避免命中n数百次:

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

At this point, your program runs and crashes on N-th call to foo .此时,您的程序在第 N 次调用foo运行并崩溃。 Use info break to find out what N is.使用info break找出 N 是什么。 And now:现在:

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

Now you are stopped on penultimate call to foo .现在您在倒数第二次调用foo停止。 Step through your code, set breakpoints, etc. until you get to the next call to foo (which will crash).单步执行您的代码,设置断点等,直到您进行下一次对foo调用(这将崩溃)。

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/在 Linux 上你可以这样做: https : //rr-project.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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