Skip to content

Commit 45d5f26

Browse files
committed
patch 8.0.1092: terminal debugger can't evaluate expressions
Problem: Terminal debugger can't evaluate expressions. Solution: Add :Evaluate and K. Various other improvements.
1 parent 95c83c6 commit 45d5f26

3 files changed

Lines changed: 93 additions & 22 deletions

File tree

runtime/doc/terminal.txt

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*terminal.txt* For Vim version 8.0. Last change: 2017 Sep 09
1+
*terminal.txt* For Vim version 8.0. Last change: 2017 Sep 10
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -318,11 +318,12 @@ To start debugging use `:TermDebug` folowed by the command name, for example: >
318318
:TermDebug vim
319319
320320
This opens two windows:
321-
- A terminal window in which "gdb vim" is executed. Here you can directly
322-
interact with gdb. The buffer name is "!gdb".
323-
- A terminal window for the executed program. When "run" is used in gdb the
324-
program I/O will happen in this window, so that it does not interfere with
325-
controlling gdb. The buffer name is "gdb program".
321+
gdb window A terminal window in which "gdb vim" is executed. Here you
322+
can directly interact with gdb. The buffer name is "!gdb".
323+
program window A terminal window for the executed program. When "run" is
324+
used in gdb the program I/O will happen in this window, so
325+
that it does not interfere with controlling gdb. The buffer
326+
name is "gdb program".
326327

327328
The current window is used to show the source code. When gdb pauses the
328329
source file location will be displayed, if possible. A sign is used to
@@ -334,7 +335,8 @@ to display the current gdb position.
334335
Focus the terminal of the executed program to interact with it. This works
335336
the same as any command running in a terminal window.
336337

337-
When the debugger ends the two opened windows are closed.
338+
When the debugger ends, typically by typing "quit" in the gdb window, the two
339+
opened windows are closed.
338340

339341

340342
Stepping through code ~
@@ -349,13 +351,29 @@ Put focus on the gdb window to type commands there. Some common ones are:
349351
- frame N go to the Nth stack frame
350352
- continue continue execution
351353

352-
In the window showing the source code some commands can passed to gdb:
353-
- Break set a breakpoint at the current line; a sign will be displayed
354-
- Delete delete a breakpoint at the current line
355-
- Step execute the gdb "step" command
356-
- NNext execute the gdb "next" command (:Next is a Vim command)
357-
- Finish execute the gdb "finish" command
358-
- Continue execute the gdb "continue" command
354+
In the window showing the source code some commands can used to control gdb:
355+
:Break set a breakpoint at the current line; a sign will be displayed
356+
:Delete delete a breakpoint at the current line
357+
:Step execute the gdb "step" command
358+
:Over execute the gdb "next" command (:Next is a Vim command)
359+
:Finish execute the gdb "finish" command
360+
:Continue execute the gdb "continue" command
361+
362+
363+
Inspecting variables ~
364+
365+
:Evaluate evaluate the expression under the cursor
366+
K same
367+
:Evaluate {expr} evaluate {expr}
368+
:'<,'>Evaluate evaluate the Visually selected text
369+
370+
This is similar to using "print" in the gdb window.
371+
372+
373+
Other commands ~
374+
375+
:Gdb jump to the gdb window
376+
:Program jump to the window with the running program
359377

360378

361379
Communication ~
@@ -386,9 +404,5 @@ When 'background' is "dark":
386404
hi debugBreakpoint term=reverse ctermbg=red guibg=red
387405

388406

389-
NOT WORKING YET: ~
390-
391-
Values of variables can be inspected, etc.
392-
393407

394408
vim:tw=78:ts=8:ft=help:norl:

runtime/pack/dist/opt/termdebug/plugin/termdebug.vim

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func s:StartDebug(cmd)
5454
return
5555
endif
5656
let pty = job_info(term_getjob(s:ptybuf))['tty_out']
57+
let s:ptywin = win_getid(winnr())
5758

