[aspectc-user] do an aspect for detect line/file of an execution of new operator ??

Olaf Spinczyk os at aspectc.org
Tue Dec 15 15:38:39 CET 2009


Dear Heni,

redefining the meaning of the token 'new' is rather dangerous. The C 
prepropressor is very simple and replaces 'new' wherever it appears. 
Also note that the code, which is transformed by a++, will once again be 
preprocessed by the C++ compiler.

The root of the problem ist that call-advice for the new operator is not 
supported by ac++, yet. If you could use cal- advice, the joinpoint-API 
would allow you to access the filename and line number easily in your 
advice code. This would be the best solution.

If you can't wait until this feature is implemented, I would recommend a 
workaround like this: Implement a kind of tracing aspect for the 
execution of all functions. Use a global 'function_name' variable and a 
global 'new_count' variable. Whenever a function is being executed 
(before-execution-advice) set the 'function_name' to 
JoinPoint::signature() and 'new_count' to 0. Additionally, define advice 
for the execution of the new operator. There you can use the current 
'function_name' and 'new_count' as context information. Also increment 
'new_count'. If your program is multi-threaded make sure the two globals 
are allocated in thread-local storage. With this workaround you don't 
get the file and line, but with the function name and the invocation 
counter you should normally be able to identify the right new-call.

So much about my two cents,

Olaf

Heni Dhiab wrote:
>
> I'm developing a debugger for C++ dynamicly memory allocated with 
> aspectC++. So i have to detect each execution of NEW and NEW [] 
> opertors. For this, i did the 'NewDetectionAspect' aspect.
>
>  
>
> The aspect working well, but i want to change the comportment of New 
> operator for return two arguments, the first is the line where New/New 
> [] operator was executed, and the second is the file where it is executed.
>
>  
>
> I did an sample but not with an spectC++ aspect, it is just with a 
> normal C++ class and a header file. it s working well..i can detect 
> each execution line and file of new[]/new operator. The pricipe is 
> like this :
>
>  
>
>  
>
> change New operator Prototype  by adding two arguments for line and 
> file in a header file "halloc.h":
>
>  
>
>  
>
> #  include <string.h>
>
> #    include <new>
>
>  
>
>  
>
>      void* operator new (size_t size, const char* nom_fich, const 
> unsigned long num_ligne)
>
>        throw (std::bad_alloc);
>
>        #define new new (__FILE__, __LINE__)
>
>  
>
>  
>
>  
>
> And develop it in hello.cpp:
>
>  
>
> #include "halloc.h"
>
> #undef new
>
>  
>
> void* operator new (size_t size, const char* nom, const unsigned long 
> ligne)
>
>   throw (std::bad_alloc)
>
> {
>
>   void*ptr= new char;
>
>  
>
>       printf ("%s:%lu - ",nom, ligne);
>
>     return ptr;// MallocSecurise (size, nom, ligne);
>
> }
>
>  
>
> When I integrate this sample in an aspect, like this 
> ‘NewDetectionAspectLineFile.ah’ :
>
>  
>
>  
>
>  #ifndef FIRSTASPECT
>
> #define FIRSTASPECT
>
>  
>
>  
>
>  
>
>  
>
> #include <stdio.h>
>
> #include <iostream>
>
> #include <string.h>
>
> #include <stdlib.h>
>
> #include <stdarg.h>
>
> #  include <new>
>
>  
>
>  
>
> #  include <string.h>
>
> #  include <new>
>
>  
>
> void* operator new (size_t size, const char* nom_fich, const unsigned 
> long num_ligne)
>
>        throw (std::bad_alloc);
>
>  
>
>  
>
>  
>
> void* operator new (size_t size, const char* nom, const unsigned long 
> ligne)
>
>   throw (std::bad_alloc)
>
> {
>
>   void*ptr= new char;
>
>       printf ("%s:%lu - ",nom, ligne);
>
>     return ptr;// MallocSecurise (size, nom, ligne);
>
> }
>
> #undef new
>
> #define new new (__FILE__, __LINE__)
>
>  
>
>  
>
> //-----------------------------------------------------------------------------
>
>  aspect FirstAspect
>
> {    
>
>       void * ptr;
>
>       int d;
>
>       FILE *F3;
>
>      
>
> public :
>
>       advice classes("%")://securise la gestion de memoire afin de 
> s'assurer qu'aucune autre gestion n'est en place (eviter les conflits)
>
>       slice class newdelete
>
>       {
>
>  
>
>       public:
>
>      
>
>       //Assure que loperateur new n'a pas ete redefinit sinon le 
> garbage collection ne peu fonctionner
>
>       void* operator new (size_t size, const char* nom, const unsigned 
> long ligne)
>
>             throw (std::bad_alloc)
>
>             {
>
>       void*ptr= new char;
>
>       printf ("%s:%lu - ",nom, ligne);
>
>     return ptr;// MallocSecurise (size, nom, ligne);
>
>             }
>
>       };
>
>  
>
>  
>
>       advice (execution("% ...::operator new(...)") )
>
>             && !within("Pointer") && !within("Pointers"): after ()
>
>       { printf ("executing heap operation \"%s\"\n", 
> JoinPoint::signature ());}
>
> };
>
> #endif //FIRSTASPECT
>
>  
>
> This error is occurred:
>
>  
>
> Error    6          error C2660: 'simple::operator new' : function 
> does not take 1 arguments        
>
>  
>
> Some one can help me for resolve this problem, and made an 
> aspect/advice/slice for detect the line and the file for a new operator ??
>
> Thank u very much, Heni.
>
>
> ------------------------------------------------------------------------
> Windows Live: Keep your friends up to date with what you do online. 
> <http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_1:092010> 
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> aspectc-user mailing list
> aspectc-user at aspectc.org
> http://www.aspectc.org/mailman/listinfo/aspectc-user




More information about the aspectc-user mailing list