CSE 374 Programming Concepts & Tools

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

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

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

CSE 351. GDB Introduction

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

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

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

Exercise Session 6 Computer Architecture and Systems Programming

CS354 gdb Tutorial Written by Chris Feilbach

Reviewing gcc, make, gdb, and Linux Editors 1

Programming Studio #9 ECE 190

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.

Lecture 07 Debugging Programs with GDB

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

EE 355 Lab 3 - Algorithms & Control Structures

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:

Class Information ANNOUCEMENTS

Problem Set 1: Unix Commands 1

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Basic functions of a debugger

CSE 374 Programming Concepts & Tools

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

CS/COE 0449 term 2174 Lab 5: gdb

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

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

Source level debugging. October 18, 2016

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

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

Intro to Segmentation Fault Handling in Linux. By Khanh Ngo-Duy

18-600: Recitation #3

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

CSE 410: Systems Programming

CMPSC 311- Introduction to Systems Programming Module: Debugging

Exceptions in Java

Debugging with GDB and DDT

CS 11 C track: lecture 6

CS2: Debugging in Java

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

The Dynamic Debugger gdb

Debugging with GDB and DDT

1 Basic functions of a debugger

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

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

GDB cheatsheet - page 1

Debugging. John Lockman Texas Advanced Computing Center

CMPSC 311- Introduction to Systems Programming Module: Debugging

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

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

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

Xcode Tricks. ios App Development Fall 2010 Lecture 13

Profilers and Debuggers. Introductory Material. One-Slide Summary

CS2141 Software Development using C/C++ Debugging

Today s presentation. Git gdb Project 1

Debugging with gdb and valgrind

Using gdb to find the point of failure

CSE 303 Concepts and Tools for Software Development. Magdalena Balazinska Winter 2010 Lecture 27 Final Exam Revision

CSE 303: Concepts and Tools for Software Development

The Perl Debugger. Avoiding Bugs with Warnings and Strict. Daniel Allen. Abstract

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

Programming Tips for CS758/858

EL2310 Scientific Programming

Development Environment & Linux Guide

Welcome. HRSK Practical on Debugging, Zellescher Weg 12 Willers-Bau A106 Tel

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

int $0x32 // call interrupt number 50

Testing, Debugging, Program Verification

Laboratory Assignment #4 Debugging in Eclipse CDT 1

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

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Software Engineering Testing and Debugging Debugging

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Reliable programming

Software Engineering

Today s Topic. Software Engineering Testing and Debugging Debugging. Today s Topic. The Main Steps in Systematic Debugging

GDB QUICK REFERENCE GDB Version 4

Boot Camp. Dave Eckhardt Bruce Maggs

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

CSE 374 Programming Concepts & Tools

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

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

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

hw6, BFS, debugging CSE 331 Section 5 10/25/12 Slides by Kellen Donohue

Announcements. 1. Forms to return today after class:

Project #1: Tracing, System Calls, and Processes

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Computer Science II Lab 3 Testing and Debugging

These are notes for the third lecture; if statements and loops.

18-642: Code Style for Compilers

Program Design: Using the Debugger

CS 520 Theory and Practice of Software Engineering Fall 2018

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

gdbtui - Linux Command

The First Real Bug. gdb. Computer Organization I McQuain

Programming in C First meeting

Lab 03 - x86-64: atoi

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

Laboratory 1 Semester 1 11/12

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

Transcription:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 11 gdb and Debugging 1

Administrivia HW4 out now, due next Thursday, Oct. 26, 11 pm: C code and libraries. Some tools: gdb (debugger) and clint.py (style checker). gdb demo today. Due next Thursday, 11 pm Midterm the following Wednesday, Nov. 1, in class Everything up to hw4/basic C Closed book will provide reference summary info on test as needed Old exams on web now for studying Review Q&A Tuesday, Oct. 31, 4:30, location TBA 2

