29
COMPUTER SYSTEM LABORATORY Lab7 - Debugging I

COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

Embed Size (px)

Citation preview

Page 1: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

COMPUTER SYSTEM

LABORATORYLab7 - Debugging I

Page 2: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 292Lab 7

Experimental Goal

•Build a cross debugger and learn how to do source-level remote debugging with GDB.

2013/10/22

Page 3: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 293Lab 7

Environment

• Host System• Windows XP

• Build System• VirtualBox + Ubuntu 8.04

• Target System• Creator XScale PXA270

• Software• GNU Debugger

• Compiled toolchain

• Test programs

• Red Hat GDB GUI (insight)

• You can download all software from RSWiki CSL Course Software2013/10/22

Page 4: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 294Lab 7

Introduction to GDB

•How do you debug?• Human brain compiler

• printf printf printf …

•GDB - GNU Debugger• Allows you to see what is going on inside your program while it is

executing.

• Start your program, specifying anything that might affect its behavior.

• Make your program stop on specified conditions.

• Examine what has happened, when your program has stopped.

• Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

2013/10/22

Page 5: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 295Lab 7

Cross-Debugger Simulator Compilation (1/2)•Step1: download GDB source codes. (gdb-6.6a.tar.gz)

•Step2: extract the source codes.

•Step3: set configuration options (check Lab3’s arm-elf-* toolchain path in PATH).• % cd gdb-6.6

• % ./configure --target=arm-elf --prefix=$HOME/<arm-elf –* toolchain path> --enable-sim

• We configure --enable-sim to build a simulator for ARM.

• We recommend to place cross-debugger and cross-toolchain in the same directory, but it is optional.

2013/10/22

Page 6: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 296Lab 7

Cross-Debugger Simulator Compilation (2/2)•Step4: compile cross-debugger.

• % make -j4

•Step5: install cross-debugger.• % make -j4 install

•Test your cross-debugger.• % arm-elf-gdb -version

2013/10/22

Page 7: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 297Lab 7

Basic Commands of GDB

• (gdb) file [executable_name]

• (gdb) target [mode]

• (gdb) load [executable_name]

• (gdb) break [line_number]

• (gdb) run

• (gdb) next

• (gdb) step

• (gdb) print [expression]

• (gdb) where

• (gdb) quit

• (gdb) help

2013/10/22

Setting the break point

Start execution

Next

Step in

Print the value

Print the backtrace of the stack frame

Use sim/remote as the mode

Page 8: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 298Lab 7

GDB References

• GDB Documentation:• http://www.gnu.org/software/gdb/documentation/

• GDB User Manual:• http://sourceware.org/gdb/current/onlinedocs/gdb/

• GDB basic command:• http://en.wikibooks.org/wiki/GCC_Debugging/gdb

• Use the help command in GDB interactive shell.• (gdb) help

• Tip:• GDB provide history & auto-complete facility. You can use the kill command

to kill current debugging process and reload it by the file command.

2013/10/22

Page 9: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 299Lab 7

Verify the Simulator Debugging (1/2)

• Step1: download test program. (foo.c)

• Step2: compile the test program.• % arm-elf-gcc foo.c -g -static -o foo1

• -g: add debug symbol into the executable.

• -static: build the executable with static library.

• Step3: connect to simulator.• % arm-elf-gdb foo1

• (gdb) target sim

• Step4: download file from the host to the target (simulator).• (gdb) load foo1

• Step5: set breakpoint at line 7.• (gdb) break 7

2013/10/22

Page 10: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2910Lab 7

Verify the Simulator Debugging (2/2)

•Step6: execute the program.• (gdb) run

•Now, you can use info, print, etc. commands in GDB to debug your program. For example, if we want to watch the variables b and c in foo.c. • (gdb) print c

• (gdb) print b

• (gdb) print &b

• Recall the line 6 in foo.c.• c = &b;

• Since we set the breakpoint at line7, the result in GDB shows that c = &b = 0x1fffe0.

2013/10/22

Page 11: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2911Lab 7

Cross-Debugger Compilation (1/2)

•Now, we will cross-compile a cross-debugger to do remote debugging on PXA270 from the same GDB source codes.

•Step1: delete all temporary files in GDB source codes.• % cd gdb-6.6

• % make distclean

•Step2: set configuration options.• % ./configure --target=arm-unknown-linux-gnu --prefix=$HOME/<arm-

unknown-linux-gnu–* toolchain path>

•Step3: compile cross-debugger.• % make -j4

2013/10/22

Page 12: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2912Lab 7

Cross-Debugger Compilation (2/2)

•Step4: install cross-debugger.• % make -j4 install

•Test.• % arm-unknown-linux-gnu-gdb -version

2013/10/22

Page 13: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2913Lab 7

GDBserver Compilation (1/2)

•To do remote debugging, we need to build a server for GDB on PXA270.

•Step1: set configuration options.• % cd gdb-6.6/gdb/gdbserver

• % CFLAGS="-g -O2 -static" ./configure --host=arm-unknown-linux-gnu --target=arm-unknown-linux-gnu

•Step2: compile the gdbserver.• % make -j4

2013/10/22

Page 14: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2914Lab 7

GDBserver Compilation (2/2)

•The resulting gdbserver is the program we want.

•You can use file to check the executable.

2013/10/22

Page 15: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2915Lab 7

Set Network Environment (1/2)

•To do remote debugging, we can build LAN environment.

•You can follow the network configuration as below figure.• Target system (PXA270): 192.168.0.100

• Host system (Windows XP): 192.168.0.10

• Build system (Ubuntu 8.04): 192.168.0.101

• Recall in Lab2, we have configured the network of target system and host system, now we only need to configure build system.

2013/10/22

GDB Server

GDB Client

ethernet cable

Target System

