[Rtai] Memory Checking

Dusan Saljic saljic at yahoo.com
Wed Sep 3 08:54:55 CEST 2008


Hi, 

Yes, if you read the post from the past you could see that debugging and using of the Valgrind is not suitable for the RTAI environment. However, there is a possibility to use memory hooks, About a year ago I have written a memory profiler, the problem is I cannot public the code, because is propriety but I can give you basic ideas:

#define malloc(SIZE) \
    MPregisterMalloc(__FILE__, __LINE__, __FUNCTION__, (SIZE))

#define realloc(PTR, SIZE) \
    MPregisterRealloc(__FILE__, __LINE__, __FUNCTION__, (void *)(PTR), (SIZE))

#define memalign(ALIGNSIZE, SIZE) \
    MPregisterMemalign(__FILE__, __LINE__, __FUNCTION__, (ALIGNSIZE), (SIZE))

#define free(PTR) \
    MPreleaseMalloc(__FILE__, __LINE__, __FUNCTION__, (void *)(PTR))

/*! \note pthread_create allocates memory, and will break any hook! */
#define pthread_create(THRD, ATTR, ROUTINE, ARGS) \
    MPcleanThxCreate((THRD), (ATTR), (ROUTINE), (ARGS))

In your written code it will trace each malloc, but not in the library calls, so you need hooks too, something like this:

    // -------------------------------------------------------------------------
    // set hooks to old ones, no need to hook this method
    //
    MP_restore_hook();
    
    // -------------------------------------------------------------------------
    // ALLOCATE MEMORY! - Call real malloc!
    //
    void *ptr = malloc(size);
    
    // -------------------------------------------------------------------------
    // Save the new default hooks, malloc call can change it!
    //
    MP_saveold_hook();

There is tutorial about malloc_hooks at the GNU site.  Personally inside those method I'm using the structure and append each call at the end of the trace file. Afterwards, I've standalone tool which reads the file and print the analysis to the caller. Be aware of the fact that hooks are global, and they are not thread safe, could happen you've been inside malloc call and comes pthread create, like from the RTAI process, that will break the hook, if the timings are misfortunate. Hope this helps, I also tried several third party profiler and analyzer but at the end I wrote my own.

Yes, almost to forget, gcc offers memory tracing mechanism, on my knowladge this works too.

Regards,
Dusan.





----- Original Message ----
From: Stephen Davies <steve_davies61 at hotmail.com>
To: "rtai at rtai.org" <rtai at rtai.org>
Sent: Tuesday, September 2, 2008 5:23:23 PM
Subject: [Rtai] Memory Checking

 
Hi all,
I have written a realtime program using RTAI in C++ and I'm trying to detect any memory faults.  I've tried using Valgrind (which seems to be pretty good from the documentation I've read) but that falls over at the first mention of any of the RTAI calls.  Has anyone tried to use any static (or dynamic) analysis tools for this kind of thing with RTAI?
Thanks for your help,
Steve
________________________________
Get Hotmail on your mobile from Vodafone Try it Now!


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.rtai.org/pipermail/rtai/attachments/20080902/7970e420/attachment.htm 


More information about the Rtai mailing list