Computer Science II Lab 3 Testing and Debugging

Similar documents
A Tutorial for ECE 175

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Programming Logic - Beginning

Exercise Session 6 Computer Architecture and Systems Programming

Lab 8 - Vectors, and Debugging. Directions

After completing this appendix, you will be able to:

Using the Xcode Debugger

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

Using Visual Studio.NET: IntelliSense and Debugging

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Getting Started. 1 by Conner Irwin

Debugging INTRODUCTION DEBUGGER WHAT IS VBA'S DEBUGGING ENVIRONMENT?

Instructions PLEASE READ (notice bold and underlined phrases)

GETTING STARTED WITH ECLIPSE Caitrin Armstrong

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

You can also start with both an executable program and a core file specified:

We first learn one useful option of gcc. Copy the following C source file to your

CS354 gdb Tutorial Written by Chris Feilbach

Supplement: Visual C++ Debugging

The NetBeans Debugger: A Brief Tutorial

CSE 351. GDB Introduction

Visual Studio.NET. Although it is possible to program.net using only the command OVERVIEW OF VISUAL STUDIO.NET

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Intro to MS Visual C++ Debugging

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

ECE2049 Embedded Computing in Engineering Design. Lab #0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

Part I. Integrated Development Environment. Chapter 2: The Solution Explorer, Toolbox, and Properties. Chapter 3: Options and Customizations

CS 150 Lab 3 Arithmetic and the Debugger. Lab 3.0 We are going to begin using the Visual Studio 2017 debugger to aid with debugging programs.

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Maintain an ILE RPG application using Remote System Explorer

11Debugging and Handling. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Slide 1 CS 170 Java Programming 1 Testing Karel

Lab 3-2: Exploring the Heap

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

18-600: Recitation #3

IDE: Integrated Development Environment

Introduction to SPSS

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Introduction to Computation and Problem Solving

Chapter 1 Getting Started

Recitation 10: Malloc Lab

Mastering the Actuarial Tool Kit

CMPT 300. Operating Systems. Brief Intro to UNIX and C

SQL Server. Management Studio. Chapter 3. In This Chapter. Management Studio. c Introduction to SQL Server

Tips on Using GDB to Track Down and Stamp Out Software Bugs

Learn to make watchosle

Jerry Cain Handout #5 CS 106AJ September 30, Using JSKarel

Problem Set 1: Unix Commands 1

CS 201 Software Development Methods Spring Tutorial #1. Eclipse

Debugging and Handling Exceptions

Integrated Software Environment. Part 2

BasicScript 2.25 User s Guide. May 29, 1996

Software Development With Emacs: The Edit-Compile-Debug Cycle

Programming Studio #9 ECE 190

Barchard Introduction to SPSS Marks

Using the KD30 Debugger

EE 355 Lab 3 - Algorithms & Control Structures

VB Net Debugging (Console)

HPCC - Hrothgar. Getting Started User Guide TotalView. High Performance Computing Center Texas Tech University

Reviewing gcc, make, gdb, and Linux Editors 1

Barchard Introduction to SPSS Marks

Installing and getting started with Visual Studio for C programming in Windows.

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

Recitation 11: More Malloc Lab

Prerequisites for Eclipse

Basic functions of a debugger

Flow of Control: Loops

Introduction to IntelliJ

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Deitel Dive-Into Series: Dive-Into Cygwin and GNU C++

Unit Testing as Hypothesis Testing

CS Programming Exercise:

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Debugging in AVR32 Studio

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Statistics with a Hemacytometer

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

6. Essential Spreadsheet Operations

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

Code::Blocks Student Manual

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

ECE QNX Real-time Lab

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

Introduction to C/C++ Programming

6L00IA - Introduction to Synergy Software Package Short Version (SSP v1.2.0) Renesas Synergy Family - S7 Series

CSE 410: Systems Programming

n! = 1 * 2 * 3 * 4 * * (n-1) * n

1. Introduction EE108A. Lab 1: Combinational Logic: Extension of the Tic Tac Toe Game

