首页 > 代码库 > 《软件调试的艺术》笔记--调试多线程程序

《软件调试的艺术》笔记--调试多线程程序

下面是于线程相关的GDB命令用法汇总:

info threads:给出关于当前所有线程的信息。

thread 3:改成线程3.

break 88 thread 3 :当线程到达源代码88时停止执行。

break 88 thread 3 if i == 2 当线程3到达源代码行88行,并且变量i的值为2时停止执行。


对下面的多线程进行调试:

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

void* thr_fn(void* arg)
{
	int i;
	for (i=0; i<5; i++) {
		printf("in thread%d,i=%d\n",*(int*)arg,i);
		sleep(1);
	}
	return (void*)0;
}

int main(void)
{
	int ret;
	pthread_t tid1, tid2;
	int t1_para = 1;
	int t2_para = 2;

	ret = pthread_create(&tid1, NULL, thr_fn, (void*)&t1_para);
	if (ret != 0) {
		printf("thread1 pthread_create:%s\n",strerror(ret));
		return 1;
	}
	
	ret = pthread_create(&tid2, NULL, thr_fn, (void*)&t2_para);
        if (ret != 0) {
                printf("thread1 pthread_create:%s\n",strerror(ret));
                return 1;
        }

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	return 0;
}
调试结果:

(gdb) b main
Breakpoint 1 at 0x4006d9: file pthread.c, line 20.
(gdb) r
Starting program: /home/yanwenjie/ctest/a.out 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".


Breakpoint 1, main () at pthread.c:20
warning: Source file is more recent than executable.
20 int t1_para = 1;
(gdb) n
21 int t2_para = 2;
(gdb) 
23 ret = pthread_create(&tid1, NULL, thr_fn, (void*)&t1_para);
(gdb) 
[New Thread 0x7ffff77fc700 (LWP 5366)]
in thread1,i=0
24 if (ret != 0) {
(gdb) 
29 ret = pthread_create(&tid2, NULL, thr_fn, (void*)&t2_para);
(gdb) 
[New Thread 0x7ffff6ffb700 (LWP 5367)]
in thread2,i=0
30        if (ret != 0) {
(gdb) info threads
  Id   Target Id         Frame 
  3    Thread 0x7ffff6ffb700 (LWP 5367) "a.out" 0x00007ffff78bd08d in nanosleep
    () from /lib/x86_64-linux-gnu/libc.so.6
  2    Thread 0x7ffff77fc700 (LWP 5366) "a.out" 0x00007ffff78bd08d in nanosleep
    () from /lib/x86_64-linux-gnu/libc.so.6
* 1    Thread 0x7ffff7fe3700 (LWP 5363) "a.out" main () at pthread.c:30
(gdb) break 10 thread 3 if i==3
Breakpoint 2 at 0x400699: file pthread.c, line 10.
(gdb) c
Continuing.
in thread2,i=1
in thread1,i=1
in thread1,i=2
in thread2,i=2
in thread1,i=3
[Switching to Thread 0x7ffff6ffb700 (LWP 5367)]


Breakpoint 2, thr_fn (arg=0x7fffffffe048) at pthread.c:10
10 printf("in thread%d,i=%d\n",*(int*)arg,i);
(gdb) p i
$1 = 3
(gdb) thread 2
[Switching to thread 2 (Thread 0x7ffff77fc700 (LWP 5366))]
#0  0x00007ffff78bd08d in nanosleep () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) n
Single stepping until exit from function nanosleep,
which has no line number information.
in thread2,i=3
0x00007ffff78bcf2c in sleep () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) break 10 thread 3
Note: breakpoint 2 (thread 3) also set at pc 0x400699.
Breakpoint 3 at 0x400699: file pthread.c, line 10.
(gdb) c
Continuing.
in thread1,i=4
[Switching to Thread 0x7ffff6ffb700 (LWP 5367)]


Breakpoint 3, thr_fn (arg=0x7fffffffe048) at pthread.c:10
10 printf("in thread%d,i=%d\n",*(int*)arg,i);
(gdb) p i
$2 = 4
(gdb) p arg
$3 = (void *) 0x7fffffffe048
(gdb) p *(int*)arg
$4 = 2
(gdb)