CS/COE 0449 Jarrett Billingsley

Slides:



Advertisements
Similar presentations
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Advertisements

Debugging Introduction to Computing Science and Programming I.
Debugging CPSC 315 – Programming Studio Fall 2008.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
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.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Problem of the Day  Why are manhole covers round?
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Debugging and Profiling With some help from Software Carpentry resources.
Debuggers in Python. The Debugger Every programming IDE has a tool called a debugger. This application does NOT locate or fix your bugs for you! It slows.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Debugging Computer Networks Sep. 26, 2007 Seunghwan Hong.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
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.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
HP-SEE Debugging with GDB Vladimir Slavnic Research Assistant SCL, Institute of Physics Belgrade The HP-SEE initiative.
Debugging using By: Samuel Ashby. What is debugging?  A bug is an error in either a program or the hardware itself.  Debugging is first locating and.
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors Locate seg faults and bus errors Prepared by Dr. Spiegel.
Debugging with Eclipse
Gnu Debugger (gdb) Debuggers are used to: Find semantic errors
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
14 Compilers, Interpreters and Debuggers
C – Multi-file development and make
BIT116: Scripting Lecture 06
Introduction to Python
Logger, Assert and Invariants
Handling Exceptionally Sticky Problems
CSE 374 Programming Concepts & Tools
CS1101X Programming Methodology
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Testing and Debugging.
COMP 2710 Software Construction Introduction to GDB
Pseudo-ops, Debugging, etc.
CSE 341: Programming Languages Section 1
CSE 341: Programming Langs
Lab: ssh, scp, gdb, valgrind
Chapter 15 Debugging.
Common C Programming Errors, GDB Debugging
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
Debugging CSCE 121 J. Michael Moore.
CSE 341: Programming Languages Section 1
Coding Concepts (Standards and Testing)
Debugging at Scale.
Debugging Taken from notes by Dr. Neil Moore
Chapter 15 Debugging.
Testing, debugging, and using support libraries
Debuggers and Debugging
Debugging “Why you were up till 2AM”
Debugging Taken from notes by Dr. Neil Moore
CS 240 – Advanced Programming Concepts
Tonga Institute of Higher Education IT 141: Information Systems
An Introduction to Debugging
Tonga Institute of Higher Education IT 141: Information Systems
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Handling Exceptionally Sticky Problems
Exceptions 10-May-19.
Introduction to Computing Lecture 09: Functions (Part II)
Review of Previous Lesson
Chapter 15 Debugging.
Exceptions 5-Jul-19.
Debugging.
Defensive Programming
Chapter 15 Debugging.
Presentation transcript:

CS/COE 0449 Jarrett Billingsley Programs - Debugging CS/COE 0449 Jarrett Billingsley

Class announcements last day of new material! exam review monday exam wednesday CS449

Writing fewer bugs in the first place CS449

Removing the training wheels as your programs get bigger… you cannot write them all at once and then compile and test. "it compiles" is a very weak indication of correctness. does it compile? TEST WHAT YOU JUST WROTE. write a little bit of code yes no think of the compiler more like a spell checker, or a partner in coding. fix only the first error - unless you're using, like, Haskell or Rust or an automated theorem prover. - in which case "it compiles" is still not perfect, but a much better indication. - the faster you can make this cycle, the faster and more correctly you can write code. CS449

Writing smaller functions smaller functions have fewer things to go wrong they have more well-defined purposes it's easier to encode assumptions (inputs and outputs) it's easier to see the control flow it's easy to see all the places they're used it is totally okay to write a function that is called exactly once. functions label your code. they indicate your intent. have a look at 11_a_terrible_function.java and 11_a_better_function.java CS449

Wall the Werrors always try to use -Wall -Werror with gcc. in any other language, turn on all the error checking you can. while developing, enable debugging info. (gcc -g) your brain is squishy and can't handle everything. CS449

look into testing frameworks for your language. most have them. WRITE TESTS ideally, you write a test for each of your small functions Good things to test normal conditions correct outputs weird inputs error conditions "contracts" effects on state state is, broadly, "the variables and environment outside of the function." look into testing frameworks for your language. most have them. CS449

