1

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.

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.

Community
  • 1
  • 1
tonysdg
  • 1,335
  • 11
  • 32

1 Answers1

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);

  ...
}
pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • I'm not sure I follow - I'm looking to suspend the thread in the first place, not wake it up. Would you mind elaborating please? – tonysdg Dec 15 '15 at 20:15
  • Where you'd want to `sigsuspend`, `read(pipefd, ...)` instead. Is the modified answer clearer? You can keep the thread suspended as long as you like, and wake it up by writing to the other end of the pipe. – pilcrow Dec 15 '15 at 20:21