1 / 22

Debugging

Debugging. CSCE 315 – Programming Studio, Spring 2019 Robert Lightfoot. About Debugging (the point of this lecture). Debugging is NOT a random process You can approach debugging systematically. Bugs. Term has been around a long time Edison Mark I – moth in machine

aclayton
Télécharger la présentation

Debugging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging CSCE 315 – Programming Studio, Spring 2019 Robert Lightfoot

  2. About Debugging(the point of this lecture) • Debugging is NOT a random process • You can approach debugging systematically

  3. Bugs • Term has been around a long time • Edison • Mark I – moth in machine • Mistake made by programmers • Also (and maybe better) called: • Errors • Defects • Faults

  4. Sources of Bugs • Bad Design • Wrong/incorrect solution to problem • Insufficient Isolation • Changes in one area affect another • Typos • Entered wrong text, chose wrong variable • Specially relevant in languages that are not strongly-typed • Later changes/fixes that aren’t complete • Especially problem with insufficient isolation • Change process gets interrupted, or not all effects are considered

  5. Debugging – Is it Worth Learning? • Programming speed has high correlation to debugging speed • Best debuggers can go up to 20 times faster • Faster finding bugs • Find more bugs • Introduce fewer new bugs

  6. Ways NOT to Debug • Guess at what’s causing it • Don’t try to understand what’s causing it • Fix the symptom instead of the cause • Special case code • Blame it on someone else’s code • Only after extensive testing/proof • Blame it on the compiler/computer • Yes, it happens, but almost never is this the real cause

  7. Student Debugging(what I’ve heard and seen) • Something’s not working • I don’t know why my code’s not working • I tried changing x and y and it seemed to make it work, but this other part’s broken • My code keeps giving me the wrong thing here and I don’t understand why • I didn’t really understand that part of the code

  8. An Orderly Approach to Debugging • Stabilize the error • Locate the source • Fix the defect • Test the fix • Look for similar errors • Goal: Figure out why it occurs and fix it completely

  9. 1. Stabilize the Error • Find a simple test case to reliably produce the error • Narrow it to as simple a case as possible • Some errors are intermittent as they resist this • Failure to initialize • Pointer problems • Timing issues

  10. 1. Stabilizing the Error • Converge on the actual (limited) error • Bad: “It crashes when I enter data” • Better: “It crashes when I enter data in non-sorted order” • Best: “It crashes when I enter something that needs to be first in sorted order” • Create hypothesis for cause • If 10 things in conjunction (even after simplification) causes the error, start with suspected subset • Then test hypothesis to see if it’s accurate

  11. 2. Locate the Source • This is where good code design helps • Good design will easily lead to the source • Again, hypothesize where things are going wrong in code itself • Then, test to see if there are errors coming in there • Simple test cases make it easier to check

  12. When it’s Tough to Find Source • Create multiple test cases that cause same error • But, from different “directions” • Refine existing test cases to simpler ones • Try to find source that encompasses all errors • Could be multiple ones, but less likely • Brainstorm for sources, and keep list to check • Talk to others • Take a break

  13. Finding Error Locations • Process of elimination • Identify cases that work/failed hypotheses • Narrow the regions of code you need to check • Use unit tests to verify smaller sections • Process of expansion: • Be suspicious of: • areas that previously had errors • code that changed recently • Expand from suspicious areas of code

  14. Alternative to Finding Specific Source • Brute Force Debugging • “Guaranteed” to find bug • Examples: • Rewrite code from scratch • Automated test suite • Full design/code review • Fully output step-by-step status • Don’t spend more time trying to do a “quick” debug than it would take to brute-force it.

  15. 3. Fix the Defect • Make sure you understand the problem • Don’t fix only the symptom • Understand what’s happening in the program, not just the place the error occurred • Understand interactions and dependencies • Save the original code • Be able to “back out” of change

  16. Fixing the Code • Change only code that you have a good reason to change • Don’t just try things till they work • Make one change at a time

  17. 4. Check Your Fix • After making the change, check that it works on test cases that caused errors • Then, make sure it still works on other cases • Regression test • Add the error case to the test suite

  18. 5. Look for Similar Errors • There’s a good chance similar errors occurred in other parts of program • Before moving on, think about rest of program • Similar routines, functions, copied code • Fix those areas immediately

  19. Preventing BugsOr Finding Difficult Ones • Good Design • Self-Checking code • Assert statements • Output options • Print statements can be your friend…

  20. Global or Static Variables • Global Variables • Global variables… a good use Debug = false; • When you want to debug, set to true, conditions react and give visibility into code. • DebugOutput = false; • DebugInput = false; • DebugTables = false; • If(DebugTables) print(Table1); • Conditional compiling may leave this code out when false.

  21. Debugging Tools • Debuggers • Often integrated • Can examine state in great detail • Don’t use debuggers to do “blind probing” • Can be far less productive than thinking harder and adding output statements • Use as “last resort” to identify sources, if you can’t understand another way

  22. Non-traditional Debugging Tools • Source code comparators (diff) • Compiler warning messages • Extended syntax/logic checkers • Test frameworks • Remote debugging is the process of running a debug session in a local development environment attached to a remotely deployed application. • Profiling is the process of measuring an application or system by running an analysis tool called a profiler. Profiling tools can focus on many aspects: functions call times and count, memory usage, cpu load, and resource usage. • Tracing is a specialized use of logging to record information about a program's execution.

More Related