Sandeep Singh
Sandeep Singh

Reputation: 5191

Tool to clearly visualize Memory Layout of a C Program

Suppose I am having this code:

int main() {
    int var1;  
    char *ptr = malloc(5 * sizeof(char));  
    //...........  
    do_something();  
    //...........    
    return 0;  
}

We know that the actual memory layout will be divided into segments like: .text, .bss, .data, .heap, .stack.

I know how to use objdump, readelf, etc. But, I want to get a better view of the memory stack, where I can see things like:

.heap       ptr  
.stack      do_something()  
.text       main()  
.bss        var1  

The main point is: The actual variable names are missing from the output of objdump, readelf etc.

I am compiling this code with -g, thus retaining the symbol table.

Then, why am I not able to see the memory layout with local/global variable names included?

objdump -x shows the names of the variables if type is static otherwise not. Why?

Upvotes: 15

Views: 19601

Answers (2)

bx2
bx2

Reputation: 6476

There are few methods to track memory allocation but none of them is a builtin method and all of them require some additional work on your side. In order to visualise memory you will have to use code instrumentation and/or event logging i.e. memory allocation and deallocation events and then replay all the events and generate graphs out of it.

Take a look at this paper:Visualizing Dynamic Memory Allocations (in C programs).

The GCSpy (for heap visualisation) is available here: https://www.cs.kent.ac.uk/projects/gc/gcspy/. While initially used for JVM, you can visualise the heap of a C program using for instance dlmalloc.

I completely understand why you would like to do that - I was looking for the same thing. While I don't find memory layout snapshotting very useful per se, I find observing how memory is being allocated over time very interesting and useful for debugging performance issues.

I remember that XCode had some instrumentation tools built in - never used them though, but perhaps worth exploring what they are offering.

Upvotes: 8

Tony Delroy
Tony Delroy

Reputation: 106126

Sorry to say you're a bit confused about this. Consider:

  • all your functions go in the .text section
  • all your non-static local variables on on the stack: that they may be pointers and you intend to assign them a value returned from malloc doesn't put them on the heap, it just attempts to create a pointed-to object on the heap. No static tool looking at the binary (such as objdump, readelf) can know whether malloc will return memory or fail.
  • your global and static variables are likely to end up in an initialised or uninitialised data segment - which depends on whether the initial bit pattern is entirely 0s, and whether the compiler can convince itself of that at compile time.

Further, if you understand the above, then you don't need anything to draw you a little chart on a variable by variable basis, you just know instantly what type of memory you're using.

Upvotes: 4

Related Questions