CSE 351. GDB Introduction

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

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

Basic functions of a debugger

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

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

CSE 374 Programming Concepts & Tools

CS354 gdb Tutorial Written by Chris Feilbach

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

Source level debugging. October 18, 2016

Exercise Session 6 Computer Architecture and Systems Programming

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

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

CS/COE 0449 term 2174 Lab 5: gdb

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

ALD Assembly Language Debugger Copyright (C) Patrick Alken

Programming Studio #9 ECE 190

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

Reviewing gcc, make, gdb, and Linux Editors 1

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

18-600: Recitation #3

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

Problem Set 1: Unix Commands 1

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

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

Scientific Programming in C IX. Debugging

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

1 Basic functions of a debugger

Using gdb to find the point of failure

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

Debugging. John Lockman Texas Advanced Computing Center

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

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

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

CS 11 C track: lecture 6

Laboratory 1 Semester 1 11/12

Unix and C Program Development SEEM

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

The First Real Bug. gdb. Computer Organization I McQuain

CSE 351. Introduction & Course Tools

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.

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

C Program Development and Debugging under Unix SEEM 3460

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

Lecture 07 Debugging Programs with GDB

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

CS 314 Principles of Programming Languages. Lecture 9

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

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

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Recitation #11 Malloc Lab. November 7th, 2017

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CSE 410: Systems Programming

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

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Programming in C S c o t t S c h r e m m e r

Understanding the Program Run

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

Recitation: Bomb Lab. September 17 th 2018

Review Topics. Midterm Exam Review Slides

CMPSC 311- Introduction to Systems Programming Module: Debugging

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

1 A Brief Introduction To GDB

Lab 03 - x86-64: atoi

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CMPSC 311- Introduction to Systems Programming Module: Debugging

Debugging with GDB and DDT

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

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

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:

Code Review and Debugging (Lab 05)

CSE 374 Programming Concepts & Tools

Program Design: Using the Debugger

Using the GNU Debugger

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

Using the GNU Debugger

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

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

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

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

SWEN-250 Personal SE. Introduction to C

CSE 374 Midterm Exam 11/2/15 Sample Solution. Question 1. (10 points) Suppose the following files and subdirectories exist in a directory:

Debugging with GDB and DDT

Debugging Techniques. CEFET Engineering Week

Debugging uclinux on Coldfire

C: How to Program. Week /Mar/05

Data and File Structures Laboratory

Understanding Software Vulnerabilities: C, Debugging Assembly, and Buffer Overflows

BLM2031 Structured Programming. Zeyneb KURT

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

EE 355 Lab 3 - Algorithms & Control Structures

Review Topics. Midterm Exam Review Slides

Computer Science II Lab 3 Testing and Debugging

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

Debugging and Profiling

Systems/DBG Debugger Version 2.20

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

Transcription:

CSE 351 GDB Introduction

Lab 2 Out either tonight or tomorrow Due April 27 th (you have ~12 days) Reading and understanding x86_64 assembly Debugging and disassembling programs Today: General debugging for C with GDB 2

scanf and sscanf() Lab 2 uses sscanf (string scan format), which parses a string into values Example: char *mystring = 123, 456 int a, b; sscanf(mystring, %d, %d, &a, &b); The first argument is parsed according to the format string. After this code is run, a = 123 and b = 456. printf( Variable a=%d and b=%d\n, a, b); This will print to the console Variable a=123 and b=456 Notice the similarities to printf()!

Format Specifier Notice the string formatter %d %d, signed int %u, unsigned int %c, char %f, float %s, string (match until it finds white-space) %x, hexadecimal int %p, pointer Subtle differences between printf and scanf http://www.cplusplus.com/reference/cstdio/printf/ http://www.cplusplus.com/reference/cstdio/scanf/

GDB Background GNU Debugger GDB can help you debug your program in four ways: It can run your program It can stop your program on specified conditions It allows you to examine what has happened once the program has stopped It allows you to modify your program s execution at runtime Today we will be going over many of the features that will make GDB a great resource for you this quarter Useful in future classes! CSE 333, CSE 451, CSE 484 etc. 5

