C++获取“分段错误”的文件名和行号Linux x64
不运行GDB,而只运行程序本身。
编辑:我已经见过这样的程序,但我不知道自己该怎么做:(
Without running GDB, but only the program itself.
edit: i've already seen such program but i can't figure how to do that myself :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决这个问题的汤米值得赞扬。
我通过使用
中有点标准的backtrace
实用程序将他的答案与 GDB 分离。您可以尝试打开文件并写入而不是
STDERR_FILENO
,但我会避免在崩溃的进程中进行如此繁重的工作。在我的系统上,输出如下所示:
所以,它没有给出文件名+行号,但它确实给出了函数名+代码偏移量,您可以很容易地翻译它。
Tommie deserves credit for solving this.
I've detached his answer from GDB by using the somewhat standard
backtrace
utility from<execinfo.h>
.You can try opening a file and writing instead of
STDERR_FILENO
, but I'd avoid such heavy lifting in a crashed process.On my system, the output looks like this:
So, it doesn't give file name + line number, but it does give function name + code offset, which you can translate easily enough.
嗯...您可以尝试这样做...
当捕获分段错误时,会调用 dumpstack 并打印直到分段错误并被重定向时的最新堆栈跟踪到一个带有进程数字pid的文件......
Hmmm... you could try doing it this way...
When a segmentation fault is trapped,
dumpstack
gets invoked and prints the most recent stack-trace up to the point when it segmentation faulted and gets redirected to a file with a numeric pid of the process....为什么你不想运行 gdb?如果使用它,您可以非常轻松地检测段错误的位置。
Why exactly do you not want to run gdb? You can detect the location of segfaults very easily if you use it.