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

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

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

CSE 351. GDB Introduction

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

Source level debugging. October 18, 2016

GDB QUICK REFERENCE GDB Version 4

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

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

Using gdb to find the point of failure

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

CSE 374 Programming Concepts & Tools

GDB cheatsheet - page 1

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

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

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

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

Problem Set 1: Unix Commands 1

CS354 gdb Tutorial Written by Chris Feilbach

Princeton University Computer Science 217: Introduction to Programming Systems. Building Multi-File Programs with the make Tool

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

Unix and C Program Development SEEM

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

ALD Assembly Language Debugger Copyright (C) Patrick Alken

CS/COE 0449 term 2174 Lab 5: gdb

Exercise Session 6 Computer Architecture and Systems Programming

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

Reviewing gcc, make, gdb, and Linux Editors 1

Lab6 GDB debugging. Conventions. Department of Computer Science and Information Engineering National Taiwan University

MIT OpenCourseWare Multicore Programming Primer, January (IAP) Please use the following citation format:

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

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

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

Migrating from Inspect to Native Inspect

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

gdbtui - Linux Command

Libgdb. Version 0.3 Oct Thomas Lord

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

The First Real Bug. gdb. Computer Organization I McQuain

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

Laboratory 1 Semester 1 11/12

C Program Development and Debugging under Unix SEEM 3460

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

Programming Studio #9 ECE 190

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

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

GDB Tutorial. University of Waterloo

Program Design: Using the Debugger

Using the Command-Line Interface

Basic functions of a debugger

Lab: Supplying Inputs to Programs

Programming in the Large Steps

Supplement: Visual C++ Debugging

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

15-122: Principles of Imperative Computation

Intro to MS Visual C++ Debugging

Debugging with GDB and DDT

The Dynamic Debugger gdb

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Debugging with GDB and DDT

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

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

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

A Tutorial for ECE 175

Debugging in LISP. trace causes a trace to be printed for a function when it is called

18-600: Recitation #3

dbx90: Fortran debugger March 9, 2009

Using the Command-Line Interface

ARM DS-5. Using the Debugger. Copyright 2010 ARM. All rights reserved. ARM DUI 0446A (ID070310)

Recitation: Bomb Lab. September 17 th 2018

Computer Labs: Debugging

April 4-7, 2016 Silicon Valley. CUDA DEBUGGING TOOLS IN CUDA8 Vyas Venkataraman, Kudbudeen Jalaludeen, April 6, 2016

Visual Studio 2008 Load Symbols Manually

An Introduction to Komodo

Introduction to IntelliJ

Today s presentation. Git gdb Project 1

7.1 INTRODUCTION 7.2 STARTING Emacs 7.3 Emacs SCREEN Menu Bar Text Mode Menu Mode Line Echo Line

General Debugging Strategies

Introduction to Computation and Problem Solving

printf( Please enter another number: ); scanf( %d, &num2);

Understanding the Program Run

Introduction: The Unix shell and C programming

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

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

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

Lecture 07 Debugging Programs with GDB

ECE 3210 Laboratory 1: Develop an Assembly Program

Laboratory Assignment #4 Debugging in Eclipse CDT 1

The Xemacs Editor. Overview

Computer Science II Lab 3 Testing and Debugging

NEW CEIBO DEBUGGER. Menus and Commands

NetBeans Tutorial. For Introduction to Java Programming By Y. Daniel Liang. This tutorial applies to NetBeans 6, 7, or a higher version.

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

DEBUGGING ON FERMI PREPARING A DEBUGGABLE APPLICATION GDB. GDB on front-end nodes

LAB WORK NO. 3 TURBO DEBUGGER ENVIRONMENT

Emacs: The Software Engineer's \Swiss Army Knife"

Emacs: the Free Software IDE

Debugging. John Lockman Texas Advanced Computing Center

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

Download, Install and Setup the Linux Development Workload Create a New Linux Project Configure a Linux Project Configure a Linux CMake Project

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