Agenda Debuggers, particularly gdb Why? To learn general features of breakpoint-debugging To learn specifics of gdb To learn general debugging survival skills Skill #1: don t panic! Skill #2: be systematic have a plan 3

How to avoid debugging Don t put bugs in the program!! Think before typing design before coding Write down design (comments) as you go Functions: declaration+comments should be complete spec Significant data: declaration + comments should be complete spec If someone has to read the code to figure out how to use something or understand data structures, comments are bad Review/check comments and compare to code as you work Will catch errors before you run the program Turn on compiler warnings (-Wall); use assert; get the computer to find problems for you. But things can still go wrong 4

An execution monitor? What would you like to see inside and do to a running program? Why might all that be helpful? What are reasonable ways to debug a program? A debugger is a tool that lets you stop running programs, inspect (sometimes set) values, run a statement or two at a time, etc. A MRI for observing executing code 5

Issues Source information for compiled code. (Get compiler help) Stopping your program too late to find the problem. (Art) Trying to debug the wrong algorithm Trying to run the debugger instead of understanding the program It s an important tool Debugging C vs. Java Eliminating crashes does not make your C program correct Debugging Java is easier because (some) crashes and memory errors do not exist But programming Java is easier for the same reason! 6

gdb gdb (Gnu debugger) is part of the standard Linux toolchain. gdb supports several languages, including C compiled by gcc. Modern IDEs have fancy GUI interfaces, which help, but concepts are the same. Compile with debugging information: gcc -g Otherwise, gdb can tell you little more than the stack of function calls. Running gdb: gdb executable Source files should be in same directory (or use the -d flag). At prompt: run args Note: You can also inspect core files, which is why they got saved on older systems after every crash (Mostly useful for analyzing crashed programs after-the-fact, not for systematic debugging. The original use of db.) 7

Basic functions backtrace frame, up, down print expression, info args, info locals Often enough for crash debugging Also often enough for learning how the compiler does things (e.g., stack direction, malloc policy,...) 8

Breakpoints break function (or line-number or...) conditional breakpoints (break XXX if expr) 1. to skip a bunch of iterations 2. to do assertion checking going forward: continue, next, step, finish Some debuggers let you go backwards (typically an illusion) Often enough for binary search debugging Also useful for learning program structure (e.g., when is some function called) Skim the manual for other features. 9

A few tricks Everyone develops their own debugging tricks ; here are a few: Always checking why a seg-fault happened (infinite stack and array-overflow very different) Printing pointer values to see how big objects were. Staring at code even if it does not crash Printing array contents (especially last elements)... 10

Advice Understand what the tool provides you Use it to accomplish a task, for example I want to know the call-stack when I get the NULL-pointer dereference Optimize your time developing software Think of debugging as a systematic experiment to discover what s wrong not a way to randomly poke around. Observation: the problem; hypothesis: I think the cause is ; experiment: use debugger to verify Use development environments that have debuggers? See also: jdb for Java Like any tool, takes extra time at first but designed to save you time in the long run Education is an investment 11

gdb summary running programs Be sure to compile with gcc -g Open the program with: gdb executable_file Start or restart the program: run program_args Quit the program: kill Quit gdb: quit Reference information: help Most commands have short abbreviations <return> often repeats the last command Particularly useful when stepping through code 12

gdb summary looking around bt stack backtrace up, down change current stack frame list display source code (list n, list function_name) print expression evaluate and print expression display expression (re-)evaluate and print expression every time execution pauses. undisplay remove an expression from this recurring list. info locals print all locals (but not parameters) x (examine) look at blocks of memory in various formats 13

gdb summary breakpoints, stepping break set breakpoint. (break function_name, break linenumber, break file:linenumber) info break print table of currently set breakpoints clear remove breakpoints disable/enable temporarily turn breakpoints off/on without removing them from the breakpoint table continue resume execution to next breakpoint or end of program step execute next source line next execute next source line, but treat function calls as a single statement and don't step into them finish execute to the conclusion of the current function How to recover if you meant next instead of step 14