<<

Carnegie Mellon

Programming in C & Living in

15-213: Introducon to Computer Systems Recitaon 6: Monday, Sept. 30, 2013 Marjorie Carlson Secon A

1 Carnegie Mellon Weekly Update

 Buffer Lab is due Tuesday (tomorrow), 11:59PM . This is another lab you don’t want to waste your late days on.

 Cache Lab is out Tuesday (tomorrow), 11:59 PM . Due Thursday October 10th . Cache Lab has two parts. 1. a 200- to 300-line C program that simulates the behavior of cache. (Let the coding in C begin!) 2. Opmize a small matrix transpose funcon in order to minimize the number of cache misses. . Next recitaon will address part 2. For part 1, pay close aenon in class this week. 

2 Carnegie Mellon Agenda

 Living in Unix  Programming in C . Beginner . Refresher . The Line . Compiling . Basic Commands . Hunng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . Commands . The Environment . Shell Scripng

3 Carnegie Mellon “In the Beginning was the Command Line…”

 Command Line Interface . “Provides a means of communicaon between a user and a computer that is based solely on textual input and output.”

 Shell . A shell is a program that reads and executes the commands entered on the command line. It provides “an interface between the user and the internal parts of the operang system ( the very core of is the kernel).” . The original is sh (the Bourne shell). . Many other versions exist: bash, csh, etc.

The Information Project: Shell The Linux Information Project: Command Line

