我最近开发的一个Release程序总是退出,后来在输出的时候 生成pdb文件以及其调试文件,我自己触发的一个异常,能够通过windbg找到错误代码的位置,但是将程序发布到客户机器上产生的异常文件,dmp文件,再拷贝到开发机器上,跟踪的时候,总是提示找不到相关的模块,
以下是分析的结果,哪位可以看出错误的位置?
.............................................................
This dump file has an exception of interest stored in it.
The stored exception information can be accessed via .ecxr.
(8a4.734): Access violation - code c0000005 (first/second chance not available)
*** WARNING: Unable to verify timestamp for ntdll.dll
*** ERROR: Module load completed but symbols could not be loaded for ntdll.dll
eax=012e0000 ebx=0018ecb0 ecx=00000007 edx=7c95845c esi=0018ec88 edi=0018ece0
eip=7c95845c esp=0012e8d8 ebp=0012e8e8 iopl=0         nv up ei pl zr na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000246
ntdll+0x2845c:
7c95845c c3              ret
0:000> !analyze -v
*******************************************************************************
*                                                                             *
*                        Exception Analysis                                   *
*                                                                             *
************************************************************************************ OS symbols are WRONG. Please fix symbols to do analysis.Unable to load image C:\WINDOWS\system32\kernel32.dll, Win32 error 0n2
*** WARNING: Unable to verify timestamp for kernel32.dll
*** ERROR: Module load completed but symbols could not be loaded for kernel32.dll
Unable to load image C:\WINDOWS\system32\ole32.dll, Win32 error 0n2
*** WARNING: Unable to verify timestamp for ole32.dll
*** ERROR: Module load completed but symbols could not be loaded for ole32.dll
*** WARNING: Unable to verify timestamp for comsvcs.dll
*** ERROR: Module load completed but symbols could not be loaded for comsvcs.dll
Unable to load image C:\WINDOWS\system32\user32.dll, Win32 error 0n2
*** WARNING: Unable to verify timestamp for user32.dll
*** ERROR: Module load completed but symbols could not be loaded for user32.dll
*** WARNING: Unable to verify timestamp for ComServer.exe
*** ERROR: Module load completed but symbols could not be loaded for ComServer.exe
*** WARNING: Unable to verify timestamp for winmm.dll
*** ERROR: Module load completed but symbols could not be loaded for winmm.dll
Unable to load image C:\WINDOWS\system32\rpcrt4.dll, Win32 error 0n2
*** WARNING: Unable to verify timestamp for rpcrt4.dll
*** ERROR: Module load completed but symbols could not be loaded for rpcrt4.dllFAULTING_IP: 
mfc42+27502
75507502 ??              ???EXCEPTION_RECORD:  ffffffff -- (.exr 0xffffffffffffffff)
ExceptionAddress: 75507502 (mfc42+0x00027502)
   ExceptionCode: c0000005 (Access violation)
  ExceptionFlags: 00000000
NumberParameters: 2
   Parameter[0]: 00000000
   Parameter[1]: 00000001
