Skip to content

Commit 32c67ba

Browse files
committed
patch 8.0.1725: terminal debugger doesn't handle command arguments
Problem: Terminal debugger doesn't handle command arguments. Solution: Add the :TermdebugCommand command. Use a ! to execute right away. (Christian Brabandt)
1 parent 06965b8 commit 32c67ba

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

runtime/doc/terminal.txt

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ Starting ~
623623
Load the plugin with this command: >
624624
packadd termdebug
625625
< *:Termdebug*
626-
To start debugging use `:Termdebug` followed by the command name, for example: >
626+
To start debugging use `:Termdebug` or `:TermdebugCommand`` followed by the
627+
command name, for example: >
627628
:Termdebug vim
628629
629630
This opens two windows:
@@ -641,7 +642,8 @@ source file location will be displayed, if possible. A sign is used to
641642
highlight the current position, using highlight group debugPC.
642643

643644
If the buffer in the current window is modified, another window will be opened
644-
to display the current gdb position.
645+
to display the current gdb position. You can use `:Winbar` to add a window
646+
toolbar there.
645647

646648
Focus the terminal of the executed program to interact with it. This works
647649
the same as any command running in a terminal window.
@@ -650,12 +652,25 @@ When the debugger ends, typically by typing "quit" in the gdb window, the two
650652
opened windows are closed.
651653

652654
Only one debugger can be active at a time.
653-
654-
To attach gdb to an already running executable, or use a core file, pass extra
655+
*:TermdebugCommand*
656+
If you want to give specific commands to the command being debugged, you can
657+
use the `:TermdebugCommand` command followed by the command name and
658+
additional parameters. >
659+
:TermdebugCommand vim --clean -c ':set nu'
660+
661+
Both the `:Termdebug` and `:TermdebugCommand` support an optional "!" bang
662+
argument to start the command right away, without pausing at the gdb window
663+
(and cursor will be in the debugged window). For example: >
664+
:TermdebugCommand! vim --clean
665+
666+
To attach gdb to an already running executable or use a core file, pass extra
655667
arguments. E.g.: >
656668
:Termdebug vim core
657669
:Termdebug vim 98343
658670
671+
If no argument is given, you'll end up in a gdb window, in which you need to
672+
specify which command to run using e.g. the gdb `file` command.
673+
659674

660675
Example session ~
661676
*termdebug-example*
@@ -728,18 +743,20 @@ Put focus on the gdb window to type commands there. Some common ones are:
728743
- frame N go to the Nth stack frame
729744
- continue continue execution
730745

731-
In the window showing the source code these commands can be used to control gdb:
746+
*:Run* *:Arguments*
747+
In the window showing the source code these commands can be used to control
748+
gdb:
732749
`:Run` [args] run the program with [args] or the previous arguments
733750
`:Arguments` {args} set arguments for the next `:Run`
734751

735-
`:Break` set a breakpoint at the current line; a sign will be displayed
736-
`:Clear` delete the breakpoint at the current line
752+
*:Break* set a breakpoint at the current line; a sign will be displayed
753+
*:Clear* delete the breakpoint at the current line
737754

738-
`:Step` execute the gdb "step" command
739-
`:Over` execute the gdb "next" command (`:Next` is a Vim command)
740-
`:Finish` execute the gdb "finish" command
741-
`:Continue` execute the gdb "continue" command
742-
`:Stop` interrupt the program
755+
*:Step* execute the gdb "step" command
756+
*:Over* execute the gdb "next" command (`:Next` is a Vim command)
757+
*:Finish* execute the gdb "finish" command
758+
*:Continue* execute the gdb "continue" command
759+
*:Stop* interrupt the program
743760

744761
If 'mouse' is set the plugin adds a window toolbar with these entries:
745762
Step `:Step`
@@ -750,7 +767,7 @@ If 'mouse' is set the plugin adds a window toolbar with these entries:
750767
Eval `:Evaluate`
751768
This way you can use the mouse to perform the most common commands. You need
752769
to have the 'mouse' option set to enable mouse clicks.
753-
770+
*:Winbar*
754771
You can add the window toolbar in other windows you open with: >
755772
:Winbar
756773
@@ -761,7 +778,7 @@ abandoned.
761778

762779

763780
Inspecting variables ~
764-
*termdebug-variables*
781+
*termdebug-variables* *:Evaluate*
765782
`:Evaluate` evaluate the expression under the cursor
766783
`K` same
767784
`:Evaluate` {expr} evaluate {expr}
@@ -773,9 +790,9 @@ You can usually shorten `:Evaluate` to `:Ev`.
773790

774791
Other commands ~
775792
*termdebug-commands*
776-
:Gdb jump to the gdb window
777-
:Program jump to the window with the running program
778-
:Source jump to the window with the source code, create it if there
793+
*:Gdb* jump to the gdb window
794+
*:Program* jump to the window with the running program
795+
*:Source* jump to the window with the source code, create it if there
779796
isn't one
780797

781798

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ endif
2525

2626
" The command that starts debugging, e.g. ":Termdebug vim".
2727
" To end type "quit" in the gdb window.
28-
command -nargs=* -complete=file Termdebug call s:StartDebug(<f-args>)
28+
command -nargs=* -complete=file -bang Termdebug call s:StartDebug(<bang>0, <f-args>)
29+
command -nargs=+ -complete=file -bang TermdebugCommand call s:StartDebugCommand(<bang>0, <f-args>)
2930

3031
" Name of the gdb command, defaults to "gdb".
3132
if !exists('termdebugger')
@@ -43,7 +44,17 @@ else
4344
endif
4445
hi default debugBreakpoint term=reverse ctermbg=red guibg=red
4546

46-
func s:StartDebug(...)
47+
func s:StartDebug(bang, ...)
48+
" First argument is the command to debug, second core file or process ID.
49+
call s:StartDebug_internal({'gdb_args': a:000, 'bang': a:bang})
50+
endfunc
51+
52+
func s:StartDebugCommand(bang, ...)
53+
" First argument is the command to debug, rest are run arguments.
54+
call s:StartDebug_internal({'gdb_args': [a:1], 'proc_args': a:000[1:], 'bang': a:bang})
55+
endfunc
56+
57+
func s:StartDebug_internal(dict)
4758
if exists('s:gdbwin')
4859
echoerr 'Terminal debugger already running'
4960
return
@@ -95,7 +106,10 @@ func s:StartDebug(...)
95106

96107
" Open a terminal window to run the debugger.
97108
" Add -quiet to avoid the intro message causing a hit-enter prompt.
98-
let cmd = [g:termdebugger, '-quiet', '-tty', pty] + a:000
109+
let gdb_args = get(a:dict, 'gdb_args', [])
110+
let proc_args = get(a:dict, 'proc_args', [])
111+
112+
let cmd = [g:termdebugger, '-quiet', '-tty', pty] + gdb_args
99113
echomsg 'executing "' . join(cmd) . '"'
100114
let s:gdbbuf = term_start(cmd, {
101115
\ 'exit_cb': function('s:EndDebug'),
@@ -109,6 +123,11 @@ func s:StartDebug(...)
109123
endif
110124
let s:gdbwin = win_getid(winnr())
111125

126+
" Set arguments to be run
127+
if len(proc_args)
128+
call term_sendkeys(s:gdbbuf, 'set args ' . join(proc_args) . "\r")
129+
endif
130+
112131
" Connect gdb to the communication pty, using the GDB/MI interface
113132
call term_sendkeys(s:gdbbuf, 'new-ui mi ' . commpty . "\r")
114133

@@ -182,6 +201,14 @@ func s:StartDebug(...)
182201
au BufRead * call s:BufRead()
183202
au BufUnload * call s:BufUnloaded()
184203
augroup END
204+
205+
" Run the command if the bang attribute was given
206+
" and got to the window
207+
if get(a:dict, 'bang', 0)
208+
call s:SendCommand('-exec-run')
209+
call win_gotoid(s:ptywin)
210+
endif
211+
185212
endfunc
186213

187214
func s:EndDebug(job, status)

src/version.c

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

763763
static int included_patches[] =
764764
{ /* Add new patch number below this line */
765+
/**/
766+
1725,
765767
/**/
766768
1724,
767769
/**/

0 commit comments

Comments
 (0)