1、调试简单多线程
学习:【Linux】gdb调试多线程与多进程
GDB 调试多线程程序的总结
gdb调试多进程与多线程
生成程序 gcc -o env threadgdb1.c -lpthread -g
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
static int count = 0;
void *pthread_fuc(void *arg)
{
int i = 0;
while(i < 100)
{
i++;
count += i;
usleep(100);
}
return NULL;
}
int main()
{
pthread_t pth1;
pthread_t pth2;
pthread_create(&pth1, NULL, &pthread_fuc, NULL);
pthread_create(&pth1, NULL, &pthread_fuc, NULL);
pthread_join(pth1, NULL);
pthread_join(pth2, NULL);
printf("cout: %d\n",count);
return 0;
}
set b threadgdb1.c:8 设置断点位置文件threadgdb1.c的第8行
info b,显示断点,info thread 显示线程
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400752 in pthread_fuc
at threadgdb1.c:8
breakpoint already hit 2 times
2 breakpoint keep y 0x00000000004007d4 in main at threadgdb1.c:26
breakpoint already hit 1 time
(gdb) info thread
Id Target Id Frame
* 1 Thread 0x7ffff7fda700 (LWP 19331) "env" main () at threadgdb1.c:26
2 Thread 0x7ffff77ef700 (LWP 19332) "env" pthread_fuc (arg=0x0)
at threadgdb1.c:10
3 Thread 0x7ffff6fee700 (LWP 19333) "env" pthread_fuc (arg=0x0)
at threadgdb1.c:10
r(run) 运行到断点, start 运行到main函数第一句,
(gdb) start
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Temporary breakpoint 3 at 0x40078f: file threadgdb1.c, line 21.
Starting program: /home/Templates/GDB/env
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Temporary breakpoint 3, main () at threadgdb1.c:21
21 {
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/Templates/GDB/env
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77ef700 (LWP 19332)]
[New Thread 0x7ffff6fee700 (LWP 19333)]
Thread 2 "env" hit Breakpoint 1, pthread_fuc (arg=0x0) at threadgdb1.c:10
10 int i = 0;
(gdb)
Thread 3 "env