Monday, 3 July 2017

GDB

GDB is free software, protected by the gnu General Public License (GPL).

help – The help provides help on commands associated with GDB
(gdb) help break –displays details about setting breakpoint
info – The command can be used for describing the state of your program
(gdb) info registers– displays value of registers used by the program
show- The command can be used to get the state of gdb itself
(gdb) show path –displays program path
The set environment command can be used to set the environmental variables used by your program.
(gdb) set environment HOME=/home/penchala
                (gdb) set environment PWD=/home/usr
(gdb) set environment PATH=/home/pencha 
Running programs under GDB  The debugging information can be generated with the option g
Example gcc –g –o client client.c
 Starting your program inside GDB
  file command can be used to load symbols from the executable file.
Example:  file a.out reads symbols from a.out 
run or r (creates a process) starts the execution of the program whose symbols are loaded.
 Debugging already running process
   attach pid The command halts process execution immediately. 
   detach pid The command detaches the process from the gdb.

Debugging programs with multiple threads
the gdb provides features to help debugging programs with several threads.

thread threadnumber – The control switches to thread denoted by threadnumber.
info threads - display the thread number assigned to thread by gdb and thread id given by the system.
 break linenumber threadno – stops the thread when it reaches the line number specified.
Watch points are used to watch the value of an expression
watch - The program stops execution when value of variable changes.
(gdb) watch a - The program stops execution when the value of a changes.
Removing Break , Watch points
clear break point/catch point/watch point – This command removes the specified break point
(gdb) break 10
(gdb) info break
1 breakpoint keep y 0x08048323 in main at s6.c:15
(gdb) clear 10
Deleted break point 1 
delete - The function removes all catch / break points. 
(gdb) delete
Remove all break points ? Y
(gdb) info break
No break points or watch points
Continuing and stepping
Continue (c)– Resume execution of the program from the current address where your program last stopped. 
step [count] – The program is used to execute the number of lines specified by count.
  until (u) – continue execution of your program until the specified location is reached or the current stack frame returns. 
stepi - execute one machine instruction then stop and return to debugger. 
nexti – execute one machine instruction then stop and return to debugger. But if it is a function call proceed until the function returns.

 Examining the stack
frame args- This command displays the stack frame currently executing that is innermost stack frame.
  Back traces – The back trace is a summary of how your program got where it is. 
    bt Print a back trace of the entire stack: one line per frame for all frames in the stack. 
    bt n Similar, but print only the innermost n frames
 
  Printing source lines 
      list function displays lines centered around the function named or line number given as argument.
Example show list size
                                 Number of source lines gdb will list by default is 5.
               set listsize count – The count of number of lines for list to print.
              Example set listsize 10
Examining the data
The print statement is used to print data and variables in program
print /f expr
/f – The format used can be x, d, u, o, c, f denoting radix, integer, unsigned, float etc.
expr – gdb supports any kind of constant, variable or operator supported by the programming language used.
    • Example –
                 (gdb) int a=10
               (gdb) print a
              print a=10 – sets the value of a to 10
             The print command can be used set values to variables.

 

No comments:

Post a Comment