gpt4 book ai didi

c++ - aspect c++ 跟踪函数控制流和输入输出参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:16:06 25 4
gpt4 key购买 nike

我正在使用 aspectc++ 来生成程序的控制流。

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.如何打印作为参数传递给函数的变量本身?

2.如何打印每个函数的输入参数值?

最佳答案

#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++ - aspect c++ 跟踪函数控制流和输入输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33386461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com