[aspectc-user] Help Needed

Olaf Spinczyk olaf at ivs.cs.uni-magdeburg.de
Tue Feb 5 12:03:16 CET 2002


Hi,

On Tuesday,  5. February 2002 05:57, you wrote:
> Hi Olaf,
>
> The workaround solution you gave yesterday is working fine. Thanks a lot.
> Now I am trying to advance a bit by printing the value of the function
> signature. But the output is different from what I expected. I have given
> the aspect and class file source code below.
>
> Aspect code
> =========
>
> #include<stdio.h>
>
> aspect methodfunctions
> {
> 	pointcut move() = execution("%  Maths::calladd(int)");
>
> 	public:
>
> 		advice move() : void before()
> 		{
> 			const char *unused = thisJoinPoint->toString ();
> 			printf("ID OF THE JOIN POINT = %d ",
> thisJoinPoint->id());
> 			printf("VALUE OF THE VARIABLE IS= %d",
> thisJoinPoint->arg(0));
> 		}
> };
>
> Source code
> ==========
>
> # include<stdio.h>
>
> class Maths
> {
> 	public:
>
> 	int tmpNum1;
> 	int tmpNum2;
>
> 	public:
>
> 	Maths(int Num1, int Num2)
> 	{
> 	  	tmpNum1 = Num1;
> 		tmpNum2 = Num2;
> 	}
>
> 	int calladd(int Num3)
> 	{
> 		return(tmpNum1+tmpNum2+Num3);
> 	}
>
> 	int callsub()
> 	{
> 		return(tmpNum1 - tmpNum2);
> 	}
>
> 	int callmul()
> 	{
> 		return(tmpNum1*tmpNum2);
> 	}
> };
>
> int main()
> {
>
> 	Maths math(4,3);
> 	int add1;
> 	int sub1;
> 	int mul1;
>
> 	add1 = math.calladd(6);
> 	sub1 = math.callsub();
> 	mul1 = math.callmul();
>
> 	printf("%d %d %d", add1, sub1, mul1 );
> }
>
>
> Output:
> =====
> ID OF THE JOIN POINT = 0 VALUE OF THE VARIABLE IS= -1073743152
>
> Are output values correct ? Is there anything need to be done to get the
> correct value of the input parameter ? Is there any preliminary document
> which I can refer for programming in AspectC++ ? I would be grateful if u
> can help me in this regard.

This is, of course, not correct. The problem is that "thisJoinPoint->arg(0)" 
is a pointer to the argument. It has the type "void*". If this is printed 
with "%d" it is clear that the output must be something like "-1073743152".

There are two correct ways to get the argument value:

1. Let the pointcut expose the arguments with "args()":

aspect methodfunctions
{
         pointcut move(int arg) = args(arg) &&
	    execution("%  Maths::calladd(int)"); // calladd(...) is ok too

         public:

                 advice move(arg) : void before(int arg)
                 {
                         const char *unused = thisJoinPoint->toString (); 
                         printf("ID OF THE JOIN POINT = %d ",
                                thisJoinPoint->id());
                         printf("VALUE OF THE VARIABLE IS= %d", arg);
                 }
};

This method is pretty simple and typesafe, but it doesn't work if your 
pointcut contains, for instance, execution join points with arbitrary 
arguments. If you don't know the type in advance you can use thisJoinPoint to 
get the type and value:

2. Use thisJoinPoint->argtype(n) and thisJoinPoint->arg(n):

#include<stdio.h>
#include <string.h>

aspect methodfunctions
{
         pointcut move() = execution("%  Maths::calladd(int)");

         public:

                 advice move() : void before()
                 {
                         const char *unused = thisJoinPoint->toString (); 
                         printf("ID OF THE JOIN POINT = %d ",
thisJoinPoint->id());
			 const char *type = thisJoinPoint->argtype(0);
			 if (strcmp (type, "i") == 0)
			    printf("VALUE OF THE VARIABLE IS= %d\n",
				   *(int*)thisJoinPoint->arg(0));
			 else
			    printf("Argument is no integer\n");
                 }
};

I am sorry that no documentation or tutorial is avaliable yet. Please be 
patient. This is a young project and our ressources are limited. Try to learn 
from the TOOLS paper and the examples. For instance, the Trace example shows 
how you can use thisJoinPoint->...

By the way: be careful with thisJoinPoint->id (). The join point ID is not a 
project-wide unique ID. It is calculated per translation unit. Perhaps this 
semantics will change in the near future...

Another "by the way": In the next release of AspectC++ the parser will be 
able to understand new-style casts like "dynamic_cast<type>(value)" as it is 
used in <iostream.h>. You will then be able to replace the printf calls with 
cout.

Best regards

Olaf



More information about the aspectc-user mailing list