Using gdb to find the point of failure

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

Program Design: Using the Debugger

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

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

GDB QUICK REFERENCE GDB Version 4

Exercise Session 6 Computer Architecture and Systems Programming

CS354 gdb Tutorial Written by Chris Feilbach

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

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 351. GDB Introduction

GDB cheatsheet - page 1

Understanding the Program Run

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

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

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

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

CMPSC 311- Introduction to Systems Programming Module: Debugging

CMPSC 311- Introduction to Systems Programming Module: Debugging

CS/COE 0449 term 2174 Lab 5: gdb

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

Problem Set 1: Unix Commands 1

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

Data and File Structures Laboratory

Scientific Programming in C IX. Debugging

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

Laboratory 1 Semester 1 11/12

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

Debugging. John Lockman Texas Advanced Computing Center

Debugging with GDB and DDT

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

18-600: Recitation #3

DAY 4. CS3600, Northeastern University. Alan Mislove

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

CSE 410: Systems Programming

CSE 374 Programming Concepts & Tools

CS2141 Software Development using C/C++ Debugging

Programming Tips for CS758/858

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

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

The Dynamic Debugger gdb

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.

Source level debugging. October 18, 2016

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

Basic functions of a debugger

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

Reviewing gcc, make, gdb, and Linux Editors 1

ALD Assembly Language Debugger Copyright (C) Patrick Alken

Hands-on Workshop on How To Debug Codes at the Institute

EE 355 Lab 3 - Algorithms & Control Structures

Development Environment & Linux Guide

CS2: Debugging in Java

Debugging with GDB and DDT

CS 11 C track: lecture 6

ECE 3210 Laboratory 1: Develop an Assembly Program

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

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

Debugging. P.Dagna, M.Cremonesi. May 2015

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

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

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:

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

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

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

Computer Labs: Debugging

CS201 Lecture 2 GDB, The C Library

Recitation: Bomb Lab. September 17 th 2018

The First Real Bug. gdb. Computer Organization I McQuain

CS 314 Principles of Programming Languages. Lecture 9

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

Using the GNU Debugger

Using the GNU Debugger

Migrating from Inspect to Native Inspect

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

1 Basic functions of a debugger

Chapter - 17 Debugging and Optimization. Practical C++ Programming Copyright 2003 O'Reilly and Associates Page 1

UNIX Makefile. C Project Library Distribution and Installation.

Section 1: Tools. Kaifei Chen, Luca Zuccarini. January 23, Make Motivation How... 2

Class Information ANNOUCEMENTS

Programming Studio #9 ECE 190

Advanced Magic - GDB

C Program Development and Debugging under Unix SEEM 3460

Debugging uclinux on Coldfire

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

Making things work as expected

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

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

Lecture 07 Debugging Programs with GDB

You can also start with both an executable program and a core file specified:

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

Introduction to GDB. Lezione 9 (taken from Owen HSU material)

The CS-220 Development Environment

SGI Altix Getting Correct Code Reiner Vogelsang SGI GmbH

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

Debugging process. The debugging process can be divided into four main steps: 1. Start your program, specifying anything that might affect its

buffer overflow exploitation

Today s presentation. Git gdb Project 1

Code Review and Debugging (Lab 05)

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Project #1: Tracing, System Calls, and Processes

DEBUGGING: OBSERVING AND TRACKING

Transcription:

gdb gdb is the GNU debugger on our CS machines. gdb is most effective when it is debugging a program that has debugging symbols linked in to it. With gcc and g++, this is accomplished using the -g option, for example, to compile prog.c and have the executable stored in testprog, use the following: gcc -Wall -g -o testprog prog.c Once you have an executable file (in this case testprog), you can use gdb to debug it. First you must launch the debugger. To launch the debugger you type gdb <executable> For the above program, this will be gdb testprog

Valgrind Valgrind is another suite of tools for debugging and profiling your programs. First compile your program using the -g option as above, then launch valgrind: valgrind [valgrind-options] [your-prog] [your-prog-options] For example, if you execute testprog using the command./testprog progdata1.txt you could use the following command valgrind --leak-check=yes./testprog progdata1.txt For more information about valgrind, refer to the man page: man valgrind

Using gdb to find the point of failure The gdb debugger is a handy tool for identifying the location at which a program failed. At the (gdb) prompt you will usually want to tell the debugger to halt the program when it reaches the start of the main() function. The b command is short for breakpoint and tells the debugger where to stop. After a function is entered, source code line numbers can be used to specify breakpoints.

