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;
}
child on 只调试子进程
parent off 同时调试两个进程,gdb跟主进程,子进程block(阻塞)在fork位置
child off 同时调试两个进程,gdb跟子进程,主进程block在fork位置
#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;
}
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在输出提示符之前等待程序报告一些线程已经终止的信息。而异步的则是直接返回。