我想使用 gdb 调试多线程。当然,线程使用信号量可以很好地协同工作。但是当有这样一句话时:
sem_wait(&empty);
现在假设空信号量应该等到空信号量可用。换句话说,它应该在这里阻塞。 gdb 是调度程序锁定的:
(gdb) show scheduler-locking
Mode for locking scheduler during execution is "on".
然后我输入
n
gdb 如下所示。 (当然应该是block。但是我想回到命令行切换到其他线程继续调试。)
(gdb) n
^C
Thread 3 "sem" received signal SIGINT, Interrupt.
0x00007ffff7bc6f96 in futex_abstimed_wait_cancelable (private=0,
abstime=0x0, expected=0, futex_word=0x555555755060 <full>)
at ../sysdeps/unix/sysv/linux/futex-internal.h:205
我想使用 Ctrl-C,但是线程退出了。所以我该怎么做?感谢您的回答。
最佳答案
I want to use Ctrl-C, but the thread exits.
当您的线程接收到 SIGINT
信号时,sem_wait
应该返回 EINTR
错误。参见 documentation :
EINTR The call was interrupted by a signal handler; see signal(7).
您可以尝试添加EINTR
的错误处理,并在出现此错误时再次调用sem_wait
,使您的代码更易于调试。您可以使用来自此 blog post 的 sem_wait
的包装器:
static inline
int sem_wait_nointr(sem_t *sem) {
while (sem_wait(sem))
if (errno == EINTR) errno = 0;
else return -1;
return 0;
}
关于c - 如何调试gdb中的sem_wait函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576154/