[aspectc-user] 2 questions: "within" a function; call vs execution context

Mike Mortensen mmo at fc.hp.com
Thu Nov 15 18:09:51 CET 2007

I think these might both be simple questions, but both are involve
practical, everyday types of things to do with aspects that I thought I 
do and then ran into problems with!

Suppose I have a Circle class, that has 2 methods, getArea and getPerimeter,
that call a function -- getPI() -- which I use to decide how precise the 
value PI should be, like this:

double getPI() {
   return 3.1415;

class Circle {
  double radius, x, y;
     double getArea() {
       return radius * radius * getPI();
    double getPerimeter() {
       return 2.0 * radius * getPI();


If I want to advise (i.e. trace or whatever) the calls to getPI() within 
the Circle
class,I can do that easily using within:

aspect AdvisePI {

   pointcut call_pi_within_Circle() =
      call("% getPI()") && within("Circle");

   advice call_pi_within_Circle() : before()
      cerr << "AOP: About to call PI within Circle class, JoinPoint="
           << JoinPoint::signature() << endl;

Thus, *any* call to getPI() from within the Circle class gets traced.

Here's my question -- what if I also have a procedural version of 
getArea, like this:

float CircleArea(float r)
   return r * r * getPI();

and I would like to trace getPI()  but only when it's called within the 
CircleArea function.
I know I could use 'cflow', but that is extra overhead (i.e. it advises 
*all* calls to getPI and
then at run time checks to see if it's within the execution of CircleArea).
Also, my real goal is to advise *all* function calls within some other 
function (like CircleArea)
for tracing.  But I don't want *every* function in the system to have 
advise that at run time
checks cflow.

I've tried advising getPI within Circle area a couple of ways:

  pointcut call_pi_within_CircleArea1() =
      call("% getPI()") && within("% %::CircleArea(...)");

   pointcut call_pi_within_CircleArea2() =
      call("% getPI()") && within("CircleArea");

and also tried using within in conjuction with a named pointcut, but the 
weave says this is illegal:
   pointcut CircleAreaExec() = execution("% CircleArea(...)");
   pointcut call_pi_within_CircleArea3() =
      call("% getPI()") && within(CircleAreaExec());  /* ILLEGAL !  */

To summarize:

    QUESTION #1: Can I use within (or other static mechanism, not cflow) 
to trace all calls to getPI()
      within another function, rather than within a class or namespace?

For question #2, consider calls to getPI in the circle class and within 
the CircleArea() function.
Whether I use a call pointcut or execution pointcut, when I print out 
within the advice, it is:
   double getPI()

This is fine...but what I really want is the name of the caller.  It 
would be nice to print out
which function was about to call getPI(), but I can't seem to do that, 
even when using the calling
pointcut so that I'm 'intercepting' the calls within the caller rather 
than at the execution.
I know that with the calling context I can get the object invoking the 
method -- but can I get
the name of the method about to call getPI()? 

To summarize:

   QUESTION #2:  When using a call pointcut where multiple methods call 
that joinpoint, is there
       some way to print out not the matching function being called 
(getPI) but instead the
       actual caller's name -- Circle::getArea() or Circle::getPerimeter()?


PS  I'm attaching my aspect file and code in case it helps clarify any 
of these questions...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cc
Type: text/x-c++src
Size: 2029 bytes
Desc: not available
URL: <http://www.aspectc.org/pipermail/aspectc-user/attachments/20071115/19b75b1c/attachment.cc>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: AdvisePI.ah
URL: <http://www.aspectc.org/pipermail/aspectc-user/attachments/20071115/19b75b1c/attachment.ksh>

More information about the aspectc-user mailing list