DAY 4. CS3600, Northeastern University. Alan Mislove

Similar documents
Reviewing gcc, make, gdb, and Linux Editors 1

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

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

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

Dalhousie University CSCI 2132 Software Development Winter 2018 Lab 8, March 22

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

Scientific Programming in C IX. Debugging

Problem Set 1: Unix Commands 1

{C} Tools of the Trade

CSE 15L Winter Midterm :) Review

Using gdb to find the point of failure

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

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

[Software Development] Makefiles. Davide Balzarotti. Eurecom Sophia Antipolis, France

CS 11 C track: lecture 6

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

DAY 3. CS3600, Northeastern University. Alan Mislove

Laboratory 1 Semester 1 11/12

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

Programming Tips for CS758/858

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

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

Exercise 1: Basic Tools

CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Debugging with GDB and DDT

GDB and Makefile. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to Supercomputing

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

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

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

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:

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.

18-600: Recitation #3

Recitation: C Review. TA s 20 Feb 2017

The make utility automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.

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

The make utility. Alark Joshi COMPSCI 253

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

Makefile Tutorial. Eric S. Missimer. December 6, 2013

Work relative to other classes

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

CS354 gdb Tutorial Written by Chris Feilbach

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

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

CSE 374 Programming Concepts & Tools

CS 107 Lecture 18: GCC and Make

Lecture 07 Debugging Programs with GDB

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

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

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

CS 361S - Network Security and Privacy Spring Project #2

CSE 351. GDB Introduction

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

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

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

CSE 303: Concepts and Tools for Software Development

PRINCIPLES OF OPERATING SYSTEMS

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

2 Compiling a C program

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

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

Make: a build automation tool

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

Make! CSC230: C and Software Tools. N.C. State Department of Computer Science. Some examples adapted from

Laboratory Assignment #4 Debugging in Eclipse CDT 1

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

CSE 333 Lecture 6 - data structures

CS Programming Languages Fall Homework #2

CS 361S - Network Security and Privacy Spring Project #2

CSE 124 Discussion (10/3) C/C++ Basics

PRACE Autumn School Basic Programming Models

Unix and C Program Development SEEM

Basic functions of a debugger

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

Review of Scientific Programming in C and Fortran. Michael McLennan Software Architect HUBzero Platform for Scientific Collaboration

UNIX Makefile. C Project Library Distribution and Installation.

Laboratory Assignment #3 Eclipse CDT

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

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

Data and File Structures Laboratory

Programming in C First meeting

Recitation: Cache Lab & C

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

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

Lecture 7: file I/O, more Unix

COMP Lecture Notes The Compiler

CS2141 Software Development using C/C++ Debugging

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Debugging with GDB and DDT

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

The CS-220 Development Environment

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

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

Understanding the Program Run

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

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

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

Transcription:

C BOOTCAMP DAY 4 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and the CS240 course at Purdue

C Debugging 2

Debugging with gdb GDB is a debugger that helps you debug your program Time you spend learning gdb will save you days of debugging time You need to compile with the -g option to use gdb The -g option adds debugging information to your program gcc -g -o hello hello.c Should be done automatically in all Makefiles we give you 3

Running gdb To run a program with gdb type gdb exename... (gdb) Then set a breakpoint in the main function Marker in your program that will make the program stop Return control back to gdb (gdb) break main Now run your program If your program has arguments, you can pass them after run (gdb) run arg1 arg2... argn 4

Stepping through Your program will start and will stop at main gdb> You have the following commands to run your program step by step (gdb) step It will run the next line of code and stop If it is a function call, it will enter into it (gdb) next It will run the next line of code and stop If it is a function call, it will go through it If the program is running without stopping, regain control CTRL-C 5

Setting breakpoints You can set breakpoints in a program in multiple ways: (gdb) break function Set a breakpoint in a function (gdb) break line Set a break point at a line number in the current file (gdb) break file:line Set a break point at a line number in a specific file 6

