Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make QT Application write a crash log

Tags:

c++

windows

qt

i am working on project developed in Qt, Windows, i use Qt Version 4.1.4 and VC6 IDE for compiling project, most of usage of product are on WindowsXp.

i frequently receive crash reports from testers and customers which does not have fix pattern and does not reproduce in my development environment.

so i come to the conclusion that i need to write a crash log from my program so that i always get the call stack when program is crashed.

in linux i know its possible to do it with some signal handling of segmentation fault. but how to do the same thing in windows/C++?

i tried to search on Google to find some ready made solution but without any luck.

Suggestion regarding any third party tools/libraries (free or licensed) are also accepted, but please do not suggest any method where i have to modify each function in my module to get log, because the project is huge and i can not afford that.

thanks in advance.

updates. I have attached StackWalker to my Code now (with bit of hassle of _MBCS & UNICODE) but atleast managed to attach it.

my main file looks like below,

class MyStackWalker : public StackWalker
{  
   FILE *sgLogFile;
   public:
   MyStackWalker() : StackWalker() 
   {
     sgLogFile = fopen("ThisIsWhatIcallLog.log", "w");
   }
   MyStackWalker(DWORD dwProcessId, HANDLE hProcess) : StackWalker(dwProcessId, hProcess) 
   {
   }
   virtual void OnOutput(LPCSTR szText) 
   { 
      printf(szText); 
      fprintf(sgLogFile, "%s",szText);
      fclose(sgLogFile);
      StackWalker::OnOutput(szText); 
   }
};

LONG Win32FaultHandler(struct _EXCEPTION_POINTERS *  ExInfo)
{
    MyStackWalker sw;   
    sw.ShowCallstack();
    return EXCEPTION_EXECUTE_HANDLER;
}

void InstallFaultHandler()
{
    SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER) Win32FaultHandler);
}

But i get linker error now that symbols are not found,

main.obj : error LNK2001: unresolved external symbol "public: int __thiscall StackWalker::ShowCallstack(void *,struct _CONTEXT const *,int (__stdcall*)(void *,unsigned __int64,void *,int,unsigned long *,void *),void *)" (?ShowCallstack@StackWalker@@QAEHPAXPBU_CONTEXT@@P6GH0_K0HPAK0@Z0@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnDbgHelpErr(char const *,int,unsigned __int64)(?OnDbgHelpErr@StackWalker@@MAEXPBDH_K@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnLoadModule(char const *,char const *,unsigned __int64,int,int,char const *,char const *,unsigned __int64)" (?OnLoadModule@StackWalker@@MAEXPBD0_KHH001@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnSymInit(char const *,int,char const *)" (?OnSymInit@StackWalker@@MAEXPBDH0@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall StackWalker::StackWalker(int,char const *,int,void *)" (??0StackWalker@@QAE@HPBDHPAX@Z)
release/Prog.exe : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.

I have modified the StackWalker.cpp file to adapt to the UNICODE support, replaced "_tcscat_s" with "wcscat".

can you see, what's going wrong here?

like image 389
maxchirag Avatar asked Aug 08 '11 06:08

maxchirag


2 Answers

Google Breakpad is a cross-platform crash-report library that seems useful.

See also the StackWalker for some windows-only code for getting a stack trace.

Also, installing an event filter by overriding QCoreApplication::notify and catch all exceptions (including asyncronous ones such as stack overflow and access violation on Windows) is a good idea.

like image 64
Macke Avatar answered Nov 11 '22 01:11

Macke


For cross-platform application Breakpad is probably better solution. But easy way for Windows -is to set unhandled exception handler via SetUnhandledExceptionFilter and write minidumps (with configuration you need) with WriteMiniDumpEx in handler.

example

like image 21
elevener Avatar answered Nov 11 '22 02:11

elevener