Skip to content

Commit 5e8d821

Browse files
copperwalltargos
authored andcommitted
doc: add conditional example for setBreakpoint()
The `node-inspect` debugging client supports passing an optional third parameter as a string to be evaluated when the breakpoint is hit. If the condition evaluates to `true` in the current context, the breakpoint pauses execution; otherwise the execution continues. This was raised as an issue in nodejs/node-inspect#68, but the client already supports that functionality, so I thought it'd be helpful to add it to the node documentation. PR-URL: #35823 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Jan Krems <[email protected]>
1 parent dd3cbb4 commit 5e8d821

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/api/debugger.md

+39
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ To begin watching an expression, type `watch('my_expression')`. The command
118118
functions body
119119
* `setBreakpoint('script.js', 1)`, `sb(...)`: Set breakpoint on first line of
120120
`script.js`
121+
* `setBreakpoint('script.js', 1, 'num < 4')`, `sb(...)`: Set conditional
122+
breakpoint on first line of `script.js` that only breaks when `num < 4`
123+
evaluates to `true`
121124
* `clearBreakpoint('script.js', 1)`, `cb(...)`: Clear breakpoint in `script.js`
122125
on line 1
123126

@@ -145,6 +148,42 @@ break in mod.js:22
145148
debug>
146149
```
147150

151+
It is also possible to set a conditional breakpoint that only breaks when a
152+
given expression evaluates to `true`:
153+
154+
```console
155+
$ node inspect main.js
156+
< Debugger listening on ws://127.0.0.1:9229/ce24daa8-3816-44d4-b8ab-8273c8a66d35
157+
< For help, see: https://nodejs.org/en/docs/inspector
158+
< Debugger attached.
159+
Break on start in main.js:7
160+
5 }
161+
6
162+
> 7 addOne(10);
163+
8 addOne(-1);
164+
9
165+
debug> setBreakpoint('main.js', 4, 'num < 0')
166+
1 'use strict';
167+
2
168+
3 function addOne(num) {
169+
> 4 return num + 1;
170+
5 }
171+
6
172+
7 addOne(10);
173+
8 addOne(-1);
174+
9
175+
debug> cont
176+
break in main.js:4
177+
2
178+
3 function addOne(num) {
179+
> 4 return num + 1;
180+
5 }
181+
6
182+
debug> exec('num')
183+
-1
184+
debug>
185+
```
186+
148187
### Information
149188

150189
* `backtrace`, `bt`: Print backtrace of current execution frame

0 commit comments

Comments
 (0)