What debugging is really about
What debugging is really about
In this context, debugging is about
Figuring out
why our program
gives the wrong result
What debugging is really about
EDSAC I in June 1948
... 'single-stepping' through the program and observing the contents of the memory and registers on the monitor tubes.
- The Airy Tape : an early chapter in the history of debugging
On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.
-- Guido van Rossum
http://python.org/doc/essays/blurb.html
I don't like debuggers. Never have, probably never will.
-- Linus Torvalds
https://lwn.net/2000/0914/a/lt-debugger.php3
import logging
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')
Configurable
Easy to manage output (e.g. Sentry)
Richer context (filename, lineno, etc)
Config is cumbersome
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
Not always available out-of-the-box
Powerful, with more features
Doesn't require familarity with code
Helps you find root cause more easily
Depends on certain Lib/Editor/IDE
Some learning curve
No way to persist information
Performance issue
What debugging is really about
No tool that is as easy-to-use as print,
yet as powerful as a debugger
Existing tools only give clues,
without telling the why
Recap: debugging is about figuring out why our program gives the wrong result
c = a + b # c should be "foo", but instead is "bar"
c = a + b # c should be "foo", but instead is "bar"
Set a break point at the program entry point
Step through and program
Util you reach c = a + b
Monitor relevant variables
Compare with
expected values
Use our brain to do all this
Memorize the results at every step
Yet, we don’t even
think it is a problem
What debugging is really about
Who has the information we need for debugging?
the Python interpreter
>>> from dis import dis
>>> dis("c = a + b")
1 0 LOAD_NAME 0 (a)
2 LOAD_NAME 1 (b)
4 BINARY_ADD
6 STORE_NAME 2 (c)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
c = a + b # c should be "foo", but instead is "bar"
It is possible to backtrace errors reliably
What if there's a tool that can automatically generate this graph?
Demo: Cyberbrain
Cyerbrain solves the two pain points:
What debugging is really about
DAP ( Debug Adapter Protocol )
with pysnooper.snoop():
lower = min(lst)
upper = max(lst)
mid = (lower + upper) / 2
New var:....... lst = [681, 267, 74, 832, 284, 678, ...]
09:37:35.881721 line 10 lower = min(lst)
New var:....... lower = 74
09:37:35.882137 line 11 upper = max(lst)
New var:....... upper = 832
09:37:35.882304 line 12 mid = (lower + upper) / 2
74 453.0 832
New var:....... mid = 453.0
The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.
- Edsger Dijkstra
keep an open mind.
Q & A