CS2141 Software Development using C/C++ Debugging

Similar documents
Use Dynamic Analysis Tools on Linux

Scientific Programming in C IX. Debugging

CptS 360 (System Programming) Unit 4: Debugging

Debugging with gdb and valgrind

Debugging. Erwan Demairy Dream

CSE 160 Discussion Section. Winter 2017 Week 3

Data and File Structures Laboratory

Class Information ANNOUCEMENTS

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

Debugging and Profiling

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

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

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

Valgrind. Philip Blakely. Laboratory for Scientific Computing, University of Cambridge. Philip Blakely (LSC) Valgrind 1 / 21

Undefined Behaviour in C

6.S096: Introduction to C/C++

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

Debugging with GDB and DDT

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

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

Massif-Visualizer. Memory Profiling UI. Milian Wolff Desktop Summit

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

CS201- Introduction to Programming Current Quizzes

18-642: Code Style for Compilers

Pointers. Héctor Menéndez 1. November 28, AIDA Research Group Computer Science Department Universidad Autónoma de Madrid.

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

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

A program execution is memory safe so long as memory access errors never occur:

CSE 374 Programming Concepts & Tools

CS 314 Principles of Programming Languages. Lecture 9

Lecture 14 Notes. Brent Edmunds

Memory Corruption 101 From Primitives to Exploit

Using Static Code Analysis to Find Bugs Before They Become Failures

Debugging with GDB and DDT

Using gdb to find the point of failure

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Programming in C First meeting

Verification & Validation of Open Source

Technical Questions. Q 1) What are the key features in C programming language?

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

CSE 124 Discussion (10/3) C/C++ Basics

1 A Brief Introduction To GDB

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

C++ Undefined Behavior What is it, and why should I care?

