Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Debugging in Matlab C. Reed 4/5/11. Bugs Debugging is a natural part of programming: Three standard types of errors: –Syntax errors: you simply have typed.
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Finding and Debugging Errors
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.
How to Debug VB .NET Code.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Using a Debugger. SWC What is ”debugging”? An error in a computer program is often called a ”bug”… …so, to ”debug” is to find and get rid of errors in.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
DEBUGGING CHAPTER Topics  Getting Started with Debugging  Types of Bugs –Compile-Time Bugs –Bugs Attaching Scripts –Runtime Errors  Stepping.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
CPS120 Introduction to Computer Science Iteration (Looping)
Python From the book “Think Python”
Chapter 02 (Part III) Introduction to C++ Programming.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Program Errors and Debugging Week 10, Thursday Lab.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Copyright © 2004 – 2013 Curt Hill Java Debugging In Eclipse.
CPS120 Introduction to Computer Science Iteration (Looping)
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual Basic nd Edition.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Debugging M-Files Steve Gu Feb 08, Outline What’s Debugging? Types of Errors Finding Errors Debugging Example Using Debugging Features.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Programming in visual basic .net Visual Basic Building Blocks
Testing and Debugging.
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Debugging CMSC 202.
Scripts & Functions Scripts and functions are contained in .m-files
CSS 161: Fundamentals of Computing
Using a Debugger 1-Jan-19.
Debuggers and Debugging
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Chapter 15 Debugging.
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Debugging your programs When you write a program, there are almost always errors. Three different types of errors are –Syntax errors: you simply have typed an illegal expression, independent of the values of the variables in the expression. Matlab will catch this once you try to run the program (even without executing your code). Matlab will usually issue a pretty clear error message, and you are directed to the problem. These types of bugs are easy to fix. –Run-time errors: logic errors that result in an illegal expression for specific values of the data (harder to fix) Matlab will catch this when you run the code. At the line containing the illegal expression, Matlab will issue an error. You can insert some “printouts” right before the expression to see what the variables are, and try to understand what is the problem –Do the variables have incorrect values? Or –The expression is indeed not correct. –Logic errors that result in the program executing completely, but the answer that they return is incorrect (hardest to fix).

Finding run-time errors (bugs) The most primitive way to debug a program is to –Work out (by “hand”) what the intermediate values of variables should be for a specific case –Remove the trailing semicolons ; from your program, so that Matlab will display the intermediate results –Call your program, and compare the printouts (your program) to your hand-calculations, and try to see where things start to differ.

Finding run-time errors (bugs) A better (but ultimately the same) way is to –Work out (by “hand”) what the intermediate values of variables should be for a specific case –Insert the command keyboard at some point in your program. This is called “setting a breakpoint” –Run the program –When execution gets to the keyboard line program execution is temporarily stopped the Command window appears, the prompt is K>> control is at the keyboard (ie., you can type), and the scope of variables is the function workspace (you are “in” the function at the line of code where the keyboard command is) –Type expressions at the K>> prompt (variables to see what their values are, upcoming expressions to see what their values will be, etc). You are a detective at this point. –Type K>> return (resume execution) or K>> dbquit (exit)

Single-line stepping through an m-file Use dbstep to step through an M-file a line-at-a-time To execute the next line of code, type K>> dbstep To execute the next 6 lines of code, type K>> dbstep 6 To execute code until another m-file function is called, and then to “stop inside” that function at its first line, type K>> dbstep in To execute code until the m-file function exits, and then to stop inside the m-file function that called it, type K>> dbstep out After any of these, the “instruction pointer” moves to the next line of code which has not executed.

Stopping in an M-file Use dbstop to stop in an M-file at a certain line if a condition is true. To stop in loancalc at line 11 if the variable G is less than 1 K>> dbstop in loancalc at 11 if G<1 This sets a conditional breakpoint. When execution gets to line 11, Matlab will evaluate the expression G<1. –If it is true, then execution will stop, the K>> prompt will appear in the Command window, and so on… –If it is false, then execution will continue on as if no breakpoint was set.

Clearing breakpoints All breakpoints can be cleared with the command >> dbclear all You can also selectively clear breakpoints that were created with dbstop. See >> help dbstop >> help dbclear for more information.

Using dbup and dbdown Normally at a breakpoint –execution is temporarily stopped –the K>> prompt appears in the command window –the variables in the function’s workspace can be examined. The command dbup changes the scope of visible variables to the caller’s workspace. K>> % examine variables in function’s workspace K>> dbup K>> % examine variables in caller’s workspace K>> dbdown K>> % examine variables in function’s workspace

Seeing all of the function instances Use dbstack to see the list of all function instances that are running. –The top line gives the function instance where the dbstack command occurred. –The (n+1)’th line is the function instance of the function which called the function instance listed on the n’th line.

Profiling your code When running a program, Matlab can keep track of –How many times a single line is executed –How many times a function is called –How much total time is spent executing a specific line of code –How much time is spent running a specific function These pieces of information can often help you discover bottlenecks that are slowing your code down, and surprising calls that you thought would happen less/more (ie.,perhaps a bug). >> profile on >> myprogram >> profile report