Friday, July 18, 2014

GDB Tutorial (In short)

Run GDB with argument
-----------------

gdb
..
(gdb) run arg1 arg2

Breakpoints
-----------
before run
(gdb) break
(gdb) break
(gdb) break e.g. break hello.c:5

To clear breakpoints use clear command or for breakpoint number
        with delete command 'enable' and 'disable' breakpoints

Examining variable
-----------------
Use 'display' command e.g. (gdb) disp var1
And 'undisplay' to forget variable display

Use 'print' command e.g. (gdb) print var1

Use 'printf' command e.g. (gdb) printf "%d\n", var1


Stack Manipulation
------------------
Use command 'backtrace' or 'bt' this will show you current function
        call stack

more info:

backtrace -- Print backtrace of all stack frames
bt         -- Print backtrace of all stack frames
down -- Select and print stack frame called by this one
frame -- Select and print a stack frame
return -- Make selected stack frame return to its caller
select-frame -- Select a stack frame without printing anything
up         -- Select and print stack frame that called this one
list           -- list the ten lines before previous ten line listing.
                                    list

Stepping methods
----------------
Use 'finish' command to exit from current function and return to 
        calling function
        Use 'stepi' to step for a single assembly instruction
        Use 'advance' to continue to specified location e.g. advance 
       

Use 'jump' and 'continue' with location specified

Changing variable and values at run time
----------------------------------------
Use 'set variable' e.g. (gdb) set (i = 20) or 
        (gdb) set variable i = 20

Hardware watchpoint
-------------------
Use 'watch' command , its a special breakpoints this will trigger 
        whenever expression changes.
e.g. (gdb) watch var1
shows 'old value' and 'new changed value'

Attach to a running process
---------------------------
Need process ID and then use 'attach' command with the PID 
        to attach and break the running program

GDB coredump
------------
set ulimit -c 'unlimited'
gdb -tui -c core
In TUI mode, you'll be greeted with a screen of information, 
        telling you why the program exited  ("signal 11, Segmentation fault"),
        and the highlight will be on the offending line. 
use bt 

Window functions
----------------
All with -tui option (terminal user interface)
       In TUI mode, you can get a list of existing windows with the 
       info win command. You can then change which window has
       focus with the focus (or fs) command. Focus takes either a 
       window name, or "prev" or "next" as an argument. 
       Valid window names are "SRC" (source window), "CMD" 
       (command window), "REGS" (registers window), and "ASM" 
       (assembly window). 
       See the next section for how to use these other windows.

Display Registers and Assembly
------------------------------

in TUI mode, use following commands
layout src    Standard layout—source on top, command 
                                window on the bottom
 
layout asm Just like the "src" layout, except it's an assembly 
                                window on top

layout split Three windows: source on top, assembly in the 
                                middle, and command at the bottom layout reg
                                Opens the register window on top of either source
                                or assembly, whichever was opened last
        
        tui reg general Show the general registers
 
tui reg float    Show the floating point registers

tui reg system    Show the "system" registers

tui reg next Show the next page of registers—this is important 
                                because there might be pages of 
        registers that aren't in the "general", "float", or "system" sets

Working with macro
--------------------
To see the definition of all macros use command 'info macro [-a]
e.g. (gdb) info macro MAXVAL
      (gdb) info macro __STDC__
macro define    -- Define a new C/C++ preprocessor macro

macro expand    -- Fully expand any C/C++ preprocessor macro
                                               invocations in EXPRESSION

macro expand-once   -- Expand C/C++ preprocessor macro
                                                invocations appearing directly in EXPRESSION

macro list     -- List all the macros defined using the `macro define'
                                                command

macro undef     -- Remove the definition of the C/C++ preprocessor
                                                macro with the given name

Specifying debugging targets
 --------------------------- 

It is possible to run GDB for different target architectures. Use following commands: 
         set architecture    
         show architecture 
         set processor
         processor

         target child        -- Win32 child process (started by the "run" command) 
         target core         -- Use a core file as a target
         target exec         -- Use an executable file as a target
         target extended-remote   -- Use a remote computer via a serial line
         target record     -- Log program while executing and replay execution 
                                        from log 
         target record-btrace    -- Collect control-flow trace and provide the 
                                                   execution history

TBD....







No comments:

Post a Comment