Call Stack at Runtime

11,406

Solution 1

I believe that this page has the answer you are looking for. You said Visual C so I assume you mean windows.

Solution 2

Have a look at StackWalk64.

If you're used to doing this on .NET, then you're in for a nasty surprise.

Solution 3

You should consider setting your unhandled exception filter and writing a minidump file from within it. It is not all that complicated and is well documented. Just stick to the minimum of things you do once in your unhandled exception filter (read what can all go wrong if you get creative).

But to be on the safe side (your unhandled exception filter might get inadvertently overwritten), you could put your code inside __try/__except block and write the minidump from within the filter function (note, you cannot have objects that require automatic unwinding in a function with __try/__except block, if you do have them, consider putting them into a separate function):

long __stdcall myfilter(EXCEPTION_POINTERS *pexcept_info)
{
    mycreateminidump(pexcept_info);
    return EXCEPTION_EXECUTE_HANDLER;
}
void myfunc()
{
__try{
    //your logic here
} __except(myfilter(GetExceptionInformation())) {
    // exception handled
}
}

You can then inspect the dump file with a debugger of your choice. Both Visual Studio and debuggers from Windows Debugging Tools package can handle minidumps.

Solution 4

If you want to get a callstack of the crash, what you really want to do is post mortem debugging. If you want to check a callstack of application while it is running, this is one of many functions SysInternals Process Explorer can offer.

Share:
11,406

Related videos on Youtube

Agnel Kurian
Author by

Agnel Kurian

Software Engineer with 18+ years of experience developing software in a wide range of areas: desktop, web, user interfaces, 2D/3D graphics, geometry, encryption and even structural analysis! I have a degree in Civil Engineering and a diploma from NIIT. I'm very comfortable on Linux. I like solving interesting problems.

Updated on June 04, 2022

Comments

  • Agnel Kurian
    Agnel Kurian almost 2 years

    I want to access the call stack at runtime in a Native C++ application. I am not using the IDE. How do I display the call stack?

    Update: I have a function which is called from many points all over the application. It crashes on rare occasions. I was looking for a way to get name of the caller and log it.