CS354 gdb Tutorial Written by Chris Feilbach

Similar documents
Reviewing gcc, make, gdb, and Linux Editors 1

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

CS/COE 0449 term 2174 Lab 5: gdb

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

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.

Laboratory 1 Semester 1 11/12

CSE 351. GDB Introduction

ECE 3210 Laboratory 1: Develop an Assembly Program

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

CS 105 Lab 2: Debugger Playing with X86-64 Assembly

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

Exercise Session 6 Computer Architecture and Systems Programming

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

CSE 374 Programming Concepts & Tools

Lab: Supplying Inputs to Programs

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

CS 105 Lab 2: Debugger Playing with X86-64 Assembly

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

Starting to Program in C++ (Basics & I/O)

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Problem Set 1: Unix Commands 1

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

CSE 361 Fall 2017 Lab Assignment L2: Defusing a Binary Bomb Assigned: Wednesday Sept. 20 Due: Wednesday Oct. 04 at 11:59 pm

Using gdb to find the point of failure

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

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Installing and Using Dev-C++

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.

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

COMP Lecture Notes The Compiler

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

Basic functions of a debugger

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Programming Tips for CS758/858

Reliable programming

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

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

Getting started with UNIX/Linux for G51PRG and G51CSA

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:

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

Project 1 Balanced binary

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

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

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

Final Project: LC-3 Simulator

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

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

Lecture 07 Debugging Programs with GDB

Software Engineering /48

Visual Studio 2008 Load Symbols Manually

Scientific Programming in C IX. Debugging

The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

18-600: Recitation #3

The CS-220 Development Environment

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

Lab 7 Unit testing and debugging

Debugging with Karel

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

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

Chapter 1 Getting Started

Testing and Debugging

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

gdbtui - Linux Command

AMD gdebugger 6.2 for Linux

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

How to approach a computational problem

Code::Blocks Student Manual

XP: Backup Your Important Files for Safety

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

Lecture 9: July 14, How to Think About Debugging

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Running MESA on Amazon EC2 Instances: A Guide

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

Lab 1 Introduction to UNIX and C

CSC209. Software Tools and Systems Programming.

Programming assignment A

CSC111 Computer Science II

Reversing. Time to get with the program

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

CS 253: Intro to Systems Programming 1/21

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

Text Input and Conditionals

API Interlude: Process Creation (DRAFT)

Source level debugging. October 18, 2016

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

CSC209. Software Tools and Systems Programming.

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

int $0x32 // call interrupt number 50

Using GitHub to Share with SparkFun a

Programming the DMCC in C

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Lab #0 Getting Started Due In Your Lab, August 25, 2004

Using the KD30 Debugger

Transcription:

CS354 gdb Tutorial Written by Chris Feilbach <crf@cs.wisc.edu> Purpose This tutorial aims to show you the basics of using gdb to debug C programs. gdb is the GNU debugger, and is provided on systems that have gcc to aid with debugging C programs. This debugger is an incredibly powerful tool and this tutorial will only cover the basics. All of the commands and techniques covered here should be enough to adequately debug all CS354 programs. Two programs are provided for you to debug. For those of you at the session, it will be structured like a lab you read through the directions and follow them step by step. I ll be around to answer questions for an hour or two. There is no specific assembly language example, but useful assembly language commands are provided for your reference for assignment 2, and the methodology for debugging C programs is not very different from assembly language ones. More information on gdb can be found in the gdb manual here: https://sourceware.org/gdb/current/onlinedocs/gdb/ What Isn t Covered Here There are a few things that aren t covered by this document and won t be helped with at the review session: Use of basic linux commands, such as how to create a directory, change directories, or how to copy files. Using the gcc compiler to compiler C programs. Learning how to use a text editor. At this point in your CS354 career these should be skills you are familiar with. Getting Started Start by obtaining the files from http://pages.cs.wisc.edu/~cs354-2/handouts/gdb. Copy these files into a private directory. Compiling Files for Use with gdb gdb requires an additional compiler flag to generate something called debug symbols and the removal of the O flag (optimized code makes it harder to debug). When the compiler generates the machine code that your program executes, it generally discards things like variables, because those don t matter to a computer. However, in order for us to debug properly, we need that information. We add a g to the end of our gcc command line to do this. Here is an example: gcc o prog1 prog1.c m32 Wall g