4 Carnegie Mellon Unix – Beginner: Basic Commands Moving Around Manipulating Files List directory contents Move (rename) files Change working directory Copy files (and directories with “-r”) Display present working directory Remove files (or directories with “-r”) links between files/directories Concatenate and print files Make directories Change permission bits Working Remotely Looking Up Commands ssh Secure remote login program man Interface to online reference manuals sftp Secure remote file transfer program which Shows the full path of shell commands scp Secure remote file copy program files by name Managing Processes Other Important Commands Report current processes status Display a line of text Terminate a process Cause the shell to exit jobs Report current shell’s job status Display the command history list fg (bg) Run jobs in foreground (background) Show who is logged on the system 5 Carnegie Mellon Quick Aside: Using the rm Command

 rm ./filename – deletes file “filename” in current dir.  rm ./*ame – deletes all files in the current directory that end in “ame.”  rm –r ./* – deletes all files and directories inside the current directory.  rm -r ./directory – deletes all files inside the given directory and the directory itself.  rm –rvf /* – deletes the enre hard drive. DO NOT DO THIS!!! . sudo runs the command as root, allowing you to delete anything. . -v (verbose) flag will list all the files being deleted. . -f (force) flag prevents it from asking for confirmaon before deleng important files.

6 Carnegie Mellon Quick Aside: Secons

 man foo prints the manual page for the foo, where foo is any user command, system call, C library funcon, etc.

 However, some programs, ulies, and funcons have the same name, so you have to specify which secon you want (e.g., man 3 gets you the man page for the C library funcon prin, not the Unix command prin).

 How do you know which secon you want? . whatis foo . man –f foo . man –k foo lists all man pages menoning “foo.”

7 Carnegie Mellon Unix – Beginner: Pipes & Redirects

 A pipe (|) between two commands sends the first command’s output to the second command as its input.  A redirect (< or >) does basically the same thing, but with a file rather than a process as the input or output.

 Remember this slide?  . Opon 1: Pipes $ cat exploitfile | ./hex2raw | ./bufbomb -t andrewID

. Opon 2: Redirects $ ./hex2raw < exploitfile > exploit-rawfile $ ./bufbomb -t andrewID < exploit-rawfile

8 Carnegie Mellon Agenda

 Living in Unix  Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hunng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng

9 Carnegie Mellon Unix – Intermediate: More Commands Transforming Text Useful with Other Commands Remove sections from each line of screen Screen manager with terminal files (or redirected text) emulation Stream editor for filtering and sudo Execute a command as another user transforming text (typically root) Translate or delete characters Delay for a specified amount of Archiving Looking Up Commands zip Package and compress files Define or display aliases tar Tar file creation, extraction and export Exposes variables to the shell manipulation (setenv)* environment and its following commands Manipulating File Attributes Searching Files and File Content Change file timestamps (creates find Search for files in a directory hierarchy empty file, if nonexistent) Set file mode creation mask Print lines matching a pattern

* – Bash uses export. Csh uses setenv. 10 Carnegie Mellon Unix – Intermediate: Environment Variables

 Your shell’s environment is a set of variables, including informaon such as your username, your home directory, even your language. . lists all your environment variables. . echo $VARIABLENAME prints a specific one.  $PATH is a colon-delimited list of directories to search for executables.  Variables can be set in config files like .login or .bashrc, or by scripts, in which case the script must use export (bash) or setenv (csh) to export changes to the scope of the shell.

11 Carnegie Mellon Unix – Intermediate: Shell Scripng

 Shell scripng is a powerful tool that lets you integrate command line tools quickly and easily to solve complex problems.  Shell scripts are wrien in a very simple, interpreted language. The simplest shells scripts are simply a list of commands to execute.  Shell scripng syntax is not at all user-friendly, but shell scripng remains popular for its real power.  Teaching shell scripng is depth is beyond the scope of this course – for more informaon, check out Kesden’s old 15-123 lectures 3, 4 and 5.

12 Carnegie Mellon Unix – Intermediate: Shell Scripng

 To write a shell script: . Open your preferred editor. . #!/bin/sh. When you run the script, this will tell the OS that what follows is a shell script. . Write the relevant commands. . Save the file. (You may want its extension to be .sh.) . Right now it’s just a text file – not an executable. You need to chmod +x foo.sh it to make it executable. . Now run it as ./foo.sh!

13 Carnegie Mellon Unix – Intermediate: Shell Scripng

hello.sh hello.sh with variables #!/bin/sh #!/bin/sh

# Prints “Hello, world.” to STDOUT str=“Hello, world.” echo “Hello, world.” echo $str # Also prints “Hello, world.”

 Anything aer a ‘#’ is a comment.  Variables  Seng a variable takes the form ‘varName=VALUE’.  There CANNOT be any spaces to the le and right of the “=“.  Evaluang a variable takes the form “$varName”.  Shell scripng is syntaccally subtle. (Unquoted , “quoted strings”, ‘single-quoted strings’ and `back quotes` all mean different things!)

14 Carnegie Mellon Agenda

 Living in Unix  Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hunng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng

15 Carnegie Mellon C – Refresher

16 Carnegie Mellon C – Refresher: Things to Remember

 If you allocate it, free it. int *array= malloc(5 * sizeof(int)); … free(array);

 If you use Standard C Library funcons that involve pointers, make sure you know if you need to free it. (This will be relevant in proxylab.)

17 Carnegie Mellon C – Refresher: Things to Remember

 There is no String type. Strings are just NULL-terminated char arrays.

 Seng pointers to NULL aer freeing them is a good habit, so is checking if they are equal to NULL.

 Global variables are evil, but if you must use make sure you use extern where appropriate.

 Define funcons with prototypes, preferably in a header file, for simplicity and clarity.

18 Carnegie Mellon C – Refresher: Command Line Arguments

 Many C funcons take command-line arguments: $ gdb bomb $ ./makecookie marjorie $ ls –l /etc

 If you want to pass arguments on the command line to your C funcons, your main funcon’s parameters must be

int main(int argc, char **argv)

# of arguments the arguments, as an (always ≥ 1) array of char*s (strings)

19 Carnegie Mellon C – Refresher: Command Line Arguments int main(int argc, char **argv)

. argv[0] is always the name of the program. . The rest of the array are the arguments, parsed on space. $ ls –l /etc

program name arguments argv[0] argv[1], argv[2]

 argc = ?

20 Carnegie Mellon C – Refresher: Headers & Libraries

bits.h  Header files make your int bitNor(int, int); int test_bitNor(int, int); funcons available to other int isNotEqual(int, int); int test_isNotEqual(int, int); source files. int anyOddBit(); int test_anyOddBit();  Implementaon in .c, and int rotateLeft(int, int); int test_rotateLeft(int, int); declaraons in .h. int bitParity(int); int test_bitParity(int); . forward declaraons int tmin(); int test_tmin(); . struct prototypes int fitsBits(int, int); int test_fitsBits(int, int); . #defines int rempwr2(int, int); int test_rempwr2(int, int);  int addOK(int, int); It should be wrapped in int test_addOK(int, int); #ifndef LINKEDLIST_H int isNonZero(int); int test_isNonZero(int); #define LINKEDLIST_H int ilog2(int); int test_ilog2(int); ... unsigned float_half(unsigned); unsigned test_float_half(unsigned); #endif unsigned float_twice(unsigned); unsigned test_float_twice(unsignned);

21 Carnegie Mellon C – Refresher: Headers & Libraries

 #include <*.h> - Used for including header files found in the C include path: standard C libraries.

 #include “*.h” – Used for including header files in the same directory.

 There are command-line flags to control where header files are searched for, but you shouldn’t need to worry about that.

22 Carnegie Mellon C – Refresher: “Reverse” Demo

 reverse takes arguments from the command line and prints each of them backwards.  It calls reverse_strdup, which is in a different compilaon unit.

 To compile: gcc – reverse.c reverse_strdup.c –o reverse

23 Carnegie Mellon Agenda

 Living in Unix  Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hunng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng

24 Carnegie Mellon C – Compiling: Command Line gcc GNU project C and C++ compiler

 When compiling C code, all dependencies must be specified.  Remember how we just compiled? gcc –Wall reverse.c reverse_strdup.c –o reverse

 This will not compile: gcc –Wall reverse.c –o reverse

25 Carnegie Mellon C – Compiling: Command Line gcc GNU project C and C++ compiler

 gcc does not requires these flags, but they encourage people to write beer C code.

Useful Flags -Wall Enables all construction warnings -Wextra Enables even more warnings not enabled by Wall -Werror Treat all warnings as Errors -pedantic Issue all mandatory diagnostics listed in C standard -ansi Compiles code according to 1989 C standards -g Produces debug information (GDB uses this information) -O1 Optimize -O2 Optimize even more -o filename Names output binary file “filename”

26 Carnegie Mellon C – Compiling: Makefiles

Make GNU make utility to maintain groups of programs

 Projects can get very complicated very fast, and it can take a long me to have GCC recompile the whole project for a small change.

 Makefiles are designed to solve this problem; make recompiles only the parts of the project that have changed and links them to those that haven’t changed.

 Makefiles commonly separate compilaon (.c  .o) and linking (.o’s  executable).

27 Carnegie Mellon C – Compiling: Makefiles

Make GNU make utility to maintain groups of programs

 Makefiles consist of one or more rules in the following form.

Makefile Rule Format Makefile for “gcc foo.c bar.c baz.c –o myapp” target : source(s) myapp: foo.o bar.o baz.o [TAB]command gcc foo.o bar.o baz.o –o myapp [TAB]command foo.o: foo.c foo.h gcc –c foo.c bar.o: bar.c bar.h gcc –c bar.c baz.o: baz.c baz.h gcc –c baz.c

28 Carnegie Mellon C – Compiling: Makefiles

Make GNU make utility to maintain groups of programs

 Comments are any line beginning with ‘#’

 The first line of each command must be a TAB.

 If you want idenfying dependencies, you can try: . gcc –MM foo.c outputs foo’s dependencies to the console.

. makedepend adds dependencies to the Makefile for you, if you already have one. E.g., foo.c bar.c baz.c .

29 Carnegie Mellon C – Compiling: Makefiles

Make GNU make utility to maintain groups of programs

 Use macros – similar to shell variables – to avoid retyping the same stuff for every .o file.

Makefile Rule Format CC = gcc CCOPT = -g –DDEBUG –DPRINT #CCOPT = -02

foo.o: foo.c foo.h $(CC) $(CCOPT) –c foo.c

 For more informaon on Makefiles, check out Kesden’s old 15-123 lecture 16.

30 Carnegie Mellon Agenda

 Living in Unix  Programming in C . Beginner . Refresher . The Command Line . Compiling . Basic Commands . Hunng Memory Bugs . Pipes & Redirects . GDB . Intermediate . Valgrind . More Commands . The Environment . Shell Scripng

31 Carnegie Mellon C – Hunng Memory Bugs: GDB

 Useful for debugging the occasional easy segfault (among other things!).  You already know how disas and stepi/nexti let you figure out what’s going on in Assembly.  If you compile with –g, you can use list and step/next to step through the C, too.  This even works aer you’ve segfaulted!  Other useful commands: . where (print funcon stack and lines) . up/down (traverse the funcon stack) . display/print variables (like you do now with registers).

32 Carnegie Mellon C – Hunng Memory Bugs: Valgrind valgrind A suite of tools for debugging and profiling programs

 Great for finding memory problems in C programs. Has a ton of tools: memcheck, leakfind, cachegrind.

 Valgrind’s memcheck tool can: . Track memory leaks & (definitely/possibly) lost blocks . Track origin for uninialized values . Tell you what line of code seems to have been the problem

 Syntax (including the verbose flag): valgrind --tool=memcheck -v ./myprogram args valgrind –-tool=leak-check=full -v ./myprogram args

33 Carnegie Mellon Sources and Useful Links

 The Linux Informaon Project: Command Line Definion  Introducon to Linux: A Hands-On Guide (Garrels) . You should be comfortable with chapters 2, 3, 4 and 5.  The On-line Manual Database  Kesden’s 15-213: Effecve Programming in C and Unix . Lectures 3, 4 and 5 cover the basics of Shell Scripng. . Lecture 16 covers Makefiles and lecture 15 covers Valgrind.

34