{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5.5 Using a debugger\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Estimated time for this notebook: 10 minutes*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stepping through the code\n", "\n", "Debuggers are programs that can be used to test other programs. They allow programmers to suspend execution of the target program and inspect variables at that point.\n", "\n", "* Mac - compiled languages:\n", " [Xcode](https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/DebugYourApp/DebugYourApp.html)\n", "* Windows - compiled languages:\n", " [Visual Studio](http://msdn.microsoft.com/en-us/library/bb483011.aspx)\n", "* Linux: [DDD](https://www.gnu.org/software/ddd/)\n", "* all platforms: [eclipse](http://www.eclipse.org), [gdb](http://www.sourceware.org/gdb/) (DDD and\n", " eclipse are GUIs for gdb)\n", "* python: [spyder](http://pythonhosted.org/spyder/index.html),\n", "* [pdb](https://docs.python.org/3.8/library/pdb.html)\n", "* R: [RStudio](http://www.rstudio.com/ide/docs/debugging/overview),\n", " [debug](http://stat.ethz.ch/R-manual/R-devel/library/base/html/debug.html),\n", " [browser](http://stat.ethz.ch/R-manual/R-devel/library/base/html/browser.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**NB.** If you are using the Windows command prompt, you will have to replace all `%%bash` directives in this notebook with `%%cmd` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the python debugger" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unfortunately this doesn't work nicely in the notebook. But from the command line, you can run a python program with:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``` bash\n", "python -m pdb my_program.py\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic navigation:\n", "\n", "Basic command to navigate the code and the python debugger:\n", "\n", "* `help`: prints the help\n", "* `help n`: prints help about command `n`\n", "* `n`(ext): executes one line of code. Executes and steps **over** functions.\n", "* `s`(tep): step into current function in line of code\n", "* `l`(ist): list program around current position\n", "* `w`(where): prints current stack (where we are in code)\n", "* `[enter]`: repeats last command\n", "* `anypythonvariable`: print the value of that variable\n", "\n", "The python debugger is **a python shell**: it can print and compute values, and even change the values\n", "of the variables at that point in the program.\n", "\n", "## Breakpoints\n", "\n", "Break points tell debugger where and when to stop\n", "We say\n", "* `b somefunctionname` " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting energy_example.py\n" ] } ], "source": [ "%%writefile energy_example.py\n", "from diffusion.model import energy\n", "\n", "print(energy([5, 6, 7, 8, 0, 1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The debugger is, of course, most used interactively, but here I'm showing a prewritten debugger script:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting commands\n" ] } ], "source": [ "%%writefile commands\n", "restart # restart session\n", "n\n", "b energy # program will stop when entering energy\n", "c # continue program until break point is reached\n", "print(density) # We are now \"inside\" the energy function and can print any variable." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ">/home/turingdev/projects/research-software/rse-course/ch03tests/energy_example.py(1)()\n", "-> from diffusion.model import energy\n", "(Pdb) Restarting energy_example.py with arguments:\n", "\tenergy_example.py\n", ">/home/turingdev/projects/research-software/rse-course/ch03tests/energy_example.py(1)()\n", "-> from diffusion.model import energy\n", "(Pdb) >/home/turingdev/projects/research-software/rse-course/ch03tests/energy_example.py(3)()\n", "-> print(energy([5, 6, 7, 8, 0, 1]))\n", "(Pdb) Breakpoint 1 at/home/turingdev/projects/research-software/rse-course/ch03tests/diffusion/model.py:5\n", "(Pdb) >/home/turingdev/projects/research-software/rse-course/ch03tests/diffusion/model.py(13)energy()\n", "-> density = array(density)\n", "(Pdb) [5, 6, 7, 8, 0, 1]\n", "(Pdb) \n" ] } ], "source": [ "%%bash\n", "python -m pdb energy_example.py < commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, break-points can be set on files: `b file.py:20` will stop on line 20 of `file.py`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Post-mortem\n", "\n", "Debugging when something goes wrong:\n", "\n", "1. Have a crash somewhere in the code\n", "1. run `python -m pdb file.py` or run the cell with `%pdb on`\n", "\n", "The program should stop where the exception was raised\n", "\n", "1. use `w` and `l` for position in code and in call stack\n", "1. use `up` and `down` to navigate up and down the call stack\n", "1. inspect variables along the way to understand failure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note** Running interactively like in the following example **does** work in the notebook. Try it out!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "%pdb on\n", "from diffusion.model import energy\n", "partial_derivative(energy, [5, 6, 7, 8, 0, 1], 5)\n", "```" ] } ], "metadata": { "jekyll": { "display_name": "Debugger" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" } }, "nbformat": 4, "nbformat_minor": 1 }