mirror of
https://github.com/Hizenberg469/GDB-tutorial.git
synced 2026-04-20 06:12:23 +03:00
188 lines
5.5 KiB
Plaintext
188 lines
5.5 KiB
Plaintext
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 ./<program>
|
|
|
|
|
|
We can also use Address-sanitizers
|
|
|
|
* Stack Overflow:
|
|
~~~~~~~~~~~~~~~
|
|
|
|
|
|
When the size of any number of call stack crosses the
|
|
finite size of the allocated stack of the program, then
|
|
it leads to stack overflow.
|
|
|
|
+---------------------------+
|
|
| |
|
|
| stack | |
|
|
|---------------------------| |
|
|
| | |
|
|
| array[0] | |
|
|
| array[1] | |
|
|
| | | |
|
|
| | | |
|
|
| | | \|/
|
|
| | | *
|
|
| array[99....] |
|
|
|---------------------------|
|
|
| array[10....0] |
|
|
| heap |
|
|
| |
|
|
|---------------------------|
|
|
| |
|
|
| data |
|
|
| |
|
|
| |
|
|
|---------------------------|
|
|
| |
|
|
| .text |
|
|
| |
|
|
+---------------------------+
|
|
|
|
|
|
* Address Sanitizer:
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
To know about potential memory leaks, we can enable stanitizer flag
|
|
|
|
Compiler flag:
|
|
-fsanitize=address
|
|
|
|
|
|
* To detect memory leak, we can use valgrind.
|
|
|
|
Command:
|
|
$valgrind --leak-check=full ./<program>
|
|
|
|
OR
|
|
|
|
$valgrind ./<program>
|