函数调用栈在C ++中 [英] Function Call Stack in C++

查看:109
本文介绍了函数调用栈在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下链接,从StackOverflow和其他网站,[我试过,但没有帮助我,所以我不能避免重复]

I have tried the following links, from StackOverflow and other sites,[I tried, but it didn't helped me, so i can't avoid duplicating]

Windows上的StackWalk64 - 获取符号名称

如何使StackWalk64()在x64上成功工作?

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

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

如何使用Windows x64记录堆栈框架
...

How to Log Stack Frames with Windows x64 ...

但没有一个代码为我工作。我是Windows C ++环境的新手,我无法获得上述任何代码。

But none of the Code worked for me.I'm new to Windows C++ environment and i can't get any of the above code to work.

我正在寻找一个电话堆栈格式,如:

FUNCTION_NAME_DEPTH_1:_LINE_NUM__

FUNCTION_NAME_DEPTH_1:_LINE_NUM__

FUNCTION_NAME_DEPTH_1:_LINE_NUM__ ...

I'm 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__ ...

只需函数名和行号。

我的环境:

Visual Studio 2010

SDK:v7.1

Windows 7 Pro SP1

My Environment:
Visual Studio 2010
SDK : v7.1
Windows 7 Pro SP1

如果有人发布头文件,那将会很简单,似乎几乎没有可用,但不工作],我们可以包含在我们的cpp文件中,并使用PrintFunctionCallStack();这样的调用打印调用堆栈。 BTW在Linux / Mac,这是一个很容易,我能够从回溯得到调用堆栈,它是如此简单,我自己做了几分钟。在Windows中,我一直在尝试过去两天,但根本没有任何惊喜。

It would be a lot simple if anyone post a header file,[there seems to be few available,but not working] which we can include in our cpp file and print the call stack with a call like 'PrintFunctionCallStack();' . BTW in Linux/Mac, it was a whole lot easier,i was able to get the call stack from backtrace and it was so simple that i did it myself in few mins. In Windows i've have been trying past two days, but no surprise at all.

Linux / Mac Stack Trace Code,我还没有将符号名称拆除。

Linux/Mac Stack Trace Code, i haven't yet demangled the symbol names.

#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


推荐答案

如果您的环境是Visual Studio,您可以插入一个跟踪点并输入

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

$CALLSTACK

在其编辑框中,检查打印消息后。

in its edit box, after checking Print a message.

要做到这一点,右键单击所需的行,然后选择断点>插入断点(或者插入断点,点击您想要的编辑器行左侧) ,然后选择当命中)。

To do it, right-click on the line you want and select Breakpoint > Insert Breakpoint (or alternatively, insert a breakpoint clicking on the left of the editor line you want, then select When Hit).

然后,您将在输出窗口中看到一个详细的报告,具有文件名,行号和功能名称。它为我成功地发现了一些内存泄漏。

Then you will see a detailed report in the Output window, having file name, line number and function name. It served me to successfully discovered some memory leaks.

这篇关于函数调用栈在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