Interactive Demonstration I encourage you to either follow along on your own machine or find someone next to you who is doing so. Download calculator.c from the class calendar page. We want to compile this file with debugging symbols included. To do this, we must use the -g flag in GCC: gcc -Wall -std=gnu99 -g calculator.c -o calculator Without debugging symbols, GDB is not nearly as useful. 6

Loading the Program In order to load a binary into GDB, you simply pass the name of the executable to the gdb program. Try this on your machine: gdb calculator You should see a bunch of version and license information. The last line before the (gdb) prompt is always the symbol loading status. If you ever see (no debugging symbols found) you may have a problem. In this case, you should see no such message. 7

Exiting GDB Before we go any further, it might be helpful to understand how to exit GDB. There are multiple ways to exit: Ctrl-D Typing quit Typing q Many GDB commands can simply be abbreviated to their first letter. If you ever want to stop the current GDB command, just use Ctrl-C. 8

Running the Executable There are multiple ways you can begin execution of a program in GDB. The run command will start your program and keep running until it hits a critical error or the program finishes. Try entering run, or just r. The start command will load your program into memory and break at the beginning of main() You will see that most times run is all you need, but there are cases when you want to just start stepping through main(). If you want to specify command-line arguments, you just pass those to run or start. To run calculator, we need to pass three arguments. Try entering: run 2 3 + 9

Viewing Program Source Code To examine source code while debugging use the list (l for short) command. Useful when trying to find line numbers. For example, let s look at the code for main(). Type list main. This will display 10 lines of code around the beginning of the main() function. If you want to display 10 lines around line 45, enter list 45 If you want to display a range of line numbers, such as lines 30-70, enter list 30,70 10

Setting Breakpoints In order to step through code, we need to be able to pause execution. GDB allows you to set breakpoints, just like when you debugged Java programs in Eclipse or jgrasp. The break (b for short) command creates breakpoints. Let s set a breakpoint at the entry to main(). Enter: break main Now enter run and see the program break at main(). Each breakpoint is given a number. Our breakpoint is given the number 1. To disable our breakpoint temporarily, enter: disable 1 To enable our breakpoint again, enter: enable 1 To delete our breakpoint, enter: delete 1 To see a summary of all your breakpoints, enter: info break 11

Stepping Through Code The next (n for short) command allows you to step through one line of C code at a time, stepping over function calls. The step (s for short) command is the same, except it steps into function calls. The finish (fin for short) command, steps out of the function. It works exactly like you would hope, most of the time Caveat: if you loaded some external library that was not compiled with debugging symbols, then calls to that library will look confusing when you step into them. Break your program at the beginning of main(), enter next until you arrive at a call to printf(), and then enter step to step into the call to printf(). Note that it doesn t step into that function call, because it wasn t compiled with debugging symbols If you have halted execution and wish to continue running the program, use the continue (c for short) command. Use that now to run the program to completion. 12

Printing Variables GDB has its own print function that is extremely useful. Let s print out our command-line arguments in various formats. Set a breakpoint on line 47 by entering: b 47 Restart running the calculator program with some custom command line arguments. Continue until the breakpoint on line 47 is hit. Once there, print out the values of the three variables holding your arguments (a, b, operator) by typing the following: print a print b print operator 13

Printing Variables (cont.) Now let s try printing out the values of the variables in different formats. Try the following: print /x operator print /t a print print_operation print *argv print *argv[1] print *argv[3] What do each of these do? 14

Debugging Let s look at how GDB enables us to easily identify runtime errors. Try making the program divide by zero: run 1 0 / If you keep continuing, eventually the program will throw an arithmetic exception, and GDB will tell you exactly that. If you want to see a backtrace, just type backtrace (bt for short) and it will show you the chain of function calls that led to the error. Viewing a backtrace can be very helpful in debugging. 15

Future Topics Next week we will be going over some more advanced topics to get you through Lab 2 These include, but are not limited to: Disassembling programs Stepping through assembly code Printing register values Examining memory If time permits, we can start getting into some of those now, but if not feel free to start messing with those on your own. 16