CS 11 C track: lecture 6

SECTION 2: HW3 Setup.

Midterm Exam, October 24th, 2000 Tuesday, October 24th, Human-Computer Interaction IT 113, 2 credits First trimester, both modules 2000/2001

Debugging Code in Access 2002

EXAMINING THE CODE. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist:

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

EXCEL Using Excel for Data Query & Management. Information Technology. MS Office Excel 2007 Users Guide. IT Training & Development

Transcription:

Computer Science II Lab 3 Testing and Debugging Introduction Testing and debugging are important steps in programming. Loosely, you can think of testing as verifying that your program works and debugging as finding and fixing errors once you ve discovered it does not. Writing test code is an important (and sometimes tedious) step. Many software libraries have regression tests that run automatically to verify that code is behaving the way it should. Here are four strategies for testing and debugging: 1. When you write a class, write a separate driver main function that calls each member function, providing input that produces a known, correct result. Output of the actual result or, better yet, automatic comparison between actual and correct result allows for verifying the correctness of a class and its member functions. 2. Carefully reading the code. In doing so, you must strive to read what the code actually says and does rather than what you think and hope it will do. Although developing this skill isn t necessarily easy, it is important. 3. Judicious use of cout statements to see what the program is actually doing. This is especially useful for printing the contents of a large data structure or class. It is often hard to visualize large objects using the debugger (see next item) alone. 4. Using the debugger to (a) step through your program, (b) check the contents of various variables, and (c) locate floating point exceptions and segmentation violations that cause your program to crash. Points and Rectangles The programming context for this lab is the problem of determining what 2D points are in what 2D rectangles. For rectangles, we will assume they are aligned with the coordinate axes, as shown in Figure 1. This makes it easy to represent and to test if a point is inside. Our code will store points in rectangles and determine which points are in which rectangles. This is a toy example of problems that must be addressed in graphics and robotics. Please download the following 3 files needed for this lab and then turn off your internet connection: http://www.cs.rpi.edu/academics/courses/spring07/cs2/labs/lab03/point2d.h http://www.cs.rpi.edu/academics/courses/spring07/cs2/labs/lab03/rectangle.h http://www.cs.rpi.edu/academics/courses/spring07/cs2/labs/lab03/rectangle.cpp Checkpoint 1 Start by creating a project and adding the files Point2D.h, Rectangle.h, and Rectangle.cpp. Examine these files briefly. Point2D.h has a simple, self-contained class for representing point

y (8,10) (17,9) (9,3.5) (4,3) x Figure 1: Example of a rectange aligned with the coordinate axes the only type of rectangle considered here. The rectangle is specified by its upper right corner point, (17, 9), and its lower left corner point, (4, 3). The point (9, 3.5) is inside the rectangle, whereas the point (8, 10) is outside. coordinates in two-dimensions. No associated.cpp file is needed because all member functions are defined in the class declaration. Rectangle.h and Rectangle.cpp contain the start to the Rectangle class. They also contain a bug. Please read the code now to see if you can find it. Do not worry if you can not, but do not fix it in the code if you do! Your job in this checkpoint is to complete the implementation of the Rectangle.cpp class. Look through Rectangle.h and Rectangle.cpp to determine what functions need to be added. Then, compile these files and remove any compilation errors. Checkpoint 2 Create a new file (within Visual Studio put this within the current project) and call it test rectangle.cpp. Create a main function within this file. In the main function, write code to test each of the member functions. For example, write code to create several rectangles, and print their contents right after they are created. Write code that should produce both true and false in the function is point within. (In fact, if there is non-trivial logic in a function, multiple inputs to the test code should be attempted to test as many possible conditions as you can.) Write code to add points (or not) to a rectangle. Write code to find what points are contained in both rectangles. To complete this checkpoint, show a TA your test cases and the error(s) that those test cases reveal in the provided code. After doing this you should be able to spot that there is an error in the code that I provided you (as well as, perhaps, errors in your own code). Even if you know where the bug or bugs occur, please do not fix them yet. Checkpoint 3 Now, we need to practice using the debugger to find and fix errors. Visual Studio has an associated visual debugger. If you compile with g++ on cygwin, you can use the separate command-line 2

