Debugging with Eclipse

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
How to Debug VB .NET Code.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
CHAPTER 6 Loop Structures.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
Author: Loh Jianxiong Christopher Editors: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang.
Playing Back Scripts In HP LoadRunner >>>>>>>>>>>>>>>>>>>>>>
Debugging Dwight Deugo Nesa Matic
Debugging. 2 © 2003, Espirity Inc. Module Road Map 1.Eclipse Debugging  Debug Perspective  Debug Session  Breakpoint  Debug Views  Breakpoint Types.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
(for Visual C++) Created by Daniel Lee, Li Mengran (past tutors) Contributions: Loke Yan Hao (current tutor)
9/2/ CS171 -Math & Computer Science Department at Emory University.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Eclipse debugger.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 What we'll cover here l Using the debugger: Starting the debugger Setting.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
NETBEANS DEBUGGER.  To create a breakpoint place the cursor at the desired location.  Go to the Run -> toogle line Breakpoint or Ctrl +F8. It creates.
Author: Loh Jianxiong Christopher Contributions: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang, Tong Chun Kit, Tania Chattopadhyay.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Debugging What coders (programmers) do to find the errors (“bugs”) in their own programs “Bugs” – Admiral Grace Hopper, developer of the world’s first.
Using the Eclipse Debugger Extra Help Session (Christina Aiello)
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Author: Loh Jianxiong Christopher Contributions: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang, Tong Chun Kit, Tania Chattopadhyay.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Eclipse.
Values vs. References Lecture 13.
CprE 185: Intro to Problem Solving (using C)
Manipulating Pictures, Arrays, and Loops part 2
Appendix A Barb Ericson Georgia Institute of Technology May 2006
CS1101X Programming Methodology
CS1010 Discussion Group 11 Week 8 – Searching and Sorting.
Debugging Dwight Deugo
Eclipse Navigation & Usage.
Testing and Debugging.
Computer Programming I
Loop Structures.
Debugging CMSC 202.
Barb Ericson Georgia Institute of Technology Dec 2009
Using Visual Studio with C#
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Debugging with Eclipse
CIS 470 Mobile App Development
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
Tonga Institute of Higher Education
Debugging Taken from notes by Dr. Neil Moore
Do … Loop Until (condition is true)
Lecture 13: Two-Dimensional Arrays
Building Java Programs
Debugging Taken from notes by Dr. Neil Moore
Debugging Visual Basic Programs
CIS 470 Mobile App Development
Debugging Dwight Deugo
Manipulating Pictures, Arrays, and Loops
Debugging with Eclipse
Workshop for Programming And Systems Management Teachers
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Debugging with Eclipse Author: Loh Jianxiong Christopher Contributions: Chua Jie Sheng, Li Mengran, Peh Shao Hong, Oo Theong Siang, Tong Chun Kit, Tania Chattopadhyay

This tutorial assumes that you have already set up Eclipse This tutorial assumes that you have already set up Eclipse. If you haven’t, follow the ‘Getting Started With Eclipse’ Tutorial first.

Eclipse Debugger Eclipse contains a powerful debugger that enables us to easily find and fix problems. It lets us suspend a program during execution, in order to allow the user to step through the code line by line. We will demonstrate the basic functionality of the debugger first and then debug a buggy program.

