暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

20230627_gdb调试技术

1. what is GDB

image-20230627153648101

2. 在线文档

https://www.sourceware.org/gdb/

3. gdb的安装

yum -y install gdb which gdb gdb -v

image-20230627153658699

4. 调试前的准备

编译程序时需要加 -g参数

image-20230627153820513

没有设置-g

image-20230627153936398

设置-g

image-20230627153955499

编译优化等级 -O0

image-20230627154049731

5. 基本调试命令

image-20230627155144261

6. 调试程序1

vim test.c #include <stdio.h> int func1(int a) { int* p=0; *p = a; } int sum(int l,int r) { if(l == 999) { func1(l); } if(l == 9999) { int i = 0; for(i=l;i>0;i--) { sleep(1); printf("current value: %d\n",i); } } printf("in the func before sum\n"); int result=l+r; printf("in the func after sum\n"); return result; } int main(int argc, char *argv[]) { int a=0; int b=0; sscanf(argv[1], "%d", &a); sscanf(argv[2], "%d", &b); int c=sum(a,b); printf("result=%d\n",c); } gcc -g -O0 test.c -o test

7. 调试core文件

程序挂掉时,系统缺省不会生成core文件 1. ulimit -a查看系统参数; 2. ulimit -c unlimited 把core文件的大小设为无限制; 3. 运行程序,生成core文件; 4. gdb程序名 core文件名。

image-20230627155301992

8. 调试运行中的程序

You can, instead, specify a process ID as a second argument or use option -p, if you want to debug a running process: gdb program 1234 gdb -p 1234 1. 运行情况 ./test 9999 1 2. 查询进程pid ps -ef|grep test 3. gdb 进程 gdb -p $pid

9. 调试多进程服务程序

调试父进程:set follow-fork-mode parent 调试子进程:set follow-fork-mode child 设置调试模式:set detach-on-fork [on|off],缺省是on, 表示调试当前进程的时候,其它的进程继续运行,如果用 off, 调试当前进程的时候,其它的进程被gdb挂起。 查看调试的进程: info inferiors 切换当前调试的进程:inferior 进程id

调试代码

vim book.c #include <stdio.h> #include <stdlib.h> int main() { printf("being\n"); if(fork() != 0) { printf("我是父进程: pid=%d,ppid=%d\n",getpid(),getppid()); int ii; for(ii=0;ii<10;ii++) { printf("ii=%d\n",ii); sleep(1); } exit(0); } else { printf("我是子进程: pid=%d,ppid=%d\n",getpid(),getppid()); int jj; for(jj=0;jj<10;jj++) { printf("jjjj=%d\n",jj); sleep(1); } exit(0); } } gcc -o book book.c -g gdb book b 5 r

10. 调试多线程服务程序

调试代码

vim thread.c #include <stdio.h> #include <unistd.h> #include <pthread.h> int x=0,y=0; //x用于线程一,y用于线程二 pthread_t pthid1,pthid2; //第一个线程的主函数 void *pth1_main(void *arg); //第二个线程的主函数 void *pth2_main(void *arg); int main() { //创建线程一 if( pthread_create(&pthid1,NULL,pth1_main,(void*)0) != 0) { printf("pthread_create pthid1 failed.\n"); return -1; } //创建线程二 if( pthread_create(&pthid2,NULL,pth2_main,(void*)0) != 0) { printf("pthread_create pthid2 failed.\n"); return -1; } printf("111\n"); pthread_join(pthid1,NULL); printf("222\n"); pthread_join(pthid2,NULL); printf("333\n"); return 0; } //第一个线程的主函数 void *pth1_main(void *arg) { for(x=0;x<100;x++) { printf("x=%d\n",x); sleep(1); } pthread_exit(NULL); } //第二个线程的主函数 void *pth2_main(void *arg) { for(y=0;y<100;y++) { printf("y=%d\n",y); sleep(1); } pthread_exit(NULL); } gcc -g -o thread thread.c -lpthread

检查线程信息

在shell中执行: 查看当前运行的进程:ps aux|grep book 查看当前运行的轻量级进程:ps -aL|grep book 或者 ps -T pid 查看主线程和新线程的关系: pstree -p 主线程id

调试命令

在gdb中执行: 查看线程: info threads 切换线程: thread 线程id 只运行当前线程:set scheduler-locking on 运行全部的线程:set scheduler-locking off 指定某线程执行某gdb命令:thread apply 线程id cmd 全部的线程执行某gdb命令:thread apply all cmd

11.思维导图下载

链接:https://pan.baidu.com/s/1fdxzPpym4iQjx9PEAeDnig 提取码:ugxf
最后修改时间:2023-06-28 09:55:15
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论