Using the Command Line

Similar documents
Unit 13. Linux Operating System Debugging Programs

Unit 10. Linux Operating System

Using the Xcode Debugger

Unit 7. 'while' Loops

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

IDE: Integrated Development Environment

1 Unit 8 'for' Loops

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Getting Started with Visual Studio

VARIABLES & ASSIGNMENTS

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

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS103 Lecture 1 Slides. Introduction Mark Redekopp

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSCE 206: Structured Programming in C++

Lab: Supplying Inputs to Programs

Laboratory 1 Semester 1 11/12

Why Is Repetition Needed?

Chapter 1 INTRODUCTION

do { statements } while (condition);

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

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

EAS230: Programming for Engineers Lab 1 Fall 2004

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

CS103 Lecture 1 Slides. Introduction Mark Redekopp

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Problem Set 1: Unix Commands 1

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

EE 355 Lab 3 - Algorithms & Control Structures

LAB: WHILE LOOPS IN C++

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

Manual Eclipse CDT Mac OS Snow Leopard

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

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:

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

CSE 374 Programming Concepts & Tools

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Using the EE 109 Tools. Allan Weber Mark Redekopp

NSIGHT ECLIPSE EDITION

Lab 1: First Steps in C++ - Eclipse

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Debugging and Debugger. Terminology. GNU gcc and gdb. Debugging C programs in Unix and Windows Environments - Part One

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

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

CS354 gdb Tutorial Written by Chris Feilbach

C++ Programming: From Problem Analysis to Program Design, Third Edition

Object Oriented Design

CS 103 Lecture 2 Slides

A Tutorial for ECE 175

Due Date: See Blackboard

Repetition Structures

To become familiar with array manipulation, searching, and sorting.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CS 253: Intro to Systems Programming 1/21

C++ Programming Lecture 1 Software Engineering Group

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

assembler Machine Code Object Files linker Executable File

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Programming Studio #9 ECE 190

CS2141 Software Development using C/C++ Compiling a C++ Program

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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.

C++ for Python Programmers

Integrated Software Environment. Part 2

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

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

1.1. EE355 Unit 1. Course Overview & Review. Mark Redekopp

My First Command-Line Program

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

JUCE TUTORIALS. INTRO methodology how to create a GUI APP and how to create a Plugin.

CS2141 Software Development using C/C++ C++ Basics

CS 103 Lecture 3 Slides

Chapter 6 Topics. While Statement Syntax Count-Controlled Loops Event-Controlled Loops Using the End-of-File Condition to Control Input Data

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

EE109 Lab Need for Speed

Cours de C++ Introduction

EE 355 Lab 4 - Party Like A Char Star

Intro to C and Binary Numbers 8/27/2007

Your First C++ Program. September 1, 2010

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

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

Programming Concepts: IDEs, Debug. Paulo Penteado. (

Computer Programming. Dr. Fahad Computer Science Deptt.

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Red Hat Developer Tools

Debugging Techniques. CEFET Engineering Week

Week 1, continued. This is CS50. Harvard University. Fall Cheng Gong

Chapter 1: Why Program? Computers and Programming. Why Program?

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CS 103 Lecture 2 Slides

Transcription:

1 Unit 15 Debugging

COMPILATION 2

3 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

4 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

5 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

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

7 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

PART 1 8

9 Bugs The original "bug"

10 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

11 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

12 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

13 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

14 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.

PART 2 15

16 Exercises Practice "narrating" search sumpairs count primes

17 Step 4: Using a Debugger Allows you to Set a breakpoint (the code will run and then stop when it reaches a certain line of code) Step through your code line by line so you can see where it goes Print variable values when you are stopped at a certain line of code

18 GDB and Other Debuggers gdb is a simple text-based debugger that comes with many Linux/Unix based system We'll focus on this debugger Other development environment have built in debuggers MS Visual Studio Apple Xcode Eclipse

19 Running the Debugger Compile your program and include the -g flag $ g++ -g search.cpp -o search Then start the debugger giving it the program you want to debug (not the source code) $ gdb./search

20 Breakpoints Stop program execution at a given line number Set before you start the program in the debugger (gdb) break 36 Get the program running (gdb) run It will run and then stop when it reaches the code on line 36

21 Stepping Type step to execute one line and then stop (aka single-step) (gdb) step If you get to a line with a function call that you don't want to go into but just have execute fully, type next (aka step-over) (gdb) next

22 Printing values At any point in time you can print a variable or an expressions (gdb) print size Would print the value of the size variable (gdb) print nums[i] Would print the value in nums[i] (gdb) print nums[i] == target Would print true or false

23 Vocareum Exercises 2 Practice using a debugger histogram (break at line 8)