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

Similar documents
CSE 374 Programming Concepts & Tools

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

CSE 351. GDB Introduction

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

Programming Studio #9 ECE 190

CS/COE 0449 term 2174 Lab 5: gdb

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

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

Source level debugging. October 18, 2016

Problem Set 1: Unix Commands 1

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

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

Exercise Session 6 Computer Architecture and Systems Programming

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.

Lecture 07 Debugging Programs with GDB

The Dynamic Debugger gdb

CS354 gdb Tutorial Written by Chris Feilbach

Unit 10. Linux Operating System

CS 11 C track: lecture 6

Basic functions of a debugger

18-600: Recitation #3

Debugging with GDB and DDT

GDB cheatsheet - page 1

Application Note: AN00193 Getting Started with Debugging in xtimecomposer

ALD Assembly Language Debugger Copyright (C) Patrick Alken

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Debugging in AnyLogic. Nathaniel Osgood CMPT

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

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

Reviewing gcc, make, gdb, and Linux Editors 1

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:

Using the Command Line

Red Hat Developer Tools

Debugging Techniques. CEFET Engineering Week

Advanced Magic - GDB

CSE 410: Systems Programming

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

Introducing LLDB for Linux on Arm and AArch64. Omair Javaid

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

Code::Blocks Student Manual

EE 355 Lab 3 - Algorithms & Control Structures

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

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

1 Basic functions of a debugger

Code Review and Debugging (Lab 05)

Using gdb to find the point of failure

CS201 Lecture 2 GDB, The C Library

Lecture # 1. SPIM & MIPS Programming

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

Code::Blocks Student Manual

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Undefined Behaviour in C

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

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

1 A Brief Introduction To GDB

Homework & Debugging Tips

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

Eclipse-Based CodeWarrior Debugger

The CS-220 Development Environment

Debugging with GDB and DDT

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

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

Unit 13. Linux Operating System Debugging Programs

AMD gdebugger 6.2 for Linux

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

Class Information ANNOUCEMENTS

SPIM & MIPS Programming

NSIGHT ECLIPSE EDITION

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

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

Programming Concepts: IDEs, Debug. Paulo Penteado. (

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

Threading Support for Byebug

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Eclipse Debug Find Current Instruction Pointer

CSE 374 Final Exam Sample Solution 3/17/11

Red Hat Developer Tools

Programming Tips for CS758/858

Libgdb. Version 0.3 Oct Thomas Lord

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

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

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

CMPSC 311- Introduction to Systems Programming Module: Debugging

Today s presentation. Git gdb Project 1

{C} Tools of the Trade

Programming in C and C++

INSTALLING INSTALLING INSTALLING

Making things work as expected

RECURSIVE FUNCTIONS ON STACK

Chapter 1 Getting Started

A Tutorial for ECE 175

Using the Xcode Debugger

CS2141 Software Development using C/C++ Debugging

Laboratorio di Tecnologie dell'informazione

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

COSC 6374 Parallel Computation. Debugging MPI applications. Edgar Gabriel. Spring 2008

CMPSC 311- Introduction to Systems Programming Module: Debugging

CIS 190: C/C++ Programming. Lecture 3 Memory Management in C

Program Design: Using the Debugger

Transcription:

Using a debugger Segmentation fault? GDB to the rescue!

But first... Let's talk about the quiz Let's talk about the previous homework assignment Let's talk about the current homework assignment

K findkey(v value) We want to use recursion for this We can't tell which subtree the value can be found in We need a way for a function to possibly return a value to us Many ways you can do this, but I suggest the following...

K findkey(v value) { K answer; findkeyhelper(root, value, answer); return answer; } void findkeyhelper(node<k,v> *node, V value, K& answer) { if (!node) { return; } if......... answer = // something, in some cases

Using a debugger What is a debugger? What debuggers exist? What debugger will we be using? How do we start up our debugger? How does our debugger get triggered? What can we do when we're in our debugger?

What is a debugger? A debugger is a programming tool that lets us inspect the state of our program Some development environments have debuggers integrated with them (Visual Studio, CLion, Xcode, Eclipse, etc.) Debuggers allow us to pause program execution, examine the contents of memory and variables as the program is running, and see what code is currently running Some debuggers will allow you to modify memory to test things out

What debuggers exist? On Windows, the most commonly used debugger is probably the one that comes with Visual Studio On Windows, Mac, Linux, and countless others, you can use gdb as a command-line debugger On Mac (and partially on Linux), you can use lldb similarly to gdb

What debugger will we be using? We will be talking about gdb It runs almost everywhere It's free Most tasks you'd try to perform with gdb are supported by other debuggers Using CodeBlocks, Cloud9, Linux, or ada, gdb will run directly (or with a nicer interface) If you're a Mac user, Xcode doesn't come with gdb, and you will need to use lldb instead

How do we start up our debugger? To use gdb to run a program, we type gdb and then the program we want to debug For the most useful results, we need to compile with -g We will then be taken from the bash command prompt to the gdb prompt All of our use of gdb will start with this prompt We need to tell gdb to Run our program with the 'r' command We can tell gdb to Quit with the 'q' command (or with ^D)

How does our debugger get triggered? Our debugger gets triggered when some exceptional circumstance triggers it These include: Segmentation fault (null or invalid pointer dereference) Infinite recursion (technically a segmentation fault) Control+C ( ^C ) abort() assert() [actually calls abort() under the hood] Integer division by zero

What can we do when we're in our debugger? backtrace (or bt) print a list of the functions currently running and which line of code is currently running in each frame n switch the current frame to the selected one info locals print out all local variables print expression prints out the result of executing the specified code

"Wait, my problem disappears when debugging it!" You probably have a non-initialized variable

Breakpoints A breakpoint is a specific instruction where we want the program to pause for us to take a look around We can create a breakpoint using the break command break filename:linenumber This will create a breakpoint at the specified line of code in the given file If wanted, the filename (and the colon) can be omitted in the case where there's a single file

Breakpoints To list your current breakpoints, you can type info break When you hit a breakpoint, you can type continue to continue execution To delete a breakpoint, you can type delete break number to delete the specified breakpoint To make an expression displayed every time your program stops, type display expression To see what expressions you have displaying, type info display To remove an expression, type undisplay number

Breakpoints When we are stuck with our program stopped (but not broken due to an error) we have several commands to use: continue Resume our program running, going until the next breakpoint or error next instruction Tell our program to move forward until the next line of code in our current stack frame (i.e., our current function) step to the next line Run the program until it reaches the next line of code

Helping the compiler help you Your compiler (g++ or clang++) can help you avoid some of these problems: -Wall Warn about all major easily detectable issues in your code -Wpedantic Warn about absolutely everything that could be an issue Some compilers are more helpful: With clang++: -fsanitize=address At runtime, create an error when using invalid memory -fsanitize=undefined At runtime, create an error when your code relies on behavior that C++ says is undefined