CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.

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

CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Georgia Institute of Technology DrJava Appendix A Barb Ericson Georgia Institute of Technology May 2006.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Chapter 4 Code Editor Goals and Objectives Program more efficiently? How can you speed up your development process? Do you want to learn useful shortcuts.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 INF160 IS Development Environments AUBG, COS dept Lecture 06 Title: Dev Env: Code::Blocks (Extract from Syllabus) Reference:
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
| | Tel: | | Computer Training & Personal Development Outlook Express Complete.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CPSC1301 Computer Science 1 Overview of Dr. Java.
Our Environment We will exercise on Microsoft Visual C++ v.6 We will exercise on Microsoft Visual C++ v.6 because that is what we have in the univ. because.
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.
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.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
CSE 232: C++ Programming in Visual Studio Graphical Development Environments for C++ Eclipse –Widely available open-source debugging environment Available.
CSE 232: C++ debugging in Visual Studio and emacs C++ Debugging (in Visual Studio and emacs) We’ve looked at programs from a text-based mode –Shell commands.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Mobile Programming Lecture 3 Debugging. Lecture 2 Review What widget would you use to allow the user to enter o a yes/no value o a range of values from.
Data Display Debugger (DDD)
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
©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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Introduction to Eclipse Programming with an Integrated Development Environment.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve looked at programs from a text-based mode –Shell commands and command lines –Text editors,
Using the Eclipse Debugger Extra Help Session (Christina Aiello)
Fundamental Programming Fundamental Programming Introduction to Functions.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Functions A bit of review (things we’ve covered so far)
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Testing and Debugging.
Barb Ericson Georgia Institute of Technology Dec 2009
Important terms Black-box testing White-box testing Regression testing
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Important terms Black-box testing White-box testing Regression testing
Functions Inputs Output
Debugging with Eclipse
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
Debugging Taken from notes by Dr. Neil Moore
When your program crashes
Our Environment We will exercise on Microsoft Visual C++ v.6
Debugging Taken from notes by Dr. Neil Moore
Debugging with Eclipse
Workshop for Programming And Systems Management Teachers
Selenium IDE Installation and Use.
Presentation transcript:

CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements –Functions and the program call stack –Passing function arguments by reference vs. by value Today we’ll cover –How to set up Eclipse to debug a simple example program –What to think about and look for when debugging –How to step through a program in Eclipse –How the call stack and variable values change as we run

CSE 332: C++ debugging in Eclipse Getting Started To set up your own environment for what we’ll cover –Log onto one of the CEC machines –Create a new directory (for example mkdir calculator ) –Save files from course web page into that directory Makefile prefix_adder.h prefix_adder.cc Now we’ll set up Eclipse to work with that code –Launching Eclipse –Setting up a project –How to step through a program in Eclipse –How the call stack and variable values change as we run

CSE 332: C++ debugging in Eclipse Launching Eclipse In the Gnome Desktop –Click on the Red Hat –Select Programming –Select Eclipse If you use another desktop environment in CEC lab –Should be similar –Ask CEC for help –Let me know if you have any problems

CSE 332: C++ debugging in Eclipse Creating an New Eclipse Project When Eclipse starts, create a new project –Click on File menu –Click on New menu item –Select Project

CSE 332: C++ debugging in Eclipse Using Standard Makefiles in Eclipse Select the Standard Make C++ Project wizard –That lets us use the kind of Makefile we’ve used so far –You can also use “Managed Make” but we won’t do that

CSE 332: C++ debugging in Eclipse Searching for an Existing Directory Eclipse lets you browse for an existing directory –Lets you find existing code –And bring it into an Eclipse project

CSE 332: C++ debugging in Eclipse Selecting an Existing Directory Find and select the directory you want to use …

CSE 332: C++ debugging in Eclipse Naming the New Eclipse Project … then fill in a name for the project

CSE 332: C++ debugging in Eclipse C/C++ Perspective in Eclipse Click on Finish Click Yes when the prompt appears –Uses the C/C++ perspective in Eclipse for the new project

CSE 332: C++ debugging in Eclipse Makefile Target “all” for Eclipse Eclipse should now show the files in that directory One change that’s needed to the Makefile –Eclipse expects a make target called “all” –Can just add a dependency on the executable

CSE 332: C++ debugging in Eclipse Setting up Project Configuration in Eclipse Still need to set up a few more options Click on Run menu Select Debug…

