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

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

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

CSE 351. GDB Introduction

GDB QUICK REFERENCE GDB Version 4

Source level debugging. October 18, 2016

ALD Assembly Language Debugger Copyright (C) Patrick Alken

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

18-600: Recitation #3

Recitation: Bomb Lab. September 17 th 2018

GDB cheatsheet - page 1

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

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

CS354 gdb Tutorial Written by Chris Feilbach

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

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

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

CS/COE 0449 term 2174 Lab 5: gdb

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

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

Using gdb to find the point of failure

Assembly Language Programming - III

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

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

Unix and C Program Development SEEM

Exercise Session 6 Computer Architecture and Systems Programming

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

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

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

Migrating from Inspect to Native Inspect

Reviewing gcc, make, gdb, and Linux Editors 1

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

Program Design: Using the Debugger

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

Problem Set 1: Unix Commands 1

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

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

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

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

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

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

Using the GNU Debugger

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

Using the GNU Debugger

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

CSE 374 Programming Concepts & Tools

Should you know scanf and printf?

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

bash, part 3 Chris GauthierDickey

Systems/DBG Debugger Version 2.20

Libgdb. Version 0.3 Oct Thomas Lord

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

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

Basic functions of a debugger

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

Lecture 03 Bits, Bytes and Data Types

CS3210: Tutorial Session 2. Kyuhong Park-- edited by Kyle Harrigan

CSE 361S Intro to Systems Software Lab Assignment #4

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

Dynamic Memory Allocation and Command-line Arguments

LAB WORK NO. 3 TURBO DEBUGGER ENVIRONMENT

Advanced Magic - GDB

C Program Development and Debugging under Unix SEEM 3460

CMPSC 311- Introduction to Systems Programming Module: Debugging

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

Exploits and gdb. Tutorial 5

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

CMPSC 311- Introduction to Systems Programming Module: Debugging

Computer Organization & Assembly Language Programming (CSE 2312)

EL2310 Scientific Programming

gdbtui - Linux Command

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

Data and File Structures Laboratory

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

Debugging with GDB and DDT

Download the tarball for this session. It will include the following files:

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

The First Real Bug. gdb. Computer Organization I McQuain

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

Lab 03 - x86-64: atoi

GDB Tutorial. University of Waterloo

Processes and Exceptions

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

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

CS201 Lecture 2 GDB, The C Library

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

DS-5 ARM. Debugger Command Reference. Version Copyright ARM. All rights reserved. ARM DUI 0452P (ID091713)

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

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

The Dynamic Debugger gdb

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

Intro to MS Visual C++ Debugging

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

EXPERIMENT 1. FAMILIARITY WITH DEBUG, x86 REGISTERS and MACHINE INSTRUCTIONS

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

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

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

Transcription:

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference for x86-64 Assembly Language Part 1: Tutorial Motivation Suppose you're developing the power.s program. Further suppose that the program assembles and links cleanly, but is producing incorrect results at runtime. What can you do to debug the program? One approach is temporarily to insert calls of printf(...) throughout the code to get a sense of the flow of control and the values of variables at critical points. That's fine, but often is inconvenient. It is especially inconvenient in assembly language: the calls of printf() will change the values of registers, and thus may corrupt the very data that you wish to view. An alternative is to use gdb. gdb allows you to set breakpoints in your code, step through your executing program one line at a time, examine the contents of registers and memory at breakpoints, etc. Building for gdb To prepare to use gdb, build the program with gcc217 using the -g option: $ gcc217 -g power.s -o power Running GDB The next step is to run gdb. You can run gdb directly from the shell. But it's much handier 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> power <Enter key> At this point you are executing gdb from within Emacs. gdb is displaying its (gdb) prompt. Page 1 of 5

Running Your Program Issue the run command to run the program: gdb runs the program to completion, indicating that the "Program exited normally." (gdb also displays the cryptic message "Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.1.x86_64". That message is innocuous; ignore it.) Incidentally and importantly, command-line arguments and file redirection can be specified as part of the run command. For example the command run 1 2 3 runs the program with command-line arguments 1, 2, and 3, and the command run < myfile runs the program with its stdin redirected to myfile. Using Breakpoints Set a breakpoint near the beginning of the main() function using the break command: (gdb) break main Run the program: gdb pauses execution at the beginning of the main() function. It opens a second window in which it displays your source code, with the about-to-be-executed line of code highlighted. 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 executes the program to completion. Stepping Through the Program Run the program again: Execution pauses at the beginning of the main() function. Issue the next command to execute the next instruction of your program: Page 2 of 5

