Section 7: Foundation of Processes (Part A): -------------------------------------------- -> The Program Stack: ++++++++++++++++++ +-----------------------+ | Bottom of stack | +-----------------------+ | stack | <- Fixed size*1 +-----------------------+ | (Unallocated) | +-----------------------+ | Heap (Dynamic Data)| +-----------------------+ | Static Data | +-----------------------+ | Literals | <- Like String literals +-----------------------+ | Instructions | <- This section contains the code (asm code) +-----------------------+ | Top of stack | +-----------------------+ *1 By fixed size we mean that when we compile the program the size of the stack is predetermined. We can increment the size of the stack but the size is fixed once compiled. Fun fact: NASA use only stack memory for their embedded system while writing code for their rovers. -> What is a Call stack? +++++++++++++++++++++ +--------+ | main | +--------+ | square |-----/ +--------+ | | | | | | | \ Frame +----------------+ | parameters | | return address| | locals | * Each Call stack has one section called as frame | exceptions | which contains the information for certain operations. | | These operations are self explainatory as written. +----------------+ -> Navigate the Call Stack with Backtrace: ++++++++++++++++++++++++++++++++++++++ * To know about the call stack of functions, we can use... Command: (gdb)bt This shows what are the function calls to trace back to the beginning function, which is "main" in C and C++. * To know about the arguments and the value passed to the current function in which we are present, we can use... Command: (gdb)info args * To know about the local variables of the current functions context, we can use... Command: (gdb)info locals * To know about the frame section of the function call stack, we can use: Command: (gdb)info frame * To get us out of the function that we are currently in without terminating the execution of the program. Command: (gdb)finish or (gdb)fin * To move up in the stack, i.e. moving to the context of the calling function , we can use.... Command: (gdb)up * To move up down the stack, i.e. moving to the context of the callee function , we can use... Command (gdb)down * Segmentation fault: ~~~~~~~~~~~~~~~~~~~ It is the section of memory which the program is trying to access and it is out of bound. For this kind of error, we get signal from kernel SIGSEGV. * Memory leak: ~~~~~~~~~~~~ Failure to reclaim memory while our program runs. We can also use tools like valgrind to detect memory leak issue. Command: $valgrind ./ We can also use Address-sanitizers