CSE 332: C++ debugging in Eclipse Local Application Configuration in Eclipse Select C/C++ Local Application –We won’t use the other C/C++ configurations right now Attaching to a running program Debugging a crashed program (Postmortem debugger) –Other options are for Java programs (may look familiar)

CSE 332: C++ debugging in Eclipse Setting up a New Debugging Configuration Click on New button to set up a debug configuration –Will fill in default “main” values for the current project

CSE 332: C++ debugging in Eclipse Filling in Command Line Arguments to Use Click on Arguments tab and fill in program arguments –The rest of the command line after the program name –Can change these to run different tests of the program

CSE 332: C++ debugging in Eclipse Choosing the Debugger to Use in Eclipse Click on Debugger tab and choose GDB Debugger –Notice that the “no debugger available” error goes away

CSE 332: C++ debugging in Eclipse Adding Toolbar Items (Optional) Click on Common tab to add toolbar items –Can check Run and/or Debug boxes, then click Apply Now you’re all set up: click Debug to run in debugger

CSE 332: C++ debugging in Eclipse Debugging an Example Program Now we’ll use Eclipse to debug a program –Step through (and into) functions –Watching the call stack and variable values But, before we start using the fancy tools… –What are we trying to achieve? –What do we expect our program to do? –How might our program fail? –Can we make predictions and test them? Thinking: the most powerful way to debug –Scientific method should guide what you do hypothesis, prediction, experiment, analysis –Eclipse can help you follow this disciplined approach faster

CSE 332: C++ debugging in Eclipse What the Example Program Does Called with command line arguments./prefix_adder Calculates prefix addition expressions These are equivalent to their infix versions (8 + (9 + 10)) ((8 + 9) + 10) Key idea: walk through expresion, calculate value same result different order

CSE 332: C++ debugging in Eclipse How the Example Program Can Fail Too few arguments in expression./prefix_adder Cannot calculate result (needs another value to finish 2 nd + operation) Try this on your own in the lab, for practice ???

CSE 332: C++ debugging in Eclipse Example Program Header File // prefix_adder.h // // author: Chris Gill // // purpose: Declarations for a simple prefix adder program, which // takes the command line arguments as a prefix addition // expression and computes an integer result. #ifndef PREFIX_ADDER_H #define PREFIX_ADDER_H // Function prototypes. void usage (char * program_name); int parse_and_compute (int & current_index, int last_index, char *argv[]); #endif /* PREFIX_ADDER_H */

CSE 332: C++ debugging in Eclipse Example Program Source File // prefix_adder.cc // // author: Chris Gill // // purpose: definitions for a simple prefix adder program, which // takes the command line arguments as a prefix addition // expression and computes an integer result. #include "prefix_adder.h" #include // For std output stream and manipulators. #include // For standard C++ strings. #include // For standard string streams. #include // For C-style string functions // Helper function to print out the program's usage message. void usage (char * program_name) { cout [ ]..." << endl << "Purpose: computes program arguments as prefix addition expression" << endl; }