Identifying Memory Corruption Bugs with Compiler Instrumentations. 이병영 ( 조지아공과대학교

Run-time Environments - 3

Programming Tips for CS758/858

EL2310 Scientific Programming

Lecture 07 Debugging Programs with GDB

The Valgrind Memory Checker. (i.e., Your best friend.)

TI2725-C, C programming lab, course

Bug Hunting and Static Analysis

CS Programming In C

CSE 351. GDB Introduction

Lecture 9 Assertions and Error Handling CS240

18-600: Recitation #3

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

The Valgrind Memory Checker. (i.e., Your best friend.)

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

18-642: Code Style for Compilers

Homework #3 CS2255 Fall 2012

Computer Labs: Debugging

Memory Analysis tools

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

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

Simple Overflow. #include <stdio.h> int main(void){ unsigned int num = 0xffffffff;

Profilers and Debuggers. Introductory Material. One-Slide Summary

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Static Program Analysis Part 1 the TIP language

Quiz 0 Review Session. October 13th, 2014

Debugging (Part 2) 1

C Program Development and Debugging under Unix SEEM 3460

CS102 Software Engineering Principles

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

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Systems software design. Software build configurations; Debugging, profiling & Quality Assurance tools

Limitations of the stack

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

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

just a ((somewhat) safer) dialect.

Lecture 3: C Programm

Programming in C First meeting Tiina Niklander

CS 11 C track: lecture 6

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Core dumped - on debuggers and other tools

Reviewing gcc, make, gdb, and Linux Editors 1

CSE 333 Midterm Exam Sample Solution 5/10/13

CSCI-1200 Data Structures Fall 2015 Lecture 6 Pointers & Dynamic Memory

Jackson Marusarz Software Technical Consulting Engineer

Debugging. John Lockman Texas Advanced Computing Center

High Performance Computing and Programming. Performance Analysis

Programming in C week 1 meeting Tiina Niklander

Recitation: C Review. TA s 20 Feb 2017

Cache Profiling with Callgrind

CMPSC 311- Introduction to Systems Programming Module: Debugging

Performance Measurement

Princeton University Computer Science 217: Introduction to Programming Systems. Data Structures

Transcription:

CS2141 Software Development using C/C++ Debugging

Debugging Tips Examine the most recent change Error likely in, or exposed by, code most recently added Developing code incrementally and testing along the way Debug it now, not later Bug may only reoccur when more difficult, costly, or impossible to debug Read before typing Debug by avoiding errors; carefully consider impact of changes prior to making them Debugging 2

Debugging Tips cont. Make the bug reproducible Hard to track down transient bug; construct input and parameter settings so that bug will appear reliably Don't make the same mistake twice If mistake found and corrected, consider if same mistake might appear elsewhere Write a log file Log records what happens in program prior to appearance of problem Debugging 3

Debugging Tips cont. Use tools printf() effective; gdb/ddd far faster once you're past the learning curve Keep records If bug hard to track down, keep track of what you've tried and what you've learned Debugging 4

Debugging Basics You should be able (at some level) to express what you expect the state of your program to be after every statement Often state predicates on program state; i.e., If control is here, I expect the following to be true. Debugging 5

Example #include <stdio.h> int sum=0, val, num=0; double ave; main() { while (scanf("%d\n",&val)!= EOF) { sum += val; num++; } if (num > 0) { ave = sum/num; } } printf("average is %f\n", ave); sum = 0, num = 0 sum should be the total of inputted values, num should be total of inputted values ave should be the floating point average of inputted values Debugging 6

Using gdb Compile source with the -g switch asserted. In our case, gcc -ansi -g ave.c Breakpoint: line in source code at which debugger will pause execution. At breakpoint, can examine values of relevant components of program state. break command sets a breakpoint; clear removes the breakpoint. Diagnostic printf() crude, but effective way of getting a snapshot of program state at a given point. Debugging 7

Using gdb cont. Once paused at a breakpoint, use gdb print, or display to show variable or expression values. display will automatically print values when execution halts at breakpoint. From a breakpoint, may step or next to single step the program. step stops after next source line is executed next similar, but executes functions without stopping. Debugging 8

Using gdb cont. Find out where execution is, in terms of function call chain, with where command; also shows function argument values To make things easier, put the problematic data set in a file named data % a.out < data Average is 2.000000 Debugging 9

Quickie post mortem debugging gdb./a.out core Debugging 10

A GUI for gdb: ddd Display values graphically Click on a pointer value, graphically display thing pointed to Visualize complex linked data structures Debugging 11

Analysis Tools Program analysis process of automatically analyzing the behavior of computer programs Ideally: Big Ugly Code Program Analysis Application Line 41 writes past end of array. Otherwise, this code is perfect Debugging 12

Program Analysis Static analysis performed without executing program Attempts to evaluate all possible executions Difficult Hard to determine all possible variable values, paths Computationally complex Static analysis tools tend to have voluminous and speculative output Commonly identify potential problems that don't exist in actual execution, false positives But, can identify problems not detectable by dynamic analysis Debugging 13

Program Analysis cont. Dynamic analysis performed by executing program Cannot detect all bugs; only those exposed by the execution being analyzed Instrumentation required for analysis Can change the execution Not prone to false positives Debugging 14

Splint Static C program checker Security vulnerabilities and coding mistakes http://www.splint.org Debugging 15

Splint cont. Problems detected Dereferencing a potentially null pointer Using potentially undefined storage or returning storage that is not properly defined Type mismatches, with greater precision and flexibility than provided by C compilers Memory management errors including uses of dangling references and memory leaks Problematic control flow such as likely infinite loops, fall through cases or incomplete switches Buffer overflow vulnerabilities And others Debugging 16

Valgrind Debugging and profiling tool http://valgrind.org/ Debugging 17

Valgrind cont. Dynamic analysis Several components: Memcheck memory management problems This is our focus Cachegrind cache profiler For performance tweaking, find source of cache misses Massif heap profiler Depict heap usage over time Helgrind data races in multithreaded programs Debugging 18

memcheck Use of uninitialized memory Read/write of memory after free off end of blocks allocated via malloc inappropriate areas of stack Leaks Mismatched use of malloc/new/new[] and free/delete/delete[] Etc. Does not bounds check on statically allocated arrays Debugging 19