Inspecting the stack The command (gdb) where Prints the current function being executed And the chain of functions that are calling that function This is also called the backtrace Example: (gdb) where #0 main () at test_mystring.c:22 #1 test () at test_mystring.c:38 (gdb) 7

Inspecting variables To print the value of a variable (gdb) print variable Will automatically print char*s and arrays (gdb) print i $1 = 5 (gdb) print s1 $1 = 0x10740 "Hello" (gdb) print stack[2] $1 = 56 (gdb) print stack $2 = {0, 0, 56, 0, 0, 0, 0, 0, 0, 0} (gdb) 8

Catching seg faults If your program seg faults, gdb will catch it (gdb) run Starting program: /home/amislove/a.out test string Program received signal SIGSEGV, Segmentation fault. 0x4007fc13 in _IO_getline_info () from /lib/libc.so.6 (gdb) backtrace #0 0x4007fc13 in _IO_getline_info () from /lib/libc.so.6 #1 0x4007fb6c in _IO_getline () from /lib/libc.so.6 #2 0x4007ef51 in fgets () from /lib/libc.so.6 #3 0x80484b2 in main (argc=1, argv=0xbffffaf4) at segfault.c:10 #4 0x40037f5c in libc_start_main () from /lib/libc.so.6 9

Other C debugging tools Purify Checks code at runtime Looks for errors like buffer overflows, accessing unallocated memory Valgrind Tool to help find memory leaks Tracks allocation, tells you where memory allocated but never freed Shark, Performance Tools OS X has many tools built into Developer Tools 10

Using Makefiles 11

Makefiles make is an early precursor to ant Uses a Makefile, which holds the build instructions In this class, I ll give you the Makefile But, you may want/need to extend it Basic idea: Dependency graph make determines what requires what Builds graph Also determines what needs to be updated Based on file timestamps Executes commands, stops if error occurs 12

Makefile format Unfortunately, Makefiles have a somewhat archaic format target: [dependency1] [dependency2]... [dependencyn] command1 command2... commandn Basically, says target depends on targets dependency[1-n] And, if those exist, build target by executing command[1-n] Note that commands must be indented with <tab> characters Otherwise, you ll be debugging your Makefile 13

Makefile variables All variables are accessed with $(name) Defined with = Built-in variables include $(input) [$<], $(output) [$@], $(inputs) [$^] A number of built-in functions Use file wildcards with $(wildcard pattern) Remove/add suffixes with $(addsuffix suffix paths), $(basename paths) Can express patterns with the % character CC = gcc %.o: %.c! $(CC) -c $< -o $@ 14

Example Makefile CFILES = $(wildcard *.c) cp%: cp%.c gcc -std=c99 -O0 -g -lm -Wall -pedantic -Werror -o $@ $< all: $(basename $(CFILES)) test: all./test $(basename $(CFILES)) clean: rm $(basename $(CFILES)) 15

Debugging Makefiles Sometimes, make will use built-in rules E.g., compile C files with gcc Can disable these with make -r Sometimes, make doesn t do what you want Executes different commands than you expect Can debug with make -n Just prints commands to be executed 16

UNIX Shell 17

Shell environment Shell environment Consists of a set of variables with values Important for the shell and the programs that run from the shell You can define new variables, change the values Usually set up in.bashrc,.tshrc files Examples PATH determines where to look for executables SHELL indicates the type of shell you are using bash% echo $PATH /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin 18

Viewing/setting env variables bash% export FOO=BAR bash% echo $FOO BAR bash% unset FOO bash% echo $FOO bash% export declare -x CLICOLOR="1" declare -x COMMAND_MODE="unix2003" declare -x HOSTNAME="joshua"... 19

Configuration files When bash is executed, it reads and runs certain configuration files:.profile,.bash_profile: runs when you log in Contains one time initialization, like TERM, HOME etc.bashrc: run each time another bash process is invoked Sets lots of variables, like PATH, HISTORY etc Only modify the lines that you fully understand! Can cause very bad errors if not careful E.g., Adding the line logout to the.profile file is bad Will cause you to be logged out every time you log in Probably not what you want 20