Unit 13. Linux Operating System Debugging Programs

Similar documents
Unit 10. Linux Operating System

Using the Command Line

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi and Cyrill Stachniss

Introduction to Lab Sessions

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Laboratory 1 Semester 1 11/12

Lab: Supplying Inputs to Programs

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

CS CS Tutorial 2 2 Winter 2018

Programming Studio #1 ECE 190

SECTION 2: CODE REASONING + PROGRAMMING TOOLS. slides borrowed and adapted from Alex Mariakis and CSE 390a

Development Environment & Linux Guide

Session 1: Accessing MUGrid and Command Line Basics

CS 261 Recitation 1 Compiling C on UNIX

C++ Programming on Linux

Practical Session 0 Introduction to Linux

Guided Tour (Version 3.3) By Steven Castellucci as Modified by Brandon Haworth

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Introduction to Linux

You should see something like this, called the prompt :

Introduction to Linux. Fundamentals of Computer Science

Eclipse Environment Setup

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

CMSC 201 Spring 2017 Lab 01 Hello World

IDE: Integrated Development Environment

Operating Systems and Using Linux. Topics What is an Operating System? Linux Overview Frequently Used Linux Commands

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Intermediate Programming, Spring Misha Kazhdan

Programming Studio #1 ECE 190

Section 2: Developer tools and you. Alex Mariakakis (staff-wide)

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Lab Working with Linux Command Line

AMS 200: Working on Linux/Unix Machines

COMP s1 Lecture 1

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Programming and Data Structure Laboratory (CS13002)

C++ For Science and Engineering Lecture 2

15-122: Principles of Imperative Computation

Using the Unix system. UNIX Introduction

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Intro to Linux & Command Line

Introduction of Linux

Introduction: What is Unix?

Linux and Git Boot Camp

Recitation #1 Boot Camp. August 30th, 2016

Unix Tutorial Haverford Astronomy 2014/2015

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Unix Tools / Command Line

Working with the Command Line

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

CMSC 201 Spring 2018 Lab 01 Hello World

Recitation #1 Unix Boot Camp. August 29th, 2017

CSE 390a Lecture 1. introduction to Linux/Unix environment

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

Tutorial 2: Compiling and Running C++ Source Code

Numerical Algorithms for Physics. Andrea Mignone Physics Department, University of Torino AA

Parallel Programming Pre-Assignment. Setting up the Software Environment

Lab 1 Introduction to UNIX and C

Essential Linux Shell Commands

Python for Astronomers. Week 1- Basic Python

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Terminal Windows, Emacs, Subversion and Make

Introduction to the Linux Command Line

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

Chapter 2. Editing And Compiling

User Guide Version 2.0

UNLV Computer Science Department CS 135 Lab Manual

Getting Started with Visual Studio

Introduction. File System. Note. Achtung!

CSE 391 Lecture 1. introduction to Linux/Unix environment

CHEM5302 Fall 2015: Introduction to Maestro and the command line

Command Line Navigation and Compiling

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Introduction to Linux

Getting Started With UNIX Lab Exercises

ROS-Industrial Basic Developer s Training Class: Linux Basics. Southwest Research Institute Last Updated 2017

CSC111 Computer Science II

Introduction to Linux Spring 2014, Section 02, Lecture 3 Jason Tang

Guided Tour (Version 3.4) By Steven Castellucci

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

CS Operating Systems, Fall 2018 Project #0 Description

Working with Basic Linux. Daniel Balagué

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

Using the Xcode Debugger

The Directory Structure

CSE 391 Lecture 1. introduction to Linux/Unix environment

EECS 211 Lab 2. Getting Started. Getting the code. Windows. Mac/Linux

Perl and R Scripting for Biologists

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

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

Linux at the Command Line Don Johnson of BU IS&T

Week 2. Exp 2 (a) (b): Introduction to LINUX OS, Installation of LINUX OS, Basic DOS commands

Linux/Unix Filesystems

Transcription:

1 Unit 13 Linux Operating System Debugging Programs

COMPILATION 2

3 Editors "Real" developers use editors designed for writing code No word processors!! You need a text editor to write your code Eclipse, Sublime, MS Visual Code, Emacs, Atom, and many others These often have handy functions for commenting, indenting, checking matching braces ({..}) etc.

4 Compilers Several free and commercial compilers are available g++: clang++ XCode MS Visual Studio Several have "integrated" editors, debuggers and other tools and thus are called IDE's (Integrated Development Environments)

5 Using the Command Line While GUIs are nice, we often have more control when using the command line interface (i.e. the terminal) Linux (the OS used by Vocareum and in CS 103, 104, etc.) has a rich set of command line utilities (Mac & Windows do to though Windows uses different names for the utilities) We can navigate the file system (like you would with Explorer or Finder), start programs (double-clicking an icon), and much more by simply typing commands Terminal Icon Linux Terminal View Vocareum Terminal View

6 Software Process #include <iostream> using namespace std; int main() { int x = 5; cout << "Hello" << endl; cout << "x=" << x; return 0; } C++ file(s) (test.cpp) Std C++ & Other Libraries g++ Compiler 1110 0010 0101 1001 0110 1011 0000 1100 0100 1101 0111 1111 1010 1100 0010 1011 0001 0110 0011 1000 Executable Binary Image ("test") Load & Execute Note: Most documentation and books use $ as a placeholder for the command line prompt. $ subl test.cpp & $ subl test.cpp & 1 Edit & write code $ g++ g Wall o test test.cpp or $ make test 2 Compile & fix compiler errors $ subl test.cpp & $ g++ g Wall o test test.cpp $./test 3 Load & run the executable program

