mirror of
https://github.com/Hizenberg469/GDB-tutorial.git
synced 2026-04-19 22:02:23 +03:00
process stack and memory leak
This commit is contained in:
2
gdb.txt
2
gdb.txt
@@ -251,7 +251,7 @@ Command: CTRL+p (key bindings)
|
|||||||
will move to previous command if focus is on src window.
|
will move to previous command if focus is on src window.
|
||||||
|
|
||||||
|
|
||||||
Command: winheight src -2
|
Command: winheight src -2 (winheight or wh)
|
||||||
|
|
||||||
To change the size of window passed as an argument. In
|
To change the size of window passed as an argument. In
|
||||||
this case, decrease by 2.
|
this case, decrease by 2.
|
||||||
|
|||||||
@@ -33,3 +33,101 @@ Section 7: Foundation of Processes (Part A):
|
|||||||
|
|
||||||
-> What is a Call stack?
|
-> 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
|
||||||
|
|||||||
Reference in New Issue
Block a user