日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下使用gdb调试崩溃和死锁实例

發布時間:2025/3/21 linux 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下使用gdb调试崩溃和死锁实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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繼續運行

總結

以上是生活随笔為你收集整理的linux下使用gdb调试崩溃和死锁实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。