Learning to program using a python debugger for n00bs

by Security Dude

Today, I’m learning about debuggers, specifically the python debugger (pdb). I’m learning that using a debugger is initially hard stuff, but it keeps you from putting a bunch of print statements all over your code.  Yesterday, I added dozens of print statements in trying to learn OPT. It may not have been the best use of my time.

The usefulness of a debugger lies in the fact that you instruct it to pause the execution of your program and inspect the variables and trace the execution of your code.

When I finally got used to the code base, I had to manually find and remove all the extra code I put into the program. I wasted hours of time. Today, I decided I would spend a couple of hours investing in learning to use pdb properly. To learn about the process of debugging. To learn the role of the different debugging tools. To learn the pros and cons of command line debugging tools vs GUI debugging. The biggest thing for me is understanding and inspecting variables. What values are being held and when are the changing?

Here are some of the commands that I’m leveraging to learn the OPT code base.

  • Getting started — import pdb; pdb.set_trace()
  • Getting help with help command – with “h”
  • Using the next statement – with “n”
  • Using the quit statement – with “q”
  • Get data from variables with the print command – with “p”
  • Look at where you are in the code with list command – with “l”
  • Enter a function and step through the code with step command – with “s”
  • Go back up and out of a function with the up command – with “u”
  • To continue or keep going in the execution of the program use the continue command – with “c”
  • End of the current subroutine with return statement – with “r”

http://docs.python.org/library/pdb.html

http://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/

Advertisements