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?
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.
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.
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.
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.
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.
Sorry to say you're a bit confused about this. Consider:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With