gdb常用命令表
backtrace(或bt):查看各级函数调用及参数
finish:连续运行到当前函数返回为止,然后停下来等待命令
frame(或f):帧编号,选择栈帧
info(或i):locals查看当前栈帧局部变量的值
list(或l):列出源代码,接着上次的位置往下列,每次列10行
list行号:列出从第几行开始的源代码
list函数名:列出某个函数的源代码
next(或n):执行下一行语句
print(或p):打印表达式的值,通过表达式可以修改变量的值或者调用函数
quit(或q):退出gdb调试函数
set var:修改变量的值
start:开始执行程序,停在main函数第一行语句前面等待命令
step(或s):执行下一行语句,如果有函数调用则进入到函数中
gdb与进程
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
pid_t id = fork();
if (id < 0)
{
perror("fork error!");
return -1;
}
else if (id == 0)
{
printf("child id : %d, my_father id is: %d\n", getpid(), getppid());
}
else
{
sleep(1);
printf("father id is :%d\n", getpid());
wait(NULL);
}
return 0;
}
默认设置下,在调试多进程程序时GDB只会调试主进程。但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序。只需要设置follow-fork-mode(默认值:parent)和detach-on-fork(默认值:on)即可。
follow-fork-mode detach-on-fork
parent on 只调试主进程(GDB默认)
child on 只调试子进程
parent off 同时调试两个进程,gdb跟主进程,子进程block(阻塞)在fork位置
child off 同时调试两个进程,gdb跟子进程,主进程block在fork位置
设置方法:set follow-fork-mode [parent|child] set detach-on-fork [on|off]
显示方法:
show follow-fork-mode ;
show detach-on-fork;
查询正在调试的进程 : info inferiors
显示GDB调试的所有inferior,GDB会为他们分配ID。其中带有*的进程是正在调试的inferior.
( GDB将每一个被调试程序的执行状态记录在一个名为inferior的结构中。一般情况下一个inferior对应一个进程,每个不同的inferior有不同的地址空间。inferior有时候会在进程没有启动的时候就存在。)
切换调试的进程:inferior <infer number>
切换到ID是num的inferior进行调试。
添加新的调试进程: add-inferior [-copies n] [-exec executable]
增加n个inferior并执行程序为executable。如果不指定n只增加一个inferior。如果不指定executable,则执行程序留空,增加后可使用file命令重新指定执行程序。这时候创建的inferior其关联的进程并没启动。
remove-inferiors infno: 删除一个infno号的inferior。如果inferior正在运行,则不能删除,所以删除前需要先kill或者detach这个inferior。
detach inferior: detach掉编号是infno的inferior。注意这个inferior还存在,可以再次用run命令执行它。
kill inferior infno: kill掉infno号inferior。注意这个inferior仍然存在,可以再次用run等命令执行它。
gdb与多线程
#include<stdio.h>
#include<pthread.h>
void* thread_run1()
{
printf("i am thread_run1\n");
printf("thread_run1 : %u\n",pthread_self());
return NULL;
}
void* thread_run2()
{
printf("i am thread_run2\n");
printf("thread_run2 : %u\n",pthread_self());
return NULL;
}
int main()
{
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1, NULL, thread_run1, NULL);
pthread_create(&tid2, NULL, thread_run2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
在多线程编程时,当我们需要调试时,有时需要控制某些线程停在断点,有些线程继续执行。有时需要控制线程的运行顺序。有时需要中断某个线程,切换到其他线程。这都可以通过gdb实现。
GDB默认支持调试 多线程,跟主线程,子线程block在create thread。
先来看一下gdb调试多线程常用命令:
info threads:显示可以调试的所有线程。gdb会为每个线程分配一个ID(和tid不同),编号一般从1开始。后面的ID是指这个ID。
thread ID:切换当前调试的线程为指定ID的线程。
break FileName.cpp:LinuNum thread all:所有线程都在文件FileName.cpp的第LineNum行有断点。
thread apply ID1 ID2 IDN command:多个线程执行gdb命令command。
thread apply all command:所有线程都执行command命令。
set scheduler-locking off|on|step:在调式某一个线程时,其他线程是否执行。off,不锁定任何线程,默认值。on,锁定其他线程,只有当前线程执行。step,在step(单步)时,只有被调试线程运行。
set non-stop on/off:当调式一个线程时,其他线程是否运行。
set pagination on/off:在使用backtrace时,在分页时是否停止。
set target-async on/ff:同步和异步。同步,gdb在输出提示符之前等待程序报告一些线程已经终止的信息。而异步的则是直接返回。