5859
" Create a hidden terminal window to communicate with gdb
5960
let s:commbuf = term_start('NONE', {
@@ -81,12 +82,15 @@ func s:StartDebug(cmd)
8182
exe 'bwipe! ' . s:commbuf
8283
return
8384
endif
85+
let s:gdbwin = win_getid(winnr())
8486

8587
" Connect gdb to the communication pty, using the GDB/MI interface
8688
call term_sendkeys(gdbbuf, 'new-ui mi ' . commpty . "\r")
8789

88-
" Install debugger commands.
90+
" Install debugger commands in the text window.
91+
call win_gotoid(s:startwin)
8992
call s:InstallCommands()
93+
call win_gotoid(s:gdbwin)
9094

9195
let s:breakpoints = {}
9296
endfunc
@@ -116,10 +120,14 @@ func s:CommOutput(chan, msg)
116120
if msg != ''
117121
if msg =~ '^\*\(stopped\|running\)'
118122
call s:HandleCursor(msg)
119-
elseif msg =~ '^\^done,bkpt='
123+
elseif msg =~ '^\^done,bkpt=' || msg =~ '^=breakpoint-created,'
120124
call s:HandleNewBreakpoint(msg)
121125
elseif msg =~ '^=breakpoint-deleted,'
122126
call s:HandleBreakpointDelete(msg)
127+
elseif msg =~ '^\^done,value='
128+
call s:HandleEvaluate(msg)
129+
elseif msg =~ '^\^error,msg='
130+
call s:HandleError(msg)
123131
endif
124132
endif
125133
endfor
@@ -130,19 +138,37 @@ func s:InstallCommands()
130138
command Break call s:SetBreakpoint()
131139
command Delete call s:DeleteBreakpoint()
132140
command Step call s:SendCommand('-exec-step')
133-
command NNext call s:SendCommand('-exec-next')
141+
command Over call s:SendCommand('-exec-next')
134142
command Finish call s:SendCommand('-exec-finish')
135143
command Continue call s:SendCommand('-exec-continue')
144+
command -range -nargs=* Evaluate call s:Evaluate(<range>, <q-args>)
145+
command Gdb call win_gotoid(s:gdbwin)
146+
command Program call win_gotoid(s:ptywin)
147+
148+
" TODO: can the K mapping be restored?
149+
nnoremap K :Evaluate<CR>
136150
endfunc
137151

138152
" Delete installed debugger commands in the current window.
139153
func s:DeleteCommands()
140154
delcommand Break
141155
delcommand Delete
142156
delcommand Step
143-
delcommand NNext
157+
delcommand Over
144158
delcommand Finish
145159
delcommand Continue
160+
delcommand Evaluate
161+
delcommand Gdb
162+
delcommand Program
163+
164+
nunmap K
165+
sign undefine debugPC
166+
sign undefine debugBreakpoint
167+
exe 'sign unplace ' . s:pc_id
168+
for key in keys(s:breakpoints)
169+
exe 'sign unplace ' . (s:break_id + key)
170+
endfor
171+
unlet s:breakpoints
146172
endfunc
147173

148174
" :Break - Set a breakpoint at the cursor position.
@@ -171,6 +197,35 @@ func s:SendCommand(cmd)
171197
call term_sendkeys(s:commbuf, a:cmd . "\r")
172198
endfunc
173199

200+
" :Evaluate - evaluate what is under the cursor
201+
func s:Evaluate(range, arg)
202+
if a:arg != ''
203+
let expr = a:arg
204+
elseif a:range == 2
205+
let pos = getcurpos()
206+
let reg = getreg('v', 1, 1)
207+
let regt = getregtype('v')
208+
normal! gv"vy
209+
let expr = @v
210+
call setpos('.', pos)
211+
call setreg('v', reg, regt)
212+
else
213+
let expr = expand('<cexpr>')
214+
endif
215+
call term_sendkeys(s:commbuf, '-data-evaluate-expression "' . expr . "\"\r")
216+
let s:evalexpr = expr
217+
endfunc
218+
219+
" Handle the result of data-evaluate-expression
220+
func s:HandleEvaluate(msg)
221+
echomsg '"' . s:evalexpr . '": ' . substitute(a:msg, '.*value="\(.*\)"', '\1', '')
222+
endfunc
223+
224+
" Handle an error.
225+
func s:HandleError(msg)
226+
echoerr substitute(a:msg, '.*msg="\(.*\)"', '\1', '')
227+
endfunc
228+
174229
" Handle stopping and running message from gdb.
175230
" Will update the sign that shows the current position.
176231
func s:HandleCursor(msg)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
1092,
772774
/**/
773775
1091,
774776
/**/

0 commit comments

Comments
 (0)