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

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

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Exercise Session 6 Computer Architecture and Systems Programming

CSE 351. GDB Introduction

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

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

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.

Programming Studio #9 ECE 190

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

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

Computer Labs: Debugging

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

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

CSE 374 Programming Concepts & Tools

CSE 410: Systems Programming

DAY 4. CS3600, Northeastern University. Alan Mislove

CS 11 C track: lecture 6

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

Debugging with GDB and DDT

Today s presentation. Git gdb Project 1

CS354 gdb Tutorial Written by Chris Feilbach

Project 1 Notes and Demo

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:

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

Reviewing gcc, make, gdb, and Linux Editors 1

Computer Science II Lab 3 Testing and Debugging

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

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

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

Debugging with GDB and DDT

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

GDB QUICK REFERENCE GDB Version 4

Debugging. John Lockman Texas Advanced Computing Center

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

Basic functions of a debugger

CS/COE 0449 term 2174 Lab 5: gdb

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

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

Recitation 10: Malloc Lab

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

ECE 3210 Laboratory 1: Develop an Assembly Program

18-600: Recitation #3

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

Debugging with gdb and valgrind

CS 392/681 Lab 6 Experiencing Buffer Overflows and Format String Vulnerabilities

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

Problem Set 1: Unix Commands 1

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

Understanding the Program Run

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

Data and File Structures Laboratory

DEBUGGING: OBSERVING AND TRACKING

PRACE Autumn School Basic Programming Models

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

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

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

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

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

Debugging. Marcelo Ponce SciNet HPC Consortium University of Toronto. July 15, /41 Ontario HPC Summerschool 2016 Central Edition: Toronto

CS356: Discussion #5 Debugging with GDB. Marco Paolieri

Fall 2015 COMP Operating Systems. Lab #3

CMPSC 311- Introduction to Systems Programming Module: Debugging

Debugging Techniques. CEFET Engineering Week

SGI Altix Getting Correct Code Reiner Vogelsang SGI GmbH

Code Review and Debugging (Lab 05)

CMPSC 311- Introduction to Systems Programming Module: Debugging

CS201 Lecture 2 GDB, The C Library

Using gdb to find the point of failure

Outline. Writing Functions and Subs. Review Immediate (1-line) Errors. Quiz Two on Thursday (2/23) Same Code Without Option Explicit

Short Introduction to Debugging Tools on the Cray XC40

Source level debugging. October 18, 2016

Embedded Software TI2726 B. 3. C tools. Koen Langendoen. Embedded Software Group

CS 300 Leftovers. CS460 Pacific University 1

A short session with gdb verifies a few facts; the student has made notes of some observations:

buffer overflow exploitation

Assembly Language Programming Debugging programs

Debugging uclinux on Coldfire

Short Introduction to tools on the Cray XC system. Making it easier to port and optimise apps on the Cray XC30

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

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

The Perl Debugger. Avoiding Bugs with Warnings and Strict. Daniel Allen. Abstract

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

CS2141 Software Development using C/C++ Debugging

Program Design: Using the Debugger

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

CS102 Software Engineering Principles

Lecture 07 Debugging Programs with GDB

EE 355 Lab 3 - Algorithms & Control Structures

Debugging and Profiling

CSE 351: Week 4. Tom Bergan, TA

Using the GNU Debugger

ELEC 377 Operating Systems. Week 4 Lab 2 Tutorial

Computer Labs: Debugging

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

DDT: A visual, parallel debugger on Ra

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

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

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

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

Transcription:

Tips on Using GDB to Track Down and Stamp Out Software Bugs Brett Viren Physics Department MINOS Week In The Woods, 2005 Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 1 / 34

Outline 1 Compiling for Debugging 2 Running GDB 3 Interrogating and Exploring a Program in GDB 4 Controlling Program Execution 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 2 / 34

Compiling for Debugging 1 Compiling for Debugging Generic Compilation Compiling ROOT Compiling MinosSoft 2 Running GDB 3 Interrogating and Exploring a Program in GDB 4 Controlling Program Execution 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 3 / 34

Compiling for Debugging Generic Compilation Compiling code for easier debugging. GDB needs extra information in the object code in order to provide most of its features. This is added with GCC s -g flag. Note: shell$ g++ -ggdb [-O] file.cxx... Using -ggdb will provide the best information for GDB. Using -g alone will use the OS s native format (which can be equivalent). One can optionally still keep optimization on (-O2) but the generated code may be very confusing when investigated in GDB. Variables can be optimized away, branches can execute out-of-order, etc. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 4 / 34

