Skip to content

Commit fe38664

Browse files
committed
patch 8.0.1077: no debugger making use of the terminal window
Problem: No debugger making use of the terminal window. Solution: Add the term debugger plugin. So far only displays the current line when stopped.
1 parent 3c51840 commit fe38664

3 files changed

Lines changed: 104 additions & 8 deletions

File tree

Filelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ RT_ALL = \
638638
runtime/pack/dist/opt/matchit/doc/tags \
639639
runtime/pack/dist/opt/shellmenu/plugin/shellmenu.vim \
640640
runtime/pack/dist/opt/swapmouse/plugin/swapmouse.vim \
641+
runtime/pack/dist/opt/termdebug/plugin/termdebug.vim \
641642

642643
# runtime files for all distributions without CR-NL translation
643644
RT_ALL_BIN = \
Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,128 @@
1-
" Debugger commands.
1+
" Debugger plugin using gdb.
22
"
33
" WORK IN PROGRESS - much doesn't work yet
44
"
5-
" Open two terminal windows:
5+
" Open two visible terminal windows:
66
" 1. run a pty, as with ":term NONE"
77
" 2. run gdb, passing the pty
8-
" The current window is used to edit source code and follows gdb.
8+
" The current window is used to view source code and follows gdb.
9+
"
10+
" A third terminal window is hidden, it is used for communication with gdb.
11+
"
12+
" The communication with gdb uses GDB/MI. See:
13+
" https://sourceware.org/gdb/current/onlinedocs/gdb/GDB_002fMI.html
914
"
1015
" Author: Bram Moolenaar
11-
" Copyright: Vim license applies
16+
" Copyright: Vim license applies, see ":help license"
1217

18+
" The command that starts debugging, e.g. ":Termdebug vim".
19+
" To end type "quit" in the gdb window.
1320
command -nargs=* -complete=file Termdebug call s:StartDebug(<q-args>)
1421

22+
" Name of the gdb command, defaults to "gdb".
1523
if !exists('debugger')
1624
let debugger = 'gdb'
1725
endif
1826

27+
" Sign used to highlight the line where the program has stopped.
28+
sign define debugPC linehl=debugPC
29+
if &background == 'light'
30+
hi debugPC term=reverse ctermbg=lightblue guibg=lightblue
31+
else
32+
hi debugPC term=reverse ctermbg=darkblue guibg=darkblue
33+
endif
34+
let s:pc_id = 12
35+
1936
func s:StartDebug(cmd)
37+
let s:startwin = win_getid(winnr())
38+
let s:startsigncolumn = &signcolumn
39+
2040
" Open a terminal window without a job, to run the debugged program
21-
let s:ptybuf = term_start('NONE', {})
22-
let pty = job_info(term_getjob(s:ptybuf))['tty']
41+
let s:ptybuf = term_start('NONE', {
42+
\ 'term_name': 'gdb program',
43+
\ })
44+
if s:ptybuf == 0
45+
echoerr 'Failed to open the program terminal window'
46+
return
47+
endif
48+
let pty = job_info(term_getjob(s:ptybuf))['tty_out']
49+
50+
" Create a hidden terminal window to communicate with gdb
51+
let s:commbuf = term_start('NONE', {
52+
\ 'term_name': 'gdb communication',
53+
\ 'out_cb': function('s:CommOutput'),
54+
\ 'hidden': 1,
55+
\ })
56+
if s:commbuf == 0
57+
echoerr 'Failed to open the communication terminal window'
58+
exe 'bwipe! ' . s:ptybuf
59+
return
60+
endif
61+
let commpty = job_info(term_getjob(s:commbuf))['tty_out']
2362

2463
" Open a terminal window to run the debugger.
2564
let cmd = [g:debugger, '-tty', pty, a:cmd]
2665
echomsg 'executing "' . join(cmd) . '"'
2766
let gdbbuf = term_start(cmd, {
2867
\ 'exit_cb': function('s:EndDebug'),
29-
\ 'term_finish': 'close'
68+
\ 'term_finish': 'close',
3069
\ })
70+
if gdbbuf == 0
71+
echoerr 'Failed to open the gdb terminal window'
72+
exe 'bwipe! ' . s:ptybuf
73+
exe 'bwipe! ' . s:commbuf
74+
return
75+
endif
76+
77+
" Connect gdb to the communication pty, using the GDB/MI interface
78+
call term_sendkeys(gdbbuf, 'new-ui mi ' . commpty . "\r")
3179
endfunc
3280

3381
func s:EndDebug(job, status)
34-
exe 'bwipe! ' . s:ptybuf
82+
exe 'bwipe! ' . s:ptybuf
83+
exe 'bwipe! ' . s:commbuf
84+
call setwinvar(s:startwin, '&signcolumn', s:startsigncolumn)
85+
endfunc
86+
87+
" Handle a message received from gdb on the GDB/MI interface.
88+
func s:CommOutput(chan, msg)
89+
let msgs = split(a:msg, "\r")
90+
91+
for msg in msgs
92+
" remove prefixed NL
93+
if msg[0] == "\n"
94+
let msg = msg[1:]
95+
endif
96+
if msg != ''
97+
if msg =~ '^\*\(stopped\|running\)'
98+
let wid = win_getid(winnr())
99+
100+
if win_gotoid(s:startwin)
101+
if msg =~ '^\*stopped'
102+
" TODO: proper parsing
103+
let fname = substitute(msg, '.*fullname="\([^"]*\)".*', '\1', '')
104+
let lnum = substitute(msg, '.*line="\([^"]*\)".*', '\1', '')
105+
if lnum =~ '^[0-9]*$'
106+
if expand('%:h') != fname
107+
if &modified
108+
" TODO: find existing window
109+
exe 'split ' . fnameescape(fname)
110+
let s:startwin = win_getid(winnr())
111+
else
112+
exe 'edit ' . fnameescape(fname)
113+
endif
114+
endif
115+
exe lnum
116+
exe 'sign place ' . s:pc_id . ' line=' . lnum . ' name=debugPC file=' . fnameescape(fname)
117+
setlocal signcolumn=yes
118+
endif
119+
else
120+
exe 'sign unplace ' . s:pc_id
121+
endif
122+
123+
call win_gotoid(wid)
124+
endif
125+
endif
126+
endif
127+
endfor
35128
endfunc

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+
1077,
772774
/**/
773775
1076,
774776
/**/

0 commit comments

Comments
 (0)