7 g++ Options Most basic usage g++ cpp_filenames Creates an executable a.out Options -o => Specifies output executable name (other than default a.out) -g => Include info needed by debuggers like gdb, kdbg, etc. -Wall => show all warnings Most common usage form: $ g++ -g -Wall hw8.cpp -o hw8

8 Listing Files (Folder Contents) In Mac/Linux, to view the files in a folder, just type ls (stands for list) Source code file Executable

9 Running the Program First ensure the program compiles $ g++ -g -Wall hw8.cpp -o hw8 Then run the program by preceding the executable name with./ $./hw8

10 (Part 2 in a few weeks) DEBUGGING PART 1

11 Bugs The original "bug"

12 Step 1: Review your Own Code Rubber Duck Debugging: Reference from an anecdote from a book, "The Pragmatic Programmer", that has become popular Idea: Explain your code line by line to yourself or some other "object" Note: Commenting your code is a way to do this

13 Step 2: Develop An Expectation You cannot effectively debug without an expectation of the right output How would you know if your code is working or not? Steps: Know your input (what inputs will you feed in) As you review your own code and explain it to your rubber duck, write out what you expect it to produce

14 Step 3: Print Statements / Narration Now that you know what to expect, the most common and easy way is to find the error is to add print statements that will "narrate" where you are and what the variable values are Be a detective by narrowing down where the error is Put a print statement in each 'for', 'while', 'if' or 'else' block this will make sure you are getting to the expected areas of your code Then print variable values so you can see what data your program is producing

15 Tips Don't write the entire program all at once Write a small portion, compile and test it Write the code to get the input values, add some couts to print out what you got from the user, and make sure it is what you expect Write a single loop and test it before doing nested loops Once one part works, add another part and test it

16 Vocareum Exercises 1 diff1 Count how many consecutive values differ by 1 atleast2 craps Input numbers until we have at least 2 from the range 0-9 and >=10 but stop immediately if the user enters a negative number 1. The player rolls 2 dice. 2. If the sum of the dice is 7 or 11 the player wins their bet and the turn continues (go back to step 1). 3. If the sum of the dice is 2, 3, or 12 the player loses their bet, but the turn continues (go back to step 1). 4. If the sum is any other number (besides 2, 3, 7, 11, or 12) then that value is known as the point number and play continues. 5. The player rolls the dice until... a. The sum of the dice is 7 in which case the player loses their bet and the turn ENDS b. The sum of the dice is the same as the point value in which case the player wins their bet and the turn continues, starting over at step 1.

17 LINUX AND NAVIGATING FILE SYSTEMS

18 Linux Based on the Unix operating system Developed as an open-source ("free") alternative by Linux Torvalds and several others starting in 1991 Originally only for Intel based processors but has now been ported to other platforms (i.e. ARM processors in your phone, etc.) Commonly used in industry and in embedded devices

19 Using the Command Line While it has a GUI interface like your Mac or Windows PC much of its power lies in its rich set of utilities that are most easily run at the command line (aka command prompt or terminal) Here we can navigate the file system (like you would with Explorer or Finder), start programs (doubleclicking an icon), and much more by simply typing commands Terminal Icon Linux Terminal View Vocareum Terminal View

20 Navigating the File System A file system has Folders (directories) Files They are organized in a hierarchy Everything we can do with a GUI we can do at the command line

21 Some Basic Commands Here are some helpful commands to use in Linux at the command prompt Command ls pwd cd dirname cp srcfiles dest mv srcfile dest rm srcfiles mkdir dirname rmdir dirname List (see) all files in the current folder Present working directory shows the current folder location of the terminal Change directory to a new folder Copy file(s) to a new location Move/rename files to a new name/location Remove files from the current folder Make directory / create a new folder Remove directory / delete a folder (must be empty first)

22 Directory Structure Ex. 1 Each circle is a directory Each name in the box is a file Starting from your home (e.g. 'mark') directory/folder Use cd to change directories (folders) cd Desktop cd cs102 cd hw7 Or go multiple folders at a time cd Desktop/cs102/hw7 hw7 hw7a.cpp hw7b.cpp you start here cs102 Desktop / home mark other Documents src test2.h test2.cpp

23 To go up a level use cd.. Directory Structure Ex. 2 To go up 2 levels use cd../.. Let's go one level to 'cs102' cd.. Now make a directory mkdir hw8 Shortcuts:. = Current directory.. = Parent directory (up one) ~ = Home directory * = Wildcard to match filenames Unix commands: pwd = Print current working dir hw7 hw7a.cpp hw7b.cpp you start here cs102 Desktop hw8 / home mark other Documents src test2.h test2.cpp

24 Directory Structure Ex. 3 Let's say we want to start a new lab with a copy of our old work and just modify it. Let's copy our work Recall I'm in cs102 folder currently cp hw7/* hw8/ you start here Desktop / home mark Documents Shortcuts:. = Current directory.. = Parent directory (up one) ~ = Home directory * = Wildcard to match filenames Unix commands: pwd = Print current working dir hw7 hw7a.cpp hw7b.cpp cs102 hw8 hw7a.cpp hw7b.cpp other src test2.h test2.cpp

25 Directory Structure Ex. 4 Let's now go into the test folder cd test Now rename the hw7a.cpp to hw8.cpp mv hw7a.cpp hw8.cpp Now delete the hw7b.cpp file rm hw7b.cpp Remember, you can see all the files in a folder by typing ls You can see what folder/directory you are in by typing pwd hw7 hw7a.cpp hw7b.cpp you start here cs102 Desktop hw8 hw7a.cpp hw7b.cpp / home mark other Documents src test2.h test2.cpp