使用gdb调试多进程和多线程
生活随笔
收集整理的這篇文章主要介紹了
使用gdb调试多进程和多线程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
GDB調(diào)試多進(jìn)程
1. 默認(rèn)設(shè)置下,在調(diào)試多進(jìn)程程序時GDB只會調(diào)試主進(jìn)程。但是GDB(>V7.0)支持多進(jìn)程的分別以及同時調(diào)試,換句話說,GDB可以同時調(diào)試多個程序。只需要設(shè)置follow-fork-mode(默認(rèn)值:parent)和detach-on-fork(默認(rèn)值:on)即可。
follow-fork-mode? detach-on-fork?? 說明 parent??????????????? ?? on?????????????? 只調(diào)試主進(jìn)程(GDB默認(rèn))child? ??? ? ??? ? ? ?? ? on?????????????? 只調(diào)試子進(jìn)程
parent?????????? ?? ? ?? off????????????? 同時調(diào)試兩個進(jìn)程,gdb跟主進(jìn)程,子進(jìn)程block在fork位置
child? ???????? ? ?? ? ?? off ???????????? 同時調(diào)試兩個進(jìn)程,gdb跟子進(jìn)程,主進(jìn)程block在fork位置 ?設(shè)置方法:set follow-fork-mode [parent|child] ? set detach-on-fork on|【off】
查詢正在調(diào)試的進(jìn)程:info inferiors
?? 切換調(diào)試的進(jìn)程: inferior <infer number>
測試代碼: ?1 #include<stdio.h>
? 2 #include<unistd.h>
? 3 #include<sys/types.h>
? 4?
? 5?
? 6 int main()
? 7 {
? 8 ? ? pid_t id;
? 9 ? ? id=fork();
?10 ? ? if(id=0){//child
?11 ? ? ? ? printf("%d,%d\n",getpid(),getppid());
?12 ? ? ? ? ? ? exit(0);
?13 ? ? ? ? }else {//father
?14 ? ? ? ? ? ?printf("%d,%d\n",getpid(),getppid());
?15 ? ? ? ? }
?16 ? ? return 0;
?17 } ?GDB調(diào)節(jié)多線程: 在多線程編程時,當(dāng)我們需要調(diào)試時,有時需要控制某些線程停在斷點(diǎn),有些線程繼續(xù)執(zhí)行。有時需要控制線程的運(yùn)行順序。有時需要中斷某個線程,切換到其他線程。這些都可以通過gdb實(shí)現(xiàn)。 GDB默認(rèn)支持調(diào)試多線程,跟主線程,子線程block在create thread。
先來看一下gdb調(diào)試多線程常用命令
- info threads:顯示可以調(diào)試的所有線程。gdb會為每個線程分配一個ID(和tid不同),編號一般從1開始。后面的ID是指這個ID。
- thread ID:切換當(dāng)前調(diào)試的線程為指定ID的線程。
- 代碼測試:
- ?1 #include<stdio.h> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? 2 #include<unistd.h>
? 3 #include<sys/types.h>
? 4 #include<pthread.h>
? 5?
? 6?
? 7?
? 8 void* thread_run1(void* val)
? 9 {
?10 ? ? int i=3;
?11 ? ? while(i){
?12 ? ? ? ?i--;
?13 ? ? ? ? printf("i am run1!! %d,%u\n",i,pthread_self());
?14 ? ? ? ? sleep(1);
?15 ? ? ? ? }
?16 ? ? ? ? return NULL;
?17 }
- 18 void* thread_run2(void* val)
?19 {
?20 ? ? int m=5;
?21 ? ? while(m){
?22 ? ? ? ? m--;
?23 ? ? ? ? printf("i ?am run2!! %d, %u\n",m,pthread_self());
?24 ? ? ?sleep(2);
?25 ? ? ? ? }
?26 ? ? ? ? return 0;
?27 }
?28 int main()
?29 {
?30 ? ? pthread_t tid1;
?31 ? ? pthread_t tid2;
?32?
?33 ? ? pthread_create(&tid1,NULL,thread_run1,NULL);
?34 ? ? pthread_create(&tid2,NULL,thread_run2,NULL);
?35?
?36 ? ? pthread_join(tid1,NULL);
?37 ? ? pthr
總結(jié)
以上是生活随笔為你收集整理的使用gdb调试多进程和多线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: crond和crontab
- 下一篇: IPC--三---共享内存