一、设置观察点
1.1 设置观察点测试代码
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a = 0;
void *thread1_func(void *p_arg) {
while (1) {
a++;
sleep(10);
}
}
int main (int argc, char *argv[]) {
pthread_t t1;
pthread_create(&t1, NULL, thread1_func, "thread 1");
sleep(1000);
return 0;
}
1.2 设置观察点测试过程
gdb可以使用
watch
命令设置观察点,也就是当一个变量值发生变化时,程序会停下来
可以看到,使用命令watch a之后,当变量a的值发生变化:比如从0变成1,从1变到2,程序都会暂停
另外,也可以通过变量的地址(使用命令
p &value
查看)来设置观察点(使用命令watch *地址
)
二、设置观察点只对特定线程生效
2.1 测试代码
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a = 0;
void *thread1_func(void *p_arg) {
while (1) {
a++;
sleep(10);
}
}
void *thread2_func(void *p_arg) {
while (1) {
a++;
sleep(10);
}
}
int main (int argc, char *argv[]) {
pthread_t t1, t2;
pthread_create(&t1, NULL, thread1_func, "thread 1");
pthread_create(&t2, NULL, thread2_func, "thread 2");
sleep(1000);
return 0;
}
2.2 测试过程
可以使用命令
info threads
来查看线程信息
使用命令watch expr thread threadnum来设置观察点只针对特定线程生效,
意思是,只有编号为threadnum的线程改变了变量的值,程序才会暂停,其他编号线程改变变量的值不会让程序暂停