【问题标题】:why c++ pthread exited by itself without process crash?为什么c ++ pthread自行退出而没有进程崩溃?
【发布时间】:2019-07-09 10:59:51
【问题描述】:

我有一个使用近 16 个线程的进程。在工作一段时间后,线程 id 会自行退出(请注意进程没有崩溃)。我不确定如何查找线程退出的原因?我尝试使用 print 语句,但这似乎没有帮助。尝试通过 gdb 捕获没有帮助。如果这是某种内存损坏,那么进程应该崩溃并且核心文件应该告诉一切(大部分时间)但进程仍在运行只是线程退出让我的工作变得困难..

您能否提出一些调试此问题的方法?

-阿皮特

【问题讨论】:

  • 不查看代码很难回答标题中的问题。 GDB(与任何其他调试器一样)有许多选项可以提供帮助,但同样,这取决于您拥有的代码。
  • @Tsyvarev,您能否提供指点,哪些 gdb 选项会有所帮助。代码大量分布在多个文件中,我不知道哪个代码块有问题(我只是想找到那个)
  • 总是退出同一个线程例程吗?如果是这样,则在其所有 return 语句上放置断点,也可能在 pthread_exit 上放置断点。
  • @G.M.是的,运行一段时间后,同一线程正在退出。我没有使用 pthread_exit() 因为这个进程是一个守护进程。整个过程在 while (1) 下运行,我没有使用任何 exit() 函数。因此我不知道它在哪里退出。
  • 好吧,您使用的是pthread_exit——只是不直接使用。当线程例程(传递给pthread_create)发出return 语句时,将为该线程调用pthread_exit。如果您查看pthread_create 的手册页,它会显示"The new thread terminates in one of the following ways: [...] It returns from start_routine(). This is equivalent to calling pthread_exit(3) with the value supplied in the return statement."。因此在其上设置断点可以提供一些有用的信息。

标签: c++ linux gdb kernel pthreads


【解决方案1】:

我不确定如何查找线程退出的原因

我能想到的只有两种方法:

  1. 线程函数返回
  2. 线程函数调用执行syscall(SYS_exit, ...)

您可以使用(gdb) catch syscall exit,然后使用where 找出发生这种情况的位置。

如果where 显示类似于此的内容:

(gdb) where
#0  0x00007ffff7bc3556 in __exit_thread () at ../sysdeps/unix/sysv/linux/exit-thread.h:36
#1  start_thread (arg=0x7ffff781c700) at pthread_create.c:478
#2  0x00007ffff7905a8f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

那么你有案例 1,你需要单步执行你的线程函数来找出它返回的位置(https://rr-project.org/ 可能有很大帮助)。

如果你看到的是这样的:

(gdb) where
#0  syscall () at ../sysdeps/unix/sysv/linux/x86_64/syscall.S:38
#1  0x0000555555554746 in do_work () at t.c:9
#2  0x0000555555554762 in fn (p=0x0) at t.c:15
#3  0x00007ffff7bc3494 in start_thread (arg=0x7ffff781c700) at pthread_create.c:333
#4  0x00007ffff7905a8f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:97

那么罪魁祸首就是名为 syscall 的例程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多