Reviewing gcc, make, gdb, and Linux Editors 1

Similar documents
CS354 gdb Tutorial Written by Chris Feilbach

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

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

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

DAY 4. CS3600, Northeastern University. Alan Mislove

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

CSE 374 Programming Concepts & Tools

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CSE 351. GDB Introduction

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

CS/COE 0449 term 2174 Lab 5: gdb

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

Problem Set 1: Unix Commands 1

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

Understanding the Program Run

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

Laboratory 1 Semester 1 11/12

Programming Tips for CS758/858

Intermediate Programming, Spring 2017*

2 Compiling a C program

Exercise 1: Basic Tools

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

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

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

C / C++ Coding Rules

PRINCIPLES OF OPERATING SYSTEMS

{C} Tools of the Trade

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.

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

Exercise Session 6 Computer Architecture and Systems Programming

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

Chris' Makefile Tutorial

CSE 374 Programming Concepts & Tools

Laboratory Assignment #4 Debugging in Eclipse CDT 1

CS11 Advanced C++ Fall Lecture 4

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

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

Programming Studio #9 ECE 190

The CS-220 Development Environment

Basic functions of a debugger

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

Introduction: The Unix shell and C programming

UNIX Makefile. C Project Library Distribution and Installation.

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

Lecture 07 Debugging Programs with GDB

COMP Lecture Notes The Compiler

How to learn C? CSCI [4 6]730: A C Refresher or Introduction. Diving In: A Simple C Program 1-hello-word.c

CSE 303: Concepts and Tools for Software Development

ECE 3210 Laboratory 1: Develop an Assembly Program

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

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

The Linux Programming Environment. Computer Science Department Texas State 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:

CSE 374 Final Exam 3/15/17. Name UW ID#

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Debugging and Version control

Software Engineering /48

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

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

Starting to Program in C++ (Basics & I/O)

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

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Makefiles SE 2XA3. Term I, 2018/19

Programming in C week 1 meeting Tiina Niklander

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.

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

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

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

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

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

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

Exercise Session 2 Systems Programming and Computer Architecture

CSE 410: Systems Programming

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

CSE 333 Lecture 6 - data structures

GNU Make 1. 1 material adapted from GNU Make by Richard Stallman

Maemo Diablo GNU Make and makefiles Training Material

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

Topic 6: A Quick Intro To C

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Using the Unix system. UNIX Introduction

Computers and Computation. The Modern Computer. The Operating System. The Operating System

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

independent compilation and Make

Programming in C S c o t t S c h r e m m e r

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

COSC345 Software Engineering. Make

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

Scientific Programming in C IX. Debugging

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

Programming in C First meeting

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Programming in C in a UNIX environment

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

CMPSC 311- Introduction to Systems Programming Module: Debugging

Recitation: Cache Lab & C

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

Compiling Code using Makefiles and GNUstep. 2501ICT Nathan

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

Using GNU make C HAPTER 4

Transcription:

Reviewing gcc, make, gdb, and Linux Editors 1 Colin Gordon csgordon@cs.washington.edu University of Washington CSE333 Section 1, 3/31/11 1 Lots of material borrowed from 351/303 slides Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 1 / 23

Today s Topics gcc: compilation, linking make: simple makefiles gdb: breakpoints, inspecting state Linux Editors: brief overview Ask questions any time! Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 2 / 23

Basic GCC The simplest way to use gcc is: gcc -o program file1.c file2.c... This creates the executable program in the current directory. Omitting the -o option generates a.out. You ll probably also want debugging information and to be warned about dangerous things you might have done: gcc -g -Wall -o program file1.c file2.c... Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 3 / 23

More Flexible GCC Sometimes we only want to build part of the program: gcc -g -Wall -c module1.c generates module1.o (an object file). An object file is compiled code with unresolved references. For example, if module1 uses printf, there will be an unresolved reference to printf in the object file. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 4 / 23