Create a new class called Search. To demonstrate the functions of the Debugger, we will be using this piece of code: public class Search { public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; boolean result = searchArray(array, array.length, 15); System.out.println(result); } public static boolean searchArray(int[] a, int length, int x) { boolean isFound = false; for (int index = 0; index < length; index++) { if (a[index] == x) { isFound = true; return isFound;

Breakpoints Before we can begin debugging, we need to set a breakpoint. Whenever the debugger encounters a breakpoint, the program will be suspended to allow the user to start debugging. To set a breakpoint, double click on the left margin in the editor window. To remove a breakpoint, double click on it again. If no breakpoint is set, the debugger will automatically suspend only if it encounters an exception during runtime. Here a breakpoint has been set next to the main method. When we start the debugger, the program will suspend when it reaches this point in the code.

Running The Debugger To run the Debugger, select Run -> Debug As -> Java Application This brings Eclipse to the Debug Perspective

If the ‘Confirm Perspective Switch’ window appears, click ‘Yes’.

Perspectives Notice how this changed from ‘Java’ to ‘Debug’ Now the IDE is in debug ‘perspective’. A perspective is a set of views and windows. It provides the user with the necessary views and windows for a certain task. The default perspective that we have seen so far is the ‘Java’ perspective, and is used for coding. When Eclipse debugger is run, the workbench automatically switches to the ‘Debug’ perspective.

Debug Perspective These are the main components of the debug perspective. Variables associated with current stack Program stack frame Editor window. The next line to be executed is highlighted

Stepping Through Code These 3 buttons, “Step Into”, “Step Over” and “Step Return”, allows the user to Navigate through the code Step Into (F5) : Moves into the method or constructor at the current line Step Over (F6): Executes current line and suspend at the next line Step Return (F7): Executes until the current function returns and pauses immediately

Step Over Using Step Over, the debugger will go through the code one line at a time

Step Over Using Step Over, the debugger will go through the code one line at a time

Step Over Using Step Over, the debugger will go through the code one line at a time

Step Into If there is a method or constructor in the current line, Step Into can be used to move into the method

Step Into If there is a method or constructor in the current line, Step Into can be used to move into the method

Step Return If we are in the middle of a method execution, Step Return will execute until the end of current method and returns to the next line of the calling method.

Step Return If we are in the middle of a method execution, Step Return will execute until the end of current method and returns to the next line of the calling method.

Stepping Through Code The program stack frame can help us understand and find out where we are in the current program execution. As calls to methods are made, they are added on top of the ‘stack’. As methods are exited, they are removed from the ‘stack’ From the above example, we can see that we are currently executing the searchArray method, and that it was called by the main method

Stepping Through Code We can select different stack frames here to inspect As we step through the code, the variables window automatically updates the values of each variable. Highlighted rows show the variables which changed.

Expressions The variables window automatically keeps track of variables in the current scope of execution. Eclipse can also keep track of expressions, for example we can add a[index], or a[index]==x to the expressions window to examine its value at any time during execution To add an expression to the expression window, highlight it in the editor window of the Debug Perspective, right click and select Watch.

Expressions Expressions may also be added manually, by clicking “Add new expression”

Conditional Breakpoints Conditional breakpoints may be used to give the user more flexibility when debugging To set conditions on a breakpoint, select the “Breakpoints” tab, right click on a breakpoint, and select “Breakpoint Properties…”

Conditional Breakpoints Hit count: This option will only suspend the program after the breakpoint has been hit a specified number of times. This is useful when large loops are involved. Conditional: This option will only suspend the program if the specified condition is true, or if the value of the condition changes.

Self-test Assignment: Buggy Sort Now time to get our hands dirty. Create a new Java project. Add a new class called “BuggySort” Copy the contents of BuggySort class in the next slide into the “BuggySort” class your created. It is supposed to print an array, sort it then print the sorted array. Now run the program.

public class BuggySort { public static void main(String args[]) { int data[] = {45, 2, 33, 11, 5, 9, 7, 21, 10, 67, 99, 123, 42, 81, 100, 54}; System.out.println("our numbers:"); printArray(data, 0, 15); System.out.println(); bubbleSort(data, 0, 15); System.out.println("after sorting:"); } public static void printArray(int[] arr, int startIndex, int endIndex) { int i; for (i = startIndex; i <= endIndex; i++) { System.out.print(arr[i] + " "); public static void bubbleSort(int[] arr, int startIndex, int endIndex) { int i, j, temp; for (i = endIndex; i > startIndex; i--) { for (j = startIndex; j < i; i++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;

Expected output and Actual output Expected output that we’d like to see Actual output: The program actually hangs after printing the initial array Now press the red rectangular button to stop execution

Start Debugging Since the initial array is printed with no problem. We suspect that it’s the sorting function that enters an infinite loop, causing the program to hang. Set a breakpoint at the start of the outer loop, and start debugging by selecting Run -> Debug As -> Java application

Step through the inner loop once Now the program pauses at the starting point of the outer loop. We want to check the loop indices as the loops step through iterations. So, step through the inner loop once and pause at the start of the inner loop.

Check loop indices Current loop indices i and j have values of 15 and 0 respectively, which are correct. As we step over the wrapping up of the loop, we notice the value of i has changed from 15 to 16, as highlighted by the IDE with the yellow color. This is wrong, we were expecting j to increment.

Locating and correcting the error As we examine the code more closely, we find that what should have been “j++” was wrongly typed as “i++”. Correct it, remove the breakpoint and run again, we should see the program behave correctly.

End