本文介绍了方面c++跟踪函数控制流程和输入输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用aspectc++来生成程序的控制流.
I am using aspectc++ to generate control flow of the program.
trace.ah
:
#ifndef __trace_ah__
#define __trace_ah__
#include <cstdio>
// Control flow tracing example
aspect trace {
// print the function name before execution starts
pointcut virtual methods() = "% ...::%(...)";
advice execution (methods()) : before () {
cout << "entering: " << JoinPoint::signature() << endl;
// print input parameters**
}
advice execution(methods()) : after() {
// print output parameters**
cout << "leaving: " << JoinPoint::signature() << endl;
}
//prints return values of non void functions
advice execution("% ...::%(...)" && !"void ...::%(...)") : after()
{
JoinPoint::Result res = *tjp->result();
cout << "leaving " << tjp->signature()<< " << returning value--" << res<<endl;
}
};
};
#endif
问题:
1.如何打印作为参数传递给函数的变量本身?
Question:
1.How to print the variables itself that are passed as arguments to the functions ?
2.如何打印每个函数的输入参数值?
2.How do I print the input parameters values of each function ?
推荐答案
#ifndef __trace_ah__
#define __trace_ah__
#include <cstdio>
#include <iostream>
using namespace std;
template <int I> struct ArgPrinter
{
template <class JP> static inline void work (JP &tjp) {
ArgPrinter<I - 1>::work (tjp);
cout << "Arg " << I << ": " << *tjp.template arg<I - 1> () << endl;
}
};
template <> struct ArgPrinter<0>
{
template <class JP> static inline void work (JP &tjp) {}
};
// Control flow tracing example
aspect trace {
pointcut virtual methods() = "% ...::%(...)";
template <class JP> void print_args (JP &tjp)
{
ArgPrinter<JP::ARGS>::work (tjp);
}
advice execution (methods()) : before ()
{
cout << "entering: " << JoinPoint::signature() << endl;
tjp->arg(0);
print_args (*tjp);
}
advice execution("% ...::%(...)" && !"void ...::%(...)") : after()
{
JoinPoint::Result res = *tjp->result();
cout << "leaving " << tjp->signature()<< " << returning value--" << res<<endl;
}
};
#endif
这篇关于方面c++跟踪函数控制流程和输入输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!