GDB study (one)

来源:互联网 发布:unity3D刚体 编辑:程序博客网 时间:2024/06/06 09:18

 Mechanism GDB rely on:

1.ptrace: a system call

references:http://blog.chinaunix.net/space.php?uid=227715&do=blog&cuid=362901

http://godorz.info/2011/02/process-tracing-using-ptrace/

2.into/int3 command

references:http://linux.die.net/man/3/int

3. hardware Debugging registers and TF flags

4.SIGTRAP/SIGSTOP/SIGCONT

5.debug signals in execute file


start of GDB learning

1) start gdb tool

If you want to use GDB,when compling you must add -g param(use gcc). 

cold boot: gdb pragram  e.g. gdb ./myProgram

                  gdb -p pid e.g. gdb -p 12345

                  gdb program core  e.g.  gdb ./myProgram core.xxx

hot boot: (gdb) attach pid  e.g. (gdb) attach 1234

paragrams:  gdb program --args arglist

                 (gdb) set args arglist

                 (gdb) run arglist

2) commands

 r 

s

bt

ni

si

Press Enter will repeat last command.

 (gdb) shell command args 

 (gdb)make make-args (=shell make make-args)


 (gdb)break function

 (gdb)break lineNum

 (gdb)break filename:lineNumber

 (gdb)break filename:function

 (gdb)break args if condition  // only when condition is true,the break can work

 (gdb)tbreak args  // the break only work once

 (gdb)rbreak regex: all functions meeting the regex will set the breakpoint


 (gdb)info breakpoints [n]  // check nth breakpoint. If there is no nth breakpoint, show all breakpoints infomation


 (gdb)disable [breakpoints] [range...]

 (gdb)enable [breakpoints] [range]

 (gdb)enable [breakpoints] once [range..]

 (gdb)enable [breakpoints] delete [range..]  //when the process is killed, these breakpoints will be deleted.

 (gdb)break args if cond // set condition for the breakpoint

 (gdb)condition bnum [conditon-expression] // If condition-expression is assigned values, set condition for bnumth breakpoint; nor calcel the condition for bnumth breakpoint;

(gdb)ingore bnum count    // ingore bnumth breakpoint count times

(gdb)commands [bnum]

    ..... command-list...

         end

         // set command automatically run for bnumth breakpoint; if there is no bnum, then set the last breakpoint

(gdb) command [bunm]

          end

         // cancel commands for bnumth breakpoint


clear breakpoints

(gdb) clear function & clear filename:function

(gdb)clear linenum & clear filename:linenum

(gdb) delete [breakpoints] [range...]


// pending breakpoints

(gdb)set breakpoint pending auto // GDB default configuration. Ask usr if set pending breakpoint

(gdb)set breakpoint pending on // GDB set unknown breakpoint as pending breakpoint

(gdb) set breakpoint pending off  //GDB report error when running into unknown breakpoint

(gdb)show breakpoint pending


(gdb)watch expt // e.g.  watch mapreduce::mapper  when expression changs, the system will stop to watch the express

(gdb)catch event //e.g. catch throw   when event happened, the process will be stopped


one step debug

(gdb)next [count]  // one line

(gdb)nexti [count] // one command

(gdb)step [count]

(gdb)stepi [count]

// next see function as one line, while step will run into the function


(gdb)continue [ignore-count] // wake up process, continuce running until the program is over or the next breakpoint

(gdb)finish // continue running until the function is over and print the return value in console

(gdb)return [expr] // interrupt current function call, return expr's value


// look at variables and memory

(gdb)print [lf] expr // f formatted express. f: x--hexadecimal  d -- decimal  u--unsigned integeration  o---octal  t---binary  a--address  c--character  f---float

                               // Any kind of constant, variable or operator defined by the programming language you are using is valid in an expression in GDB.

(gdb)p *array@len

(gdb)p file::variable

(gdb)p function::variable

(gdb)p {type}address // interprete memory represented by address as type


(gdb)x lnfu address

// n---repeated time, default 1

// f -- printing format. except p format also support s --- C-string  i---machine command

//u -- print size  b--bytes  h--halfwords(2 bytes)  w--word  g--giantwords(8bytes)


(gdb)displaty lf expr |addr

(gdb)undisplay dnums

(gdb)delete display dnums

(gdb)disable display dnums

(gdb)enable display dnums

(gdb)info display


// stop application

(gdb)kill // kill the subprocess

(gdb)detach //stop debugging process

(gdb)End-of-File(ctrl+d)

(gdb)quit


// help

(gdb)help class-name

(gdb) help all

(gdb)help command

(gdb)apropos word

(gdb)complete prefix


// check information

(gdb)info name  // f name

(gdb)show // look up GDB related information e.g. show print pretty


(gdb) whatis //e.g. whatis str

(gdb)ptype //e.g. ptype str


3) Text Window Pattern

enter into TUI pattern:

 gdb --tui

|gdbtui

|C-X A 快捷键

when encountering blurred screen, run refresh command

We can change to cgdb tool


TUI has four windows:

command  abbreviation as cmd, command window can input debug command

source  abbreviation as src, show current line and breakpoints and so on

assembly abbreviation as asm, assemable window

register abbreviation as reg, CPU register window


can use 'help layout'  and focus commad

原创粉丝点击