Attempt to read from address 00000001DEFAULT_BUCKET_ID:  WRONG_SYMBOLSPROCESS_NAME:  ComServer.exeADDITIONAL_DEBUG_TEXT:  
You can run '.symfix; .reload' to try to fix the symbol path and load symbols.MODULE_NAME: mfc42FAULTING_MODULE: 7c930000 ntdllDEBUG_FLR_IMAGE_TIMESTAMP:  4d79d59aERROR_CODE: (NTSTATUS) 0xc0000005 - "0x%08lx"EXCEPTION_CODE: (NTSTATUS) 0xc0000005 - "0x%08lx"EXCEPTION_PARAMETER1:  00000000EXCEPTION_PARAMETER2:  00000001READ_ADDRESS:  00000001 FOLLOWUP_IP: 
mfc42+27502
75507502 ??              ???FAILED_INSTRUCTION_ADDRESS: 
mfc42+27502
75507502 ??              ???APP:  comserver.exePRIMARY_PROBLEM_CLASS:  WRONG_SYMBOLSBUGCHECK_STR:  APPLICATION_FAULT_WRONG_SYMBOLSLAST_CONTROL_TRANSFER:  from 00000000 to 75507502STACK_TEXT:  
0012f92c 00000000 7c82bf97 0012f9a0 755e1a10 mfc42+0x27502
STACK_COMMAND:  ~0s; .ecxr ; kbSYMBOL_STACK_INDEX:  0SYMBOL_NAME:  mfc42+27502FOLLOWUP_NAME:  MachineOwnerIMAGE_NAME:  mfc42.dllBUCKET_ID:  WRONG_SYMBOLSFAILURE_BUCKET_ID:  WRONG_SYMBOLS_c0000005_mfc42.dll!UnknownWATSON_STAGEONE_URL:  http://watson.microsoft.com/StageOne/ComServer_exe/1_0_0_1/51cbf2f6/mfc42_dll/6_6_8064_0/4d79d59a/c0000005/00027502.htm?Retriage=1Followup: MachineOwner
---------

