De
BUG  with
GDB

                                                                                                                                                                   << CONTENTS >>

                    Example sessions Debugging with gdb:
                                      GDB startup:
                                            gdb can be started with silent mode or patch mode or normal mode.
               
                                      silent mode:

                                        gdb startup without any warrenty messages
                                        # gdb -q   or gdb --silent or gdb --quiet
   
                                      patch mode:
                                        gdb execution with a file as execution input to it.
                                           ----- patchfile ----------
                                           show endian
                                           show os
                                           quit
                                           ----------------------------
                     
                                        # gdb -x patchfile
HP gdb 4.0 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 4.0 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.

The target endianness is set automatically (currently big endian)
Operating system is "".
HP-UX ntc63 B.11.23 U ia64 3727639447 unlimited-user license
                                       You can combine patch mode with silent mode as like
                                       # gdb -q -x patchfile
                                  Normal mode:
                                        gdb will be started with warrenty messages ( version informations )
   
                                   File's inclusion for debugging:
                     
                                           Inclusion of File(s) which is(are) to be debugged included using gdb startup command line or after startup
                                   command line:
                                          # gdb <mode> <executable-file> <core-file if any want to be debugged> (or)
                                   After startup:  
                                         # gdb -q
                                        gdb> file <filename>

                                         You have to get the string as "Reading symbols from strlen...done." after file inclusion so that included files are ready to debug. Else your file may not have debugging symbols or problem on compilation or file and you will get a message of error as "(no debugging symbols found)..."

                         gdb command line options:

                               To view gdb command line options use gdb --help or gdb -h

                               HP gdb 4.0 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x command line versions:
This is the GNU debugger.  Usage:

    gdb [options] [executable-file [core-file or process-id]]

Options:

  --[no]async    p;    Enable (disable) asynchronous version of CLI
  -b BAUDRATE        Set serial port baud rate used for remote debugging.
  --batch    &nbbsp;       Exit after processing options.
  --cd=DIR    &nnbsp;      Change current directory to DIR.
  --command=FILE   &nnbsp; Execute GDB commands from FILE.
  --core=COREFILE    Analyze the core dump COREFILE.
  --pid=PID    &      Attach to running process PID.
  --dbx     p;         DBX compatibility mode.
  --directory=DIR    Search for source files in DIR.
  --objectdir=DIR    Search for object files in DIR.
  --epoch    &nbbsp;       Output information used by epoch emacs-GDB interface.
  --exec=EXECFILE    Use EXECFILE as the executable.
  --fullname    ;     Output information used by emacs-GDB interface.
  --help    &nbssp;        Print this message.
  --interpreter=INTERP
                     Select a specific interpreter / user interface
  --mapped    &nnbsp;      Use mapped symbol files if supported on this system.
  --nw     ;          Do not use a window interface.
  --nx     ;          Do not read .gdbinit file.
  --quiet    &nbbsp;       Do not print version number on startup.
  --readnow    &      Fully read symbol files on first access.
  --se=FILE    &      Use FILE as symbol file and executable file.
  --symbols=SYMFILE  Read symbols from SYMFILE.
  --tty=TTY    &      Use TTY for input/output by the program being debugged.
  --tui     p;         Use a terminal user interface.
  --version    &      Print version information and then exit.
  -w     &nnbsp;           Use a window interface.
  --write    &nbbsp;       Set writing into executable and core files.
  --xdb     p;         XDB compatibility mode.

