Displaying Program Variables Displaying Variables

Displaying Variables 155 XPVM_ROOT=usrsharepvm3xpvm KDEDIR=usr USER=rr LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=4 0;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:.c md=01;32:.exe=01;32:.com=01;32:.btm=01;32:.bat=01;32:.sh= 01;32:.csh=01;32:.tar=01;31:.tgz=01;31:.arj=01;31:.taz=01 ;31:.lzh=01;31:.zip=01;31:.z=01;31:.Z=01;31:.gz=01;31:.b z2=01;31:.bz=01;31:.tz=01;31:.rpm=01;31:.cpio=01;31:.jpg= 01;35:.gif=01;35:.bmp=01;35:.xbm=01;35:.xpm=01;35:.png=01 ;35:.tif=01;35: MACHTYPE=i386-redhat-linux-gnu OLDPWD=homerr MAIL=varspoolmailrr INPUTRC=etcinputrc BASH_ENV=homerr.bashrc LANG=en_US DISPLAY=192.168.2.115:1 LOGNAME=rr SHLVL=1 SHELL=binbash HOSTTYPE=i386 OSTYPE=linux-gnu HISTSIZE=1000 LAMHELPFILE=etclamlam-helpfile PVM_ROOT=usrsharepvm3 TERM=xterm HOME=homerr SSH_ASKPASS=usrlibexecopensshgnome-ssh-askpass PATH=usrkerberosbin:bin:usrbin:usrlocalbin:usrbin X11:usrX11R6bin _=usrbingdb gdb You can also use the set env command and the unset env command to modify envi- ronment variables or to create or delete variables.

5.5.4 Modifying Variables

Using the set command, you can modify any program variable during the debugging process. The following session shows an example where you entered a value 34 for program variable num1 but modified it to 11 using set command before the sum was calculated. You can see that the sum calculation process takes into account the new value. Enter first number : 34 11 printfEnter second number : ; gdb n 12 scanfd, num2; 156 Chapter 5 • Working with GNU Debugger gdb n Enter second number : 12 14 total = sumnum1, num2; gdb print num1 1 = 34 gdb set num1=11 gdb print num1 2 = 11 gdb print num2 3 = 12 gdb n Calculation complete. Returning ... 15 printf\nThe sum is : d\n, total; gdb n The sum is : 23 16 } gdb

5.6 Adding Break Points

When you start debugging a program, you use the run command. This command executes the program until the end of the program or a break point is met. A break point is a place in your source code file where you temporarily want to stop execution of the program being debugged. Break points in GNU debugger can be placed using the break command. Look at the fol- lowing list of the source code file sum.c which you already have used: gdb list 1 include stdio.h 2 main 3 { 4 int num1, num2, total ; 5 6 printfEnter first number : ; 7 scanfd, num1; 8 printfEnter second number : ; 9 scanfd, num2; 10 gdb To place a break point at line number 6 in file sum.c displayed above, you can use the following command: gdb break sum.c:6 Breakpoint 1 at 0x8048496: file sum.c, line 6. gdb Adding Break Points 157 As you can see from the above lines, when you set a break point, GNU debugger will dis- play its information in the next line. This information contains the number of the breakpoint, memory address, file name and line number. You can also see a list of currently set break points using the following command: gdb info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048496 in main at sum.c:6 gdb Break points can also be set on function names. The following command sets a break point where function main starts: gdb break main Breakpoint 1 at 0x8048496: file sum.c, line 6. gdb Note that although the function main starts at line number 2, the break point is set at line number 6. This is because the first executable instruction of the function main is located at this line number. You can also set a break point at a particular line number in the currently loaded file. The following command creates a break point at line number 8: gdb break 8 Breakpoint 2 at 0x80484ba: file sum.c, line 8. gdb In a multi-source file project, you set up a break point by including the file name and line number on the command line. The following command sets up a break point at line number 9 in file sum.c. gdb break sum.c:9 Breakpoint 3 at 0x80484ca: file sum.c, line 9. gdb You can also use an offset value to set up a break point. For example if the execution pointer is on line number 6, you can set up a break point at line number 9 using the following command. Note that you can also use a minus symbol to specify an offset. 6 printfEnter first number : ; gdb break +3 Note: breakpoint 3 also set at pc 0x80484ca. Breakpoint 4 at 0x80484ca: file sum.c, line 9. gdb All break points can be displayed using the info command. The following command dis- plays three break points that we have specified: gdb info break Num Type Disp Enb Address What