解决方案 »

  1.   

    有时不写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #ifdef WIN32
        #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 WIN32
    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 WIN32
        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 WIN32
        DeleteCriticalSection(&cs_log);
    #else
        pthread_mutex_destroy(&cs_log);
    #endif
        return 0;
    }
    //1-78行添加到你带main的.c或.cpp的那个文件的最前面
    //81-85行添加到你的main函数开头
    //89-93行添加到你的main函数结束前
    //在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中例如
    //循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
    //a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
    //在处理的时候每次从新buffer中取两个字节打印
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <process.h>
    #include <io.h>
    //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;
    void Lock(CRITICAL_SECTION *l) {
        EnterCriticalSection(l);
    }
    void Unlock(CRITICAL_SECTION *l) {
        LeaveCriticalSection(l);
    }
    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}
    #define ASIZE    200
    #define BSIZE    240
    #define CSIZE      2
    char Abuf[ASIZE];
    char Cbuf[CSIZE];
    CRITICAL_SECTION cs_HEX ;
    CRITICAL_SECTION cs_BBB ;
    struct FIFO_BUFFER {
        int  head;
        int  tail;
        int  size;
        char data[BSIZE];
    } BBB;
    int No_Loop=0;
    void HexDump(int cn,char *buf,int len) {
        int i,j,k;
        char binstr[80];    Lock(&cs_HEX);
        for (i=0;i<len;i++) {
            if (0==(i%16)) {
                sprintf(binstr,"%03d %04x -",cn,i);
                sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            } else if (15==(i%16)) {
                sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
                sprintf(binstr,"%s  ",binstr);
                for (j=i-15;j<=i;j++) {
                    sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
                }
                Log("%s\n",binstr);
            } else {
                sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            }
        }
        if (0!=(i%16)) {
            k=16-(i%16);
            for (j=0;j<k;j++) {
                sprintf(binstr,"%s   ",binstr);
            }
            sprintf(binstr,"%s  ",binstr);
            k=16-k;
            for (j=i-k;j<i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            Log("%s\n",binstr);
        }
        Unlock(&cs_HEX);
    }
    int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
        int lent,len1,len2;    lent=0;
        Lock(cs);
        if (fbuf->size>=len) {
            lent=len;
            if (fbuf->head+lent>BSIZE) {
                len1=BSIZE-fbuf->head;
                memcpy(buf     ,fbuf->data+fbuf->head,len1);
                len2=lent-len1;
                memcpy(buf+len1,fbuf->data           ,len2);
                fbuf->head=len2;
            } else {
                memcpy(buf     ,fbuf->data+fbuf->head,lent);
                fbuf->head+=lent;
            }
            fbuf->size-=lent;
        }
        Unlock(cs);
        return lent;
    }
    void thdB(void *pcn) {
        char        *recv_buf;
        int          recv_nbytes;
        int          cn;
        int          wc;
        int          pb;    cn=(int)pcn;
        Log("%03d thdB              thread begin...\n",cn);
        while (1) {
            Sleep(10);
            recv_buf=(char *)Cbuf;
            recv_nbytes=CSIZE;
            wc=0;
            while (1) {
                pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
                if (pb) {
                    Log("%03d recv %d bytes\n",cn,pb);
                    HexDump(cn,recv_buf,pb);
                    Sleep(1);
                } else {
                    Sleep(1000);
                }
                if (No_Loop) break;//
                wc++;
                if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
            }
            if (No_Loop) break;//
        }
    }
    int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
        int lent,len1,len2;    Lock(cs);
        lent=len;
        if (fbuf->size+lent>BSIZE) {
            lent=BSIZE-fbuf->size;
        }
        if (fbuf->tail+lent>BSIZE) {
            len1=BSIZE-fbuf->tail;
            memcpy(fbuf->data+fbuf->tail,buf     ,len1);
            len2=lent-len1;
            memcpy(fbuf->data           ,buf+len1,len2);
            fbuf->tail=len2;
        } else {
            memcpy(fbuf->data+fbuf->tail,buf     ,lent);
            fbuf->tail+=lent;
        }
        fbuf->size+=lent;
        Unlock(cs);
        return lent;
    }
    void thdA(void *pcn) {
        char        *send_buf;
        int          send_nbytes;
        int          cn;
        int          wc;
        int           a;
        int          pa;    cn=(int)pcn;
        Log("%03d thdA              thread begin...\n",cn);
        a=0;
        while (1) {
            Sleep(100);
            memset(Abuf,a,ASIZE);
            a=(a+1)%256;
            if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
            send_buf=(char *)Abuf;
            send_nbytes=ASIZE;
            Log("%03d sending %d bytes\n",cn,send_nbytes);
            HexDump(cn,send_buf,send_nbytes);
            wc=0;
            while (1) {
                pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
                Log("%03d sent %d bytes\n",cn,pa);
                HexDump(cn,send_buf,pa);
                send_buf+=pa;
                send_nbytes-=pa;
                if (send_nbytes<=0) break;//
                Sleep(1000);
                if (No_Loop) break;//
                wc++;
                if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
            }
            if (No_Loop) break;//
        }
    }
    int main() {
        InitializeCriticalSection(&cs_log );
        Log("Start===========================================================\n");
        InitializeCriticalSection(&cs_HEX );
        InitializeCriticalSection(&cs_BBB );    BBB.head=0;
        BBB.tail=0;
        BBB.size=0;    _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
        _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);    if (!access("No_Loop",0)) {
            remove("No_Loop");
            if (!access("No_Loop",0)) {
                No_Loop=1;
            }
        }
        while (1) {
            Sleep(1000);
            if (No_Loop) break;//
            if (!access("No_Loop",0)) {
                No_Loop=1;
            }
        }
        Sleep(3000);
        DeleteCriticalSection(&cs_BBB );
        DeleteCriticalSection(&cs_HEX );
        Log("End=============================================================\n");
        DeleteCriticalSection(&cs_log );
        return 0;
    }
      

  2.   

    使用windbg调试时,需要注意的是pdb的版本与产生dump的exe或dll版本必须一致(保证是同一时间编译后生成的)。
    有时候只加载自己的pdb还看不出问题所在,需要加载微软的pdb:
    在symbols输入框里,除了自己pdb的路径外,还应输入“srv*sym*http://msdl.microsoft.com/download/symbols”,自动加载系统模块所需的dll。
    输入pdb路径后,最好再用命令reload -f强制加载pdb。
    另外,一般不通过命令行来分析,因为windbg有各种窗口。我一般打开进程窗口、堆栈窗口和源代码窗口进行分析。