For more information, type "help" from within GDB, or consult the
GDB manual (available as on-line info or a printed manual).
Report bugs to "[email protected]".

                         File contents viewing
                  
                                  We can view the sourece lines with inclusion of object file with or without having source file there.
      
                                   gdb) list 1,10
                        
                                        It will view the source lines of 1,10

                                  By default it will view only 10 lines. To make it more use,
   
                                  gdb) set listsize 50
                                 
                                  To make it default ( 50 lines) the use .gdbinit file for that. Check here for more.
                                 
                           
                            Break points:

                            It is good to have atleast a single break point line in the debugging source. To set break point use as,

                                  gdb) b <line-number>
                                  gdb) b <function-name>

                                  To set break points in all functions in a file then,
                                   gdb) rbp
                                   end
                                 
                                  To delete a particular break points ,
                                        view all break points as,
                                         gdb) info b
                                         gdb) d b <break-point number>

                                        If you use gdb) d b then it will prompt as Do you want to delete all break points (y/n) if you put y ,it will delete all break points

                                  To delete all break points set in every functions then,
                                        gdb) rdp
   
                               A sample program debugging:
                                   
<ntc63> gdb -q
(gdb) file strlen
Reading symbols from strlen...done.
>> we are having debugging symbols to process the operation
(gdb) li 1,20
1       #include <stdio.h>
2       #include <stdlib.h>
3       main()
4       {
5               char *p;
6               fflush(stdout);
7               p=(char *)malloc(100*sizeof(char));
8               printf("\nEnter a string for test\n");
9               gets(p);
10              printf("%s\n",p);
11              free(p);
12      }

>> Source lines of input files. list (li) will show all lines 1,20 (if it is not there too it will try to show upto the limit)

(gdb) b main
Breakpoint 1 at 0x4000920:1: file strlen.c, line 6.

>> break point at main function. It will give the informations of break points

(gdb) info b
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x04000921 in main at strlen.c:6

>> To know all break point informations then use info breakpoint (info b)

(gdb) r
Starting program: /muthu/gdbtest/strlen

Breakpoint 1, main () at strlen.c:6
6               fflush(stdout);

>> Startup of program debugging as run (r). If you have command line argument(s) then specify here as r <arg1> <arg2> ..<argn>

(gdb) disp p
1: p = 0x1 <Error reading address 0x1: Invalid argument>

>> If you want to keep track of some variables informations every stage then specify the variable with display <variable> >> name. To make it undisplay in next step then undisplay <variable>. It will be very effective to know the status at every stage

(gdb) s
7               p=(char *)malloc(100*sizeof(char));
1: p = 0x1 <Error reading address 0x1: Invalid argument>

>> To go to next debugging instruction use step(s) or next(n) If you want to move to step more than 1 then use
>>
step <count> (s 10) to move to the 10 the instruction for now

(gdb) s
8               printf("\nEnter a string for test\n");
1: p = 0x40002420 ""
(gdb) s

Enter a string for test
9               gets(p);
1: p = 0x40002420 ""
(gdb)
hai gdb
10              printf("%s\n",p);
1: p = 0x40002420 "hai gdb"
(gdb) s
hai gdb
11              free(p);
1: p = 0x40002420 "hai gdb"
(gdb)
12      }
1: p = 0x40002420 "hai gdb"

>> If you are going to use particular prev command on next time then just press <ENTER> it will do it. !!!
>> To know about gdb command history then try # apropos history

(gdb)
0x20000000679abcc0:0 in main_opd_entry+0x50 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function main_opd_entry,
which has no line number information.
0x20000000679abc50:0 in enter_main_opd+0x40 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function enter_main_opd,
which has no line number information.
0x20000000679b11d0:0 in dld_main_startup+0xf50 () from /usr/lib/hpux32/dld.so
(gdb)
Single stepping until exit from function dld_main_startup,
which has no line number information.
warning: Cannot insert breakpoint 0: in /usr/lib/hpux32/dld.so
warning: dld.so is a shared library and will never be loaded
warning: as private, so breakpoint 0 has been ignored.
warning: Temporarily disabling shared library breakpoints:
0

Program exited normally.
>> It will give you execution status of a program from gdb
(gdb)
         
DEBUGGING EXAMPLES                                                        Muthukumar Kandasamy <[email protected]>
                           
Hosted by www.Geocities.ws

1