Programming Tips for CS758/858

Similar documents
CSE 410: Systems Programming

Scientific Programming in C IX. Debugging

CMPSC 311- Introduction to Systems Programming Module: Debugging

CMPSC 311- Introduction to Systems Programming Module: Debugging

Problem Set 1: Unix Commands 1

CS/COE 0449 term 2174 Lab 5: gdb

Laboratory 1 Semester 1 11/12

Reviewing gcc, make, gdb, and Linux Editors 1

Project #1: Tracing, System Calls, and Processes

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

12. Debugging. Overview. COMP1917: Computing 1. Developing Programs. The Programming Cycle. Programming cycle. Do-it-yourself debugging

Binghamton University. CS-220 Spring C Debugging Basics. No relevant text

CS354 gdb Tutorial Written by Chris Feilbach

The Dynamic Debugger gdb

Recitation: C Review. TA s 20 Feb 2017

COP4530 Data Structures, Algorithms and Generic Programming Recitation 3 Date: January 20 & 22, 2009

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

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:

Recitation: Cache Lab & C

Laboratory Assignment #4 Debugging in Eclipse CDT 1

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.

CS 241 Data Organization Binary Trees

Project 4: Implementing Malloc Introduction & Problem statement

Creating a String Data Type in C

EE355 Lab 5 - The Files Are *In* the Computer

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

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

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

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS61, Fall 2012 Section 2 Notes

Project C: B+Tree. This project may be done in groups of up to three people.

CMSC 341 Lecture 2 Dynamic Memory and Pointers

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

15-213/18-213/15-513, Spring 2018 C Programming Lab: Assessing Your C Programming Skills

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

CS 241 Data Organization using C

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

Exercise Session 6 Computer Architecture and Systems Programming

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Programs in memory. The layout of memory is roughly:

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Using gdb to find the point of failure

manifold Documentation

Debugging with gdb and valgrind

CSE 374 Programming Concepts & Tools

Homework 1 CS 642: Information Security

ENCE 3241 Data Lab. 60 points Due February 19, 2010, by 11:59 PM

mp2 Warmup Instructions (Updated 1/25/2016 by Ron Cheung for using VMs)

CS 11 C track: lecture 6

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

CS 211 Programming I for Engineers

377 Student Guide to C++

Project 0: Implementing a Hash Table

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

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

CS2141 Software Development using C/C++ Debugging

GDB Linux GNU Linux Distribution. gdb gcc g++ -g gdb EB_01.cpp

Here's how you declare a function that returns a pointer to a character:

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

15-213/18-213/15-513, Fall 2017 C Programming Lab: Assessing Your C Programming Skills

Operating System Labs. Yuanbin Wu

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Recitation 2/18/2012

DEBUGGING: DYNAMIC PROGRAM ANALYSIS

CSE 333 Midterm Exam 5/10/13

Project #1 Exceptions and Simple System Calls

ANITA S SUPER AWESOME RECITATION SLIDES

PROGRAMMAZIONE I A.A. 2017/2018

Homework & Debugging Tips

DAY 4. CS3600, Northeastern University. Alan Mislove

Blue Gene/Q User Workshop. Debugging

Source level debugging. October 18, 2016

CS 103 Lab The Files are *In* the Computer

Lecture 07 Debugging Programs with GDB

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

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

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

Programming Assignment #4 Arrays and Pointers

CS155: Computer Security Spring Project #1

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CSCI 2021, Fall 2018 Malloc Lab: Writing a Dynamic Storage Allocator Assigned: Monday November 5th Due: Monday November 19th, 11:55PM

TI2725-C, C programming lab, course

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Project 0: Implementing a Hash Table

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

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

EE 355 Lab 3 - Algorithms & Control Structures

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

CS61 Scribe Notes Date: Topic: Fork, Advanced Virtual Memory. Scribes: Mitchel Cole Emily Lawton Jefferson Lee Wentao Xu

Recitation #11 Malloc Lab. November 7th, 2017

6.S096: Introduction to C/C++

Class Information ANNOUCEMENTS

Assignment 5: Priority Queue

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

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

