Click here to Skip to main content
14,775,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Is there any way that i can actually access or read the memory address allocated by any c program. I want to do a sample project in writing a C code to detect memory bugs or what memory locations a c program is accessing during the run time.
Similar tool like Valgrind on linux.
Any suggestions or concepts to implement this particular feature through C.

Regards,
Posted
Comments
JackDingler 4-Jan-12 16:38pm
   
What version of C are you using?

You selected Windows and Linux. There is no generic way to do this across all platforms and all development environments. Please choose one. :)
TRK3 4-Jan-12 18:52pm
   
Is the goal to monitor an arbitrary program for which you only have the executable? Or is the goal to provide debugging facilities for a program that you are building?

If the latter, then you could write a debugging version of malloc() and free() that keeps track of whatever you want (as well as initializing allocated memory, or adding buffer on either end of each allocated block). You'd have to link your version of malloc() in ahead of the CRT. This could be done in a mostly platform independent manner.

If the former, then any solution is going to be very platform dependent.
[no name] 5-Jan-12 8:34am
   
i got your point. But how do i track the values of the local variables declared inside the program
JackDingler 5-Jan-12 8:39am
   
I'm glad that you got the point, that we need to know what operating system and compiler you're using, before we can give you a straight answer.

Without that information, all we can do is give you generic advice that works with the lowest common denominator.
BrainlessLabs.com 11-Jan-12 1:47am
   
Valgrind is a system emulator. They work on different principle. Better to use new handler as proposed by stefan. But what is the problem statement? Is it a need or is it some requirement?

1 solution

The basic methods most common tools use is replace all functions that directly manipulate memory addresses, i. e. alloc, malloc, calloc, realloc, free, new, delete, and maybe also functions like memset, memmove, etc.. You could do this by replacing all of these functions with macros, but a safer way would be defining a new_handler (see http://www.cplusplus.com/reference/std/new/set_new_handler/[^]), but doing so will not affect compiled libs you link to your program or dlls loaded at runtime. The concept is, everytime you allocate memory, you allocate some additional memory to save statistics and data for analysis, either in a separate place, or right in the same memory block. Then, everytime you free memory, you use that additional data to verify the memory block at the given address was indeed allocated and hasn't been freed already.

That said, most compilers implement the existing functions already just like that, at least when you compile them in debug mode. You'll have to check the compiler documentation to find out more. E. g. Visual Studio, if used to run a program in debug mode, will at the normal exit of the program print a list of memory blocks that weren't properly freed, along with the source file locations they were allocated in. (if that information is available)

Also, all modern OSes allocate memory through specialiced memory managers that are capable of emulating a huge chunk of contiguous memory, even if the actual, physical memory currently in use is already fragmented to smaller bits. All memory accesses have to pass through that memory manager in order to determine the physical address, so if you are interested in the actual physical addresses your program uses, it will be a lot harder to determine. This will not be relevant however as long as you don't intend to analyze memory locations of other programs from a different process.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900