Compiling for Debugging Compiling ROOT Compiling ROOT with Debugging Symbols ROOT s build system uses the ROOTBUILD environment variable to control whether or not debugging symbols are added to the libraries: shell$ make ROOTBUILD=debug The actual debug flags are just -g but can be changed by editing the DEBUGFLAGS variable at the top of root/config/makefile.linux. Debug and optimization are mutually exclusive in ROOT s build system, if both are desired, this file will need editing. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 5 / 34

Compiling for Debugging Compiling MinosSoft Compiling MinosSoft with Debugging Symbols By default MinosSoft s SRT will use debugging with just -g so there is nothing special needed to produce libraries with debugging symbols. To produce both optimized and debug libraries set the environment variable bash$ export SRT QUAL="debug maxopt" tcsh$ setenv SRT QUAL "debug maxopt" Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 6 / 34

Running GDB 1 Compiling for Debugging 2 Running GDB Staring the Process with GDB Using a core File Attaching to a Running Process Caveats 3 Interrogating and Exploring a Program in GDB 4 Controlling Program Execution 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 7 / 34

Running GDB Start the Process with GDB Staring the Process with GDB Two ways to pass in command line arguments, from inside GDB: shell$ gdb program (gdb) run arg1 arg2 Starting program: /path/to/program... or from the shell: shell$ gdb --args program arg1 arg2... (gdb) run Starting program: /path/to/program... Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 9 / 34

Running GDB Using a core File Postmortem with a core file A core file holds an image of the program s memory at the time of the crash. To be produced your environment must be properly set: bash$ ulimit -c unlimited tcsh$ limit coredumpsize unlimited GDB can attach to the core file along with the program like: shell$ gdb program core GDB will position itself at the point where execution crashed. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 10 / 34

Running GDB Attaching to a Running Process Connecting to a process that is already running This can be useful if you suspect the program is in an infinite loop, or catch the program after all libraries are loaded (but see below for other ways). shell$ gdb program PID Where PID is the running process s ID. GDB will halt the process and will position itself where the process was just executing. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 11 / 34

Running GDB Caveats Caveat About Running root with GDB The root executable is a small program that just handles a few things like printing a help message or showing the graphical ROOT splash screen. It calls exec to start the root.exe program which then does the heavy lifting. This exec can confuse GDB so when debugging ROOT, do: shell$ gdb root.exe... Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 12 / 34

Running GDB Caveats Caveat About Running GDB under tcsh GDB will run the program under your shell but only if the SHELL environment variable is correctly set. The tcsh shell does not always set this variable. It can be set like: tcsh$ setenv SHELL /usr/bin/tcsh If this variable is left unset, /bin/sh is used. Or better yet, switch to bash... Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 13 / 34

Interrogating and Exploring a Program in GDB 1 Compiling for Debugging 2 Running GDB 3 Interrogating and Exploring a Program in GDB Sample Program Examining the Execution Stack and Variable Values Traversing the Stack 4 Controlling Program Execution 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 14 / 34

Interrogating and Exploring a Program in GDB Simple Test Program Sample Program 1 void func2 ( i n t p ) 2 { 3 i n t i = p ; 4 } 5 void func1 ( i n t p ) 6 { 7 func2 ( p ) ; 8 } 9 void func0 ( i n t &p ) 10 { 11 p = new i n t ; 12 func1 ( p ) ; 13 d e l e t e p ; 14 p = 0 ; 15 } 16 i n t main ( i n t argc, char a r g v [ ] ) 17 { 18 i n t p=0; 19 func0 ( p ) ; 20 func1 ( p ) ; 21 r e t u r n 0 ; 22 } Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 16 / 34

Interrogating and Exploring a Program in GDB Where am I and how did I get here? Examining the Execution Stack and Variable Values When GDB runs a program and stops either due to a crash or an interupt it is positioned somewhere in the function call stack. You can see where with the backtrace (aka bt, where): (gdb) backtrace #0 0x0804838d in func2(int*) (p=0x0) at crash.cc:3 #1 0x080483a5 in func1(int*) (p=0x0) at crash.cc:7 #2 0x080483ca in main (argc=1, argv=0xbffffae4) at crash.cc:20 This shows main called func1 which called func2. The function argument types and values are printed as are their file and line numbers. If this info is missing it means the code was not compiled with debugging turned on. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 18 / 34

What is here? Interrogating and Exploring a Program in GDB Examining the Execution Stack and Variable Values Show the source code coresponding to the current execution point: (gdb) list 20 func1(p); 21 return 0; 22 } } Check values of variables: (gdb) print p $1 = (int *) 0x0 The type and value are printed. The $1 is now a variable that can be used later. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 20 / 34

