Pierre
Pierre

Reputation: 2090

How to get the stacktrace of a DLL who crashes?

I develop some DLLs for an application and I would like to know if it is possible to get the stacktrace (in a log file) of my DLL when it crashes and then without make any modification in the code of the main application.

Here is a dummy example of a DLL I want to log the stacktrace when it crashes:

HelloDLL.h

#pragma once

//more about this in reference 1
#ifdef DLLDIR_EX
   #define DLLDIR  __declspec(dllexport)   // export DLL information

#else
   #define DLLDIR  __declspec(dllimport)   // import DLL information

#endif 

class DLLDIR HelloDLL
{
public:
    HelloDLL(void);
    ~HelloDLL(void);

    void crash();

private:
    void danger();
};

HelloDLL.cpp

#include "stdafx.h"
#include "HelloDLL.h"
#include <iostream>

using namespace std; 

HelloDLL::HelloDLL(void)
{
}

HelloDLL::~HelloDLL(void)
{
}

void HelloDLL::crash()
{
    danger();
}

void HelloDLL::danger()
{
    abort();
}

And the application I cannot change:

#include "stdafx.h"
#include "HelloDLL.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    HelloDLL helloDLL;
    helloDLL.crash();

    getchar();

    return 0;
}

I used this website to create this example.

In other words, how can I get the maximum information from my DLL when it crashes in order to facilitate the debugging process?

Upvotes: 0

Views: 1907

Answers (1)

Pierre
Pierre

Reputation: 2090

As said in comments, the best way to get the stack trace of a crashed process is to generate a dump file. To do it, you first need to edit the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps registry key. Here is an example to get a full dump of the Hello.exe application:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\Hello.exe]
"DumpCount"=dword:a
"DumpType"=dword:2

More informations about the available values can be found here. By default, the dump files will be store in %LOCALAPPDATA%\CrashDumps.

Then, you need to compile your application without any optimization option and you have to generate the .pdb file (with the /DEBUG option).

You can now run your program and get to dump file in the CrashDumps directory. To read it, you can use VisualStudio (but also other applications like WinDbg, for instance):

  1. Open the .dmp file with VisualStudio (you can drop the file into the opened solution)
  2. Add the .pdb file into the configuration of VisualStudio (Tools > Option > Debugging > Symbols)
  3. Click on "Run with native only"

You can now get the stack trace of all of your threads when the crash occurs but also read the value of the variables before your program stopped.

Upvotes: 1

Related Questions