PavanMysore
PavanMysore

Reputation: 189

How can i track memory allocation?

I have a very unique problem, i am running Linux on a special hardware which is an Octeon based hardware. I see that my process keeps allocating memory upon performing a specific operation. I would like to track which part of the code is making this call.

Here is the details about my environment

  1. Octeon MIPS architecture 32 bit.
  2. Linux kernel 2.6
  3. Minimal linux implementation.

However here are the my options/limitations that i need to work around.

  1. Not sure if creating my own malloc will help. If this is possible can someone explain please?
  2. Cannot use tools like walgrind, not available for this architecture.
  3. Code base is huge, dont understand it fully, hence review isnt feasible and quick.
  4. strace gives me the memory address that is allocated, but how can i figure out the code from that created this?

Please suggest if there is some way to go about this.

Thanks a lot.

Another thing i forgot to mention, the virtual memory of the process goes on increasing up-to 1.4GB at which, it stops and i see the allocs in the code fail with ENOMEM. Is this 1.4 GB limitation something to do with 32 bit machine? AFAIU 32 bit machine should allow 3 GB of virtual memory per process isnt it? Also, there is no per process limitation, i have confirmed this with setrlimit/getrlimit.

Cheers, Pavan

Upvotes: 4

Views: 2027

Answers (2)

Alok Save
Alok Save

Reputation: 206526

If you want to write an wrapper function over malloc, You can do so without modifying each and every instance in your code where the function is called, a simple macro trick shall suffice:

void* my_malloc(size_t size, const char *file, int line, const char *func);

#define malloc(X) my_malloc( X, __FILE__, __LINE__, __FUNCTION__)

void* my_malloc(size_t size, const char *file, int line, const char *func)
{

    void *p = malloc(size);
    printf ("Allocated = %s, %i, %s, %p[%li]\n", file, line, func, p, size);

    /*Link List functionality goes in here*/

    return p;
}

Similarly, You can also map free to call your own function.

You can maintain a list of allocated addresses and keep on adding new entries in malloc and removing relevant entries from the list in free. What remains in the list at the end of the program is leaking memory with locations.

Upvotes: 5

harald
harald

Reputation: 6126

You may want to have a look at Dmalloc. Should be a lot less architecture dependent than valgrind.

Upvotes: 0

Related Questions