Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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 如何暂停pthread程序的执行,以便检查线程的pid和tgid?_C_Linux_Gdb_Pthreads - Fatal编程技术网

C 如何暂停pthread程序的执行,以便检查线程的pid和tgid?

C 如何暂停pthread程序的执行,以便检查线程的pid和tgid?,c,linux,gdb,pthreads,C,Linux,Gdb,Pthreads,我正在尝试一个来自 我也试着按照帖子上说的去做: 通过在gdb中使用调度程序锁定,我可以使程序及其线程保持活动状态,以便捕获最重要的内容 但因为我不熟悉gdb,所以程序运行到结束时不会暂停(见下文)。我还试图通过break43设置断点,但gdb说当前文件中没有第40行。如何暂停执行,以便使用top或ps检查线程的pid和tgid $ gdb thread_test GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 201

我正在尝试一个来自

我也试着按照帖子上说的去做:

通过在gdb中使用调度程序锁定,我可以使程序及其线程保持活动状态,以便捕获最重要的内容

但因为我不熟悉gdb,所以程序运行到结束时不会暂停(见下文)。我还试图通过
break43
设置断点,但gdb说当前文件中没有第40行。如何暂停执行,以便使用
top
ps
检查线程的pid和tgid

$ gdb thread_test
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from thread_test...(no debugging symbols found)...done.
(gdb) set scheduler-locking
Requires an argument. Valid arguments are off, on, step, replay.
(gdb) set scheduler-locking on
Target 'exec' cannot support this command.
(gdb) run
Starting program: /tmp/test/pthreads/thread_test 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77c4700 (LWP 4711)]
[New Thread 0x7ffff6fc3700 (LWP 4712)]
[Thread 1] getpid() = 4707
[Thread 1] pthread_self() = 140737345505024
[Process] getpid() = 4707
[Process] pthread_self() = 140737353951040
[Thread 0x7ffff77c4700 (LWP 4711) exited]
[Thread 2] getpid() = 4707
[Thread 2] pthread_self() = 140737337112320
[Thread 0x7ffff6fc3700 (LWP 4712) exited]
[Inferior 1 (process 4707) exited normally]
(gdb) 
$gdb线程\u测试
GNU gdb(Ubuntu 8.1-0ubuntu3)8.1.0.20180409-git
版权所有(C)2018免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-linux-gnu”。
键入“显示配置”以获取配置详细信息。
有关错误报告说明,请参阅:
.
在线查找GDB手册和其他文档资源,网址为:
.
要获得帮助,请键入“帮助”。
键入“apropos word”以搜索与“word”相关的命令。。。
正在从线程_测试中读取符号…(未找到调试符号)…已完成。
(gdb)设置计划程序锁定
需要一个论点。有效参数包括关闭、打开、步骤和重播。
(gdb)将计划程序锁定设置为打开
目标“exec”无法支持此命令。
(gdb)运行
启动程序:/tmp/test/pthreads/thread\u test
[已启用使用libthread_db的线程调试]
使用主机libthread_db library“/lib/x86_64-linux-gnu/libthread_db.so.1”。
[新螺纹0x7ffff77c4700(LWP 4711)]
[新螺纹0x7ffff6fc3700(LWP 4712)]
[线程1]getpid()=4707
[Thread 1]pthread_self()=140737345505024
[进程]getpid()=4707
[Process]pthread_self()=1407353951040
[线程0x7ffff77c4700(LWP 4711)已退出]
[线程2]getpid()=4707
[线程2]pthread_self()=140737337112320
[线程0x7ffff6fc3700(LWP 4712)已退出]
[次1(进程4707)正常退出]
(gdb)
您有两个问题:

  • 您构建的程序没有调试信息(添加
    -g
    标志),并且
  • 在程序启动之前,您试图
    将调度程序锁定设置为打开状态(不起作用)
这应该起作用:

 gcc -g -pthread -o thread_test thread_test.c
 gdb -q ./thread_test
 (gdb) start
 (gdb) set scheduler-locking on
但是,您必须特别小心此设置——从这一点继续将使程序在
pthread\u join
中阻塞,因为只有主线程将继续运行。

您有两个问题:

  • 您构建的程序没有调试信息(添加
    -g
    标志),并且
  • 在程序启动之前,您试图
    将调度程序锁定设置为打开状态(不起作用)