Use the right programming language all languages are more robust in some areas than others. If you're doing… Then try… lots of strings!! Lua, Ruby daily tasks, file management Python databases C#, Python GUIs C#, Ruby? low-level stuff Rust, C multithreading Rust, Haskell theoretical math Haskell, OCaml these recommendations are based on personal and vicarious experience; there are many languages, and many factors go into language choice. CS449

So something went wrong. CS449

Step 1: admitting you have a problem a crash is an obvious indication that something is wrong. in C, you can use a debugger (gdb) to get a stack trace. but many problems don't lead to a crash… in that case, you need to follow the control flow at some point, your program goes from "working correctly" to "something is bad." your job is to find that point; do not try to guess based on the output or the final results. - programs are proofs (thanks, Howard-Curry isomorphism!) - and like a proof, you can make mistakes in your assumptions or proof rules; you can do great for several steps and then oops. CS449

printf("x = %d\n", x); you can absolutely use prints or logging frameworks (e.g. Log4J in Java) to spam informational messages as your program runs logging frameworks let you have a "severity" for each message "debug", "info", "warning", "error", "fatal" if you don't already have prints, using a debugger to follow control flow is probably easier than writing a bunch in there. especially since you have to remove/comment them out later. CS449

Step 2: determining the cause once you pinpoint the location… the cause may or may not be obvious. Rectangle r = get_bounding_rect(); int x = do_a_thing(r.x, r.y, r.x + r.width, r.y + r.height); int y = smth_else (r.y, r.x, r.y + r.width, r.x + r.height); copy-and-paste errors are often hard to spot. splitting code into smaller functions can help avoid this. if(x < 0) { do_one_thing(); } else if(x > 0) { do_another(); } missing cases are another common problem – and you don't see them cause they're not there. - we probably shouldn't be adding the y coordinate to the width… we probably meant to swap width and height too. - what if x == 0? CS449

Check your assumptions assumptions are "shoulds" "when I enter this function, what should the parameters be?" "when I return this value, what should it look like?" "after this search loop, what should be in this variable?" "when this method runs, what state should the object be in?" assertions are a great way to catch bugs early during development Java has em. C has em. assertions should only be used to test for things that should be absolutely impossible to happen under any circumstance. #include <assert.h> ... assert(x >= 0); assert(self.state == READY); don't e.g. use them to check parameters on a public method. CS449

Working backwards from crashes when a program crashes… fairly often, that specific line is not at fault. it's because some earlier code broke things, and your program kept running for a while until things finally crashed. like a ticking time bomb. look at what happened immediately before. set breakpoints earlier than the crash and re-run the program. check out what's happening at those breakpoints. then step closer and closer to narrow down the actual error location. CS449

Finding bugs faster CS449

IDEs IDEs can continually compile and find errors they can make suggestions to fix them or show help on functions while you write so that you don't make mistakes to begin with IDEs can be a helpful tool… but they can also be "helpy" - "I'M BEING HELPFUL!!!!!!" they can also be so complex that you just get overwhelmed by all the settings and features. probably the nicest feature of IDEs is an integrated debugger no need to use arcane typed commands CS449

Debugger breakpoints a breakpoint lets you pause the program and look around. in gdb: b func will pause whenever func is called. b mymalloc.c:45 will pause when line 45 of mymalloc.c is reached. b *0x8004030 will pause when the PC gets to address 0x8004030. b location if x == 5 will pause at a location but only if the condition is satisfied. location can be any of the above. tb location is a breakpoint that only happens once – it's deleted after the first time it's hit. (you can make these conditional too.) check out the breakpoint features in your IDEs! CS449

Watchpoints sometimes a variable gets set to some weird value and you don't know where or when it happens. a watchpoint is like a breakpoint, but it pauses whenever a variable is about to be written (or read, or both). in gdb: watch globalvar will pause when a global variable is changed. watch localvar will only work when you are paused in a function, and it will last until the local variable goes out of scope. rwatch and awatch work the same, except they pause when a variable is read (rwatch) or on all accesses (awatch). you can't set a condition on these, sadly… CS449