Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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

3 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

4 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

5 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

6 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

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

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

9 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

10 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

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

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

13 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

14 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

15 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

16 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

17 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

18 CSE 332: C++ debugging in Eclipse 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

19 CSE 332: C++ debugging in Eclipse How the Example Program Can 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???

20 CSE 332: C++ debugging in Eclipse 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 */

21 CSE 332: C++ debugging in Eclipse 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; }

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

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

24 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”

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

26 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

27 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

28 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

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

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

31 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

32 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

33 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

34 CSE 332: C++ debugging in Eclipse Example Program Execution in Eclipse We’re parsing + + 8 9 10 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

35 CSE 332: C++ debugging in Eclipse Call Stack is Nested 4 Calls Deep Still parsing + + 8 9 10 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 CSE 332: C++ debugging in Eclipse For Next Time Topic: C++ Exceptions Guest Lecturer: Professor Smart Assigned readings: –pages 805-827 and 834-837


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

Similar presentations


Ads by Google