这应该起作用:

 gcc -g -pthread -o thread_test thread_test.c
 gdb -q ./thread_test
 (gdb) start
 (gdb) set scheduler-locking on

但是,您必须特别小心此设置——从这一点继续将使您的程序在
pthread\u join
中阻塞,因为只有主线程将继续运行。

以下是使用
gdb
和发布的代码暂停所有内容的示例:

注意:编译此文件是为了通过以下方式查找/修复编译问题:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c untitled.c
最终通过以下方式进行编译/链接:

gcc -ggdb -Wall -o untitled untitled.c   -lpthread 
然后使用调试器:
gdb
,从而显示我的输入和
gdb
输出:

$ gdb untitled
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from untitled...done.
(gdb) br main
Breakpoint 1 at 0x9a5: file untitled.c, line 20.
(gdb) br threadMethod
Breakpoint 2 at 0x946: file untitled.c, line 9.
(gdb) r
Starting program: /home/richard/Documents/forum/untitled 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at untitled.c:20
20  {
(gdb) c
Continuing.
[New Thread 0x7ffff77c4700 (LWP 8645)]
[New Thread 0x7ffff6fc3700 (LWP 8646)]
[Switching to Thread 0x7ffff77c4700 (LWP 8645)]

Thread 2 "untitled" hit Breakpoint 2, threadMethod (arg=0x7fffffffdf4c)
    at untitled.c:9
9       int intArg = (int) *((int*) arg);
(gdb) 

以下是使用
gdb
和发布的代码暂停所有内容的示例:

注意:编译此文件是为了通过以下方式查找/修复编译问题:

gcc -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c untitled.c
最终通过以下方式进行编译/链接:

gcc -ggdb -Wall -o untitled untitled.c   -lpthread 
然后使用调试器:
gdb
,从而显示我的输入和
gdb
输出:

$ gdb untitled
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from untitled...done.
(gdb) br main
Breakpoint 1 at 0x9a5: file untitled.c, line 20.
(gdb) br threadMethod
Breakpoint 2 at 0x946: file untitled.c, line 9.
(gdb) r
Starting program: /home/richard/Documents/forum/untitled 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at untitled.c:20
20  {
(gdb) c
Continuing.
[New Thread 0x7ffff77c4700 (LWP 8645)]
[New Thread 0x7ffff6fc3700 (LWP 8646)]
[Switching to Thread 0x7ffff77c4700 (LWP 8645)]

Thread 2 "untitled" hit Breakpoint 2, threadMethod (arg=0x7fffffffdf4c)
    at untitled.c:9
9       int intArg = (int) *((int*) arg);
(gdb) 

您需要使用
-g
标志编译程序,然后才能设置断点。谢谢。我链接的帖子没有将其与-g合并,但它说“通过在gdb中使用调度程序锁定,我可以使程序及其线程保持活动状态,这样我就可以捕获顶层内容”。编译时,始终启用警告,然后修复这些警告。建议使用:`gcc-ggdb3-Wall-Wextra-Wconversion-pedantic-std=gnu11-o thread_test thread_test.c-pthreadOT:about:
fprintf(stderr,“pthread_create:error\n”)这无法告诉用户函数失败的原因。强烈建议:
perror(“pthread_创建失败”)
因为这将输出错误消息和文本原因,系统认为错误发生在
stderr
Re:“我链接的帖子不符合-g”您可以不使用-g进行编译,但是您将只能在函数和数字对象代码地址处放置断点,不是行号。您需要使用
-g
标志编译程序,然后才能设置断点。谢谢。我链接的帖子没有将其与-g合并,但它说“通过在gdb中使用调度程序锁定,我可以使程序及其线程保持活动状态,这样我就可以捕获顶层内容”。编译时,始终启用警告,然后修复这些警告。建议使用:`gcc-ggdb3-Wall-Wextra-Wconversion-pedantic-std=gnu11-o thread_test thread_test.c-pthreadOT:about:
fprintf(stderr,“pthread_create:error\n”)这无法告诉用户函数失败的原因。强烈建议:
perror(“pthread_创建失败”)
因为它将输出错误消息和文本原因,系统认为错误发生在
stderr
Re:“我链接的帖子不符合-g”您可以在不使用-g的情况下编译,但是您将只能在函数和数字对象代码地址处放置断点,而不能在行号处放置断点。