Is it possible to pause C program inside SIGSEGV handler for further investigation?

Viewed 85

I'm currently preparing my application for more real-world testing in order to catch all super rare and hard to reproduce edge cases. I've already installed SIGSEGV handler that prints backtrace to log file however it doesn't provide all data I would like to get.

I would prefer my program to stop execution (something like sending -STOP signal) when program crashes, so that I could attach QtCreator with gdb to it while it's still alive and check current variables in all threads active during crash, across whole stacktrace.

void MessFS_sigsegvHandler(int sig) {
  void *array[20];
  size_t size;

  size = backtrace(array, 20);
  MessLog_log(MESS_LOG_ERROR,MESS_LOG_ERROR, "[Core] Critical error: unexpected signal (%d):\n", sig);
  backtrace_symbols_fd(array, size, 1);
  exit(1);               // i would like to pause here instead of terminate
}

Is it possible? If it is - will it be possible to attach debugger to such paused process?

1 Answers

I would prefer my program to stop execution

You can call sleep() with large enough number of seconds to sleep. sleep() can be safely used in signal handler. According to man signal-safety, it should be async-signal-safe by POSIX.1.

Related