Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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?

3 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

4 CSE 332: Scientific debugging in C++ Setting Up Workspace Directory

5 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

6 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…

7 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

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

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

10 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)

11 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

12 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

13 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

14 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

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

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

17 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

18 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

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

20 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)

21 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

22 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

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

24 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

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

26 CSE 332: Scientific debugging in C++ Mental Model: How can the Program Fail? Too few arguments in expression./prefix_adder + 8 + 9 Cannot calculate result + 8 + 9 (needs another value to finish 2 nd + operation) Try this on your own in the lab, for practice + +8 9 1 23 4???

27 CSE 332: Scientific debugging in C++ Example Program Header File // prefix_adder.h // // author: Chris Gill cdgill@cse.wustl.edu // // 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 */

28 CSE 332: Scientific debugging in C++ Example Program Source File // prefix_adder.cc // // author: Chris Gill cdgill@cse.wustl.edu // // 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; }

29 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; }

30 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; }

31 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)

32 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

33 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

34 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

35 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

36 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

37 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

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

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

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

41 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

42 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

43 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

44 CSE 332: Scientific debugging in C++ Example Program Execution in Eclipse We’re parsing + 20 30 We’ve seen the + –Previous call to parse_and_compute We’re on the first operand, 20 –Hit F6 to step over lines

45 CSE 332: Scientific debugging in C++ Result of the First Operand Still parsing + 20 30 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

46 CSE 332: Scientific debugging in C++ Result of the Second Operand Still parsing + 20 30 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

47 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

48 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

49 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

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

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

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

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

54 CSE 332: Scientific debugging in C++ Compilation Succeeded

55 CSE 332: Scientific debugging in C++ Launch Debugger

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

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

58 CSE 332: Scientific debugging in C++ Setting Breakpoints

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

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

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

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

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

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

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

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

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

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

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

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

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

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

73 CSE 332: Scientific debugging in C++ For Next Time Topic: –C++ Exceptions Suggested readings: –Prata pp. 805-827 and 834-837 –Deitel pp. 817-828 and 829-831


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

Similar presentations


Ads by Google