Using gdb to find the point of failure (gdb) b main Breakpoint 1 at 0x804833b: file prog.c, line 11. To start the program use the run command: (gdb) run Starting program: /home/rlowe/cs1070/examples/ When the program reaches a breakpoint gdb will tell you and display the next line of code to be executed. Breakpoint 1, main () at prog.c:11 11 printf("val of ptr is %p \n", ptr);

Using gdb to find the point of failure Use the next command to execute a single source statement. The next command will treat a function call as a single statement and not single step into the function being called. If you want to single step through the function use the step command to step into it. The output of the printf() is intermixed with gdb's output and is shown in blue below. (gdb) next val of ptr is (nil) 13 *ptr = 99;

Using gdb to find the point of failure Saying next again will cause the program to execute the flawed assignment. gdb will show you the line that caused the error (line # 13). (gdb) next Program received signal SIGSEGV, Segmentation fault. 0x08048357 in main () at prog.c:13 13 *ptr = 99;

Using gdb to find the point of failure The where command can show you where the failure occurred (along with a complete function activation trace. (gdb) where #0 0x08048357 in main () at prog.c:13 #1 0x40049917 in libc_start_main () from /lib/libc.so.6 (gdb) To print the value of a variable use the print command. (gdb) print ptr $1 = (int *) 0x0 Attempting to print what ptr points to reaffirms what the problem is: (gdb) print *ptr Cannot access memory at address 0x0

Using gdb to find the point of failure Use the q (quit) command to terminate gdb (gdb) quit The program is running. Exit anyway? (y or n) y home/rlowe/cs2100/examples ==>

Common gdb commands Command Description help help topic-classes help command apropos search-word info args i args info breakpoints info break info break breakpointnumber info watchpoints List gdb command topics. List gdb command within class. Command description. Search for commands and command topics containing search-word. List program command line arguments Print info about breakpoints and watchpoints Print breakpoint numbers. Print info about specific breakpoint. Print info about watchpoints

Common gdb commands tbreak watch condition clear clear function clear line-number delete d delete breakpoint-number delete range disable breakpointnumber-or-range enable breakpoint-numberor-range Temporary break. Break once only. Break is then removed. See "break" above for options. Suspend processing when condition is met. i.e. x > 5 Delete breakpoints as identified by command option. Delete all breakpoints, watchpoints, or catchpoints. Delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges specified as arguments. Does not delete breakpoints. Just enables/disables them. Example: Show breakpoints: info break Disable: disable 2-9

Common gdb commands Break and Watch break funtion-name break line-number break ClassName::functionNam e break +offset break -offset break filename:function break filename:linenumber break line-number if condition Suspend program at specified function of line number. Set a breakpoint specified number of lines forward or back from the position at which execution stopped. Don't specify path, just the file name and function name. Don't specify path, just the file name and line number. break Directory/Path/filename.cpp:62 Where condition is an expression. i.e. x > 5 Suspend when boolean expression is true.

Common gdb commands enable breakpoint-number once continue c continue number finish Enables once Continue executing until next break point/watchpoint. Continue but ignore current breakpoint number times. Usefull for breakpoints within a loop. Continue to end of function. Line Execution step s step number-of-steps-toperform next n next number Step to next line of code. Will step into a function. Execute next line of code. Will not enter functions.

Common gdb commands until until line-number where Source Code list l list line-number list function list - list start#,end# list filename:function set listsize count show listsize Continue processing until you reacha aspecified line number. Also: function name, address, filename:function or filename:line-number. Shows current line number and which function you are in. List source code. Number of lines listed when list command given.

Common gdb commands Examine Variables print variable-name p variable-name p file-name::variable-name p 'file-name'::variable-name Start and Stop run r run command-linearguments run < infile > outfile continue c kill k quit q Print value stored in variable. Start program execution from the beginning of the program. The command break main will get you started. Also allows basic I/O redirection. Continue execution to next break point. Stop program execution. Exit GDB debugger.

gdb Quick reference guid run run args break function break linenum break if cond clear funct delete bnum disable bnum enable bnum condition bnum commands bnum cont -- run the program -- run program with command line args. -- set breakpoint at function entry -- set breakpoint at line -- set breakpoint; break if condition -- remove breakpoint at function entry -- delete breakpoint bnum -- disable breakpoint bnum -- enable breakpoint bnum -- set conditions for breakpoint bnum -- set commands for breakpoint bnum -- continue execution to next break point

gdb Quick reference guid next nexti step stepi print expr info dat list where kill quit -- step next source level statement or function -- step next machine instruction or function -- step next source level statement -- step next machine instruction -- print value of expression -- information about break, display, registers, functions, variables -- list ten source lines -- show call stack -- stop program execution -- exit gdb Note: pressing Enter repeats the last command.