Transcription:

Programming Tips for CS758/858 January 28, 2016 1 Introduction The programming assignments for CS758/858 will all be done in C. If you are not very familiar with the C programming language we recommend the book The C Programming Language by Kernighan and Ritchie. Feel free to use C99 if you happen to know it. In addition, this handout is intended to give you some tips on programming in C on agate.cs.unh.edu which is the machine that will be used to grade your assignments. Your submissions will need to run on this system without any changes, so you probably want do most of your work there. 2 Accessing agate.cs.unh.edu The easiest way to access agate.cs.unh.edu is to use a secure shell (ssh) client. 2.1 UNIX From a UNIX machine (this includes Macs) this can typically be done with the ssh. For example the command ssh <username>@agate.cs.unh.edu will prompt you for your password on agate.cs.unh.edu and then will give you a shell. Since most of the assignments allow you to see a graphical display of the performance of your algorithms you may want to enable X11 forwarding by using the -X option. By using the -X option agate.cs.unh.educan open an X11 window on your local X server to display plots. 2.2 Windows To access agate.cs.unh.edu from a Windows machine we recommend that you use putty as your ssh client. Putty is available at http://www.chiark.greenend.org.uk/ sgtatham/putty/download.html or google for putty downloads. From the downloads page, you should download the putty.exe file. You can then run this program to log into agate.cs.unh.edu. While putty is supposed to support X forwarding, you will need a local X server to use it. Windows does not typically come with an X server. You can setup cygwin (a Linux-like environment for Windows ) if you would like a free X server, however cygwin may not be trivial to setup and we do not provide instructions here. 1

3 Programming Assignments All of the programming assignments will be done in C. We will provide some skeleton code for you. You are expected to read and understand the skeleton code first, before designing and implementing your part of the solution. (The provided code is also often a good example of the coding style that we expect.) You will implement all of the remaining functions necessary for a complete working assignment. You should not have to change any of the skeleton code in order to complete the assignments. For most assignments, libraries like the standard math library are not needed. Please post a question on Piazza if it is unclear what you are supposed to implement! In addition to skeleton code you will be given a reference solution and a harness. Be warned that the reference solution may not be perfect and you should use it as a guideline. The harness distributed with each assignment will run your solution and verify that its results are legal. It will not tell you if your programuses the wrong algorithm or is implemented inefficiently or with messy convoluted code! As mentioned before, the harness will often allow you to see a graphical display of the performance of your algorithms (more information on using this will be given in each assignment handout). You are required to run your solution with the harness before submitting your assignments to ensure that: 1) the output from your solution is readable by the harness (which will be used for grading) and 2) to make sure that your solution gives correct output. If you have trouble getting your solution to work with the harness please setup a meeting with the TA immediately to get the issue resolved. 3.1 Submitting an assignment Electronically submit your source code using the ~cs758/scripts/sub758 script on agate. To submit your assignment, makesurethatallofyoursourcecodeandthemakefilethatcompilesyourcodeareinadirectory. You will then submit this entire directory. This script takes two arguments: 1) the assignment number (in this case 1 ) and 2) the name of the directory containing your code. You should run this command one directory up from your code directory. For example: jtd7@agate:~$ ~cs758/sub758 <assignment #> <directory with your submission> So, to submit the directory sort ints for assignment 1, the command would look like: jtd7@agate:~$ ~cs758/sub758 1 sort_ints or if you wanted to submit it while in the directory: jtd7@agate:~/sort_ints$ ~cs758/sub758 1. To check that you submitted something, run checksub like so: jtd7@agate:~$ ~cs758/checksub <assignment #> So, to see if you submitted anything for assignment 1, you would type: jtd7@agate:~$ ~cs758/checksub 1 If you are enrolled in 858, please append -grad to the assignment number, as in cs758/sub758 1-grad sort ints. 4 Tips on Programming in C Write very simple code. C will do just about anything that you tell it without regard for whether or not it is a good idea. If you make your code very complicated then there will be bugs and you will not be able to find them. 2

