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

Similar documents
CSE 374 Programming Concepts & Tools

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

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

CSE 351. GDB Introduction

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

CS354 gdb Tutorial Written by Chris Feilbach

Exercise Session 6 Computer Architecture and Systems Programming

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

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

Programming Studio #9 ECE 190

Reviewing gcc, make, gdb, and Linux Editors 1

CS 11 C track: lecture 6

Lecture 07 Debugging Programs with GDB

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

Class Information ANNOUCEMENTS

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

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.

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

CS/COE 0449 term 2174 Lab 5: gdb

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

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

1 Basic functions of a debugger

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

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

CS2141 Software Development using C/C++ Debugging

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

18-600: Recitation #3

CSE 410: Systems Programming

Problem Set 1: Unix Commands 1

Debugging with GDB and DDT

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

EE 355 Lab 3 - Algorithms & Control Structures

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

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:

Basic functions of a debugger

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.

Debugging with GDB and DDT

CSE 303: Concepts and Tools for Software Development

Using gdb to find the point of failure

Source level debugging. October 18, 2016

Profilers and Debuggers. Introductory Material. One-Slide Summary

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

Debugging with gdb and valgrind

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Debugging Techniques. CEFET Engineering Week

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

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

Laboratory 1 Semester 1 11/12

CMPSC 311- Introduction to Systems Programming Module: Debugging

The Dynamic Debugger gdb

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

Unix and C Program Development SEEM

GDB cheatsheet - page 1

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

GDB QUICK REFERENCE GDB Version 4

GDB Tutorial. University of Waterloo

EL2310 Scientific Programming

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

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

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

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

Scientific Programming in C IX. Debugging

Programming Tips for CS758/858

CMPSC 311- Introduction to Systems Programming Module: Debugging

int $0x32 // call interrupt number 50

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Debugging. John Lockman Texas Advanced Computing Center

CS201 Lecture 2 GDB, The C Library

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

Debugging and Profiling

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Advanced Magic - GDB

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

Chapter - 17 Debugging and Optimization. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

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

Portland State University Maseeh College of Engineering and Computer Science. Proficiency Examination Process

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

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

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

CS2: Debugging in Java

Introduction to Computation and Problem Solving

Testing, Debugging, Program Verification

Program Design: Using the Debugger

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Recitation #11 Malloc Lab. November 7th, 2017

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

1.00 Lecture 2. What s an IDE?

VFS, Continued. Don Porter CSE 506

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

Software Engineering

CSE 303: Concepts and Tools for Software Development

1 A Brief Introduction To GDB

Computer Science II Lab 3 Testing and Debugging

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Chapter 12 Visual Program Debugger

Learning Objectives. A Meta Comment. Exercise 1. Contents. From CS61Wiki

Transcription:

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

Hacker tool of the week (tags) Problem: I want to find the definition of a function or variable. Standard in most IDEs. What about vim and emacs? exuberant ctags or etags! vim: create tags file ctags *.c (creates./tags by default) in buffer, put cursor on symbol usage, then C-] to go to definition emacs: create tags file etags *.c (creates./tags by default) in buffer, put cursor on symbol usage, then M-. to go to the definition Lots of other code browsing tools out there 2

Agenda Last bits of arrays 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 Why now? might help on HW4! 3

Arrays on the stack A local variable that is an array is allocated on the stack (that s why a size is required) its address is the same as that array variable s value but they are different types see array_address.c and array_types.c 4

Arrays revisited Implicit array promotion : a variable of type T[ ] becomes a variable of type T* in an expression void f1(int* p) { *p = 5; } int* f2() { int x[3]; /* x on stack */ x[0] = 5; /* (&x)[0] = 5; wrong */ *x = 5; *(x+0) = 5; f1(x); /* f1(&x); wrong watch types! */ /* x = &x[2]; wrong x isn t really a pointer! */ int *p = &x[2]; return x; /* wrong dangling pointer but type correct */ } 5

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, etc. An MRI for observing executing code 6

Issues Source information for compiled code. (Get compiler help) Stopping your program too late to find the problem. Trying to debug the wrong algorithm Trying to run the debugger instead of understanding the program 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 programming Java is easier for the same reason! 7

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. Compiling 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 get saved (Mostly useful for analyzing crashed programs after-the-fact, not for systematic debugging. The original use of db.) 8

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,...) 9

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. 10

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

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 12

Course news HW4 deadline is Thursday 2/5 midterm is the following Monday 2/9 will cover through C pointers and arrays review session next week 13

gdb summary running programs Be sure to compile with gcc g Open the program with: gdb <executable file> Start or restart the program: run <command 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 14

gdb summary looking around bt stack backtrace up, down change current stack frame f <num> - change current stack frame to frame #num 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 15

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 16