linux下使用gdb调试崩溃丶死锁实例
来源:互联网 发布:平板软件市场 编辑:程序博客网 时间:2024/06/05 12:45
gdb是linux下一款功能强大的调试工具,windows下对应的有windbg,下面举例说明常见程序错误解决方法
1.gdb启动
要想使用gdb调试,编译时指定-g选项加入调试信息,gdb可以启动执行文件,attach正在运行程序,调试程序崩溃产生core文件
启动gdb后输入run运行,continue继续,quiet退出,下面是调试一段崩溃和死锁的源码
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> pthread_mutex_t mutex; int count = 0;void print_pid_tid() { pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); printf("pid %u tid %u (0x%x)\n", (unsigned int)pid, (unsigned int)tid, (unsigned int)tid); } void callback_func(){pthread_mutex_lock(&mutex); printf("count:%d\n",count);}void *thread_func1(void *arg) { while (1){int n = *((int *)arg);pthread_mutex_lock(&mutex); print_pid_tid(); count += 2;for (int i=0 ; i < 5; ++i ) {count += n;} callback_func();pthread_mutex_unlock(&mutex); sleep(1);}return 0;} void *thread_func2(void *arg){while (1){pthread_mutex_lock(&mutex); printf("thread_func2 run\n");pthread_mutex_unlock(&mutex); sleep(1);}return 0;}void *thread_func3(void *arg){while(1){char *str = NULL;//strcpy(str,"hello world");sleep(1);}return 0;}int main(void) { pthread_t ntid; int count = 10; pthread_mutex_init(&mutex, NULL); int err = pthread_create(&ntid, NULL, thread_func1, &count); if ( 0 != err ) { printf("pthread_create1:%s\n", strerror(err)); } err = pthread_create(&ntid, NULL, thread_func2, &count); if ( 0 != err ) { printf("pthread_create2:%s\n", strerror(err)); }err = pthread_create(&ntid, NULL, thread_func3, &count); if ( 0 != err ) { printf("pthread_create3:%s\n", strerror(err)); }getchar();int **ret = NULL; pthread_join(ntid, (void**)ret); printf("pthread_join:%p\n", *ret); pthread_mutex_destroy(&mutex); return 0; }
2.调试崩溃
gdb绑定程序运行崩溃时,gdb会停留在程序最后运行栈位置,一般输入bt查看堆栈,frame n切换栈帧,print打印是否空指针导致崩溃,where查看对于源码位置或者list列出源代码,崩溃一般有空指针,数组越界,内存非法访问
3.调试死锁
程序出现死锁时会是卡死状态,如果gdb绑定运行使用ctrl+c中断程序,输入info threads查看所有线程,使用thread n切换线程,在线程中输入bt查看线程堆栈,定位程序停留位置,一般比较多个线程锁或者是否有死循环
4.断点调试
设置断点,如b main.cpp:31,执行到断点后next单步,step进入函数,continue继续运行
阅读全文
0 0
- linux下使用gdb调试崩溃丶死锁实例
- Linux下用GDB调试程序崩溃错误
- 使用gdb调试多线程死锁
- 使用gdb调试死锁线程
- linux gdb 调试实例
- LINUX GDB调试实例
- Linux下gdb调试器的使用
- linux下使用gdb调试nasm程序
- Linux下gdb调试工具的使用
- Linux下使用GDB调试应用程序示例
- Linux下使用gdb调试core文件
- linux下使用gdb调试程序
- Linux下使用GDB工具调试程序
- linux 下使用gdb调试总结
- linux下使用GDB调试程序
- Linux下GDB调试工具使用
- linux下gdb的调试使用
- LINUX下GDB调试
- 计蒜客-等和的分隔子集
- Ceph的Paxos源码注释
- 向函数传递结构体
- 第八届蓝桥杯省赛真题
- Scala学习笔记
- linux下使用gdb调试崩溃丶死锁实例
- 表单的三种提交方式
- Odd Palindromes Two Pointers
- 开始深度学习双色球演算法哈哈
- Handler机制解析(源码角度)
- java实现加密电话号码,有具体的加密流程注释
- mac 上安装 nvm 遇到的坑
- 堆排序原理及算法实现(最大堆)
- USB2.0之data PID