Call Stack Function in C ++

I tried the following links: from StackOverflow and other sites, [I tried, but it didn’t help me, so I can’t avoid duplication)

StackWalk64 on Windows - get character name

How to make StackWalk64 () work successful on x64?

http://www.codeproject.com/KB/threads/StackWalker.aspx

http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/

How to record stacks using Windows x64 ...

But none of the codes worked for me. I am new to Windows C ++ and I cannot get any of the above codes to work.

I am looking for a call stack format like
FUNCTION_NAME_DEPTH_1: _LINE_NUM__
FUNCTION_NAME_DEPTH_1: _LINE_NUM__
FUNCTION_NAME_DEPTH_1: _LINE_NUM __...

Just the function name and line numbers.

My environment:
Visual studio 2010
SDK: v7.1
Windows 7 Pro SP1

It would be very simple if someone posted a header file, [it seems there are few available but not working] that we can include in our cpp file and print a call stack with a call like PrintFunctionCallStack (); ', BTW on Linux / Mac, it was much easier, I was able to get the call stack from backtrace, and it was so simple that I did it myself in a few minutes. On Windows, I tried the last two days, but not surprisingly.

Linux / Mac Stack trace code, I haven't mixed up the symbol names yet.

#ifndef _STACKTRACE_H_ #define _STACKTRACE_H_ #include <stdio.h> #include <stdlib.h> #include <execinfo.h> #include <cxxabi.h> #include <iostream> static inline void PrintStackTrace() { cout<<"##############################################\n"; unsigned int maxStackCount = 63; void* addressList[maxStackCount+1]; int addrLen = backtrace(addressList, sizeof(addressList) / sizeof(void*)); if (addrLen == 0) { cout<<"Empty Stack, Probably Corrupted it seems ###\n"; return; } char** symbolList = backtrace_symbols(addressList, addrLen); for (int i = 1; i < addrLen; i++) // Skipped First, 'i' begins with '1' { cout<<"###: "<<symbolList[i]<<":###\n"; } free(symbolList); cout<<"##############################################\n"; } #endif 
+4
source share
1 answer

If your environment is Visual Studio, you can insert Tracepoint and enter

 $CALLSTACK 

in the edit box after checking the print message.

To do this, right-click on the desired line and choose Breakpoint> Insert Breakpoint (or, conversely, insert a breakpoint by clicking to the left of the desired line in the editor, then select On Hit).

Then you will see a detailed report in the "Exit" window with the file name, line number and function name. This helped me successfully detect memory leaks.

+1
source

Source: https://habr.com/ru/post/1485838/


All Articles