开发者

Visual Studio Function Debugging

I am working on VS 2008. I wish to get the following information for all my methods:

1)开发者_C百科 Time at call entry

2) Time at call exit and the return value.

GDB allows me to set a break point at each function entry and exit and run a script at the breakpoint and then continue debugging. I am tired of looking for solutions to do something similar on VS. I even thought of writing a script to parse my entire code and write fprintf's at entry and exit but this is very complex. Desperately looking for help.


using windbg, you can also set at each function entry and run a script. For instance the following command will add a breakpoint on all functions of your module, display the name of the function, the current time, run until the function exit, display the time and continue.

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


Basically this is a function level Time-Based Profiling (TBP). Several tools can help you on this:

  • Visual Studio Profiling Tools: which is available with Visual Studio Ultimate and Premium version only. http://msdn.microsoft.com/en-us/library/z9z62c29.aspx
  • Intel vTune: It can do lots of things, including function level profiling. http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/
  • AMD CodeAnalyst: it is a free tool. It can work with Intel CPU as well (with limited function, but enough for your purpose). It can do source code level profiling: http://developer.amd.com/cpu/codeanalyst/codeanalystwindows/pages/default.aspx

I suggest you to try with AMD CodeAnalyst first. If you don't have Visual Studio Premium or Ultimate edition.


I assume you are suing c++. You can define a time trace class which display the timestamps

/* 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_++) << ":begin\t" << GetTickCount64() << std::endl;
    }

    ~ShowTimestamp() {
        std::cout << func_ << ":" << (--level_) << ":end\t" << GetTickCount64() << std::endl;
    }
};

#ifndef NO_TRACE_TIMER
  #define TIMESTAMP_TRACER     ShowTimestamp _stt_(__FUNCTION__); 
#elif
  #define TIMESTAMP_TRACER
#endif

The level_ should be declared in a CPP file separately.

// You need to define the static member in a CPP file         
int ShowTimestamp::level_ = 0;

In your code, you can do

int Foo(int bar) {
  TIMESTAMP_TRACER 

  // all the other things.
  ......

  return bar;
}

If you don't want to trace timer any longer, you can just define NO_TRACE_TIMER


Visual Studio is not suited for this; you would have to use WinDbg. It has its own scripting language that would allow you to do what you are seeking. Unfortunately I don't know the first thing about it's scripting language; you will have to read the help file (which is actually more-or-less useful, for once).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