回溯()故障(SIGSEGV)信号处理过程中的功能 [英] backtrace() function during fault (SIGSEGV) signal handler

查看:442
本文介绍了回溯()故障(SIGSEGV)信号处理过程中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读(看到这里)认为,普遍做法,以使用打印堆栈跟踪回溯()故障信号处理过程中(如操作时 SIGSEGV ),在Linux下是:

I have read (see here) that "common practice" to print a stack trace using backtrace() during a fault signal handler (e.g. when handling SIGSEGV) under Linux is to:

1获得从无证 sigcontext <指令指针( EIP RIP ) / code>结构。

1 Get the instruction pointer (EIP or RIP) from the undocumented sigcontext structure.

2更换与指令指针堆栈跟踪第二帧,因为第一帧是信号处理程序,而第二个框架应该是在的libc 中在的sigaction code,这已经覆盖了发生故障的原始帧。

2 Replace the 2nd frame in the stack trace with the instruction pointer, since the first frame is the signal handler, and the 2nd frame is supposed to be within libc in the sigaction code, which has overwritten the original frame in which the fault occurred.

3打印回溯来自新更换的第2帧开始。

3 Print the backtrace starting from the newly replaced 2nd frame.

在我看来,在我的测试(在 x86_64的 2.6内核),其中发生了故障,其实原来的帧在堆栈跟踪present给出通过回溯()在第3帧 - 第一个是信号处理程序和第二点是在的libc 的信号处理$ C $℃。

It seems to me in my testing (on x86_64 2.6 kernel) that in fact the original frame in which the fault occurred is present in the stack trace given by backtrace() in the 3rd frame - the first is the signal handler and the 2nd is in libc signal handling code.

这是改变内核的信号处理记录的地方,你可以为我参考一下?

Is this change in kernel signal handling documented somewhere that you can reference for me?

在我看来,该结果是你能避免从指令指针替换任何框架,以及刚刚从<$ ​​C $ C>打印堆栈跟踪回溯()开始框3 ,但我想确认,这是已知的行为和做正确的方式。

It seems to me that the upshot is that you can avoid replacing any frames from the instruction pointer, and just print the stack trace from backtrace() starting with frame 3, but I want confirmation that this is known behavior and the correct way to do it.

推荐答案

这是尝试做一件有趣的事情,但它不是真正的便携,很可能将永远是100%可靠。因此,只要实现它,你说对了,如果你的平台上工作,并包括其一些小的单元测试,让你立刻知道如果一些你在未来使用的系统不相同的方式工作。毕竟,调用此code时,你已经完蛋了,所以只是做最好的,你可以和待着。

This is an interesting thing to try to do, but it's not really portable and probably will never be 100% reliable. So just implement it the way you say, if that works on your platform, and include a couple little unit tests for it so that you know right away if some system you use in the future doesn't work the same way. After all, when this code is invoked, you're already screwed, so just do the best you can and move along.

一个完全不同的另一种是可以在任,而不是你的计划的同时也可以使用,是写在程序的核心转储将被Linux调用的脚本。这个脚本就可以在批处理模式下的核心文件运行GDB得到回溯和您发送一封电子邮件或什么的。

A totally different alternative which is possible to use either at the same time or instead of your scheme, is to write a script to be invoked by Linux when a program dumps core. This script can then run gdb in batch mode on the core file to get the backtrace and send you an email or whatever.

这篇关于回溯()故障(SIGSEGV)信号处理过程中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