Norman Ramsey
Norman Ramsey

Reputation: 202505

How to get valgrind to cooperate with libsigsegv?

As noted in this question about using libsigsegv to detect multiple stack overflows, I'm working with a colleague to try to detect and recover from stack overflow in an interpreter. In brief,

  1. We set up a stack-overflow handler using libsigsegv.
  2. The handler leaves via sigsegv_leave_handler(), which then returns to the interpreter's main loop via siglongjmp.

This setup successfully detects the first stack overflow, but the second stack overflow leads to a bus error. I would like to hit this problem with valgrind, but valgrind takes over at the first segfault. My question is, therefore how can I get valgrind to let libsigsegv handle the first segfault, then take over memory checking?

Upvotes: 2

Views: 623

Answers (1)

Employed Russian
Employed Russian

Reputation: 213466

Valgrind is the wrong tool to debug this problem -- you are likely suffering not from heap corruption (which is what Valgrind is great at), but from something else.

I would use GDB to debug this. When you hit the first SIGSEGV, GDB will stop. You can ask it to deliver the signal to the application with (gdb) signal SIGSEGV, at which point your interpreter will execute the siglongjmp. Eventually you'll get SIGBUS, and can debug how you got there.

Since you are likely on Linux, note that SIGBUS is rather rare, and usually results from trying to access memory that is either not mapped at all, or with wrong protections. Examining /proc/<pid>/maps at the point where SIGBUS is delivered will likely help.

Upvotes: 1

Related Questions