简体   繁体   English

如何在信号处理程序中获取执行上下文?

[英]How can I get execution context in a signal handler?

I am getting illegal operand error when I run a program. 我运行程序时收到illegal operand错误。 The program where the crash happens is written in assembly and linked as an object file, so I'm not sure how to use gdb with it. 发生崩溃的程序是用汇编语言编写的,并作为目标文件链接,因此我不确定如何使用gdb I noticed the fault by trapping the SIGILL signal.I want to get the address of the offending instruction. 我通过捕获SIGILL信号注意到了这个错误。我想获取有问题的指令的地址。 One method that comes to my mind is to get the last execution context in the signal handler and note down the value of rip register from it. 我想到的一种方法是在信号处理程序中获取最后一个执行上下文,并记下rip寄存器的值。 I know that context is passed to signal handlers, but don't know how. 我知道上下文传递给信号处理程序,但不知道如何。

If you do want to follow the signal handler route it is not hard to get the address of the instruction that causes the signal handler to run. 如果您确实想要遵循信号处理程序路由,那么获取导致信号处理程序运行的指令的地址并不困难。 When you create your signal handler set the SA_SIGINFO bit in the flags setting. 创建信号处理程序时,在flags设置中设置SA_SIGINFO位。 Then when you write your handler it will have access to a siginfo_t structure that includes the field si_addr which is the address of the memory location that triggered the exception. 然后,当您编写处理程序时,它将可以访问包含字段si_addrsiginfo_t结构,该字段是触发异常的内存位置的地址。

written in assembly and linked as an object file, so I'm not sure how to use gdb with it 用汇编语言编写并链接为目标文件,所以我不确定如何使用gdb

GDB is perfectly fine for debugging assembly code. GDB非常适合调试汇编代码。 You'll want info registers , x/i $pc . 你需要info registersx/i $pc

One method that comes to my mind is to get the last execution context in the signal handler and note down the value of rip register from it. 我想到的一种方法是在信号处理程序中获取最后一个执行上下文,并记下rip寄存器的值。

That's fine (see Gabriel's answer on how to get the context). 那很好(参见Gabriel关于如何获得背景的答案)。

Please note that this will not give you any additional info compared to what you get inside GDB at crash point. 请注意,与崩溃点GDB内部的内容相比,这不会给您任何额外的信息。 If you didn't know how to use GDB in the first place, then I don't see what you'll gain by getting the context in a signal handler. 如果您不知道如何首先使用GDB,那么我不会通过在信号处理程序中获取上下文来看到您将获得什么。

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

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