简体   繁体   English

gdb nostop SIGSEGV在特定的线程上

[英]gdb nostop SIGSEGV on a specific thread

I have a program that purposely segfaults on one threads, but I have a problem that the other thread is segfaulting, I'd like to catch it with GDB, I saw that I can: 我有一个故意在一个线程上进行段错误的程序,但我有一个问题,即另一个线程是segfaulting,我想用GDB捕获它,我看到我可以:

handle SIGSEGV nostop noprint

but I'd like to do that only on the thread that purposely does that.. is it possible? 但我只想在故意这样做的线程上这样做..这可能吗?

I'll explain: I have 2 threads, one thread is segfaulting(and recovers(mprotect read only and then releasing memory)), that works fine, the other thread does something else, but sadly, there is a bug and it is segfaulting, I want to catch that segfault, and not the other ones that occur in the other thread. 我将解释:我有2个线程,一个线程是segfaulting(并恢复(mprotect只读,然后释放内存)),这工作正常,另一个线程做其他事情,但遗憾的是,有一个错误,它是segfaulting ,我想抓住那个段错误,而不是其他线程中出现的其他段错误。

As I know, depending on the OS, and I assume linux for my answer and the answer is 'NO'! 据我所知,取决于操作系统,我假设linux为我的答案,答案是'不'!

Posix exceptions can have a sigmask per thread but only one handler per task. Posix异常每个线程可以有一个sigmask,但每个任务只有一个处理程序。 So it is not possible to set different handling for each thread. 因此无法为每个线程设置不同的处理方式。 sigaction will handle it for the complete process. sigaction将为整个过程处理它。 So I see no way for gdb to change this. 所以我认为gdb无法改变这一点。

I'll explain: I have 2 threads, one thread is segfaulting(and recovers(mprotect read only and then releasing memory)), that works fine, the other thread does something else, but sadly, there is a bug and it is segfaulting, I want to catch that segfault, and not the other ones that occur in the other thread 我将解释:我有2个线程,一个线程是segfaulting(并恢复(mprotect只读,然后释放内存)),这工作正常,另一个线程做其他事情,但遗憾的是,有一个错误,它是segfaulting ,我想抓住那个段错误,而不是其他线程中出现的其他段错误

You have to tell gdb to ignore the first SIGSEGV signal. 你必须告诉gdb忽略第一个SIGSEGV信号。 So after the first sagfault use the signal 0 command in this thread. 所以在第一个sagfault之后在这个线程中使用signal 0命令。 Your program will resume execution under gdb and that is that you want. 您的程序将在gdb下继续执行,这就是您想要的。 Then it will stop at the second segfault in your second thread and this is what you want to inspect. 然后它会停在第二个线程中的第二个段错误,这就是你要检查的内容。

(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.

So 所以

  1. Do not use handle SIGSEGV nostop noprint . 不要使用handle SIGSEGV nostop noprint Run your program under gdb. 在gdb下运行程序。
  2. When it segfaults in the first threead do signal 0 . 当它在前三个段中发生段错误时会signal 0 Your program resumes execution. 您的程序将恢复执行。
  3. Then it segfaults in another thread. 然后它在另一个线程中进行segfaults。 Now use backtrace to see the problem. 现在使用backtrace来查看问题。

Or if your two thread are not dependent on each other you can wait in the thread that first segfaulted while another segfault happen. 或者如果你的两个线程彼此不依赖,你可以在第一个segfaulted的线程中等待,同时发生另一个段错误。 Just do call sleep(60) in the first thread as soon as it causes a segfault and wait for another segfault in another thread. 只要它导致段错误就在第一个线程中call sleep(60)并在另一个线程中等待另一个段错误。 Your first thread will wait: 你的第一个帖子会等待:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM