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

Similar documents
CS354 gdb Tutorial Written by Chris Feilbach

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

CSE 374 Programming Concepts & Tools

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

Reviewing gcc, make, gdb, and Linux Editors 1

CSE 351. GDB Introduction

Source level debugging. October 18, 2016

Project #1: Tracing, System Calls, and Processes

Scientific Programming in C IX. Debugging

CMPSC 311- Introduction to Systems Programming Module: Debugging

Programming Studio #9 ECE 190

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

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

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

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

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

Data and File Structures Laboratory

CSE 410: Systems Programming

Debugging with gdb and valgrind

Lecture 07 Debugging Programs with GDB

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

CMPSC 311- Introduction to Systems Programming Module: Debugging

Using gdb to find the point of failure

Black Box Debugging of Embedded Systems

Exercise Session 6 Computer Architecture and Systems Programming

Profilers and Debuggers. Introductory Material. One-Slide Summary

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

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

C / C++ Coding Rules

18-600: Recitation #3

1 A Brief Introduction To GDB

CS/COE 0449 term 2174 Lab 5: gdb

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

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

ECE 3210 Laboratory 1: Develop an Assembly Program

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

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

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

Chapter 1 Getting Started

Programming Tips for CS758/858

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS201 Lecture 2 GDB, The C Library

Debugging and Profiling

Software Development With Emacs: The Edit-Compile-Debug Cycle

Important From Last Time

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

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

CS Prof J.P.Morrison

Making Address Spaces Smaller

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

gdbtui - Linux Command

Important From Last Time

UNIX Makefile. C Project Library Distribution and Installation.

EE355 Lab 5 - The Files Are *In* the Computer

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

Computer Labs: Debugging

Debugging with GDB and DDT

CS2141 Software Development using C/C++ Debugging

Problem Set 1: Unix Commands 1

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

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

Compiling and Linking

Reversing. Time to get with the program

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

Compilation, Disassembly, and Profiling (in Linux)

COMP Lecture Notes The Compiler

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

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

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

U Reverse Engineering

Debugging with GDB and DDT

CSE 4/521 Introduction to Operating Systems

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

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

Program Design: Using the Debugger

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.

Laboratory 1 Semester 1 11/12

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

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

Simulavr. A simulator for the Atmel AVR family of microcontrollers. For simulavr version , 18 January by Theodore A.

The Dynamic Debugger gdb

Intermediate Programming, Spring 2017*

Debugging. John Lockman Texas Advanced Computing Center

Subprograms: Local Variables

Enhanced Debugging with Traces

Programming in the Real World. Dr. Baldassano Yu s Elite Education

Efficient and Large Scale Program Flow Tracing in Linux. Alexander Shishkin, Intel

GDB 1 GDB 2 GDB. Fortran Pascal GDB 4. hoge.c. Fig. 1. calc.c. Fig GDB. GDB Debian. # apt-get install gdb

EE 355 Lab 3 - Algorithms & Control Structures

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

Lecture 9: July 14, How to Think About Debugging

CS480. Compilers Eclipse, SVN, Makefile examples

Recitation 10: Malloc Lab

377 Student Guide to C++

Exploring the file system. Johan Montelius HT2016

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

Using the Command Line

Functions, Pointers, and the Basics of C++ Classes

CS168: Debugging. Introduc)on to GDB, Wireshark and Valgrind. CS168 - Debugging Helpsession

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Transcription:

Debugging ICS312 Machine-Level and Systems Programming Henri Casanova (henric@hawaii.edu)

Debugging Even when written in high-level languages, programs have bugs Recall the thought that when moving away from assembly bugs would disappear! Some famous bugs Mariner I Venus probe (1962) Had to be destroyed as it went off course (wrong loop? wrong cut-and-paste?) Ariane 5 failure (1996) Some variables changed from int to long, some not...

Debugging Programmers and debugging Some people love debugging Sense of accomplishment Some people hate it Difficult, and not really taught Debugging: determining the exact nature and location of a suspected error and fixing it Locating the error is often 95% of the work Question: how do we find bugs? Two main approaches: Static Debugging Visual Inspection Fancy name for all types of monkeying around with the code Dynamic Debugging Using a debugger

Static Debugging Stare at the code Has its limits, as we know Although asking a peer for code review can work better Using all types of compiler flags so that it generates all possible warnings is a good idea e.g., gcc -Wall -pedantic Puts a bunch of printf statements I m here, I am seeing this value Widely used and pretty effective But can be labor-intensive Both to instrument the code and to look at the output Comment-out portions of the code To find compilation errors mostly Stare at the code again But not too passively!!!

Static Debugging: Limitations The problem with all static debugging techniques is that there are things you almost cannot do: What about code in libraries that you use but didn t write? What about memory bugs in languages like C? For these above limitations, and the ones seen on the previous slides, we have dynamic debugging

Dynamic Debugging Dynamic debugging makes it possible to observe a program as it runs And to control its execution To do this we use a debugger The principles of a debugger You compile the code enabling debugging Debugging information is embedded inside the object files, so that the debugger can relate machine code to high-level code You run your code within the debugger The debugger allows you to: Run step-by-step Insert breakpoints Look at variable contents Modify variable contents

The GNU Debugger The GNU Debugger is gdb run code until some bkpt header text gdb... 0x40FFFF01: 43FE 0x40FFFF03: 02EA... running debugger attached to the running program read/write data in memory data... 0xF8E30001: F4 0xF8E40002: 02... running program in memory

Including a Symbol Table The problem with the previous picture is that the everything is binary (or hex, which is only a little bit more readable) So as a user of the debugger, if you want to look at the content of a variable, you have to specify its address: Tell me the 2-byte value at 0xFFE40123 What we would really like to do is only use variable names as declared/used in the program Tell me the value of variable x To do so, we must have a symbol table Recall the linker/loader lecture In general the symbol table is removed from the final executable Takes space and isn t used for running the program You can tell gcc to keep it around by compiling with the -g flag: gcc -c -g main.c -o main.o

The GNU Debugger The GNU Debugger is gdb header gdb run code until function func: read/write variable x in memory text... 0x40FFFF01: 43FE 0x40FFFF03: 02EA... data... 0xF8E30001: F4 0xF8E40002: 02... symbol table Info used by the compiler and kept around for easier debugging I define x in my data segment at 0xF8E30001 (and it s a 4-byte int) I define func in my text segment at 0x40FFFF01 (and its prototype is..)

Running gdb To run your program under the control of the debugger you just invoke is as follows: gdb./prog At this point we are within the gdb prompt and we can type gdb commands Let s try this and look at the gdb ever useful help command On a Linux box...

Useful gdb Commands run (or r ): starts the program with potential command-line arguments the program will run all the way through list (or l ): shows 10 lines of code around where we are break (or b ): sets a breakpoint In a function, e.g., break main At a specific line in the code, e.g., break main.c:154 step (or s ): runs the program step-by-step After stopping at a breakpoint next (or n ): like step, but skips over functions continue (or c ): continues until next breakpoint print (or p ): to print variable values or function call! quit (or q ): quits the program/debugger

Managing Breakpoints To list all existing breakpoints you can use the info break command Example: (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x001f7c in main at main.c:4 2 breakpoint keep y 0x001f96 in main at main.c:12 3 breakpoint keep y 0x001fa9 in main at main.c:17 To delete a breakpoint you can use the delete command Example: delete 2

Post-Mortem debugging? A very important use of a debugger is when the program simply causes a segmentation fault or a bus error Meaning, there is something fishy that happens with the use of the memory Especially useful for languages like C and C++ of course Note that such errors are very rare in a language like Java So much hand holding of the developer The program has already crashed so we can t observe the bug live

Post-Mortem with Cores When a program encounters a fatal error, the Kernel terminates execution and creates a core file (a.k.a. core dump ) A core file is a snapshot of the program at the moment the error occurred This happens upon Segmentation faults: trying to access memory that is not in the address space of the process Bus errors: (often) trying to address an address that cannot be physically addressed Illegal instruction: happens when execution branches into data Arithmetic exception: e.g., dividing by zero Once a core file has been generated, you can invoke gdb as: gdb <prog name> <core file>

Bells and Whistles... gdb has tons of cool options One example: conditional breakpoints Say you want a breakpoint to stop the code only if a certain condition is met So that you don t have to type continue a bunch of times until you get the code in the place you want (with the risk of missing it!) You can just type something like: cond 3 x > 100 which will make breakpoint #3 stop only if variable x (in the context of the breakpoint) is strictly larger than 100!

So now what? At this point, we can do a detail step-by-step run through the code and inspect all relevant memory content But what about the most vexing bugs (in C): memory corruption! going over the end of an array writing the memory that was freed Memory corruption is very tricky to debug It may cause the program to give a wrong result, without a segfault A segfault may be caused by an instruction that is not the one that corrupted memory Using printf statements to find memory corruption bugs is hopeless adding printf s can appear to magically fix the memory bug! Using a debugger is also quite difficult One has to look at all memory content...

Memory Bugs? Memory bugs in languages like C can be very hard to fix What we really need is an automatic way in which memory integrity can be checked A popular opensource/free tool is valgrind You simple run your code through valgrind as: valgrind./prog [arguments] And then you look at the valgrind report

Conclusion Using print statements to debug code is extremely limited Of course when writing assembly we have used it, but with high-level code it s really cumbersome When you start having many data structures, especially in a language like C, you have to use more powerful tools A debugger, like gdb A memory checker, like valgrind Somehow, the temptation to not use them is great! But one should not succumb to it, especially when facing memory issues