gdb nostop SIGSEGV on a specific thread

gdb nostop SIGSEGV on a specific thread

本文关键字:specific thread on nostop SIGSEGV gdb      更新时间:2023-10-16

我有一个程序故意在一个线程上分段,但我遇到了另一个线程正在分段的问题,我想用GDB来捕捉它,我看到我可以:

handle SIGSEGV nostop noprint

但我只想在有意这样做的线程上这样做。。有可能吗?

我会解释:我有两个线程,一个线程是分段故障(并恢复(mprotect只读,然后释放内存)),这很好,另一个线程做了其他事情,但遗憾的是,有一个错误,它是分段故障,我想捕捉那个分段故障,而不是其他线程中发生的其他故障。

正如我所知,根据操作系统的不同,我假设linux是我的答案,答案是"否"!

Posix异常每个线程可以有一个sigmask,但每个任务只能有一个处理程序。因此,不可能为每个线程设置不同的处理。sigaction将处理整个过程。所以我认为gdb没有办法改变这一点。

我将解释:我有两个线程,一个线程是分段故障(并恢复(mprotect只读,然后释放内存)),这很好,另一个线程做了其他事情,但遗憾的是,有一个错误,它是分段故障,我想捕捉那个分段故障,而不是其他线程中发生的其他故障

您必须告诉gdb忽略第一个SIGSEGV信号。因此,在第一次sagfault之后,在此线程中使用signal 0命令。您的程序将在gdb下恢复执行,这正是您想要的。然后它将在第二个线程中的第二个segfault处停止,这就是您想要检查的内容。

(gdb) help signal
Continue program with the specified signal.
Usage: signal SIGNAL
The SIGNAL argument is processed the same as the handle command.
An argument of "0" means continue the program without sending it a signal.
This is useful in cases where the program stopped because of a signal,
and you want to resume the program while discarding the signal.

所以

  1. 不要使用handle SIGSEGV nostop noprint。在下运行程序gdb
  2. 当它在前三个中出现故障时,执行signal 0。您的程序恢复执行
  3. 然后它在另一个线程中分段。现在使用backtrace查看问题

或者,如果你的两个线程不相互依赖,你可以在第一个segfault的线程中等待,而另一个segfoot发生。只要在第一个线程中执行call sleep(60),它就会导致segfault,并在另一个线程中等待另一个segfault。您的第一个线程将等待:

Program received signal SIGFPE, Arithmetic exception.
[Switching to Thread 0x7ffff7fde700 (LWP 25744)]
0x000000000040075d in my_thread_func1 (arg=0x0) at my_test_2.cpp:17
17        ptr1 = ptr1 / 0;
(gdb) call sleep(60)
Thread 140737343510272:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff75dd700 (LWP 25745)]
0x00000000004007a3 in my_thread_func2 (arg=0x0) at my_test_2.cpp:27
27        *ptr2 = *ptr2 + 2;
The program received a signal in another thread while
making a function call from GDB.
Evaluation of the expression containing the function
(sleep) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb)