1. 程式人生 > >GDB命令,逆向除錯很有用

GDB命令,逆向除錯很有用

感覺比很多網站的教程有用,分享一下

對於我自己用gdb反彙編十分有幫助啊,感謝外國友人了~

實用的幾個command:

Restart>>>run / r

Break any point>>>break *address Break func >>> break [function] F9>>>continue F7>>>step,next finish this function >>> finish show current code >>> x /10i  (10 lines)

GDB Command Line Arguments:

Starting GDB:

  • gdbname-of-executable
  • gdb -ename-of-executable-cname-of-core-file
  • gdbname-of-executable--pid=process-id
    Useps -auxwto list process id's:
    Attach to a process already running:
    [prompt]$ ps -auxw | grep myapp
    user1     2812  0.7  2.0 1009328 164768 ?      Sl   Jun07   1:18 /opt/bin/myapp
    [prompt]$
    gdb /opt/bin/myapp 2812 OR [prompt]$ gdb /opt/bin/myapp --pid=2812

Command line options: (version 6. Older versions use a single "-")

Option Description
--help
-h
List command line arguments
--exec=file-name
-efile-name
Identify executable associated with core file.
--core=name-of-core-file
-cname-of-core-file
Specify core file.
--command=command-file
-xcommand-file
File listing GDB commands to perform. Good for automating set-up.
--directory=directory
-ddirectory
Add directory to the path to search for source files.
--cd=directory Run GDB using specified directory as the current working directory.
--nx
-n
Do not execute commands from~/.gdbinitinitialization file. Default is to look at this file and execute the list of commands.
--batch -xcommand-file Run in batch (not interactive) mode. Execute commands from file. Requires-xoption.
--symbols=file-name
-sfile-name
Read symbol table from file file.
--se=file-name Use FILE as symbol file and executable file.
--write Enable writing into executable and core files.
--quiet
-q
Do not print the introductory and copyright messages.
--tty=device Specifydevicefor running program's standard input and output.
--tui Use a terminal user interface. Console curses based GUI interface for GDB. Generates a source and debug console area.
--pid=process-id
-pprocess-id
Specify process ID number to attach to.
--version Print version information and then exit.
GDB Commands:

Commands used within GDB:

Command Description
help List gdb command topics.
helptopic-classes List gdb command within class.
helpcommand Command description.
eghelp showto list the show commands
apropossearch-word Search for commands and command topics containingsearch-word.
info args
i args
List program command line arguments
info breakpoints List breakpoints
info break List breakpoint numbers.
info breakbreakpoint-number List info about specific breakpoint.
info watchpoints List breakpoints
info registers List registers in use
info threads List threads in use
info set List set-able option
Break and Watch
breakfuntion-name
breakline-number
breakClassName::functionName
Suspend program at specified function of line number.
break +offset
break -offset
Set a breakpoint specified number of lines forward or back from the position at which execution stopped.
breakfilename:function Don't specify path, just the file name and function name.
breakfilename:line-number Don't specify path, just the file name and line number.
breakDirectory/Path/filename.cpp:62
break *address Suspend processing at an instruction address. Used when you do not have source.
breakline-numberifcondition Where condition is an expression. i.e.x > 5
Suspend when boolean expression is true.
breaklinethreadthread-number Break in thread at specified line number. Useinfo threadsto display thread numbers.
tbreak Temporary break. Break once only. Break is then removed. See "break" above for options.
watchcondition Suspend processing when condition is met. i.e.x > 5
clear
clearfunction
clearline-number
Delete breakpoints as identified by command option.
Delete all breakpoints infunction
Delete breakpoints at a given line
delete
d
Delete all breakpoints, watchpoints, or catchpoints.
deletebreakpoint-number
deleterange
Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments.
disablebreakpoint-number-or-range
enablebreakpoint-number-or-range
Does not delete breakpoints. Just enables/disables them.
Example:
Show breakpoints:info break
Disable:disable 2-9
enablebreakpoint-numberonce Enables once
continue
c
Continue executing until next break point/watchpoint.
continuenumber Continue but ignore current breakpointnumbertimes. Usefull for breakpoints within a loop.
finish Continue to end of function.
Line Execution
step
s
stepnumber-of-steps-to-perform
Step to next line of code. Will step into a function.
next
n
nextnumber
Execute next line of code. Will not enter functions.
until
untilline-number
Continue processing until you reach a specified line number. Also: function name, address, filename:function or filename:line-number.
info signals
info handle
handleSIGNAL-NAMEoption
Perform the following option when signal recieved: nostop, stop, print, noprint, pass/noignore or nopass/ignore
where Shows current line number and which function you are in.
Stack
backtrace
bt
btinner-function-nesting-depth
bt -outer-function-nesting-depth
Show trace of where you are currently. Which functions you are in. Prints stack backtrace.
backtrace full Print values of local variables.
frame
framenumber
fnumber
Show current stack frame (function where you are stopped)
Select frame number. (can also user up/down to navigate frames)
up
down
upnumber
downnumber
Move up a single frame (element in the call stack)
Move down a single frame
Move up/down the specified number of frames in the stack.
info frame List address, language, address of arguments/local variables and which registers were saved in frame.
info args
info locals
info catch
Info arguments of selected frame, local variables and exception handlers.
Source Code
list
l
listline-number
listfunction
list -
liststart#,end#
listfilename:function
List source code.
set listsizecount
show listsize
Number of lines listed whenlist command given.
directorydirectory-name
dirdirectory-name
show directories
Add specified directory to front of source code path.
directory Clear sourcepath when nothing specified.
Machine Language
info line
info linenumber
Displays the start and end position in object code for the current line in source.
Display position in object code for a specified line in source.
disassemble0xstart 0xend Displays machine code for positions in object code specified (can use start and end hex memory values given by theinfo linecommand.
stepi
si
nexti
ni
step/next assembly/processor instruction.
x0xaddress
x/nfu0xaddress
Examine the contents of memory.
Examine the contents of memory and specify formatting.
  • n: number of display items to print
  • f: specify the format for the output
  • u: specify the size of the data unit (eg. byte, word, ...)
Example:x/4dw var
Examine Variables
printvariable-name
pvariable-name
pfile-name::variable-name
p 'file-name'::variable-name
Print value stored in variable.
p *array-variable@length Print first # values of array specified bylength. Good for pointers to dynamicaly allocated memory.
p/xvariable Print as integer variable in hex.
p/dvariable Print variable as a signed integer.
p/uvariable Print variable as a un-signed integer.
p/ovariable Print variable as a octal.
p/tvariable
x/baddress
x/b &variable
Print as integer value in binary. (1 byte/8bits)
p/cvariable Print integer as character.
p/fvariable Print variable as floating point number.
p/avariable Print as a hex address.
x/waddress
x/4b &variable
Print binary representation of 4 bytes (1 32 bit word) of memory pointed to by address.
ptypevariable
ptypedata-type
Prints type definition of the variable or declared variable type. Helpful for viewing class or struct definitions while debugging.
GDB Modes
setgdb-optionvalue Set a GDB option
set logging on
set logging off
show logging
set logging filelog-file
Turn on/off logging. Default name of file isgdb.txt
set print array on
set print array off
show print array
Default is off. Convient readable format for arrays turned on/off.
set print array-indexes on
set print array-indexes off
show print array-indexes
Default off. Print index of array elements.
set print pretty on
set print pretty off
show print pretty
Format printing of C structures.
set print union on
set print union off
show print union
Default is on. Print C unions.
set print demangle on
set print demangle off
show print demangle
Default on. Controls printing of C++ names.
Start and Stop
run
r
runcommand-line-arguments
run <infile>outfile
Start program execution from the beginning of the program. The commandbreak mainwill get you started. Also allows basic I/O redirection.
continue
c
Continue execution to next break point.
kill Stop program execution.
quit
q
Exit GDB debugger.
GDB Operation:
  • Compile with the "-g" option (for most GNU and Intel compilers) which generates added information in the object code so the debugger can match a line of source code with the step of execution.
  • Do not use compiler optimization directive such as "-O" or "-O2" which rearrange computing operations to gain speed as this reordering will not match the order of execution in the source code and it may be impossible to follow.
  • control+c: Stop execution. It can stop program anywhere, in your source or a C library or anywhere.
  • To execute a shell command:!command
    orshellcommand
  • GDB command completion: Use TAB key
    info bre+ TAB will complete the command resulting ininfo breakpoints
    Press TAB twice to see all available options if more than one option is available or type "M-?" + RETURN.
  • GDB command abreviation:
    info bre+ RETURN will work asbreis a valid abreviation forbreakpoints
De-Referencing STL Containers:

Displaying STL container classes using the GDB "pvariable-name" results in an cryptic display of template definitions and pointers. Use the followingfile (V1.03 09/15/08). Now works with GDB 4.3+.
(Archived versions: [V1.01GDB 6.4+ only])
Thanks toDr. Eng. Dan C. Marinescufor permission to post this script.

Use the following commands provided by the script:

Data type GDB command
std::vector<T> pvectorstl_variable
std::list<T> pliststl_variableT
std::map<T,T> pmapstl_variable
std::multimap<T,T> pmapstl_variable
std::set<T> psetstl_variableT
std::multiset<T> psetstl_variable
std::deque<T> pdequeuestl_variable
std::stack<T> pstackstl_variable
std::queue<T> pqueuestl_variable
std::priority_queue<T> ppqueuestl_variable
std::bitset<n>td> pbitsetstl_variable
std::string pstringstl_variable
std::widestring pwstringstl_variable
Where T refers to native C++ data types. While classes and other STL data types will work with the STL container classes, this de-reference tool may not handle non-native types.
De-Referencing a vector:

Example:STL_vector_int.cpp

01 #include <iostream>
02 #include <vector>
03 #include <string>
04
05 usingnamespacestd;
06