带着神奇的梦想 2022-12-02 10:04 采纳率: 0%
浏览 7

gdb怎么排查段错误

img


有没有小伙伴知道gdb怎么排查这种段错误的原因?唯一能知道的就是memcpy的段错误一定是越界了。

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-12-02 10:54
    关注

    进入gdb然后使用bt命令
    可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。
    如果这个调用历史中有你能修改对应源代码的函数,在那个/哪些函数调用开始,将函数中用到的所有变量(包括函数参数,全局变量,……)的当前值写日志到文件;
    如果这个调用历史中没有你能修改对应源代码的函数,根据调用历史中函数的用途,猜测它们源自你能修改对应源代码的那个/哪些函数,将函数中用到的所有变量(包括函数参数,全局变量,……)的当前值写日志到文件;
    然后重新生成可执行程序,重新运行,看日志里面的函数中用到的所有变量(包括函数参数,全局变量,……)的当前值必然有不对的地方!
    写日志参考“尺有所短寸有所长”我土造的这款MyLog:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #ifdef _MSC_VER
        #pragma warning(disable:4996)
        #include <windows.h>
        #include <io.h>
    #else
        #include <unistd.h>
        #include <sys/time.h>
        #include <pthread.h>
        #define  CRITICAL_SECTION   pthread_mutex_t
        #define  _vsnprintf         vsnprintf
    #endif
    //Log{
    #define MAXLOGSIZE 20000000
    #define MAXLINSIZE 16000
    #include <time.h>
    #include <sys/timeb.h>
    #include <stdarg.h>
    char logfilename1[]="MyLog1.log";
    char logfilename2[]="MyLog2.log";
    static char logstr[MAXLINSIZE+1];
    char datestr[16];
    char timestr[16];
    char mss[4];
    CRITICAL_SECTION cs_log;
    FILE *flog;
    #ifdef _MSC_VER
    void Lock(CRITICAL_SECTION *l) {
        EnterCriticalSection(l);
    }
    void Unlock(CRITICAL_SECTION *l) {
        LeaveCriticalSection(l);
    }
    #else
    void Lock(CRITICAL_SECTION *l) {
        pthread_mutex_lock(l);
    }
    void Unlock(CRITICAL_SECTION *l) {
        pthread_mutex_unlock(l);
    }
    #endif
    void LogV(const char *pszFmt,va_list argp) {
        struct tm *now;
        struct timeb tb;
    
        if (NULL==pszFmt||0==pszFmt[0]) return;
        _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
        ftime(&tb);
        now=localtime(&tb.time);
        sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
        sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
        sprintf(mss,"%03d",tb.millitm);
        printf("%s %s.%s %s",datestr,timestr,mss,logstr);
        flog=fopen(logfilename1,"a");
        if (NULL!=flog) {
            fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
            if (ftell(flog)>MAXLOGSIZE) {
                fclose(flog);
                if (rename(logfilename1,logfilename2)) {
                    remove(logfilename2);
                    rename(logfilename1,logfilename2);
                }
            } else {
                fclose(flog);
            }
        }
    }
    void Log(const char *pszFmt,...) {
        va_list argp;
    
        Lock(&cs_log);
        va_start(argp,pszFmt);
        LogV(pszFmt,argp);
        va_end(argp);
        Unlock(&cs_log);
    }
    //Log}
    int main(int argc,char * argv[]) {
        int i;
    #ifdef _MSC_VER
        InitializeCriticalSection(&cs_log);
    #else
        pthread_mutex_init(&cs_log,NULL);
    #endif
        for (i=0;i<10000;i++) {
            Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
        }
    #ifdef _MSC_VER
        DeleteCriticalSection(&cs_log);
    #else
        pthread_mutex_destroy(&cs_log);
    #endif
        return 0;
    }
    //1-79行添加到你带main的.c或.cpp的那个文件的最前面
    //82-86行添加到你的main函数开头
    //90-94行添加到你的main函数结束前
    //在要写LOG的地方仿照第88行的写法写LOG到文件MyLog1.log中
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月2日

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集