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

Similar documents
Problem Set 1: Unix Commands 1

Computer Labs: Debugging

Computer Labs: Debugging

Understanding the Program Run

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

Performance Metrics (I)

CSE 374 Programming Concepts & Tools

Programming Studio #9 ECE 190

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

We first learn one useful option of gcc. Copy the following C source file to your

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

CSE 351. GDB Introduction

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

Intermediate Programming, Spring 2017*

Lecture 07 Debugging Programs with GDB

Using gdb to find the point of failure

Debugging. Erwan Demairy Dream

Programming Tips for CS758/858

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference

Exercise Session 6 Computer Architecture and Systems Programming

Using a debugger. Segmentation fault? GDB to the rescue!

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Data and File Structures Laboratory

NAME gcov coverage testing tool SYNOPSIS

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

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

DEBUGGING: OBSERVING AND TRACKING

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

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

CMPSC 311- Introduction to Systems Programming Module: Debugging

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

Debug for GDB Users. Action Description Debug GDB $debug <program> <args> >create <program> <args>

Using the Command Line

Introduction to Problem Solving and Programming in Python.

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CMPSC 311- Introduction to Systems Programming Module: Debugging

CptS 360 (System Programming) Unit 4: Debugging

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Unit Testing as Hypothesis Testing

Programming in C First meeting

Programming in C week 1 meeting Tiina Niklander

SE420 Software Quality Assurance

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

CS2141 Software Development using C/C++ Debugging

Tips on Using GDB to Track Down and Stamp Out Software Bugs

Reliable programming

Debugging and Debugger. Terminology. GNU gcc and gdb. Debugging C programs in Unix and Windows Environments - Part One

Computer Science II Lab 3 Testing and Debugging

The Linux Programming Environment. Computer Science Department Texas State University

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference for x86-64 Assembly Language

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

Princeton University Computer Science 217: Introduction to Programming Systems. Testing

The Dynamic Debugger gdb

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

CS201 Lecture 2 GDB, The C Library

Reviewing gcc, make, gdb, and Linux Editors 1

CS 314 Principles of Programming Languages. Lecture 9

Princeton University. Testing. Computer Science 217: Introduction to Programming Systems

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Debugging with GDB and DDT

Laboratory 1 Semester 1 11/12

Recitation: Cache Lab & C

SE420 Software Quality Assurance

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Code Review and Debugging (Lab 05)

EXTERNAL TESTING. Princeton University Computer Science 217: Introduction to Programming Systems. Why Test? Testing. Why Test? Why Test?

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

GDB QUICK REFERENCE GDB Version 4

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

CS 11 C track: lecture 6

Program Design: Using the Debugger

CS354 gdb Tutorial Written by Chris Feilbach

Programming in C and C++

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Heaps in C. CHAN Hou Pong, Ken CSCI2100 Data Structures Tutorial 7

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

EE 355 Lab 3 - Algorithms & Control Structures

For Your Amusement. Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth. Program Verification

Testing. ECE/CS 5780/6780: Embedded System Design. Why is testing so hard? Why do testing?

1 A Brief Introduction To GDB

Programming in C First meeting Tiina Niklander

Source level debugging. October 18, 2016

TOS Test Center (TTC)

Program Correctness and Efficiency. Chapter 2

Your code must have been compiled with the -g compiler option. Example:

Main concepts to be covered. Testing and Debugging. Code snippet of the day. Results. Testing Debugging Test automation Writing for maintainability

CMPT 300. Operating Systems. Brief Intro to UNIX and C

MPLAB X Debugging Techniques

18-600: Recitation #3

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

Debugging! 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

CS/COE 0449 term 2174 Lab 5: gdb

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

Advanced practical Programming for Scientists

Jlint status of version 3.0

Debugging with gdb and valgrind

Debugging and Profiling

Transcription:

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

Introduction Majority of software development is testing, debugging, and bug fixing The best software developers are 10X (!) more productive than other developers; why??? CSC230: C and Software Tools NC State Computer Science Faculty 2

Why Do Bugs Happen? OS problem? Compiler? Hardware? not likely Unclear requirements / specifications, constantly changing, unreasonable schedules, Lack of mastery of programming tools / language Inadequate testing procedures Faulty logic Addressed in this course CSC230: C and Software Tools NC State Computer Science Faculty 3

Testing Procedures Types of testing Unit testing test each function Integration testing test interaction between units and components System testing testing complete system Regression testing selective retesting Acceptance Testing testing of acceptance criteria Beta Testing 3 rd party testing Test logging Bug fixing test one change at a time maintain old versions, and log the changes CSC230: C and Software Tools NC State Computer Science Faculty 4

Test Case Information Unique Identifier Black box: name of test input/output files Input into the program or program unit Black box: how the user runs and interacts with the program Could be redirection input and output Expected output from the program or program unit What you expect to get based on input and requirements Stored in a file that can be compared with actual output Actual results of running the test case Black box: what the user gets from the program Could be redirection of std out CSC216: Programming Concepts Sarah Heckman 5

Test Creation Some test-generating strategies typical, common cases Equivalence Classes corner or extreme cases Boundary Value Tests random cases & deliberate errors Diabolic tests What are some tests for the program description? CSC230: C and Software Tools NC State Computer Science Faculty 6

Sample Tests Test ID Description Expected Result Actual Result String? Lower Bound (0-9): Upper Bound (0-9): String? Lower Bound (0-9): Upper Bound (0-9): String? Lower Bound (0-9): Upper Bound (0-9): String? Lower Bound (0-9): Upper Bound (0-9): CSC230: C and Software Tools NC State Computer Science Faculty 7

