gdb调试多进程与多线程
来源:互联网 发布:mac error 1045 28000 编辑:程序博客网 时间:2024/06/08 01:29
gdb使用:
介绍:gdb是Linux环境下的代码调试工具。
使用:需要在源代码生成的时候加上 -g 选项。
开始使用: gdb binFIle
退出: Ctrl+d 或者quit
调试过程:
1. list/l行号:显示binFile源代码,接着上次的位置往下列,每次列十行;2. list/l函数名,列出某个函数的源代码;3. r/run:运行程序4. s/step:进入函数调用5. breaktrace(或bt):查看各级函数调用及参数6. info(i)locals:查看当前栈帧局部变量的值7. info break:查看断点信息;8. finish:执行到当前函数返回,然后停下来等待命令。9. print(p):打印表达式的值,通过表达式可以修改变量的值或者调用函数10. set var:修改变量的值11. quit:退出gdb12. break(b)行号:在某一行设置断点;13. continue(或c):从当前位置开始而非单步执行程序14. run(或r):从开始连续而非单步执行程序15. delete breakpoints:删除所有断点16. delete breakpoints n:删除序号为n的断点17. disable breakpoints :禁用断点18. enable breakpoints:启用断点19. info(或i)breakpoints :查看当前设置了那些断点;20. display变量名:跟踪查看一个变量,每次停下来都显示它的值21. undisplay:取消对当前设置的那些变量的跟踪22. until X:跳转至X行23. n或next:单条执行。24. p变量:打印变量值。
gdb调试多进程
多进程时,进入调试状态后,默认调试主进程。
Makefile
mychild: mychild.c gcc -o $@ $^ -g -lpthread.PHONY:cleanclean: rm -f mychild
源程序:
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/wait.h>int main(){ pid_t id=fork(); if(id==0) { //child printf("I am child id:%d,pid:%d\n",getpid(),getppid()); exit(1); } else { //parent printf("I am parent id:%d,pid:%d\n",getpid(),getppid()); if(wait(NULL)!=-1) { printf("wait success!\n"); } } return 0;}
- 查找fork出子进程的代码在第几行,然后打上断点。
- 运行至断点处,info feriors可查看当前调试的进程
- 设置follow-fork-mode 的值为child,表示只调试的子进程,再查看当前调试 进程发现
变成了新产生出来的进程,即子进程。follow-fork-mode(默认值是parent)
和detach-on-fork(默认值是on)结合使用。
使用set follow-fork-mode child ,detach-on-fork没有设置,默认是on,
即第二种使用场景,只调试子进程。
4.可以使用inferior+进程编号(即info inferiors后显示在第一列的号码)来切换当前正在调试的进程。
以上就是调试多进程用到的命令。
gdb调试多线程
代码如下:
void *pthread1_run(void *arg) { int count=10; while(count--) { sleep(1); printf("i am new pthread1 %lu\n",pthread_self()); } pthread_exit(NULL); return 0; } void *pthread2_run(void *arg) { int count=10; while(count--) { sleep(1); printf("i am new pthread2 %lu\n",pthread_self()); } pthread_exit(NULL); return 0; } int main() { pthread_t tid1; pthread_t tid2; pthread_create(&tid1,NULL,pthread1_run,NULL); pthread_create(&tid2,NULL,pthread2_run,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; }
1.在创建完线程的代码处打上断点,运行至断点处,info threads查看当前进程内的
线程。可以看到,当前正在调试的是主线程。
2.和多进程类似,可以使用thread+线程编号切换当前正在调试的线程。
所以,thread 后*号到了编号为2的线程的前面,表示当前调试的就是该线程。
3.set scheduler-locking[off|on|step].因为你在调试一个线程时,其他线程也在执行。
但有时我们只想让被调试的线程执行。我们可以设置scheduler-locking的值:off表示不锁定,
on表示只有当前进程会运行,step表示单步运行时只有当前进程运行。
以上就是调试多线程用到的命令。
阅读全文
0 0
- gdb调试多进程与多线程
- GDB调试-多进程与多线程程序
- gdb调试多进程与多线程
- gdb调试多进程与多线程
- gdb调试多进程与多线程
- 使用gdb调试多线程与多进程
- gdb调试多线程与多进程
- GDB调试多进程、多线程
- gdb多进程多线程调试
- gdb 多进程,多线程调试
- GDB多进程多线程调试
- gdb调试多线程多进程
- gdb调试多进程,多线程
- gdb调试多进程多线程
- gdb调试多进程多线程
- gdb调试多进程多线程
- gdb调试多进程、多线程
- gdb调试多进程多线程
- stm32f030R8 boot1
- BOZJ 1123: [POI2008]BLO tarjan求割点
- 远程连接 MySQL 数据库
- The connection to adb is down, and a severe error has occured.
- Spring MVC报错:NoSuchMethodError: org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()
- gdb调试多进程与多线程
- iOS CoreAnimation (五) convert方法,hitTest方法,contains,zPosition的使用
- SourceTree使用图解
- String...的使用
- visual studio 删除附加项
- linux 装jdk和hadoop环境
- 21.Java中的锁
- java中几种常见的排序算法
- JS判断页面由哪个APP打开