1: Launch GDB
1.1 Type gdb and executable file
-bash-3.2$ gdb executable file
1.2 Type gdb first then attach to the process
-bash-3.2$ gdb
(gdb) attach 21429
Attaching to process 21429
We can get the process id with command ps-ef.
1.3 Launch gdb with core dump file
bash-3.2$ gdb ./readdir core
1.4 Launch gdb with arguments
gdb --args path/to/executable -every -arg you can=think < of
1.5 Launch gdb with commands file
gdb -x commandFile execfile
set auto-load safe-path /
set solib-search-path /tmp/debug_dir/Base:/tmp/debug_dir/dep_libs:/lib
set solib-absolute-prefix /tmp/debug_dir/:/tmp/debug_dir/dep_libs:/lib
file /tmp/debug_dir/Base/cvd
core /tmp/debug_dir/core
bt
thread apply all bt
2: Breakpoint management
2.1 Set breakpoint
2.1.1 Break at line number
(gdb) break filename:line_number
2.1.2 Break at a function
(gdb) break function_name
2.1.3 Conditional break which only breaks when some conditions are hit
(gdb) break line-or-function if condition
break x if strcmp(y,"hello") == 0
Num Type Disp Enb Address What 1 breakpoint keep y 0x000028bc in init_random at qsort2.c:155
2 breakpoint keep y 0x0000291c in init_organ at qsort2.c:168
2.2 Show breakpoints info
(gdb) info break
2.3 Delete breakpoint
(gdb) delete breakpoint number // Delete one breakpoint whose number is number
(gdb) delete breakpoint //Delete all breakpoints
2.4 Disable/Enable breakpoint
(gdb) disable breakpoint number
(gdb) enable breakpoint number
2.5 Delete all breakpoints on a certain line of code
(gdb) clean filename:linenumber
3: Watch the context
Watch the context at some breakpoints or crash.
(gdb) bt
(gdb) where
4: Variable management
4.1 Print a variable
(gdb) p variablename
Print variable in HEX
(gdb) p "%s", stringVariableName
Print string variable in string format
(gdb) p /x variablename
Print variable in Binary
(gdb) p /t variablename
4.2 Set a variable to a new value
(gdb) set variablename=value
4.3 Show variable type or strcuture
(gdb) whatis variablename
(gdb) ptype variablename
5: Function management
5.l call functionname
(gdb) call gen_and_sork( 1234,1,0 )
(gdb) call printf(“abcd”)
$1=4
5.2 Finish current executing function and show the return value.
(gdb) finish
6: Ignore an line
(gdb) jump +1
jumps to the next line line i.e. skipping the current line. You may also want to combine it with
tbreak +1 to set a temporary breakpoint at the jump target.7: Run a program with command line arguments
(gdb) gdb --args executibleFileName arg1 arg2...
7: dbx -p (path to exe or libs)
8: Get code address in core file
1) info sharedlibrary: show begin and end address of each .so file
2) x/10i (any virtual address)
3) p/x: calculate addr
4) bt: show virtual addr for each call
10: Important files
ld-linux-x86-64.so.2, /lib64/libdl.so.2 /lib64/librt.so.1 /lib64/libpthread.so.0, libc.so.6(OES), libstdc++.so.6, libthread_db-1.0.so, libthread_db.so.1, libpthread-2.11.3.so, libpthread.so.0
11: Ignore signals
handle SIGUSR1 noprint nostop
8: Specify symbol files
gdb) symbol-file <path-of-symbol-file>
9: thread debug
set scheduler-locking on/off: Only one thread execute at a time
10: Important files: /lib64/libdl.so.2 /lib64/librt.so.1 /lib64/libpthread.so.0, ld-linux-x86-64.so.2
11: set step-mode on
Yet, it doesn't cover the three basic single-stepping commands: step in, step over, and step out.
ReplyDeletePerhaps update the blog post?