CSE 332: C++ debugging in Eclipse Example Program Main Function int main (int argc, char *argv[]) { // A few useful constants for argument positions const int minimum_arguments = 2; const int starting_index = 1; const int program_name_index = 0; if (argc < minimum_arguments || strcmp (argv[starting_index], "--help") == 0) { usage (argv[program_name_index]); return 1; } try { // Pass the current and last index to use, and the array, to the // expression parsing function, and store the result. int current_position = starting_index; int value = parse_and_compute (current_position, argc - 1, argv); // Print out the result, and return success value. cout << "The value calculated is " << value << endl; return 0; } catch (...) { cout << "caught exception" << endl; return -1; }

CSE 332: C++ debugging in Eclipse Example Program Parsing Function // Helper function to parse the input symbols and compute a value. int parse_and_compute (int & current_index, int last_index, char *argv[]) { // make sure we're still in the argument range if (current_index > last_index) { throw; } // look for a single-symbol addition operator if (strlen (argv[current_index]) == 1 && *(argv[current_index]) == '+') { int first_operand = parse_and_compute (++current_index, last_index, argv); int second_operand = parse_and_compute (current_index, last_index, argv); return first_operand + second_operand; } // treat anything else as an integer else { int result; istringstream i (argv[current_index++]); i >> result; return result; }

CSE 332: C++ debugging in Eclipse Debugging our Example Program in Eclipse When you click Debug (or use toolbar option) –Program loads with command line arguments you gave –Ready to run first statement in function “main”

CSE 332: C++ debugging in Eclipse Debugging Options in Eclipse Click on Run menu to see options for debugging –Can click on menu items –Most have toolbar buttons too –Keyboard shortcuts are useful Ctrl Shift B (toggle breakpoint) F5 (step into a function) F6 (step over a function) F8 (resume exection)

CSE 332: C++ debugging in Eclipse Watching Variables and the Program Call Stack Notice the call stack –Only has main initially –Will grow as other function calls are made Click on Variables tab –Shows what’s in scope –And shows values

CSE 332: C++ debugging in Eclipse Tracing Through the Program Execution Variables change –With scope changes –With assignments Trace through program statements –Use F6 to step over –Use F5 to step into

CSE 332: C++ debugging in Eclipse Function Calls and Reference Variables Reference variables –Show l-value address –But can cast to r-value Now we’ve stepped into a function call –Note code, call stack –Can jump stack frames

CSE 332: C++ debugging in Eclipse Eclipse Can Type Cast Watched Variables Dialog shows default type of variable ( int & )

CSE 332: C++ debugging in Eclipse Example of Casting from int & to int Change type to int to view as an r-value

CSE 332: C++ debugging in Eclipse Values of Reference Variables in Expressions Now you see the value of the aliased variable (instead of its address) Makes it easier to think about what expressions it’s in should mean

CSE 332: C++ debugging in Eclipse Setting Breakpoints in Eclipse Stepping through tells us a lot of information But it soon gets tedious Breakpoints tell program where to stop next Can use menu, toolbar, or Ctrl Shift B shortcut –Toggles them on or off

CSE 332: C++ debugging in Eclipse Resuming Execution in Eclipse Once we’ve set the breakpoint(s) we want We can resume program execution Can use menu, toolbar, or F8 shortcut –Runs to next breakpoint –Or to end of program

CSE 332: C++ debugging in Eclipse Example Program Execution in Eclipse We’re parsing We’ve seen first + –Previous call to parse_and_compute We’re on the second + –Hit F8 to resume –Stops in another nested call to parse_and_compute

CSE 332: C++ debugging in Eclipse Call Stack is Nested 4 Calls Deep Still parsing We’ve seen + and + –Previous calls to parse_and_compute We start at the 8 –Step (F6) through call –Notice result is 8 –Notice current_index now 4

CSE 332: C++ debugging in Eclipse Pop Back to Previous Function on Return Return pops us back Now in previous call to parse_and_compute (for second + ) Notice first_operand is 8 Notice current_index is 4 Hit F8 to resume run

CSE 332: C++ debugging in Eclipse Another Recursive Call is Pushed Breaks in the new call pushed on the stack Step to end again (F6) Notice result is 9 Notice current_index is 5 Hit F6 (twice) to return

CSE 332: C++ debugging in Eclipse Back to Call at Depth 3 Again Pops back again Notice second_operand is 9 Notice current_index is 5 Hit F6 (twice) to return

CSE 332: C++ debugging in Eclipse Back at Original Call Made from Main Pops back farther Notice first_operand is 17 Need to cast int & to int in each stack frame Hit F5 to trace in of F8 to resume execution

CSE 332: C++ debugging in Eclipse One Last Push Call is pushed on stack Debugger stops –at start of call if we did F5 –at breakpoint if we did F8 Trace from here using F6

CSE 332: C++ debugging in Eclipse Tracing Through One Last Time Traced to end of the call Notice result is 10 Notice current_index > last_index Hit F6 (twice) to return

CSE 332: C++ debugging in Eclipse Almost Finished Back in original call made from main Ready to return value of complete expression Hit F6 (twice) to return

CSE 332: C++ debugging in Eclipse Ready to Print the Result Back in main function Ready to print value of complete expression Hit F6 (once) to step over

CSE 332: C++ debugging in Eclipse That’s All, Folks Ready to return 0 to indicate success Notice program output Hit F6 (twice) or F8 to finish

CSE 332: C++ debugging in Eclipse For Next Time Topic: C++ Exceptions Guest Lecturer: Professor Smart Assigned readings: –pages and