Skip to content

Commit ac74a44

Browse files
committed
More documentation for the debugger
1 parent 855a7f4 commit ac74a44

File tree

1 file changed

+86
-7
lines changed

1 file changed

+86
-7
lines changed

doc/debugging.md

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CIDER ships with a powerful interactive Clojure debugger inspired by Emacs's own
77

88
The debugger can be invoked in several ways, the simplest one is to type
99
<kbd>C-u C-M-x</kbd>. This will take the current top-level form, place as many
10-
breakpoints inside it as possible (instrument it), and then evaluate it a
10+
breakpoints inside it as possible (instrument it), and then evaluate it as
1111
normal. Whenever a breakpoint is reached, you'll be shown the value and asked
1212
for input (see below). Note that if the current form is a `defn`, it will stay
1313
instrumented, so the debugger will be triggered every time the function is
@@ -39,26 +39,105 @@ instrumented as well, but they will not be listed by this command.
3939

4040
## Keys
4141

42-
`cider-debug` tries to be consistent with
43-
[Edebug][]. So
44-
it makes available the following bindings while stepping through code.
42+
`cider-debug` tries to be consistent with [Edebug][], although there are some
43+
differences. It makes available the following bindings while stepping through
44+
code.
4545

4646
Keyboard shortcut | Description
4747
--------------------------------|-------------------------------
4848
<kbd>n</kbd> | Next step
49+
<kbd>i</kbd> | Step in to a function
50+
<kbd>o</kbd> | Step out of the current sexp (like `up-list`)
51+
<kbd>O</kbd> | Force-step out of the current sexp
52+
<kbd>h</kbd> | Skip all sexps up to “here” (current position). Move the cursor before doing this.
53+
<kbd>H</kbd> | Force-step to “here”
4954
<kbd>c</kbd> | Continue without stopping
50-
<kbd>o</kbd> | Move out of the current sexp (like `up-list`)
51-
<kbd>i</kbd> | Inject a value into running code
5255
<kbd>e</kbd> | Eval code in current context
56+
<kbd>p</kbd> | Inspect a value
5357
<kbd>l</kbd> | Inspect local variables
58+
<kbd>j</kbd> | Inject a value into running code
5459
<kbd>s</kbd> | Show the current stack
55-
<kbd>h</kbd> | Skip all sexps up to “here” (current position). Move the cursor before doing this.
60+
<kbd>t</kbd> | Trace. Continue, printing expressions and their values.
5661
<kbd>q</kbd> | Quit execution
5762

5863
In addition, all the usual evaluation commands (such as <kbd>C-x C-e</kbd> or
5964
<kbd>C-c M-:</kbd>) will use the current lexical context (local variables) while
6065
the debugger is active.
6166

67+
## Command Details
68+
69+
Here are some more details about what each of the above commands does.
70+
71+
### Stepping Commands
72+
73+
These commands continue execution until reaching a breakpoint.
74+
75+
In the cider debugger, the term "breakpoint" refers to a place where the
76+
debugger can halt execution and display the value of an expression. You can set
77+
a single breakpoint with `#break`, or set breakpoints throughout a form with
78+
`#dbg` (or by evaluating with `C-u C-M-x`). Not every form is wrapped in a
79+
breakpoint; the debugger tries to avoid setting breakpoints on expressions that
80+
would not be interesting to stop at, such as constants. For example, there would
81+
not be much point in stopping execution at a literal number 23 in your code and
82+
showing that its value is 23 - you already know that.
83+
84+
- **next**: Steps to the next breakpoint
85+
- **in**: Steps in to the function about to be called. If the next breakpoint is
86+
not around a function call, does the same as `next`. Note that not all
87+
functions can be stepped in to - only normal functions stored in vars, for
88+
which cider can find the source. You cannot currently step in to multimethods,
89+
protocol functions, or functions in clojure.core (although multimethods and
90+
protocols can be instrumented manually).
91+
- **out**: Steps to the next breakpoint that is outside of the current sexp.
92+
- **Out**: Same as `o`, but skips breakpoints in other functions. That is, if
93+
the code being skipped over contains a call to another instrumented function,
94+
the debugger will stop in that function if you step out with `o`, but not if
95+
you step out with `O`.
96+
- **here**: Place the point somewhere further on in the function being debugged,
97+
at the point where you want to stop next. Then press `h`, and the debugger
98+
will skip all breakpoints up until that spot.
99+
- **Here**: Same as `h`, but skips breakpoints in other functions, as with `O`.
100+
- **continue**: Continues without stopping, skipping all breakpoints.
101+
102+
### Other Commands
103+
104+
- **eval**: Prompts for a clojure expression, which can reference local
105+
variables that are in scope where the debugger is stopped. Displays the result
106+
in an overlay.
107+
- **inspect**: Like eval, but displays the value in a `cider-inspector` buffer.
108+
- **locals**: Opens a `cider-inspector` buffer displaying all local variables
109+
defined in the context where the debugger is stopped.
110+
- **inject**: Replaces the currently-displayed value with the value of an
111+
expression that you type in. Subsequent code will see the new value that you
112+
entered.
113+
- **stacktrace**: Shows the stacktrace of the point where the debugger is
114+
stopped.
115+
- **trace**: Continues execution, but at each breakpoint, instead of stopping
116+
and displaying the value in an overlay, prints the form and its value to the
117+
REPL.
118+
- **quit**: Quits execution immediately. Unlike with `continue`, the rest of the
119+
code in the debugged function is not executed.
120+
121+
### Conditional Breakpoints
122+
123+
Breakpoints can be conditional, such that the debugger will only stop when the
124+
condition is true.
125+
126+
Conditions are specified using `:break/when` metadata attached to a form.
127+
128+
```clojure
129+
(dotimes [i 10]
130+
#dbg ^{:break/when (= i 7)}
131+
(prn i))
132+
```
133+
134+
Evaluating the above with `C-M-x`, the debugger will stop only once, when i
135+
is 7.
136+
137+
You can also have cider insert the break-condition into your code for you. Place
138+
the point where you want the condition to go and evaluate with `C-u C-u
139+
C-M-x` or `C-u C-u C-c C-c`.
140+
62141
## Internal Details
63142

64143
*This section explains a bit of the inner workings of the debugger. It is mostly

0 commit comments

Comments
 (0)