Skip to content

Commit

Permalink
Reorder some content in "Using a debugger"
Browse files Browse the repository at this point in the history
This page now begins with a short list of key concepts for using a
debugger, and has a short introduction paragraph before the debugger
session recordings (where we direct participants to use editors that
support running a debugger from within the editor itself).
  • Loading branch information
robmoss committed Aug 22, 2024
1 parent f1043c3 commit f821979
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions docs/community/training/debugging/using-a-debugger.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@

Debuggers can help us to find errors because they show us what the code **is actually doing**.

!!! info

Editors such as [RStudio](https://support.posit.co/hc/en-us/articles/205612627-Debugging-with-the-RStudio-IDE), [PyCharm](https://www.jetbrains.com/pycharm/features/debugger.html), and [Spyder](https://docs.spyder-ide.org/current/panes/debugging.html) allow you to run a debugger inside the editor itself.

!!! question

What is the "state" of a running program?

How to use a debugger to examine the program state:

- [`breakpoint()`](https://docs.python.org/3/library/functions.html#breakpoint) and [`pdb`](https://docs.python.org/3/library/pdb.html#module-pdb) for Python
- [`browser()`](https://rdrr.io/r/base/browser.html), [`debug()`](https://rdrr.io/r/base/debug.html) and [`traceback()`](https://rdrr.io/r/base/traceback.html) for base R
- step over, step into, etc
- Advanced R, chapter 22: [Debugging](https://adv-r.hadley.nz/debugging.html)

How to move up and down the call stack!
- Breakpoints
- Conditional breakpoints
- Printing values
- Stepping into and over lines
- Understanding the call stack and interpreting tracebacks

- Start with some toy examples and some simple diagrams and asciinema videos.

- Relate this to tracebacks!
When you know where a failure occurs, you can set a breakpoint and inspect the current state of the program, but also walk up the stack!

## Example: Square numbers
## Code: Square numbers

[Square numbers](https://en.wikipedia.org/wiki/Square_number) are positive integers that are equal to the square of an integer.
Here we have provided example Python and R scripts that print all of the square numbers between 1 and 100:
Expand Down Expand Up @@ -83,6 +73,21 @@ sequenceDiagram
--8<-- "square_numbers.R"
```

## Stepping through the code

The recorded terminal sessions demonstrate how to use Python and R debuggers from the command line.
They cover:

- How to define breakpoints;
- How to inspect the current values of variables; and
- How to step through, and over, lines of code.

!!! tip "Interactive debugger sessions"

If your editor supports running a debugger, **use this feature!**
See these examples for [RStudio](https://support.posit.co/hc/en-us/articles/205612627-Debugging-with-the-RStudio-IDE), [PyCharm](https://www.jetbrains.com/pycharm/features/debugger.html), and [Spyder](https://docs.spyder-ide.org/current/panes/debugging.html).


=== "Python debugger"

<div id="pdb-demo" data-cast-file="../square-numbers-pdb.cast"></div>
Expand Down

0 comments on commit f821979

Please sign in to comment.