Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
DEBUGGING IN THE REAL WORLD : Recitation 4.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Rings This chapter demonstrates how processes can be formed into a ring using pipes for communication purposes.
. Plab – Tirgul 4 structs & arrays, file I/O, debugging memory errors.
CSE 451 Section 4 Project 2 Design Considerations.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Stack and Heap Memory Stack resident variables include:
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
File Handling Spring 2013Programming and Data Structure1.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
File IO and command line input CSE 2451 Rong Shi.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Agenda Attack Lab C Exercises C Conventions C Debugging
Operating Systems Process Creation
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Hank Childs, University of Oregon April 15th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / /
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Recitation 9: 11/04/02 Outline Error Handling I/O Linux man pages Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean 8402.
CSC Programming for Science Lecture 18: More Data Files.
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Error handling I/O Man pages
Content Coverity Static Analysis Use cases of Coverity Examples
Week 3 Redirection, Pipes, and Background
Sorting Tutorial Using C On Linux.
Recitation 6: C Review 30 Sept 2016.
Valgrind Overview What is Valgrind?
2016.
An Introduction to C Programming
Checking Memory Management
Recitation: C Review TA’s 19 Feb 2018.
Hank Childs, University of Oregon
Recitation 9: Processes, Signals, TSHLab
Programming Assignment # 2 – Supplementary Discussion
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Introduction to Static Analyzer
TUTORIAL 7 CS 137 F18 October 30th.
CISC 361 Operating Systems Midterm Review
C Programming - Lecture 3
Valgrind Overview What is Valgrind?
Intro to the Shell with Fork, Exec, Wait
Recitation 9: Processes, Signals, TSHLab
Presentation transcript:

Recitation By yzhuang, sseshadr

Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization – malloc() related bugs System IO

Debugging Practices

General Debugging printfs are a good start, but won’t solve everything Remember printfs CHANGE your code – And how it’s compiled – And how it runs Especially for races A lot of the debugging tools should be used with the –g compiler flag

GDB From bomblab / buflab You WILL need it for malloc Demo?

Valgrind Memory related issues Lots of options man valgrind valgrind --leak-check=full./a.out Demo?

strace From the man page – “In the simplest case strace runs the specified command until it exits. It intercepts and records the system calls which are called by a process and the signals which are received by a process. The name of each system call, its arguments and its return value are printed on standard error or to the file specified with the -o option.” Cool for debugging!

Errors and Wrappers

System Call Error Handling Always handle errors for every system call – #include – Failed system calls almost always return -1 – Global integer error number: errno – Getting error description: strerror(errno) We deduct style points for not handling system call errors

Wrappers If a system call is frequently used, create a wrapper for it. For example: pid_t Fork(void){ pid_t pid; if ( (pid = fork() ) < 0 ){ //error handling } return pid; } Proclab: always handle errors – You can choose whether to use wrappers

malloc #include void *malloc(size_t size); Allocates size bytes of memory A pointer is returned Returned memory uninitialized!! p=(struct cacheline*) malloc( sizeof(cacheline) ); P->valid = ???? – Cachelab: using uninitialized valid bit (very bad)!

calloc With malloc – Either initialize – Or use calloc void * calloc ( size_t num, size_t size ); – Allocate num * size bytes of memory – Initialized to 0 Caveat: what if num * size causes an overflow? Check before calling calloc.

free Free memory allocated by malloc/calloc Common mistakes: – Freeing memory already freed – Freeing memory not allocated – Writing to memory already freed – Index-out-of-bound accesses of allocated array – Not freeing allocated memory

I/O

System I/O Basics Four basic operations: – open – close – read – write What’s a file descriptor? – Returned by open. – int fd = open(“/path/to/file”, O_RDONLY); fd is some positive value or -1 to denote error

System I/O Basics Every process starts with 3 open file descriptors – 0-STDIN – 1-STDOUT – 2-STDERR Can I close these file descriptors? – Yes! – But you shouldn’t… this next example is just for illustrative purposes

Sample Code #include int main(int argc, char ** argv){ int fd = atoi(argv[1]); argc = argc; /* Keep GCC Happy */ fprintf(stdout, "STDOUT:close(%d) = %d\n",fd,close(fd)); fprintf(stderr, "ERROR:close(%d) = %d\n",fd,close(fd)); return 1; } What are the outputs when run with./a.out 0,./a.out 1, and./a.out 2 ?

Sample Code #include int main(int argc, char ** argv){ int fd = atoi(argv[1]); argc = argc; /* Keep GCC Happy */ fprintf(stdout, "STDOUT:close(%d) = %d\n",fd,close(fd)); fprintf(stderr, "STDERR:close(%d) = %d\n",fd,close(fd)); return 1; } >>./a.out 0 STDOUT:close(0) = 0 STDERR:close(0) = -1 >>./a.out 1 STDERR:close(1) = -1 >>./a.out 2 STDOUT:close(2) = 0 Why -1 on the second time?? Why no STDOUT output? And why -1 for close return value? Why no STDERR output? And why 0 for close return value this time?

Some Real Stuff From Lecture:

Some Real Stuff From Lecture: CALL TO fork()

Some Real Stuff From Lecture:

Dup What is file redirection? – Redirection vs. Pipes Redirection has one “input” of a file, pipes can be between tasks Ex: cat < filename Ex: find. | cat – What is dup2 ? It switches which file a file descriptor is pointing to! – What is dup ? It initializes another file descriptor to point to an already existing file!

Some Real Stuff From Lecture:

Some Real Stuff From Lecture: CALL TO dup2(4,1)

Dup From Lecture

Dup So what is dup, and what is dup2 ? – int fd1 = open(…); int fd2 = dup(fd1); – int fd1 = open(…); int fd2; dup2(fd1, fd2); Are these the same?? – NO! – The first is OK, the second uses an uninitialized variable! Remember, it’s not dup2(fd1, &fd2);

File writing example Questions – What are all of the possible contents of the file after running this code?? – What is wrong with the style of this code? – How would you close these file descriptors?

#include int main() { int fd1, fd2, fd3, parent = 0; char *fname = "filename"; fd1 = open(fname, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR); write(fd1, "A", 1); fd3 = open(fname, O_APPEND|O_WRONLY, 0); write(fd3, "BBB", 3); if((parent = fork())) fd2 = dup(fd1); else fd2 = fd3; write(fd2, "C", 1); write(fd3, "D", 1); if(parent) waitpid(-1, NULL, 0); return 0; }

File writing example Answers – Possible outputs: ACBBDCD ACBBCDD – What is wrong with the style of this code? Didn’t check error codes, didn’t close anything, no comments. – How would you close these file descriptors? fd2 sometimes needs close() ’d, sometimes doesn’t! So: don’t do both fd2 = fd3 ; and fd2 = dup(f1);

Practice!! Tons of practice available in past exam #2’s Very likely to be an I/O question on the next test!