Tuesday, 24 January 2017

gdb basics tutorial

      Note these commands are universal except the first two are specific for llvm build and debug. For other application you want to debug, simply build a debug version using your own way (either make or cmake), then just 'gdb yourApplication' . For some complex applications you may need --args to pass some flags.

(1)    Build a debug version executable.  
cmake -DGCC_INSTALL_PREFIX=/home/llvm/gcc/install/gcc-6.2.0 -DCMAKE_BUILD_TYPE="Debug" -DBUILD_SHARED_LIBS=ON -DLLVM_ENABLE_ASSERTIONS=ON  ~/src/llvm ; make -j20

(2)    Start gdb:
gdb --args /home/jtony/debug/bin/llc  test/CodeGen/PowerPC/expand-isel.ll

(3)    Query function:
  info func expandMergeableISELs  (info func MergeableISEL also works, fuzzy matching?)
(4)     Set break points:
Function breakpoint:
b[reak] function_name
e.g: b(reak) PPCExpandISEL::expandMergeableISELs
set breakpoint at a specified file-line pair:
b[reak] file.cpp:7
break current line:
b[reak]
conditional break is also allowed:  b[reak] file.cpp:7 if a > 5

(5)    Start the program and run to breakpoint
r[un]
Run to next breakpoint:
c[ontinue]
typing r(un) again would restart the program from beginning

(6)    Switch to TUI mode
lay src          (ctrl + x  + a)

Step into:   ‘s[tep]’             -- will go into a function call, and walk through every line of code
Step over:  ‘n[ext]’            -- treat function call as one instruction, won’t go into it
Step out:    ‘fin[ish]’          --  finish the rest of the function call and return to the caller
Note: press ENTER gdb will repeat the same command you just gave to it

Save breakpoints:  save breakpoints filename
Reload breakpints:  source filename
Show breakpoints:   ‘info break’

‘show commands’
Print value of a variable:      p[rint] varName
Print in hexadecimal:       p[rint]/x  varName
print the type of var:             ptype ‘varName

Print calling stack:   ‘where’ or ‘backtrace
Navigate between calling stack:
Go up one level:       ‘up
Go down one level:  ‘down
Go up N level:           ‘up  N
Go down N level;     ‘down N
refresh the screen:   ‘refresh

call a function: call function_name(parameters)

(13) jump to a line (not safe, unpredictable, unless you know what you are doing)   
                 First set a break point on the line you want to jump to:   b[reak]   line_number    e.g  b 321
                 Then jump line number:  jump  line_number                       e.g  jump 321


New skills learned during the tutorial:

n(ext) N       will step over N steps, if N = 1, is equivalent to just n(ext)

No comments:

Post a Comment