This is a bit of a follow-up to this question.
这是对这个问题的一个后续行动。
Suppose I use sigaction
to set up a signal handler of my own design. In this signal handler, I access information about the CPU state (the thread's stack pointer, for example) and copy it to a predetermined location in memory. I now want to examine a value on this thread's stack from another thread.
假设我使用sigaction来设置我自己设计的信号处理程序。在此信号处理程序中,我访问有关CPU状态的信息(例如,线程的堆栈指针)并将其复制到内存中的预定位置。我现在想要从另一个线程检查此线程堆栈上的值。
Is there a way to suspend execution of the thread in the signal handler from within the same signal handler, so that I can safely examine this thread's stack from another thread? (Note: I define "safely" here to mean "without worrying about the thread completing or returning from a function.)
有没有办法在同一个信号处理程序中暂停信号处理程序中线程的执行,这样我就可以安全地从另一个线程检查这个线程的堆栈了? (注意:我在这里定义“安全”是指“不必担心线程完成或从函数返回。”
If I was outside of the signal handler, I could use sigsupend
; however, it's not safe to use within the signal handler according to the GNU documentation. I could also try extending a signal using the method described in this question, but I don't think any of the standard *nix signals will help me here.
如果我在信号处理程序之外,我可以使用sigsupend;但是,根据GNU文档在信号处理程序中使用是不安全的。我也可以尝试使用此问题中描述的方法扩展信号,但我认为任何标准的* nix信号都不会对我有所帮助。
1 个解决方案
#1
2
A blocking read
on a pipe is async-signal-safe, and provides a convenient way to wake up your "suspended" thread.
管道上的阻塞读取是异步信号安全的,并提供了一种方便的方法来唤醒“挂起”线程。
E.g.,
例如。,
static int the_pipe[2] = {-1, -1}; // file descriptors allocated in main via pipe()
static void
siginfo_handler(int s, siginfo_t *si, void *ctx) {
char c;
// do something with (ucontext_t *)ctx
// now wait for someone else to wake us up
read(the_pipe[0], &c, 1);
...
}
#1
2
A blocking read
on a pipe is async-signal-safe, and provides a convenient way to wake up your "suspended" thread.
管道上的阻塞读取是异步信号安全的,并提供了一种方便的方法来唤醒“挂起”线程。
E.g.,
例如。,
static int the_pipe[2] = {-1, -1}; // file descriptors allocated in main via pipe()
static void
siginfo_handler(int s, siginfo_t *si, void *ctx) {
char c;
// do something with (ucontext_t *)ctx
// now wait for someone else to wake us up
read(the_pipe[0], &c, 1);
...
}