(gdb) next Continue issuing the next command repeatedly until the next instruction to be executed is call printf. The step command is the same as the next command, except that it commands gdb to step into a called function which you have defined. The step command will not cause gdb to step into a standard C function. Incidentally, the stepi (step instruction) command will cause gdb to step into any function, including a standard C function. Examining Registers Issue the info registers command to examine the contents of the registers: (gdb) info registers Issue the print command to examine the contents of any given register. Some examples: (gdb) print/d $rsi (gdb) print/a $rdi (gdb) print/d $eax Print as a decimal integer the 8 bytes which are the contents of register RSI Print as a hexadecimal address the 8 bytes which are the contents of register RDI Print as a decimal integer the 4 bytes which are the contents of register EAX Note that you must precede the name of the register with $ rather than %. Examining Memory Issue the x command to examine the contents of memory at any given address. Some examples: (gdb) x/d &lbase (gdb) x/gd &lbase (gdb) x/c &cresult (gdb) x/s &cresult (gdb) x/s $rdi Examine as a decimal integer the 4 bytes of memory at lbase (not really meaningful) Examine as a "giant" decimal integer the 8 bytes of memory at lbase Examine as a char the 1 byte of memory at cresult Examine as a string the bytes in memory at cresult Examine as a string the bytes of memory at the address contained in register RDI Quitting GDB Issue the quit command to quit gdb: Page 3 of 5

(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 4 of 5

Part 2: Reference gcc217... -o program gdb [-d sourcefiledir] [-d sourcefiledir]... program [corefile] ESC x gdb [-d sourcefiledir] [-d sourcefiledir]... program [corefile] Assemble and link with debugging information Run gdb from a shell Run gdb from 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 command cmd Running the Program run [arg1],[arg2] Run the program with command-line arguments arg1, arg2,... set args arg1 arg2... Set program's the command-line arguments to arg1, arg2,... show args Print the program's command-line arguments. Using Breakpoints info breakpoints break label break fn condition bpnum expr commands [bpnum] cmd1 cmd2... continue kill delete [bpnum1][,bpnum2]... clear [*addr] clear [fn] disable [bpnum1][,bpnum2]... enable [bpnum1][,bpnum2]... Stepping through the Program next step finish Examining Registers and Memory info registers print/f $reg x/rsf addr x/rsf $reg info display display/f $reg display/si addr display/ss addr undisplay displaynum Examining the Call Stack where backtrace frame up down Print a list of all breakpoints. Set a breakpoint at the memory address denoted by label. Set a breakpoint at the third instruction of function fn. Break at breakpoint bpnum only if expression expr is non-zero (TRUE). Execute commands cmd1, cmd2,... whenever breakpoint bpnum (or the current breakpoint) is hit. Continue executing the program. Stop executing the program. Delete breakpoints bpnum1, bpnum2,..., or all breakpoints. Clear the breakpoint at memory address addr, or the current breakpoint. Clear the breakpoint at function fn, or the current breakpoint. Disable breakpoints bpnum1, bpnum2,..., or all breakpoints. Enable breakpoints bpnum1, bpnum2,..., or all breakpoints. "Step over" the next instruction. "Step into" the next instruction. "Step out" of the current function. Print the contents of all registers. Print the contents of register reg using format f. The format can be x (hexadecimal), d (decimal), u (unsigned decimal), o (octal), a (address), c (character), or f (floating point). Examine the contents of memory at address addr using repeat count r, size s, and format f. The repeat count is optional; it defaults to 1. The size is optional; it can be b (1 byte), h (2 bytes), w (4 bytes), or g (8 bytes). The format can be x (hexadecimal), d (decimal), u (unsigned decimal), o (octal), a (address), c (character), f (floating point), s (string), or i (instruction). Examine the contents of memory at the address contained in register reg. Print the display list. At each break, print the contents of register reg using format f (as with a print command). At each break, print the contents of memory at address addr using size s (as with an x command). At each break, print the string of size s that begins in memory at address addr (as with an x command). Remove displaynum from the display list Print the call stack. 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 Copyright 2015 by Robert M. Dondero, Jr. Page 5 of 5