cse 232: c++ debugging in visual studio and emacs c++ debugging (in visual studio and emacs) we’ve...

26
SE 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 and command lines Text editors, compiler, linker How the program receives input and generates output General program structure and logic We’ve also looked at Visual Studio In which all of these functions are integrated… …within a nice graphical environment Today we’ll bring those perspectives together Debug a simple example program in Visual Studio Look at how do the same thing within emacs

Upload: brent-armstrong

Post on 04-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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 and command lines– Text editors, compiler, linker– How the program receives input and generates output– General program structure and logic

• We’ve also looked at Visual Studio– In which all of these functions are integrated…– …within a nice graphical environment

• Today we’ll bring those perspectives together– Debug a simple example program in Visual Studio– Look at how do the same thing within emacs

Page 2: 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

CSE 232: C++ debugging in Visual Studio and emacs

Getting Started

• To set up your own environment for what we’ll cover– Log on and then ssh into grid.cec.wustl.edu– Create a new directory (e.g., mkdir prefix_adder)– Save files from course web page (or copy them from the

~cse232/.www-docs/ directory) into that directory:

Makefile prefix_adder.h prefix_adder.cc

• Also download those files to your Windows desktop – Create a new project in Visual Studio– Copy those files into the new project in Visual Studio– Build the project

Page 3: 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

CSE 232: C++ debugging in Visual Studio and emacs

Debugging an Example Program

• Now we’ll use Visual Studio to debug a program– Step through (and into) functions– Watching the call stack and variable values

• But, before we start using Visual Studio…– 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

– Tools can help you follow this disciplined approach faster

Page 4: 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

CSE 232: C++ debugging in Visual Studio and emacs

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

2 3

4 5

+

+

8 9

10

1

2

3 4

5

same result

different order

Page 5: 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

CSE 232: C++ debugging in Visual Studio and emacs

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 2nd + operation)

• Exercise: try this on your own, for practice

+

+8

9

1

2 3

4 ???

Page 6: 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

CSE 232: C++ debugging in Visual Studio and emacs

Example Program: Header File // prefix_adder.h//// author: Chris Gill [email protected]//// 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 */

Page 7: 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

CSE 232: C++ debugging in Visual Studio and emacs

Example Program: Start of the Source File// prefix_adder.cc//// author: Chris Gill [email protected]//// 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 <iostream> // For std output stream and manipulators.#include <string> // For standard C++ strings.#include <sstream> // For standard string streams.#include <cstring> // For C-style string functions

// Helper function to print out the program's usage message.void usage (char * program_name) {

cout << "Usage: " << program_name << " <argument> [<argument>]..." << endl << "Purpose: computes program arguments as prefix addition expression" << endl;}

Page 8: 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

CSE 232: C++ debugging in Visual Studio and emacs

Example Program: Main Functionint 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; }}

Page 9: 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

CSE 232: C++ debugging in Visual Studio and emacs

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;

}}

• Exercise:– Set a break point at the

first if statement in this function

– Watch how stack grows/shrinks if you have debug continue

Page 10: 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

CSE 232: C++ debugging in Visual Studio and emacs

Debugging in emacs

Page 11: 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

CSE 232: C++ debugging in Visual Studio and emacs

Open Makefile, Source, Header Files in emacs

Page 12: 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

CSE 232: C++ debugging in Visual Studio and emacs

Compile the Program: Esc x compile

Page 13: 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

CSE 232: C++ debugging in Visual Studio and emacs

Hit Enter to Run “make -k”

Page 14: 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

CSE 232: C++ debugging in Visual Studio and emacs

Compilation Succeeded

Page 15: 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

CSE 232: C++ debugging in Visual Studio and emacs

Launch Debugger: Esc x gdb

Page 16: 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

CSE 232: C++ debugging in Visual Studio and emacs

Run “gdb prefix_adder”

Page 17: 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

CSE 232: C++ debugging in Visual Studio and emacs

GDB Debugging Prompt

Page 18: 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

CSE 232: C++ debugging in Visual Studio and emacs

Setting Breakpoints

Page 19: 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

CSE 232: C++ debugging in Visual Studio and emacs

Running With Command Line Arguments

Page 20: 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

CSE 232: C++ debugging in Visual Studio and emacs

At Breakpoint in Main

Page 21: 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

CSE 232: C++ debugging in Visual Studio and emacs

Stepping Through the Program

Page 22: 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

CSE 232: C++ debugging in Visual Studio and emacs

What Options Does Emacs/GUD Provide?

Page 23: 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

CSE 232: C++ debugging in Visual Studio and emacs

Stepped into Function Call, now at a Breakpoint

Page 24: 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

CSE 232: C++ debugging in Visual Studio and emacs

Printing out Values of Variables

Page 25: 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

CSE 232: C++ debugging in Visual Studio and emacs

Continue to next Breakpoint, Look at Stack

Page 26: 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

CSE 232: C++ debugging in Visual Studio and emacs

Exercise

• Build and debug a damaged version of the program– Save another file from course web page into the project (or

create a new project with same Makefile and header file)bad_prefix_adder.cc

– Build the new version of the program

• Debug what’s wrong with the program(no fair using diff to detect code differences ;-)– Run the program with different inputs– Observe what happens (how does it go wrong?)– Trace through the program in Visual Studio to narrow down

the possible cause(s) of the problem(s)• If you’d like to, also try the same thing in emacs

– Fix the error(s), rebuild, and re-run to observe its behavior