Check the return value of all non-void functions. Functions return a value for a reason, usually to report errors and you do not want to miss these. Think carefully before disregarding the return value of a function. Make functions very short. This really an extension of the first bullet point. If a function gets too long then you should probably break it into multiple functions. Since you are checking the return values of all functions that you call, your functions will get long quickly and this means that you will probably make a lot of small helper functions. Attempt to minimize the amount of memory that you allocate on the heap (using malloc, calloc, etc). Heap allocations must be freed to prevent memory leaks and we expect that you will free all of your heap allocated memory before your program exits. valgrind may be used to find and track down memory leaks and invalid memory access errors in your program (more below). gdb may be used to track down bugs in your program such as segfaults. 5 Tools This section discusses a few tools may be helpful for tracking down bugs and memory leaks in your program. The makefile provided with the skeleton code for your assignments will build two binaries. The binary with the debug extension is built without compiler optimizations and with debugging symbols. This means that the binary is seeded with information that may be used by external applications to print information about the source code that generated portions of the binary. 5.1 valgrind When you are initially developing your program we recommend that you always use the valgrind application. valgrind is a simple tool that wraps around the execution of your program and can track down many types of bugs such as memory leaks and accesses to uninitialized and invalid memory locations. To run your program in valgrind, simply type valgrind before the command line to run your program. For example: valgrind./a.out_debug arg1 arg2 < input will run a hypothetical binary called a.out debug (compiled with debugging symbols) which takes two command-line arguments and an input file redirected to standard input. If there are errors in the execution of a.out then valgrind will report them to standard output along with a stack trace containing the source code file and line number of each function call leading up to the error. If valgrind reports that there are memory leaks in the program than it may be re-run with the --leak-check=full option to perform finer grained memory leak detection. With this option valgrind will report more information such as where the leaked memory was allocated. If valgrind is giving too much output then you may use the --log-file=<filename> option to have it output to a file instead of standard output. 5.2 gdb The GNU debugger (gdb) can allow you to track down harder-to-find bugs in your program. gdb is a fairly complicated program and we will only give some basic commands here: 3

5.2.1 The run command To run gdb just type gdb <binary> at the command line where <binary> is the name of your binary that is compiled with debugging symbols. You will then be given a (gdb) prompt at which you can issue various commands. The first command that you will normally issue is the run command to run your program. The run command should be followed by any command-line arguments that you would like to be passed to your program. You can also use input redirection like at the shell. For example, the following is a transcript of gdb finding a segfault in a simple program: $ gdb a.out_debug GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/aifs2/eaburns/tmp/gdb/a.out_debug...done. (gdb) r world Starting program: /home/aifs2/eaburns/tmp/gdb/a.out_debug world Program received signal SIGSEGV, Segmentation fault. 0x0000000000400538 in bad_function () at test.c:9 9 printf("accessing a null pointer: %d\n", *null); 5.2.2 The list command Thelistcommand canbe used to viewsome contextwhengdbreportsanerrorin yourprogram sexecution. For example, if the list command is executed in the above program trace we will see: (gdb) list 4 5 void bad_function(void) 6 { 7 int *null = NULL; 8 9 printf("accessing a null pointer: %d\n", *null); 10 } 11 12 void function_that_calls_bad_function(void) 13 { 5.2.3 The backtrace command The backtracecommand can be used to view a stack trace when gdb detects an error in program execution. So, in our running example, the backtrace command gives the following output. (gdb) backtrace #0 0x0000000000400538 in bad_function () at test.c:9 #1 0x0000000000400559 in function_that_calls_bad_function () at test.c:14 #2 0x000000000040058f in main (argc=2, argv=0x7fffffffe4d8) at test.c:20 4

Here,weseethatbad function(locatedintest.conline9)wascalledfromafunctionnamedfunction that calls bad funct (located on line 14 in test.c) which was called by the main function (in test.c on line 20). 5.2.4 Other commands gdb supports many more sophisticated commands for debugging programs such as setting break points, stepping through execution line-by-line or instruction-by-instruction and watching the values of variables as they change in your program. You can find these commands and more in the GDB documentation, which is located here: http://www.gnu.org/software/gdb/documentation/ 5