Interrogating and Exploring a Program in GDB Going elsewhere. Traversing the Stack You can go up and down the call stack with the up and down commands. This helps get to the real root cause: (gdb) up #1 0x080483a5 in func1(int*) (p=0x0) at crash.cc:7 7 func2(p); (gdb) up #2 0x080483ca in main (argc=1, argv=0xbffffae4) at crash.cc:12 20 func1(p); We could also have jumped to that frame via frame 2 command. If the frame is not specified the current one will be printed. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 22 / 34

Controlling Program Execution 1 Compiling for Debugging 2 Running GDB 3 Interrogating and Exploring a Program in GDB 4 Controlling Program Execution Stopping Execution with Break Points Watch Points 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 23 / 34

Setting Break Points Controlling Program Execution Stopping Execution with Break Points Break points tell GDB to stop execution at some point in the program. Break when named function is run: (gdb) break func1 Breakpoint 1 at 0x804839a: file crash.cc, line 7. (gdb) run Breakpoint 1, func1(int*) (p=0x0) at crash.cc:7 7 func2(p); Break at file:line (gdb) break crash.cc:11 Breakpoint 2 at 0x8048384: file crash.cc, line 11. (gdb) run Breakpoint 2, func0(int*&) (p=@0xbffffa24) at crash.cc:11 11 p = new int; Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 25 / 34

Controlling Program Execution Stopping Execution with Break Points Caveats with Break Points There are some things to keep in mind about break points. 1 The code must be linked (see below) 2 When breaking in a class method, the full name, including arguments, must be used. It helps to put the partial name in single quotes and hit tab: (gdb) break JobCModule::An<TAB> (gdb) break JobCModule::Ana(MomNavigator const*) Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 26 / 34

Controlling Program Execution Watch Points Setting Watch Points Setting a watch point will cause GDB to halt execution when some expression changes. (gdb) break main Breakpoint 5 at 0x804850a: file crash.cc, line 18. (gdb) run Breakpoint 5, main (argc=1, argv=0xbffffa84) at crash.cc:18 18 int* p=0; (gdb) watch p Hardware watchpoint 6: (gdb) continue Continuing. Hardware watchpoint 6: p Old value = (int *) 0xbffffa84 New value = (int *) 0x8049830 func0(int*&) (p=@0xbffffa24) at crash.cc:12 12 func1(p); p Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 28 / 34

Controlling Program Execution Watch Points Watch Points Caveats Hardware Watchpoints are handled by the CPU directly and thus incur almost no slowdown. They are limited in number and in the amount of memory they can watch. Software Watchpoints are handled by GDB and don t have the limitation of those in hardware. However they will slow down the execution of the program by orders of magnitude. This makes them essentially unusable. Sometimes setting a watch point on a class member is difficult. Here is a tip from Mike Kordosky; (gdb) print fconst $40 = (double*) 0xdcf6260 (gdb) print &fconst $20 = (double**) 0xb960578 (gdb) awatch *0xdcf6260 (gdb) awatch *0xb960578 Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 30 / 34

Stepping Through the Execution Controlling Execution Once execution has been broken there are a number of ways to control it. step or s step to the next executable statement. This lets one step into a function call. next or n step to the next executable statement in current frame. This avoids stepping into a function call. continue or c continue to the next break or watch point or until the program execution is terminated. delete or d delete all break and watch points. Given an optional number, delete just that point. Hitting ENTER repeats the last command. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 31 / 34

Dealing with Dynamically Loaded Libraries 1 Compiling for Debugging 2 Running GDB 3 Interrogating and Exploring a Program in GDB 4 Controlling Program Execution 5 Stepping Through the Execution 6 Dealing with Dynamically Loaded Libraries Dynamically Loaded Libraries Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 32 / 34

Dealing with Dynamically Loaded Libraries Dynamically Loaded Libraries Handling Dynamically Loaded Libraries Before code in a library can be accessed by GDB, it must be linked into the running process. However, much of ROOT s and MinosSoft s libraries are linked after running. There are various ways to handle this: 1 Set a break in main, run and then break by file:line. 2 Set a break in main, run, break in JobCPath::Run and continue. At next stop, any libraries loaded in the job macro should be available. Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 33 / 34

For Further Reading I Appendix For Further Reading GDB manual. On the web at: http://www.gnu.org/software/gdb/documentation/ Two GUI GDB frontends: GVD, http://libre.act-europe.fr/gvd/ DDD, http://www.gnu.org/software/ddd/ For fun: TCSH considered harmfull, http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ Brett Viren (Brookhaven National Lab) Using GDB to Debug Ely 2005 34 / 34