Host System192.168.0.100

192.168.0.101

192.168.0.10

Build System

Page 16: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2916Lab 7

Set Network Environment (2/2)

• Step1: configure Ubuntu IP (build system).• % sudo vim /etc/network/interfaces

auto eth0

iface eth0 inet static

address <build-system-ip>

netmask 255.255.255.0

gateway <host-system-ip>

• % sudo /etc/init.d/networking restart

• Step2: configure virtual machine network setting. • “ 裝置” “網路卡” “網路”• “ 附加到” “橋接介面卡”

• Please configure correct network interface in“ 名稱” , i.e., the network interface connected to PXA270.

• Note that the network will be disconnected in build system after this configuration. If you want to reconnect to WAN, you can use dhclient command to get new DHCP address.

2013/10/22

Page 17: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2917Lab 7

Verify the Remote Debugging (1/4)

•Step1: compile test program.• % arm-unknown-linux-gnu-gcc foo.c -g -static -o foo2

•Step2: copy gdbserver & foo2 to Linux on PXA270.• You can refer to Lab2 to transfer files to Linux.

• Do not forget to change permission of these executable.

•Step3: execute gdbserver with port 1234 on PXA270.• $ ./gdbserver <build-system-ip>:1234 foo2

2013/10/22

Page 18: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2918Lab 7

Verify the Remote Debugging (2/4)

•Step4: connect to target from build system.• % arm-unknown-linux-gnu-gdb foo2

• (gdb) target remote <target-system-ip>:1234

2013/10/22

Target System

Build System

Page 19: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2919Lab 7

Verify the Remote Debugging (3/4)

•Step5: now, you can follow previous slides to set breakpoints for debugging, e.g. set to line 7.• (gdb) break 7

•Step6: continue executing the program.• (gdb) continue

• You must use the continue command instead of the run command in GDB remote debugging because the program is already running on the remote system when you set remote target.

• If you type run, GDB is attempting to run the program itself, thus dropping the existing target setting, i.e. connection.

• Running the ARM executable on the i386 machine tends to yield the don’t know how to run error.

2013/10/22

Page 20: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2920Lab 7

Verify the Remote Debugging (4/4)

•As slide10 does, if we want to watch the variables b and c, the print command can help you.• You can see that c = &b = 0xbe896ce0 on PXA270.

• The root filesystem is the 20M one.

2013/10/22

Page 21: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2921Lab 7

The GDB GUI – Insight (1/5)

• Insight is a graphical user interface to GDB written in Tcl/Tk by people working at Red Hat.• http://sources.redhat.com/insight/

•You can follow previous slides to compile insight (insight-6.6.tar.bz2) by both arm-elf-* and arm-unknown-linux-gnu-* toolchains.• You should install libx11-dev package before compiling.

• % arm-elf-insight -version

2013/10/22

Page 22: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2922Lab 7

The GDB GUI – Insight (2/5)

•There are three modes of insight:• command-line mode:

• % arm-elf-insight -nw

• Text-based UI mode:• % arm-elf-insight -tui

2013/10/22

command-line mode (like GDB)

text-based UI mode

Page 23: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2923Lab 7

The GDB GUI – Insight (3/5)

•GUI mode:• % arm-elf-insight

• “Run” “Connect to target”

• Choose your target, e.g., simulator.

• “File” “Open”

• Choose your executable, e.g., foo1.

• Click the source codes to set breakpoints.

• “Run” “Run”

2013/10/22

breakpoint

Page 24: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2924Lab 7

The GDB GUI – Insight (4/5)

•“View” can help you see related debug information.• Please refer to http://sources.redhat.com/insight/index.php for more

information.

2013/10/22

local variables

consoleregisters

Page 25: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2925Lab 7

The GDB GUI – Insight (5/5)

•You also can follow previous slides to do remote debugging by insight.• % arm-unknown-linux-gnu-insight foo2

• “Run” “Connect to target”

• Target GDBServer/TCP

• Hostname 192.168.0.100

• Port 1234

• Now, you can start to debug.

• Do not forget to use continue to execute.

2013/10/22

continue

Page 26: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2926Lab 7

Debug Program (1/2)

•Step1: download the test program (pi-agm.tar.gz).• The program is used to compute digits of .𝜋

•Step2: try to cross-compile the program for PXA270.• Please compile with “ntt-gen” configuration.

• i.e., type “make ntt-gen” in the directory pi-agm/src.

• Do not forget to change your compiler and add -static and -g flags.

•Step3: transfer the resulting binary pi-agm and configuration pi.ini to PXA270.

•Step4: execute the binary.• $ ./pi-agm

2013/10/22

Page 27: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2927Lab 7

Debug Program (2/2)

•However, we will get negative execution time from the results.

•Please use GDB to find why we get the negative execution time and try to fix it.

2013/10/22

?

Page 28: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2928Lab 7

Another GUI: GDB DDD (Optional)

•GNU DDD is a graphical front-end for command-line debuggers such as GDB, DBX, WDB, Ladebug, JDB, XDB, the Perl debugger, the bash debugger bashdb, the GNU Make debugger remake, or the Python debugger pydb. • We do not ask you use this GUI, and you can try it by yourself.

2013/10/22

Page 29: COMPUTER SYSTEM LABORATORY Lab7 - Debugging I. Lab 7 Experimental Goal Build a cross debugger and learn how to do source-level remote debugging with GDB

/ 2929Lab 7

Lab Requirement & Bonus

•Show how you find and correct the bug of pi-agm.

•Bonus:Answer the question of lab7 on RSWiki CSL course website.Write it in your report.

•Please send your report to both TAs.• [email protected], [email protected]

• Please use this title format: [CSL] G# Lab# Ver#

• E.g., [CSL] G13 Lab7 Ver1

2013/10/22