debugger gdb. Other debuggers are available, many of them built on top of gdb. Of special note, gdb may be run from inside of the emacs and xemacs editors. In the following outline, Step 1 is for everyone. Step 2 shows you how to get started, and has separate instructions for using the Visual Studio debugger and using cygwin/g++/gdb. Steps 3 and beyond are written primarily for Visual Studio. Those of you using other debuggers should read the Visual Studio instructions and then adapt them to your debugger. Specific pointers to gdb commands are provided at the end of each item. Many introductory gdb debugging tutorials can be found on-line; just type gdb tutorial into Google. Here are three: http://www.cs.princeton.edu/~benjasik/gdb/gdbtut.html http://www.cs.cmu.edu/~gilpin/tutorial/ http://arioch.unomaha.edu/~jclark/gdb_plus.html I like the last one best, but you may feel differently. You may re-enable your internet connection to read reference material specific to your debugger & development environment. 1. Run your program that has tests for the basic member functions. Even though the program will compile and run, it will not give the correct output. You may be suspicious about a place in your code where the error occurs. It is time to start the debugger. 2. Getting started: Getting started with the Visual Studio debugger: Begin by Setting a breakpoint. In the source code, go to the file where the error might have originated. Select Debug -> New Breakpoint. Four options will be presented. You can set a breakpoint based on the program entering a function, at specific point in a file, at a memory address, or when a specific data condition is met. Click File and you will notice that the current line number is displayed. Click OK. A breakpoint is now set. Your program will halt when execution reaches this location in the code (when run using the debugger). Note, that you can set this simplest Breakpoint (with standard options) not only using this menu, but also by right clicking at a particular line (then Insert Breakpoint), or even more quickly clicking on a grey bar along left side of your window. Look at the main menu and click Debug -> Start (F5). This will start your program under control of the debugger. A console display will immediately pop up. Your program will execute until reaching the breakpoint you ve set. At this point, several windows will appear including the source code window. You should still be able to see the console where you typed the input, but this may be hidden. It is important to look back and forth between this and the.net display. Getting started with g++/gdb: If you are using gdb or any other debugger that works with code compiled by g++, you will need to compile your code differently. In particular, you will need to compile using the -g option, as in g++ -g Rectangle.cpp rectangle_test.cpp -o rect_test This creates code with debugging information stored about the variables and functions in the program. (Options that you set with Visual Studio compiler generate this debugging 3

information automatically.) Note that the format of this information is different for different compilers, so code compiled using g++ can not be debugged using Visual Studio. In order to start gdb type gdb rect_test This puts you into the command-line debugger. Type help in order to see the list of commands. There are several for setting breakpoints. You can set a breakpoint by specifying a function name or a line number within a file. For example break main sets a breakpoint at the start of the main function. You can also set a breakpoint at the start of a member function, as in break Rectangle::add_point Finally, to set a breakpoint at a specific line number in a file, you may type, break foo.cpp:65 to set a breakpoint at line 65 of file foo.cpp. Set a breakpoint at some point in your code just before (in order of execution!) you think the first error might occur. Finally, in order to actually start running the program under control of the debugger, you will need to type run at the gdb command line. As mentioned above, the remaining instructions are centered on the use of Visual Studio. Each ends, however, with pointers to equivalent gdb commands. Use the tutorials to figure out the differences if you are using gdb. 3. Stepping through the program: You can now step through your program one line at a time. Use F10 to step to the next line of code in the current function. This executes this line before stopping and redisplaying (it happens very quickly, though). This works even if the line involves a function call. If you want to see what happens inside the function call, typing F11 will put you into the called function s code. Try not to do this at calls to standard library functions. It can get messy. If you do, Shift-F11 will get you out. Hit F10 several times and watch the console display. The output lines of code you wrote will appear so that you can see what s happening there. In gdb you can step through the code using the commands next and step. The command continue allows you to move to the next breakpoint. 4. Content of variables: The default set of debugging windows includes tabs Autos, Locals, and Watch in one panel (usually bottom left corner). If you do not see a window that we discuss here, you can click Debug -> Windows and select the window you want to see. Autos display variables used in the previous statement and the current statement. This is useful when you are at a particular line in a program and only care about the subset of all variables when investigating a problem. Locals will list all local variables in the current scope. In the Watch window, you can display any valid expression that is recognized by the debugger (for example sum of two variables). 4