Testing Strategies Encode our tests into file to facilitate automation (scripts or programs) Using redirection of program input and output for system and acceptance testing Use diff to compare expected and actual output %./string_analyzer < in1 > aout1 % diff aout1 eout1 CSC230: C and Software Tools NC State Computer Science Faculty 8

Test Quality How measure test coverage? Functions executed Statements executed Branches executed Conditionals executed Use gcov Compile using the fprofile-arcs and ftestcoverage flags Execute your program with redirected input and output Observe coverage CSC230: C and Software Tools NC State Computer Science Faculty 9

Example gcov Execution % gcc Wall std=c99 fprofile-arcs ftestcoverage string_analyzer.c o string_analyzer %./string_analyzer < in1 > aout1 % gcov string_analyzer.c File string_analyzer.c Lines executed:87.88% of 33 string_analyzer.c:creating string_analyzer.c.gcov CSC230: C and Software Tools NC State Computer Science Faculty 10

Example gcov Output execution_count: line_num: source_text If no code, execution_count is - If you want branch information (for branch coverage), add b option CSC230: C and Software Tools NC State Computer Science Faculty 11

Handling Errors in Production? Recover or abort? Audit logs and meaningful error messages Assertions in C #include <assert.h> int f ( int a, int b) { assert ((a > b) && (b!= 0)); } If condition is FALSE at run time, automatically prints the following, and aborts execution: CSC230: C and Software Tools NC State Computer Science Faculty 12

assert() Example c = getc(stdin); assert(c == A ); Output > a.out x test.c:15: failed assertion c == A > CSC230: C and Software Tools NC State Computer Science Faculty 13

assert() (cont d) If NDEBUG defined (using #define) when assert.h #include'd, assert() is ignored You can also define NDEBUG on compiler s command line - no change to program #define NDEBUG /* turns off assertions */ #include <assert.h> CSC230: C and Software Tools NC State Computer Science Faculty 14

Source Level Debugging Symbolic debugging lets you single step through program, and modify/examine variables while program executes Drawbacks / limitations?? On the Linux platform: gdb Source-level debuggers built into most IDEs CSC230: C and Software Tools NC State Computer Science Faculty 15

Debugging approaches Just change stuff until it works Exception: Add printfs and test theories or Use a debugger CSC230: C and Software Tools NC State Computer Science Faculty 16

gdb commands list <line> list <function> list <line>,<line> run continue step next print <var> printf fmt, <var> list (show) 10 lines of code at specified location in program List from first line to last line start running the program continue execution single step execution, including into functions that are called single step over function calls show variable value display <var> undisplay <var> show variable each time execution stops CSC230: C and Software Tools NC State Computer Science Faculty 17

gdb commands break <line> break <function> break <line> if <cond> set breakpoints (including conditional breakpoints) info breakpoints delete breakpoint <n> list, and delete, breakpoints set <var> <expr> backtrace full bt set variable to a value show the call stack & args arguments and local variables CSC230: C and Software Tools NC State Computer Science Faculty 18

gdb quick reference card GDB Quick Reference.pdf print it! Also available annotated by me with most important commands for a beginner: GDB Quick Reference - annotated.pdf CSC230: C and Software Tools NC State Computer Science Faculty 19

GDB exercise: underscorify (1) void underscorify_bad(char* s) { char* p = s; while (*p!= '0') { if (*p == 0) { *p = '_'; } p++; } } int main() { char msg[] = "Here are words"; puts(msg); underscorify_bad(msg); puts(msg); }

GDB exercise: underscorify (2) void underscorify_bad2(char* s) { char* p = s; while (*p!= '0') { if (*p == ' ') { *p = '_'; } p++; } } int main() { char msg[] = "Here are words"; puts(msg); underscorify_bad2(msg); puts(msg); } Worked but crashed on exit Worked totally!! Worked totally!! Worked totally!! Worked totally!! Worked totally!! Worked totally!! Worked totally!!

Finding Bugs 1. Test as you write the code (write test harness) Make sure you remove before delivery 2. Write trivial programs to test your mastery of the programming language, library functions, etc. 3. Working backwards from an error: divide and conquer you can t do better than binary search to isolate the problem CSC230: C and Software Tools NC State Computer Science Faculty 22

Finding (cont d) 4. Make the bug reproducible (eliminate all variations in execution conditions) 5. Try simple things first (sanity checking) including, check the inputs 6. Inspect your code and think about it! 7. Ask for help, explain code / bug to TA or instructor 8. Write an automated test program or script CSC230: C and Software Tools NC State Computer Science Faculty 23

Bug Reports Technical Document Failure of system under test (SUT) Product of testing Used to communicate failures to developers Shows specific quality problems Text based on Rex Black CSC326: Software Engineering NC State Software Engineering Faculty L01-24

Key Elements in Bug Reporting Reproduce: test it again Isolate: test it differently Generalize: test it elsewhere CSC326: Software Engineering NC State Software Engineering Faculty Text Rex Black L01-25

Example Bug Report Steps to Reproduce Test input file: in1 Expected output: eout1 %./pgm < in1 >! aout1 The actual results print 3, when we expect 2 Isolation & Generalization The test focuses on the bounds of the input The program may make an incorrect check on input Also happens with new input file, in7, where the input value considers another boundary value CSC230: C and Software Tools NC State Computer Science Faculty 26

Comments from the Gnome Project It is extremely important that code be correct and robust. This means that the code should do what is expected of it, and it should handle exceptional conditions gracefully. Use assertion macros to ensure that your program's state is consistent. These macros help locate bugs very quickly, and you'll spend much less time in the debugger if you use them liberally and consistently. Insert sanity checks in your code at important spots like the beginning of public functions, at the end of code that does a search that must always succeed, and any place where the range of computed values is important. CSC230: C and Software Tools NC State Computer Science Faculty 27