Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 495
Sandeep Singh Avatar asked Apr 22 '11 05:04

Sandeep Singh


People also ask

What is the memory layout of C program?

A C program memory layout in C mainly comprises six components these are heap, stack, code segment, command-line arguments, uninitialized and initialized data segments. Each of these segments has its own read, write permissions.

How is memory managed in C?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

What do you mean by memory layout?

The computer memory is built to store bit patterns. Not only data but also instructions are bit patterns and these can be stored in memory. In systems software, they are stored in separate segment of memory. And the segments are also divided by data and program type.

How many types of memory are there in C?

C Memory Model The C runtime memory model can be divided in to three types; global/static memory, the heap, and the stack. These all share the RAM available on the microcontroller.


2 Answers

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.

like image 87
bx2 Avatar answered Sep 29 '22 08:09

bx2


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.

like image 26
Tony Delroy Avatar answered Sep 29 '22 06:09

Tony Delroy