Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 特定线程上的gdb nostop SIGSEGV_C++_C_Linux_Gdb_Segmentation Fault - Fatal编程技术网

C++ 特定线程上的gdb nostop SIGSEGV

C++ 特定线程上的gdb nostop SIGSEGV,c++,c,linux,gdb,segmentation-fault,C++,C,Linux,Gdb,Segmentation Fault,我有一个程序故意在一个线程上执行segfaults,但我有一个问题,另一个线程正在执行segfaulting,我想用GDB捕获它,我发现我可以: handle SIGSEGV nostop noprint 但我只想在有目的地这样做的线程上这样做。。可能吗 我会解释: 我有两个线程,一个线程是segfaulting(并恢复(mprotect read only,然后释放内存)),工作正常,另一个线程执行其他操作,但遗憾的是,有一个bug,它是segfaulting,我想捕获该segfaultin

我有一个程序故意在一个线程上执行segfaults,但我有一个问题,另一个线程正在执行segfaulting,我想用GDB捕获它,我发现我可以:

handle SIGSEGV nostop noprint
但我只想在有目的地这样做的线程上这样做。。可能吗

我会解释:
我有两个线程,一个线程是segfaulting(并恢复(mprotect read only,然后释放内存)),工作正常,另一个线程执行其他操作,但遗憾的是,有一个bug,它是segfaulting,我想捕获该segfaulting,而不是在另一个线程中发生的其他segfaults。

我知道,这取决于操作系统,我假设linux是我的答案,答案是‘不’

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

我会解释:我有两个线程,一个线程是segfaulting(并恢复(mprotect read only,然后释放内存)),这很好,另一个线程做了其他事情,但遗憾的是,有一个bug,它是segfaulting,我想捕捉这个segfaulting,而不是发生在另一个线程中的其他segfault

您必须告诉gdb忽略第一个SIGSEGV信号。因此,在第一个SAG故障之后,在此线程中使用
信号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.
所以

  • 不要使用
    句柄SIGSEGV nostop noprint
    。运行你的程序 gdb
  • 当它在前三个位置出现故障时,执行信号0。你的程序 恢复执行
  • 然后它在另一个线程中分离故障。现在使用
    backtrace
    查看 问题
  • 或者,如果两个线程互不依赖,则可以在第一个segfault发生的线程中等待,而另一个segfault发生。只要在第一个线程中调用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)
    

    对不起,你能更详细地解释一下这个问题吗?我读到:你有两个线程,都在SEGFULT中运行。您只想让第二个线程分别从指定的线程发出segfauls?您可以尝试为目标线程生成除
    SIGSEGV
    以外的信号。我可以使用mprotect选择我发出的信号?但是第一个线程segfauls很多,这是它正常运行的一部分我需要他继续运行,以防第一个bug发生。我结束于,非常感谢@user184968,你让我度过了美好的一天。