CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations.

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’
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
The Internet. Telnet Telnet means using your computer as a terminal. All commands you type are sent to the host computer you are connected to and executed.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
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
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.
| | Tel: | | Computer Training & Personal Development Outlook Express Complete.
MICROSOFT WORD GETTING STARTED WITH WORD. CONTENTS 1.STARTING THE PROGRAMSTARTING THE PROGRAM 2.BASIC TEXT EDITINGBASIC TEXT EDITING 3.SAVING A DOCUMENTSAVING.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
A1 Visual C++.NET Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Quick Introduction The following.
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
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.
Active-HDL Interfaces Debugging C Code Course 10.
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.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
1 CS161 Introduction to Computer Science Topic #9.
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.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Navigating the C++ Development Environment
©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.
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.
Lecture 7 Conditional Scripting and Importing/Exporting.
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
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,
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Using the Eclipse Debugger Extra Help Session (Christina Aiello)
Fundamental Programming Fundamental Programming Introduction to Functions.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
C++ Functions A bit of review (things we’ve covered so far)
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Development Environment
User-Written Functions
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Eclipse Navigation & Usage.
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Debugging with Eclipse
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
When your program crashes
Our Environment We will exercise on Microsoft Visual C++ v.6
Debugging Taken from notes by Dr. Neil Moore
Software Setup & Validation
Debugging.
Debugging with Eclipse
Workshop for Programming And Systems Management Teachers
Presentation transcript:

CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations and program control statements –Functions and the program call stack –Passing arguments to functions by reference vs. by value Combined, they produce non-trivial program behavior –From lab 2 forward, we’ll need more powerful debugging Today we’ll cover –What to think about and look for when debugging –How to set up Eclipse to debug a simple example program –How to step through a program in Eclipse –How the call stack and variable values change as we run –How to do much the same thing without Eclipse E.g., in an environment where you only have say gdb and emacs

CSE 332: Scientific debugging in C++ Scientific Debugging Eclipse and gdb are (merely) helpful tools to use –Help you conduct guided investigations more quickly –They can also take you on unproductive tangents otherwise –The difference is in whether you follow a good method “Growing” a mental model of the program is crucial –What do we expect our program to do? –How might our program fail? –Can we make predictions about its behavior, and test them? Scientific method is a good one for guided debugging –Hypothesis: a guess/theory why program behaves as it does –Prediction: How program will behave if hypothesis is correct –Experiment: Try out your prediction under given condition(s) –Analysis: Do the results confirm/refute/refine hypothesis? What do they say about your ideal or actual models of the program?

CSE 332: Scientific debugging in C++ Now Let’s Get Started with Eclipse To set up your own environment for what we’ll cover –Log onto one of the CEC machines –Change into your workspace directory –Create a new directory (for example mkdir demo ) –Save these files (from course syllabus) into that directory Makefile prefix_adder.h prefix_adder.cc Then 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: Scientific debugging in C++ Setting Up Workspace Directory

CSE 332: Scientific debugging in C++ Launching Eclipse On the Desktop –Click on the Red Hat –Select CEC Applications –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: Scientific debugging in C++ Open a Perspective When Eclipse starts, first open up the Resource perspective –Click on button to right for a list of perspectives that have been used –If Resource doesn’t appear in the list, select Other…

CSE 332: Scientific debugging in C++ Select the Resource Perspective Select Resource in the list brought up by Other… –We’ll associate our project’s directory in this perspective –This will let us see the Makefile and other files there

CSE 332: Scientific debugging in C++ Create a New Project in Resource Perspective

CSE 332: Scientific debugging in C++ Choose a C++ Project

CSE 332: Scientific debugging in C++ Fill In C++ Project Details Select Makefile project and Linux GCC options … then fill in the name for the project (same as directory we created – should get warning it already exists)

CSE 332: Scientific debugging in C++ C/C++ Perspective in Eclipse C/C++ perspective should appear now If not, we’ll need to select it later Just as we did for the Resource perspective

CSE 332: Scientific debugging in C++ Switch Back to Resource Perspective We’ll use Resource perspective to add/edit files We’ve already set up your Makefiles to do this, but… –Eclipse expects a make target called “all” –You can just add a dependency on the executable for this

CSE 332: Scientific debugging in C++ Looking at Files in Resource Perspective Click on the folder name (to the left) List of files should appear Some were added by Eclipse Should see files you copied too

CSE 332: Scientific debugging in C++ Open and Edit Makefile Open up the Makefile –Edit it as you would have done for labs 0 and 1 Fill in name of executable program Fill in source and header files in the appropriate spots

CSE 332: Scientific debugging in C++ Open the Source File

CSE 332: Scientific debugging in C++ Open the Header File

CSE 332: Scientific debugging in C++ Switch Back to the C/C++ Perspective Click on >> symbol to right of Resource Arguments C/C++ should be in the list (if so select it) –If not, click on button and select in the list there as before

CSE 332: Scientific debugging in C++ Build the Project Within the C/C++ Perspective, select build project –Calls “make all” which compiles as your Makefile says

