Computer Labs: Debugging

Similar documents
Computer Labs: Debugging

Intermediate Programming, Spring 2017*

Testing and Debugging C Programming and Software Tools. N.C. State Department of Computer Science

CptS 360 (System Programming) Unit 4: Debugging

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

CS354 gdb Tutorial Written by Chris Feilbach

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Reliable programming

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS2141 Software Development using C/C++ Debugging

Jlint status of version 3.0

Software Engineering /48

Debugging. Erwan Demairy Dream

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CSE 374 Programming Concepts & Tools

Chapter 5. Debugging. 5.1 A Debugging Mindset

Lecture 19 Notes Data Structures in C

Computer Labs: Version Control with Subversion

Computer Labs: Version Control with Subversion

QUIZ. What are 3 differences between C and C++ const variables?

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

QUIZ. Source:

COSC 6374 Parallel Computation. Analytical Modeling of Parallel Programs (I) Edgar Gabriel Fall Execution Time

COSC 6374 Parallel Computation. Debugging MPI applications. Edgar Gabriel. Spring 2008

Deep C (and C++) by Olve Maudal

Preventing and Finding Bugs in Parallel Programs

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

CS61C : Machine Structures

The C standard library

ANITA S SUPER AWESOME RECITATION SLIDES

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Reviewing gcc, make, gdb, and Linux Editors 1

Project #1: Tracing, System Calls, and Processes

Debugging! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 5!

Performance Metrics (I)

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

ECE/ME/EMA/CS 759 High Performance Computing for Engineering Applications

CSE2301. Introduction

Better variadic functions in C

Design and Implementation

Recitation: Cache Lab & C

DEBUGGING: OBSERVING AND TRACKING

Debugging with gdb and valgrind

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

1.7 Limit of a Function

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

Unit Testing as Hypothesis Testing

Figure 1 Common Sub Expression Optimization Example

Programming in C First meeting

Lecture 4 CSE July 1992

Scientific Programming in C IX. Debugging

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Understanding the Program Run

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

CMPSC 311- Introduction to Systems Programming Module: Debugging

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Programming Studio #9 ECE 190

Conditional Compilation

CSE 374 Programming Concepts & Tools

Important From Last Time

CMPSC 311- Introduction to Systems Programming Module: Debugging

Computer Labs: Lab 5 & Sprites

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

CSE 303 Final Exam. March 16, 2009

Project 1 Balanced binary

Testing and Debugging

CS 11 C track: lecture 6

CS 105, Spring 2015 Ring Buffer

COSC Software Engineering. Lecture 16: Managing Memory Managers

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

HOT-Compilation: Garbage Collection

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Blocky: A Game of Falling Blocks

CS 103 Lab - Party Like A Char Star

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Topic 6: A Quick Intro To C

Programming in C and C++

Programming in C week 1 meeting Tiina Niklander

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

2/28/2018. Let s Talk About Testing the Nonogram Code. ECE 220: Computer Systems & Programming. Example Nonogram Code Solution

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Text Input and Conditionals

Unit Testing as Hypothesis Testing

Advanced Pointer & Data Storage

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

C Bounds Non-Checking. Code Red. Reasons Not to Use C. Good Reasons to Use C. So, why would anyone use C today? Lecture 10: *&!

Finding and Fixing Bugs

COSC 4397 Parallel Computation. Debugging and Performance Analysis of Parallel MPI Applications

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

Transcription:

Computer Labs: Debugging 2 o MIEIC Pedro F. Souto (pfs@fe.up.pt) October 29, 2012

