Docsity

Debugging Strategies: Scientific Debugging, Incremental Development, Defensive Programming, Study notes of Computer Science

Toggle sidebar
bg1
1
Last Time
Advanced interrupt issues
¾ColdFire interrupts
¾System design
¾Prioritized interrupts
¾Interrupt latency
¾Race conditions
¾Reentrancy
¾Interrupt overload
¾Missed interrupts
¾Spurious interrupts
Today
Debugging embedded software
Your friend calls you and says “Oh no – the lamp in
my room is all dark!”
What questions do you ask to help?
Debugging
Today is about strategies for humans
¾Talked about tools a few weeks ago
Lecture contents
¾Definitions
¾Good debugging techniques
¾Ways to avoid debugging
Why do we care?
¾The most difficult bugs that you’ll run into will cannot be
found casually
•Systematic and thorough approach is required
¾Probably 100X difference in effectiveness between good
and bad debuggers
Debugging is twice as hard as
writing the code in the first
place. Therefore, if you write
code as cleverly as possible,
you are, by definition, not smart
enough to debug it.
– Brian Kernighan
An Early Computer Bug
pf3
pf4

Partial preview of the text

Download Debugging Strategies: Scientific Debugging, Incremental Development, Defensive Programming and more Study notes Computer Science in PDF only on Docsity!

Last Time

‹ Advanced interrupt issues

¾ ColdFire interrupts ¾ System design ¾ Prioritized interrupts ¾ Interrupt latency ¾ Race conditions ¾ Reentrancy ¾ Interrupt overload ¾ Missed interrupts ¾ Spurious interrupts

Today

‹ Debugging embedded software

‹ Your friend calls you and says “Oh no – the lamp in

my room is all dark!”

‹ What questions do you ask to help?

Debugging

‹ Today is about strategies for humans

¾ Talked about tools a few weeks ago

‹ Lecture contents

¾ Definitions ¾ Good debugging techniques ¾ Ways to avoid debugging

‹ Why do we care?

¾ The most difficult bugs that you’ll run into will cannot be found casually

  • Systematic and thorough approach is required ¾ Probably 100X difference in effectiveness between good and bad debuggers

Debugging is twice as hard as

writing the code in the first

place. Therefore, if you write

code as cleverly as possible,

you are, by definition, not smart

enough to debug it.

– Brian Kernighan

An Early Computer Bug

Definitions

‹ Symptom – Something wrong that you can see

‹ Bug – Software error that causes a symptom

‹ Debugging –

**1. Finding the bug that causes a symptom – hard!

  1. Fixing the bug – usually not too hard**

‹ Failure-inducing input – Input that causes a bug to

execute

¾ Complication: The time at which events occur must be considered part of the input

  • E.g. it can matter when an interrupt fires
  • Hard to reproduce this

More Definitions

‹ Deterministic system – One where failure-inducing

inputs can be reproduced

¾ Nondeterministic systems are much harder to debug ¾ Sometimes non-deterministic systems can be turned into deterministic ones ¾ Simulators are generally deterministic

Debugging is harder when…

‹ Visibility into the executing system is limited

‹ Several bugs are collaborating to cause the

symptom that you see

‹ The bug and symptom are widely separated in space

or time

‹ The “bug” is a mistaken assumption

‹ The bug is in a library, hardware, OS, or compiler

‹ The system is nondeterministic

‹ Worst case scenario: Many of these complications

are present at the same time

¾ You will need to stare at the code very hard ¾ Or throw it all away and start over

Ugly Fact

‹ Often the bug is something so stupid that you’ll have

a hard time thinking of it

¾ Accidentally running an old version of the software ¾ Environment variable (e.g. PATH) is hosed ¾ Buggy Makefile resulted in a file not being recompiled ¾ Software upgrade added a DLL that is buggy ¾ File not created since disk is full or you’re over quota ¾ File unavailable due to network glitch

‹ There is an infinite variety of these – develop quick

sanity checks to rule them out

¾ Rebuild entire application ¾ Reboot or switch to a different machine ¾ Etc.

Rest of this Lecture

1. Debugging techniques

2. Techniques for avoiding debugging

Tip 1: Scientific Debugging

1. Stabilize

‹ Pin down system inputs and as many other variables as possible ‹ Buggy behavior must be repeatable (not necessarily deterministic)

2. Isolate

‹ Get rid of as much extraneous hardware and software as possible while still retaining the buggy behavior

3. Hypothesize

‹ Make educated guesses about causes that might explain the observed behavior ‹ Initial hypothesis is often pretty vague – this is ok ‹ Write down the hypothesis ‹ Rank possibilities in order of likelihood

More assert()

‹ Good asserts check

¾ Preconditions – must be true for code to run successfully ¾ Postconditions – must be true after code runs ¾ Invariants – must be true at all times (except while data structure is being manipulated)

  • E.g. length field of a linked list ADT stores the length

‹ Some assertions are “static” – they can be resolved

when your system is compiled

¾ Compiler support for these would be nice

‹ Assertions are not for runtime error handling

¾ Failed assert == bug

‹ Usually leave assertions turned on in production

code

Delta Debugging

‹ Idea: Searching for some kinds of bugs can be

automated!

‹ Requires

¾ A way to run the program and automatically determine if a bug was encountered ¾ A deterministic system

‹ Delta algorithm:

**1. Run the program on input that makes the bug happen

  1. Delete some of the input
  2. See if the bug still happens**
    • If yes, keep the new input and go to step 1
    • If no, go back to old input and go to step 1

‹ Result: Smaller input that makes the bug happen

Conclusions

‹ Debugging is harder than programming

‹ Major debugging strategies

¾ Scientific debugging ¾ Incremental development ¾ Defensive programming

‹ When confronted with a hard bug

¾ First impulse is to start hacking ¾ Rather, stop and think

‹ Good debuggers probably 100x more effective than

bad ones

¾ But the skill is learned, not hereditary