[aspectc-user] tracing object creation

Olaf Spinczyk os at aspectc.org
Wed Nov 11 12:39:40 CET 2020


Hi Darrel,

does this go into the right direction? ...

===
#include <iostream>
using namespace std;

namespace casa {
  class C1 {
  public:
    void foo();
  };
  class C2 {
    C1 _member;
  public:
    C2 (int p) {}
    void bar () { cout << "in bar" << endl; C2 c2(0); C1 c1; }
  };
}

int main () {
  casa::C1 c1;
  casa::C2 c2(42);
  c2.bar();
}

aspect TraceCasaConstruction {
  advice construction("casa::%") && cflow(execution("%
casa::%::%(...)")) : before () {
    cout << JoinPoint::signature() << endl;
  }
};
===

It can be compiled with "ag++ -k -a0 -o casa casa.cc". When you run it,
the output is ...

in bar
casa::C1::C1()
casa::C2::C2(int)
casa::C1::C1()

As you can see, the object construction triggered directly by main() is
ignored, but the object construction triggered by casa::C2::bar() is traced.

I don't know if that is a problem in your case, but the implementation
of the cflow feature is not thread safe.

It would be better to have a kind of call-site advice for object
construction, but this is not implemented, yet.

Best regards,
Olaf

Am 11.11.20 um 02:01 schrieb Darrell Schiebel:
> 
> Hello,
> 
> I would like to use Aspect C++ to track the construction of objects in a
> specific namespace (in this example "casa"). I want to have a message
> printed each time an object is created from one of the classes in the
> namespace.
> 
> I have gone through the reference manual but I am still having trouble
> understanding how one narrows the methods down to only the constructors.
> This is what I've come up with based on an example from the reference
> manual:
> 
>     aspect Trace {
> 
>            pointcut methods() = "casa::...::;
> 
>            advice execution(methods()) : before() {
> 
>                std::cout << "executing " << JoinPoint::signature() << "(";
> 
>                for (unsigned i = 0; i < JoinPoint::args(); i++)
> 
>                    printvalue(tjp->arg(i), JoinPoint::argtype(i));
> 
>                cout << ")" << endl;
> 
>                tjp->proceed();
> 
>                cout << "after" << endl;
> 
>            }
> 
>     };
> 
> 
> 
> but I am sure it falls short.
> 
> Thanks for any advice.
> 
> Darrell
> 
> 
> _______________________________________________
> aspectc-user mailing list
> aspectc-user at mail.aspectc.org
> https://www.aspectc.org/cgi-bin/mailman/listinfo/aspectc-user
> 


More information about the aspectc-user mailing list