mirror of
https://github.com/Hizenberg469/GDB-tutorial.git
synced 2026-04-19 22:02:23 +03:00
first commit
This commit is contained in:
251
gdb.txt
Normal file
251
gdb.txt
Normal file
@@ -0,0 +1,251 @@
|
||||
Flags for compiling the code:
|
||||
------------------------------
|
||||
|
||||
-W: Warnings
|
||||
-Wall: To detect all the warnings. By all warnings means, most
|
||||
commonly used warnings.
|
||||
|
||||
-Wconversion: To warns use about implicit type conversion.
|
||||
|
||||
-Werror: To treat all the warnings as errors.
|
||||
|
||||
|
||||
Some codes in C++ implicitly provides warning even if
|
||||
we don't mentions warnings flags.
|
||||
|
||||
For ex:
|
||||
int x{square(i)};
|
||||
|
||||
Here, square function returns a float value. But, curly
|
||||
braces initialization gives warning for conversion (implicit-conversion,
|
||||
narrowing-conversion).
|
||||
|
||||
|
||||
|
||||
Printing values to check and debugging:
|
||||
--------------------------------------
|
||||
|
||||
We can use -D flag to do #define a macro from
|
||||
compile command itself.
|
||||
|
||||
For ex:
|
||||
gcc -DDEBUG <code>.c -o <exe>
|
||||
|
||||
Same as doing #define DEBUG 1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GDB (GNU Debugger):
|
||||
--------------------
|
||||
|
||||
Command: gdb --version
|
||||
To see the version of gdb.
|
||||
|
||||
|
||||
Command: gdb --silent ./<exe>
|
||||
|
||||
To to print all the info that gdb prints while starting.
|
||||
|
||||
Command: gdb --tui
|
||||
|
||||
To run gdb in Text-user interface mode.
|
||||
To open TUI when gdb is already On.
|
||||
|
||||
use command: layout src
|
||||
|
||||
|
||||
We can use these flags with gcc for C code or g++ for C++ code.
|
||||
|
||||
Note: To use gdb with compiled code. We need to compile the source
|
||||
code with '-g' flag this will compile the source code and
|
||||
generate executable along with Debug symbol which will be used
|
||||
by GDB.
|
||||
|
||||
Also, it is not advisable to optimize the code with flags like
|
||||
-O3, etc and if the compiler by default optimize the code, we
|
||||
can disable it by using -O0. We do this because, optimization
|
||||
introduce extra code and make the debugging more complex.
|
||||
|
||||
Command: gdb ./<executable>
|
||||
|
||||
To run gdb with given argument as the program to debug.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
GDB commands:
|
||||
-------------
|
||||
|
||||
Command: quit or q
|
||||
|
||||
To quit out of current gdb session.
|
||||
|
||||
|
||||
Command: run or r
|
||||
|
||||
To run the executable in gdb.
|
||||
|
||||
Command: CTRL + l
|
||||
|
||||
To clear screen in gdb.
|
||||
|
||||
Command: start
|
||||
|
||||
It will add temporary breakpoint to start
|
||||
of the execution i.e. at the main() function
|
||||
and stop the execution.
|
||||
|
||||
Command: next or n
|
||||
|
||||
To move to next line of code and execute it.
|
||||
Next statement and execute it.
|
||||
|
||||
|
||||
Command: step or s
|
||||
|
||||
To move into the function execution instead of
|
||||
just executing the function and returning the result.
|
||||
|
||||
|
||||
Command: finish or fin
|
||||
|
||||
To step out of the function.
|
||||
|
||||
Command: list or l
|
||||
|
||||
|
||||
To show some context of the current execution w.r.t
|
||||
code
|
||||
|
||||
|
||||
To show some context of the current execution w.r.t
|
||||
code
|
||||
|
||||
Also using command "list <line-number>" prints the
|
||||
context w.r.t given line number.
|
||||
|
||||
Command: set listsize <size>
|
||||
|
||||
To show more content while using list command
|
||||
|
||||
|
||||
Command: print <variable-name> or p <variable-name>
|
||||
|
||||
To print the current value of the variable name.
|
||||
It also de-reference operator to show the content
|
||||
as well to print the value present at the address
|
||||
in form of structure.
|
||||
|
||||
for ex:
|
||||
|
||||
print l->root
|
||||
|
||||
address with pointer type specified
|
||||
|
||||
print *l.root
|
||||
|
||||
print the whole structure of the object.
|
||||
|
||||
|
||||
Command: c or continue
|
||||
|
||||
To continue the execution till next breakpoint
|
||||
or termination of program.
|
||||
|
||||
Command: whatis <type>
|
||||
|
||||
To specify what is symbol which maybe user-defined
|
||||
actually is....
|
||||
|
||||
For ex:
|
||||
|
||||
whatis list_t
|
||||
type = struct list
|
||||
|
||||
|
||||
Command: ptype <type>
|
||||
|
||||
It gives more information as compare to whatis.
|
||||
|
||||
For ex:
|
||||
ptype list_t
|
||||
type = struct list{
|
||||
node_t *root;
|
||||
}
|
||||
|
||||
|
||||
Note: Both ptype and whatis can be used with variable as well
|
||||
|
||||
|
||||
Command: info scope <argument>
|
||||
|
||||
To show what is present in the scope of the current function in execution.
|
||||
|
||||
|
||||
Commaond: info types
|
||||
|
||||
To list all the different type of symbols in the program.
|
||||
|
||||
|
||||
Command: tbreak <line-number>
|
||||
|
||||
To give temporary breakpoint to the program.
|
||||
This breakpoint will stop the program execution only once.
|
||||
|
||||
Command: info breakpoints
|
||||
|
||||
To show some information about breakpoints.
|
||||
|
||||
Command: break <line-number>
|
||||
|
||||
These are permanent breakpoint
|
||||
|
||||
Command: break <line-number>
|
||||
|
||||
These are permanent breakpoint
|
||||
|
||||
Command: layout src
|
||||
|
||||
To open the TUI with src code.
|
||||
|
||||
Command: help layout
|
||||
|
||||
To know more about layout.
|
||||
|
||||
Command: layout reg
|
||||
|
||||
To show register info.
|
||||
|
||||
Command: layout asm
|
||||
|
||||
To show asm info.
|
||||
|
||||
Command: CTRL+x 1,2...
|
||||
|
||||
To cycle through various layout for a single window.
|
||||
|
||||
Command: CTRL+x 0
|
||||
|
||||
To navigate to gdb terminal and TUI window
|
||||
|
||||
|
||||
If view gets messed up use CTRL+l
|
||||
|
||||
|
||||
If view gets messed up use CTRL+l
|
||||
|
||||
Same thing for navigation can be done
|
||||
using focus cmd and focus src window.
|
||||
|
||||
Command: CTRL+p
|
||||
|
||||
will move to previous command if focus is on src window.
|
||||
|
||||
|
||||
Command: winheight src -2
|
||||
|
||||
To change the size of window passed as an argument. In
|
||||
this case, decrease by 2.
|
||||
Reference in New Issue
Block a user