How to locate Bug (WinDbg)——Qt

 1 Introduction

In the process of writing programs, various bugs will inevitably appear. How to quickly locate the bug is one of the necessary skills for programmers.

2. Several methods

2.1. Logical analysis

Based on the problems in the program, analyze several locations where the problem may be located, find the bugs through logical analysis, and solve the bugs. Many bugs are caused by carelessness and can generally be found through logical analysis.

2.2. VS debugging

I have been using Qt for development since graduation, so I am more accustomed to using Qt Creator to write code, but it is really difficult to use for debugging. Many times it crashes in the Qt Creator environment and I can’t find the location. There are no useful tips at all. At that time, I will configure the VS environment and debug on VS. Generally, you can see where it is dead, or which library is missing, and the real yyds of VS debugging.

2.3. WinDbg (dump file)

There is another situation: the program is not in your own hands, such as in the hands of a test or user, and something suddenly goes wrong. In addition to analyzing the problem logically, you can also add code to save the dump file to the program during development, and tools can be used when problems arise. Open WinDbg to view the bug location.

The following describes the usage:

2.3.1 Download WinDbg Preview

WinDbg Preview is a new version of WinDbg, with more modern visual effects, faster windows and mature scripting experience. The download address is below and you can install it in a fool-proof way.

Download Debugging Tools for Windows - WinDbg - Windows drivers | Microsoft Learn

2.3.2. Add code to save Dump files 

 First include it in the pro file

  QMAKE_CXXFLAGS_RELEASE += $$QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO
  QMAKE_LFLAGS_RELEASE += $$QMAKE_LFLAGS_RELEASE_WITH_DEBUGINFO
  LIBS += -lDbgHelp

Then add in the main.cpp file

long  __stdcall CrashInfocallback(_EXCEPTION_POINTERS *pexcp)
{
    QString appName = QCoreApplication::applicationDirPath();
    appName.append("/dmpDir");
    QDir dir(appName);
    if(!dir.exists()){
        dir.mkpath(appName);
    }
    appName.append("/")
            .append(QCoreApplication::applicationName())
            .append(".")
            .append(QString::number(QDateTime::currentMSecsSinceEpoch()))
            .append("_CRASH_DUMP.DMP");

    //create Dump file
    HANDLE hDumpFile = ::CreateFile(
        (LPCWSTR)appName.utf16(),
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if (hDumpFile != INVALID_HANDLE_VALUE)
    {
        //Dump info
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pexcp;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;
        //write Dump
        ::MiniDumpWriteDump(
            GetCurrentProcess(),
            GetCurrentProcessId(),
            hDumpFile,
            MiniDumpNormal,
            &dumpInfo,
            NULL,
            NULL
        );
    }
    return 0;
}
int main(int argc, char *argv[])
{
    ::SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)CrashInfocallback);

    QApplication a(argc, argv);

    ...

    return a.exec();
}

2.3.3. Error analysis

After an error occurs, a dump file will be generated in the corresponding directory.

(1) If WinDbg Preview is installed, double-click the dump file and WinDbg Preview will open.

(2) Configuration path: You can click Settings on the toolbar or click Settings under File

      (Figure 1), and then configure the path as shown in Figure 2.

 (3) Click  !analyze -v to analyze, as shown in Figure 3.

  (4) After the analysis is completed, you can see the results, as shown in Figure 4.

figure 1

figure 2 

image 3 

 

 Figure 4

 3. Conclusion

 At present, these are the only methods I have mastered. I use different methods to locate bugs according to different situations!

Guess you like

Origin blog.csdn.net/xiaopei_yan/article/details/128445733