When your program crashes

Slides:



Advertisements
Similar presentations
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.
Advertisements

Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
CS 202 Computer Science II Lab Fall 2009 September 24.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
CSE 303 Lecture 13a Debugging C programs
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
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.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CSE 332: C++ debugging in Eclipse C++ Debugging in Eclipse We’ve now covered several key program features –Variable declarations, expressions and statements.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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 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.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
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”
Navigating the C++ Development Environment
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
17/02/2016S. Ponce / EP-LBC1 Debugging Under Linux Sebastien Ponce Friday, 8 March 2002.
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,
Exception Handling How to handle the runtime errors.
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)
1 ENERGY 211 / CME 211 Lecture 14 October 22, 2008.
CSE 332: Scientific debugging in C++ Scientific Debugging in C++ (with Eclipse & gdb) By now we’ve covered several key C++ features –Variable declarations.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
DEBUG.
Pointers What is the data type of pointer variables?
Chapter 1.2 Introduction to C++ Programming
Procedural and Object-Oriented Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 CS 3370 – C++ Functions.
Chapter 1.2 Introduction to C++ Programming
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
CSE 374 Programming Concepts & Tools
Basic Elements of C++.
Command Line Arguments
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Andy Wang Object Oriented Programming in C++ COP 3330
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Overview 4 major memory segments Key differences from Java stack
C Stuff CS 2308.
CS 2308 Exam I Review.
C Preprocessor(CPP).
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
CSC 270 – Survey of Programming Languages
C++ Tutorial Rob Jagnow
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
ASEE / GradSWE C++ Workshop
Presentation transcript:

When your program crashes Why Debug a Program? When your program crashes Finding out where it crashed Examining program memory at that point When a bug occurs in your program Watching program variables Identifying where a problem occurs (and why) Tracing through a program’s behaviors Learning what a program does Good basic testing approach

Debugging on Various Platforms We’ll use Visual Studio’s graphical debugger Much more on this as we go through the semester Set command line arguments in the project properties It’s also good to know about common text-based tools gdb Debugger provided with the g++ compiler (e.g., on Linux) Using –g switch embeds debug info during compilation dbx Debugger provided with Sun CC Also requires –g switch to embed debug symbols

Some Essential Debugging Commands start (or restart) execution in the debugger Visual Studio: F5 (Start Debugging) gdb: run (also give its command line arguments) execute one line, stepping into any functions Visual Studio: F11 (Step Into) gdb: step execute one line, stepping over any functions VS: F10 (Step Over) gdb: next set breakpoint at a line or function entry point VS: F9 (Toggle Breakpoint) gdb: break (can give file and line, function, etc.)

“Core” Dumps A program dumps a memory image when it crashes creates a file (called “core” on Linux) and then exits this happens when a program receives certain signals: Segmentation fault accessed memory it does not own or dereferenced a 0 pointer Bus error divided by zero corrupted stack Debuggers are very useful to examine “core” files When your program crashes within Visual Studio On Linux: gdb myprogram core

Debugging an Example Program A debugger helps us observe a program’s behavior Step through (and into) functions Watching the call stack and variable values But, before we start using the fancy tools… What do we expect the program to do? How might the 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

What We Expect the Example Program To Do 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 same result different order 1 1 + + 2 3 2 5 8 + + 10 4 5 3 4 9 8 10 9

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) Try this on your own in the studio, for practice 1 + 2 3 8 + 4 ??? 9

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 */

Example Program Source File / Usage Function // 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 <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; }

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;

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 1; } // 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;