Linking There is a tool called the linker that puts object files together. This includes resolving references: Linking an object file with an unresolved reference to foo() to an object file with the code for foo() resolves the reference. The linker program is called ld, but you ll almost always just have gcc invoke the linker for you. gcc -g -Wall -c module1.c gcc -g -Wall -c module2.c gcc -g -Wall -c main.c gcc -o program main.o module1.o module2.o gcc implicitly links against the standard C libraries. Great, but why did we bother doing this? Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 5 / 23

Why Object Files and Linkers? Linking object files has two big advantages: Can ship code around without source or building an executable Usually done as a library, which is a slighly souped-up object file Can (re)compile part of your program without compiling all of it Recompile only what has changed, avoid wasting time recompiling unaffected code Compiling just the core parts of Linux or Solaris takes 8+ hours; compiling all of Windows TM takes weeks But there are disadvantages, too: It is tedious to re-type these commands as needed It is easy to make mistakes about which parts of your program need to be recompiled. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 6 / 23

Introducing make make is a tool that does all of this tedious work for you. All you do is: Write a makefile Specify what depends on what. For example: An object file depends on its C source and the headers that source uses The program depends on all the object files Specify how to generate a file given what it depends on e.g. how to generate an object file from C files and headers Run the make command and tell it what final result you would like. Something to generate is called a target. Something processed to produce a target is called a source. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 7 / 23

Format of a Makefile target: source1 source2... command1 command2... queue.o: queue.c queue.h gcc -g -Wall -c queue.c Command lines must start with a TAB character, not spaces. You can split a long command across multiple lines by putting \ at the end of the first line. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 8 / 23

Running make On the command line: make target Looks for Makefile in the current directory, can be overridden with -f file If no target specified, uses first target in file Make decides what to do based on the dependency graph and file modification times. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 9 / 23

Standard make Targets all: make everything all: client server clean: remove generated files, start over with just source clean: rm -f *.o client server These are called phony targets, because they never exist on disk. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 10 / 23

Advanced Makefiles How many times do you want to write all the arguments to gcc? How many places do you want to update arguments to gcc? Variables / Macros make allows variables to hold common expressions. For example: CC = gcc CFLAGS = -g -Wall queue.o: queue.c queue.h $(CC) $(CFLAGS) -c queue.c Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 11 / 23

Built-in Macros and Patterns Built-in Macros $@ current target $^ all sources $< leftmost source Patterns %.o: %.c $(CC) $(CFLAGS) -c $< Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 12 / 23

Automatically Generating Dependencies We now have a tool to exploit dependencies for us, but it s still a hassle to write them down correctly. gcc -MM [src files] Useful variants like -M and -MG (man gcc) Automatically create makefile rules to generate object files Often run via a phony target: depend: $(SRC) $(CC) -M $^ >.depend Then include the result in your makefile: include.depend Also a tool called makedepend Read more if you re interested Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 13 / 23

GCC Flags -o file Writes result to file -c Stops compilation with an object file; no linking -g Outputs debugging information -On Uses optimization level n, for 0 n 3 -I dir Looks for header files in dir - an include directory -L dir Looks for libraries to link against in dir -l lib Link against the library lib -Wall Warn about anything questionable -Werror Treat all warnings as compilation errors Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 14 / 23

GDB gdb is the debugger accompanying gcc A text-mode debugger as an interactive shell, though GUI frontends exist Provides standard debugger functionality: Breakpoints Stepping over lines of code, into/out of functions Stack traces Print variables, heap structures Listing code Also has more advanced functionality, like data breakpoints, disassembling code... Pretty good built-in help system (e.g. help backtrace) Not very useful without -g flag (emit debugging info) to gcc Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 15 / 23

Finding a SEGFAULT #include<s t d i o. h> #include<s t d l i b. h> i n t main ( i n t argc, char argv [ ] ) { i n t i ; i n t t o t a l = 0; for ( i = 0; i <= argc ; i ++) { t o t a l += a t o i ( argv [ i ] ) ; } p r i n t f ( The t o t a l i s %d\n, t o t a l ) ; return 0; } This program should print the sum of all the numbers in its arguments. Instead: [csgordon@monarch:~/cse333]$ gcc -g sum.c -o sum [csgordon@monarch:~/cse333]$./sum 3 4 5 Segmentation fault [csgordon@monarch:~/cse333]$ Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 16 / 23

