Visual Studio函数调试

Visual Studio Function Debugging

本文关键字:调试 函数 Studio Visual      更新时间:2023-10-16

我正在开发VS 2008。我想获得以下所有方法的信息:

1)呼叫进入时间

2)调用退出时间和返回值。

GDB允许我在每个函数入口和退出处设置断点,并在断点处运行脚本,然后继续调试。我厌倦了寻找解决方案来做类似的VS.我甚至想到写一个脚本来解析我的整个代码,并在进入和退出时编写fprintf,但这是非常复杂的。迫切寻求帮助

使用windbg,您还可以在每个函数入口设置并运行脚本。例如,下面的命令将在模块的所有函数上添加一个断点,显示函数的名称,当前时间,运行直到函数退出,显示时间并继续。

bm yourmodule!* "kcL1;.echotime;gu;.echotime;gc"

基本上这是一个功能级别的基于时间的分析(TBP)。以下工具可以帮助您:

  • Visual Studio Profiling Tools:仅在Visual Studio Ultimate和Premium版本中可用。http://msdn.microsoft.com/en-us/library/z9z62c29.aspx
  • Intel vTune:它可以做很多事情,包括功能级分析。http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/
  • AMD CodeAnalyst:这是一个免费的工具。它也可以与英特尔CPU一起工作(功能有限,但足以满足您的目的)。它可以做源代码级别的分析:http://developer.amd.com/cpu/codeanalyst/codeanalystwindows/pages/default.aspx

我建议您先尝试使用AMD CodeAnalyst。如果你没有Visual Studio高级版或终极版。

我假设您正在使用c++。您可以定义一个显示时间戳

的时间跟踪类。
/* define this in a header file */
class ShowTimestamp {
private:
    static int level_;   // nested level for function call tree
private:
    const char *func_;
public:
    ShowTimestamp(const char* f) : func_(f) {
        std::cout << func_ << ":" << (level_++) << ":begint" << GetTickCount64() << std::endl;
    }
    ~ShowTimestamp() {
        std::cout << func_ << ":" << (--level_) << ":endt" << GetTickCount64() << std::endl;
    }
};
#ifndef NO_TRACE_TIMER
  #define TIMESTAMP_TRACER     ShowTimestamp _stt_(__FUNCTION__); 
#elif
  #define TIMESTAMP_TRACER
#endif

level_应该在CPP文件中单独声明。

// You need to define the static member in a CPP file         
int ShowTimestamp::level_ = 0;
在您的代码中,您可以执行
int Foo(int bar) {
  TIMESTAMP_TRACER 
  // all the other things.
  ......
  return bar;
}

如果您不想再跟踪计时器,您可以定义NO_TRACE_TIMER

Visual Studio不适合;你必须使用WinDbg。它有自己的脚本语言,可以让你做你想做的事情。不幸的是,我对它的脚本语言一无所知;您必须阅读帮助文件(这实际上或多或少有用一次)。