In gdb, you can use print to see the values of variables and expressions. Use display to specify the values and variables to print every time your program is stopped. 5. Program flow: You will find another set of debugging windows in the second panel. These are Call Stack, Breakpoints, Command Window, and Output. The Call Stack displays the current execution path (in terms of function calls). Click on different entries in the call stack and different code will be displayed. Be sure to get back to the code for Rectangle. You can tell from the position of the yellow arrow. Breakpoints lists all breakpoints in your program. Output window shows status of various features in the development environment and we won t use it much in this class. Command Window is very useful and powerful. It allows you to evaluate expressions, execute statements, print variable values, and sometimes even change them. It operates in two modes, we will use only Immediate mode (you should see the tab name as Command Window - Immediate, if you do not, type immed). In gdb, you can use the command backtrace to show the contents of the call stack. This is particularly important if your program crashes. Unfortunately, the crash often occurs inside C++ library code. Therefore, when you look at the call stack the first few functions listed may not be your code. Find the function on the stack that is the first one (nearest to the top of the stack) that is your code. By typing frame N, where N is the index of function on the stack, you can examine your code and variables and you can see the line number in your code that caused the crash. 6. Command Window Let s try some of the features of the Command Window. Type?m_upper_right. You should see complete report about this variable. Now try to change the +x+ coordinate of the upper right corner by typing m_upper_right.m_x=2. Again, check the status of the variable. When you type?m_points_contained, you get report about your vector variable. You will see three pointers with their values; these are pointer to an array which is internal representation of the vector. Type?m_points_contained._Myfirst and you will find out about the first element (Point2D). To see the second, type?m_points_contained._myfirst[1], and so forth (until you reach the total vector size). In gdb many of these options are handled through the print and display commands. 7. Now, use F10 to step through the execution one line at a line. Look at the source code, the console, and the watch window. You should see the error pretty soon. Use the Watch window to find the bug in the program. Hint: You can even display one coordinate of your rectangle. When you have found it, show a TA how you found it using the debugger. Have your Watch window visible and also show the contents of your Command Window where you tried examples above (enlarge the windows to take about half of your laptop screen). Be ready to answer questions about the purpose of the other debugging windows. In gdb, you can use the command display to see the contents of a particular variable or expression when the program is stopped. Therefore you can complete this part of the exercise easily using gdb. 8. Breakpoint on Variable Change: The last powerful debugger feature you will find out about today is variable monitoring. Create an instance of Point2D in your main function and change the coordinates using Set function. You will now monitor the change of this point using the debugger. Select Debug -> New Breakpoint and choose the Data tab. In Variable 5

field, type pt.m_x, pt is my point in this case. Context is to tell the debugger, where can the variable be found. The syntax is {[function],[source],[module]}, in our case, it is enough if we fill in {,test_rectangle.cpp,}. Hit OK and run the debugger. Show your TA that your program successfully stopped when the variable got changed. Your TA may also ask you questions about the other steps in debugging. This will complete this checkpoint and the lab. In gdb, you can use the command watch to halt the program when a variable or expression changes. Therefore, you can complete this part of the lab using gdb. Please note that the 3rd checkpoint has only given you a brief introduction to debugging. You will learn much more through extensive practice. In fact, when you go to see one of us the TAs or Prof. Stewart to ask for help in debugging your assignments, we will constantly be asking you to show us how you have attempted to find the problem on your own. This will include combinations of the four steps listed at the start of the lab. 6