Debugging a Segmentation Fault First approach Stare at the code. Really hard. Works sometimes. Often doesn t work at all. Better approach Run gdb Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 17 / 23

Debugging with GDB I First, start gdb: [csgordon@monarch:~/cse333]$ gdb sum GNU gdb (GDB) Fedora (7.1-34.fc13)... Reading symbols from /homes/gws/csgordon/cse333/sum...done. (gdb) Now run the program with arguments: (gdb) run 3 4 5 Starting program: /homes/gws/csgordon/cse333/sum 3 4 5 Program received signal SIGSEGV, Segmentation fault. 0x4debfc0c in strtol_l_internal () from /lib/libc.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.12.2-1.i686 (gdb) Now we ve reproduced the bug in the debugger... Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 18 / 23

Debugging with GDB II Now we need to find out where we are (gdb) backtrace #0 0x4debfc0c in strtol_l_internal () from /lib/libc.so.6 #1 0x4debf970 in strtol () from /lib/libc.so.6 #2 0x4debc2c1 in atoi () from /lib/libc.so.6 #3 0x08048423 in main (argc=4, argv=0xbffff7e4) at sum.c:7 (gdb) The library probably shouldn t be dereferencing a bad pointer unless we re providing bad input. It looks like we re calling the library at line 7, let s look at that: (gdb) up 3 #3 0x08048423 in main (argc=4, argv=0xbffff7e4) at sum.c:7 7 total += atoi(argv[i]); (gdb) Can also do up with no argument for moving by one frame There is also down to go in the other direction Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 19 / 23

Debugging with GDB III What s wrong with our call?... #3 0x08048423 in main (argc=4, argv=0xbffff7e4) at sum.c:7 7 total += atoi(argv[i]); (gdb) print i $1 = 4 (gdb) p argv[i] $2 = 0x0 (gdb) quit Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 20 / 23

Breakpoints [csgordon@monarch:~/cse333]$ gdb sum... (gdb) break sum.c:7 Breakpoint 1 at 0x804840f: file sum.c, line 7. (gdb) r 3 4 5 Starting program: /homes/gws/csgordon/cse333/sum 3 4 5 Breakpoint 1, main (argc=4, argv=0xbffff7f4) at sum.c:7 7 total += atoi(argv[i]); (gdb) p i $1 = 0 (gdb) p argv[i] $2 = 0xbffff991 "/homes/gws/csgordon/cse333/sum" (gdb) c Continuing. Breakpoint 1, main (argc=4, argv=0xbffff7f4) at sum.c:7 7 total += atoi(argv[i]); (gdb) p i $3 = 1 (gdb) p argv[i] $4 = 0xbffff9b0 "3" (gdb) q [csgordon@monarch:~/cse333]$ Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 21 / 23

GDB Cheat Sheet Abbr. Command Result r run args Runs program from the start with args b break file:n Sets a breakpoint on line n of file b break fn Sets a breakpoint at start of fn b break file:fn Sets a breakpoint at start of fn in file d delete breakpoint Delete breakpoint breakpoint, which can be a file and line number or a breakpoint number info breakpoints List current breakpoints info locals List local variables info variables List local & global variables c continue Continues execution to next breakpoint n next Execute one statement and stop s step Step inside function l list Lists code: defaults to current code, takes optional location bt backtrace Show stack trace, with arguments w where Show stack trace, with arguments h help topic Get help on topic Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 22 / 23

Text Editors Linux has many great text editors. We don t care which you use, but here are a few options. gedit - like Notepad or TextEdit, but with syntax highlighting Eclipse - has a C/C++ mode emacs - probably seen it before, a very powerful text editor, which can integrate with build systems, version control, debuggers... vim - another powerful text editor with a more unusual interface All of these have syntax highlighting, which might make it easier to read code. We will post links to tutorials for the more complicated editors online. Colin Gordon (University of Washington) Section 1: gcc/make/gdb Review CSE333 - Spring 2011 23 / 23