Transcription:

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference Part 1: Tutorial This tutorial describes how to use a minimal subset of the gdb debugger. For more information see Part 2 of this document and the online gdb tutorial at http://sourceware.org/gdb/current/onlinedocs/gdb/. The tutorial assumes that you've created files named testintmath.c, intmath.h, and intmath.c in your working directory, containing the (version 4) program recently discussed in precepts. Those files are available through the course Schedule Web page. Introduction Suppose you're developing the testintmath (version 4) program. Further suppose that the program preprocesses, compiles, assembles, and links cleanly, but produces incorrect results at run-time. What can you do to debug the program? One approach is temporarily to insert calls to printf(...) or fprintf(stderr,...) throughout the program to get a sense of the flow of control and the values of variables at critical points. That's fine, but often is inconvenient. An alternative is to use gdb. gdb is a powerful debugger. It allows you to set breakpoints in your program, step through your executing program one line at a time, examine the values of variables at breakpoints, examine the function call stack, etc. Building To prepare to use gdb, build your program with the -g option: $ gcc217 -g testintmath.c intmath.c -o testintmath The -g option tells gcc217 to place extra information in the testintmath file that gdb uses. It's a common error to forget to specify the -g option when building in preparation for using gdb. Page 1 of 6

Running gdb The next step is to run gdb. You can run gdb directly from the shell, but it's much better to run it from within emacs. So launch emacs, with no command-line arguments: $ emacs Now call the emacs gdb function via these keystrokes: <Esc key> x gdb <Enter key> The emacs editor displays the message: Run gud-gdb (like this): gdb - fullname followed by the name of some executable binary file. If that name is not testintmath, then use the backspace key to delete it, and type testintmath. Then type the Enter key. At this point you're executing gdb from within emacs. gdb is displaying its (gdb) prompt. Running Your Program Issue the run command to run the program: Enter 8 as the first integer, and 12 as the second integer. gdb runs the program to completion, indicating that the Program exited normally. File redirection is specified as part of the run command. For example, the command run < somefile runs the program, redirecting its standard input to somefile. Using Breakpoints Set a breakpoint at the beginnings of some functions using the break command: (gdb) break main (gdb) break IntMath_gcd Another way to set a breakpoint is by specifying a file name and line number separated by a colon, for example, break intmath.c:20. Page 2 of 6

Then run the program: gdb pauses execution near the beginning of main(). It opens a second window in which it displays your source code, with an arrow pointing to the about-to-be-executed line. Issue the continue command to tell command gdb to continue execution past the breakpoint: (gdb) continue gdb continues past the breakpoint at the beginning of main(), and execution is paused at a call of scanf(). Enter 8 as the first number. Execution is paused at the second call of scanf(). Enter 12 as the second number. gdb is paused at the beginning of IntMath_gcd(). Then issue another continue command: (gdb) continue Note that gdb is paused, again, at the beginning of IntMath_gcd(). (Recall the IntMath_gcd() is called twice: once by main(), and once by IntMath_lcm().) While paused at a breakpoint, issue the kill command to stop execution: (gdb) kill Type y to confirm that you want gdb to stop execution. Issue the clear command to get rid of a breakpoint: (gdb) clear IntMath_gcd At this point only one breakpoint remains: the one at the beginning of main(). Stepping Through the Program Run the program again: Execution pauses at the beginning of main(). Issue the next command to execute the next line of your program: Page 3 of 6