Before Using gdb Can You Locate the Bugs? While debugging tools are very handy, it is a valuable skill to be able to look at code and determine whether or not there are mistakes. You should take some time to look at prog1.c and prog2.c and note anything that looks incorrect. Don t fix these bugs before compiling even if you see them it is still good practice to use the debugger to verify they are actually bugs. When you note where things look wrong, keep track of both the name of the file you re examining, and also the line number where the bug occurs. This information will be needed when you start to use gdb. Another useful debugging technique is to compile the programs and run them. Do they behave like you expect them to? If not, what specifically are they doing wrong? By examining what the current program does, you can make guesses as to what might be wrong, and that will limit your time working with the debugger. Don t just stop at the first sign of failure, either! Most bugs in prog1.c can be found by running the program with varying inputs, although some of the bugs are harder to pick up than others. Another helpful place to look is the warnings that gcc produces, if any. Warnings exist to let you know that while the compiler generated the code, there were things about the code that it didn t like. For example, a warning about an unused variable in code could mean that Fixing a Bug in prog1.c prog1.c is a program that takes two numbers from the command line, and then adds them together and prints the result out to standard output (stdout). At this point you should have reviewed the code in prog1.c. Let s tackle the first bug that I injected, which can be observed by trying to run prog1 with two numbers after it. However, that doesn t work: So let s look at the code. I found that the usage statement is printed out when the argument argc is not equal to 2. The program is not working, and we can use gdb to find out why.

The first technique that I will go over is how to start gdb and examine the contents of a variable or argument. This is a very useful technique that can be used to solve a large amount of problems. While looking through the program I noticed that the if statement starts at line 18. I am going to make note of this because gcc will need it later on. Starting Up gdb We can run gdb by typing the following in the shell: gdb prog1 gdb will now load and will be ready to debug the binary prog1. When I debug, I like to open up a second terminal so I can examine the code I m debugging in vim.

Setting a Breakpoint and Running prog1 in gdb To get gdb to do something for us we must give it commands telling it what to do. We are going to set a breakpoint and then run the program. The program will run until gdb hits the breakpoint, which in our case will be line 18 (where the if statement starts). We tell gdb about the breakpoint by specifying the name of the c file followed by a colon, followed by the line to break on. We enter breakpoint using the break command and then hit enter: break prog1.c:18 gdb let us know that the breakpoint was set. Let s run the program! We can specify that we want the program to run by giving gdb the run command. If we want to specify command line arguments, we can append them to the run command, just like we did when we ran prog1. Here s the gdb command:

run 12 34 Note the line that says 18, and shows the conditional portion of the if statement. Also note two lines up that gdb stopped at breakpoint 1, which is the breakpoint we set at line 18. Please ignore the warning about the source file: you won t see it when you run gdb. You also can safely ignore Missing separate debuginfos Now that we stopped the program at line 18, we can do a bunch of things. One thing that we really want to be able to do is figure out what value is currently in argc. For any variable in our code, we can use the print command to determine what is currently stored inside it. print argc

Right under our print command, it says $1 = 3, which is gdb s way of telling us that argc = 3 (like we expected!). We can now go back to prog1.c, fix that bug, and recompile the code to search for new warnings. While we re here however, there are also some cool and useful things we can do with the print command. The print command can print out variables, but it s not just limited to variables. The print command can also print out the result of expressions. For example, if I wanted to know what argc + 4 was, I could ask gdb to print that and it would tell me. If I wanted to know something like the address of argc, I could ask gdb to print that for me too, in the exact same way you d ask a C program for the address of something (&argc). print argc + 4

We can also see what is inside arrays with print, although not quite the way you might expect. Say for instance we want to examine argv[][]. We can do print argv, but that will only return us an address to a pointer, like so: print argv

We can examine memory locations using the x command, which is a command that you can look up in the manual for more information. Single Stepping Sometimes setting a breakpoint and examining values in variables is not enough to find the bug. Being able to run your program line by line is helpful so you can see how the variables change. This technique is especially useful for debugging problems with loops. It also is very helpful if you can t quite pinpoint where a bug is. Let s use single stepping to find other errors. First quit out of the debugger by typing the following: quit Go ahead and fix the argc bug that we just found and take a look at the code. Now that we know that argc is fixed (although, we should recompile and re-run the program to verify our fix works), let s put a breakpoint on the first statement in the else block: If you haven t already, recompile your code and then start up gdb so that it is debugging prog1. Place a breakpoint on line 24 in prog1.c, and run the program with the same command line arguments as last time. Here s what gdb outputted for me:

We can make gdb execute the next line using the step command. By using the step command in conjunction with the print command and with breakpoints, we find several types of bugs. You should step through the program line by line until you find where all the bugs are. Once they are found go back and fix and recompile, and continue the debugging process. Debugging prog2.c prog2.c contains a linked list program that has bugs. Using the techniques you learned above you should be able to find and squash the bugs in prog2.c Debugging Assembly Language Code gdb can also debug assembly language! The process is similar, except that breakpoints are set at addresses as opposed to the line. The methodology doesn t change much from debugging a C program. Here is a list of useful commands: To set a breakpoint at an address addr: break *addr To step instruction by instruction: stepi To see the current state of all registers: info registers How Do I Get Better at Debugging? Learning to debug programs well is an art and is best taught by debugging programs. It takes a lot of practice to learn to do it well and you will get better as you write more programs and debug more code.