CSE 332: Scientific debugging in C++ Successful Compilation Compilation messages appear in Console window Now set up the Run environment: click Open Run Dialog

CSE 332: Scientific debugging in C++ C/C++ Local Application Configuration Choose C/C++ Local Application in the list on the left Click on the New Launch Configuration button (top left)

CSE 332: Scientific debugging in C++ Check Settings on Main Tab Should default to correct values If not, fill in project directory name and name of executable program

CSE 332: Scientific debugging in C++ Fill in Command Line Arguments Click on the Arguments tab Fill in the arguments with which you want to run the program Then click the Run button

CSE 332: Scientific debugging in C++ Run Output Appears in the Console Window

CSE 332: Scientific debugging in C++ Debugging an Example C++ Program Now we’ll use Eclipse to debug a C++ 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: Scientific debugging in C++ Mental Model: 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: Scientific debugging in C++ Mental Model: How can the Program 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: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ One Minor Detail Before We Start Debugging Eclipse may use C/C++ perspective for debugging –What we really want is to use the Debug perspective –Need to check/fix settings under Window Preferences –Avoids having Eclipse switch back to C/C++ (annoyingly)

CSE 332: Scientific debugging in C++ Perspectives for C/C++ Local Applications Choose Perspectives in the leftmost list Choose C/C++ Local Application in the next list Make sure perspective for Debug mode says Debug (and if not change it so it does, as above) then Apply

CSE 332: Scientific debugging in C++ Debugging Options in Eclipse Click on Run menu and select Open Debug Dialog –Similar to how we accessed settings for Run mode

CSE 332: Scientific debugging in C++ Check Debugging Options in Eclipse Click on Debugger tab –Should default correctly and if not set up as shown here –Command line arguments should still be on Arguments tab if you’d like to check –Click Debug to start debugging

CSE 332: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ Function Calls and Reference Variables Reference variables –Show l-value (address) only (now shows both) –Can cast to r-value (next) Now we’ve stepped into a function call –Note code, call stack –Can jump stack frames

CSE 332: Scientific debugging in C++ Eclipse Can Type Cast Watched Variables Right click on variable Choose Cast To Type

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

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

CSE 332: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ 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: Scientific debugging in C++ Example Program Execution in Eclipse We’re parsing We’ve seen the + –Previous call to parse_and_compute We’re on the first operand, 20 –Hit F6 to step over lines

CSE 332: Scientific debugging in C++ Result of the First Operand Still parsing Stepped to point where result of first operand has been calculated Notice result is 20 –Hit F8 to continue –Step (F6) to same point for the second operand

CSE 332: Scientific debugging in C++ Result of the Second Operand Still parsing At the point where the second operand result has been calculated Notice result is 30 –Step (F6) until we leave this function and return to the calling function

CSE 332: Scientific debugging in C++ Pop Back to Previous Function on Return Return pops us back Now in previous call to parse_and_compute Use F6 to step until we return from this function

CSE 332: Scientific debugging in C++ Finishing Up in Main Now we’re back in Main Step to end again (F6) Look at the output as it’s produced in the Console

CSE 332: Scientific debugging in C++ All Done Output appears in the Console window, ready to return from main Hit F6 to return and then again to see program exit message

CSE 332: Scientific debugging in C++ What if We Don’t Have Eclipse?

CSE 332: Scientific debugging in C++ Files Open in Emacs

CSE 332: Scientific debugging in C++ Compile the Program

CSE 332: Scientific debugging in C++ Hit Enter to Run “make -k”

CSE 332: Scientific debugging in C++ Compilation Succeeded

CSE 332: Scientific debugging in C++ Launch Debugger

CSE 332: Scientific debugging in C++ Run “gdb prefix_adder”

CSE 332: Scientific debugging in C++ GDB Debugging Prompt

CSE 332: Scientific debugging in C++ Setting Breakpoints

CSE 332: Scientific debugging in C++ Running With Command Line Arguments

CSE 332: Scientific debugging in C++ At Breakpoint in Main

CSE 332: Scientific debugging in C++ Stepping Through the Program

CSE 332: Scientific debugging in C++ What Options Does Emacs/GUD Provide?

CSE 332: Scientific debugging in C++ After a few More Steps

CSE 332: Scientific debugging in C++ Stepped into Function Call, at Breakpoint

CSE 332: Scientific debugging in C++ Printing out Values of Variables

CSE 332: Scientific debugging in C++ Continue to next Breakpoint, Look at Stack

CSE 332: Scientific debugging in C++ First Operand Result

CSE 332: Scientific debugging in C++ Second Operand Result

CSE 332: Scientific debugging in C++ Adding Operand Results

CSE 332: Scientific debugging in C++ Finishing Up in Main

CSE 332: Scientific debugging in C++ Output Produced, Ready to Return 0

CSE 332: Scientific debugging in C++ Normal Program Exit

CSE 332: Scientific debugging in C++ For Next Time Topic: –C++ Exceptions Suggested readings: –Prata pp and –Deitel pp and