Bugs and Debugging Problem To err is human This is specially true when the human is a programmer :( Solution There is none. But we can make it less likely By programming carefully By heeding the compiler warnings By using, if possible, a language different from C/C++ Otherwise, use assert() generously By designing good test programs: If a test program does not detect bugs, most likely it was poorly designed

Using assert() //#define NDEBUG // uncomment for public release #include <assert.h> void bounds(int i) { static int t[100]; assert( i>=0 && i>100 ); // abort program if false... } assert() aborts the program and prints information showing where, when the condition specified as its argument is false po1: po1.c:50: bounds: Assertion i>=0 && i>100 failed Aborted Should not be used in production Define the NDEBUG constant A program should rarely abort in normal usage Even if there is nothing else to do, the information provided by assert() is not useful to a user Be careful when writing the condition for assert() A bug in the condition may mislead you in a wasted search for a non-existing bug

Debugging and the Scientific Method Degugging is a ludic activity, based on logic 1. Locate/identify the bug (the fun part, sometimes) 2. Fix the bug Algorithm for identifying a bug: While the bug has not been found: 1. put forward a hypothesis about where the bug is 2. design a test to prove/reject the hypothesis 3. carry out the test (possibly, changing the code) 4. interpret the test result

Debugging Rule #1: Debug with Purpose Don t just change code and hope you ll fix the problem! I ve seen many of you doing it out of despair! (I confess, that I ve done it, but... it does not help) Use the scientific method What is the simplest input that produces the bug? What assumptions have I made about the program operation? How does the outcome of this test guide me towards finding the problem? Use pen and paper to keep track of what you ve done

Debugging Rule #2: Explain it to Someone Else Often explaining the bug to someone makes your neurons click. The someone may be: Another group member or colleague Even someone that is not familiar with the subject If there is nobody available, you can try explain it to yourself

Debugging Rule #3: Focus on Recent Changes Ask your self: What code did I change recently? It helps if you: write and test the code incrementally use SVN do regression testing, to make sure that new changes don t break old code However, remember that: new code may expose bugs in old code

Debugging Rule #4: Get Some Distance... Sometimes, you can be TOO CLOSE to the code to see the problem Go for a walk, or do something else Sleep on the problem May not be an alternative if your deadline is the following day

Debugging Rule #5: Use Tools Sometimes, bug finding can be very easy by using error detection tools You just have to use them properly Use gcc flags to catch errors at compile time: -Wall, -Wextra, -Wshadow, -Wunreachable-code Use a debugger such as gdb This is not an option in LCOM Use runtime memory debugging tools E.g. Electric Fence, Valgrind (not really an option in LCOM)

Debugging Rule #6: Dump State... For complex programs, reasoning about where the bug is can be hard, and stepping through in a debugger time-consuming Sometimes, it is easier to just dump state, i.e. use printf(), and scan it for what seems odd This may help you zero in on the problem

Debugging Rule #7: Think Ahead Once you ve fixed such a bug, ask yourself: Can a similar bug exist elsewhere in my code? Bugs are often a consequence of a misunderstanding of an API How can I avoid a similar bug in the future? Maybe coding 36 straight hours before the deadline won t help...

Tools of the Trade Different bugs require different tools: printf() Can be used to: gdb Check simple hypothesis Zero in on hard to reproduce or highly complex bugs Very useful when the program crashes with segfault

Debugging with printf(): debug.h #include <stdio.h> #define DEBUG // comment/uncomment as needed #ifdef DEBUG #define print_ident() fprintf(stderr, \ "At file %d, function %s, line %d\n", \ FILE, FUNCTION, LINE ); #define pring_dbg(...) fprintf(stderr, VA_ARGS ) #else // does nothing, not even generates code! #define print_ident() #define print_dbg(...) #endif // DEBUG

Debugging with printf(): Usage #include "debug.h" int main() { print_ident(); print_dbg("dir=%s, count=%d\n", "popo", 5); print_dbg("bye\n"); print_ident(); return 0; } At file po.c, function main, line 18 dir=popo, count=5 bye At file po.c, function main, line 21 Macros do not genarate code, if DEBUG is not defined It is not necessary to comment/uncomment printf() in code It may be conveninent to define different DBG_XXX constants, by using bit-masks you can print debugging messages related to different aspects

Thanks to: I.e. shamelessly copied material by: Dave Andersen (dga@cs.cmu.edu) Debugging rules João Cardoso I (jcard@fe.up.pt) assert(), printf()