(gdb) next Continue issuing the next command repeatedly until the program ends. Run the program again: Execution pauses at the beginning of main(). Issue the step command to execute the next line of your program: (gdb) step Continue issuing the step command repeatedly until the program ends. Is the difference between next and step clear? The next command tells gdb to execute the next line, while staying at the same function call level. In contrast, the step command tells gdb to step into a called function. Examining Variables Set a breakpoint at the beginning of IntMath_gcd(): (gdb) break IntMath_gcd Run the program until execution reaches that breakpoint: (gdb) continue Now issue the print command to examine the values of the parameters of IntMath_gcd(): (gdb) print ifirst (gdb) print isecond In general, when paused at a breakpoint you can issue the print command to examine the value of any expression containing variables that are in scope. Examining the Call Stack While paused at IntMath_gcd(), issue the where command: (gdb) where Page 4 of 6

In response, gdb displays a call stack trace. Reading the output from bottom to top gives you a trace from a specific line of the main() function, through specific lines of intermediate functions, to the about-to-be-executed line. The where command is particularly useful when your program is crashing via a segmentation fault error at runtime. When that occurs, try to make the error occur within gdb. Then, after the program has crashed, issue the where command. Doing so will give you a good idea of which line of your code is causing the error. Quitting gdb Issue the quit command to quit gdb: (gdb) quit Then, as usual, type: <Ctrl-x> <Ctrl-c> to exit emacs. Command Abbreviations The most commonly used gdb commands have one-letter abbreviations (r, b, c, n, s, p). Also, pressing the Enter key without typing a command tells gdb to reissue the previous command. Page 5 of 6

Part 2: Reference gdb [-d sourcefiledir] [-d sourcefiledir]... program [corefile] ESC x gdb [-d sourcefiledir] [-d sourcefiledir]... program Run gdb from a shell Run gdb within Emacs Miscellaneous quit directory [dir1] [dir2]... help [cmd] Exit gdb. Add directories dir1, dir2,... to the list of directories searched for source files, or clear the directory list. Print a description of command cmd. Running the Program run [arg1],[arg2] Run the program with command-line arguments arg1, arg2,... set args arg1 arg2... Set the program's command-line arguments to arg1, arg2,... show args Print the program's command-line arguments. Using Breakpoints info breakpoints break [file:]linenum break [file:]fn condition bpnum expr commands [bpnum] cmds continue kill delete [bpnum1][,bpnum2]... clear [[file:]linenum] clear [[file:]fn] disable [bpnum1][,bpnum2]... enable [bpnum1][,bpnum2]... Print a list of all breakpoints. Set a breakpoint at line linenum in file file. Set a breakpoint at the beginning of function fn in file file. Break at breakpoint bpnum only if expression expr is non-zero (TRUE). Execute commands cmds whenever breakpoint bpnum is hit. Continue executing the program. Stop executing the program. Delete breakpoints bpnum1, bpnum2,..., or all breakpoints. Clear the breakpoint at linenum in file file, or the current breakpoint. Clear the breakpoint at the beginning of function fn in file file, or the current breakpoint. Disable breakpoints bpnum1, bpnum2,..., or all breakpoints. Enable breakpoints bpnum1, bpnum2,..., or all breakpoints. Stepping through the Program next step finish "Step over" the next line of the program. "Step into" the next line of the program. "Step out" of the current function. Examining Variables print expr Print the value of expression expr. print ['file'::]var Print the value of variable var as defined in file file. (file is used to resolve static variables.) print [function::]var Print the value of variable var as defined in function function. (Function is used to resolve static variables.) printf format, expr1, expr2, Print the values expressions expr1, expr2,... using the specified format string. whatis var Print the type of variable var. ptype t Print the definition of type t. info display Print the display list. display expr At each break, print the value of expression expr. undisplay displaynum Remove displaynum from the display list. Examining the Call Stack where frame up down Print the call stack. Print the top of the call stack. Move the context toward the bottom of the call stack. Move the context toward the top of the call stack. Working with Signals info signals handle sig action1 [action2...] signal sig Print a list of all signals that the operating system makes available. When GDB receives signal sig, it should perform actions action1, action2,... Valid actions are nostop, stop, print, noprint, pass, and nopass. Send the program signal sig. Copyright 2016 by Robert M. Dondero, Jr. Page 6 of 6