@@ -20,6 +20,9 @@ if exists(':Termdebug')
2020 finish
2121endif
2222
23+ " Uncomment this line to write logging in "debuglog".
24+ " call ch_logfile('debuglog', 'w')
25+
2326" The command that starts debugging, e.g. ":Termdebug vim".
2427" To end type "quit" in the gdb window.
2528command -nargs =* -complete =file Termdebug call s: StartDebug (<q-args> )
3134
3235let s: pc_id = 12
3336let s: break_id = 13
37+ let s: stopped = 1
3438
3539if &background == ' light'
3640 hi default debugPC term = reverse ctermbg= lightblue guibg= lightblue
@@ -83,11 +87,11 @@ func s:StartDebug(cmd)
8387 " Add -quiet to avoid the intro message causing a hit-enter prompt.
8488 let cmd = [g: termdebugger , ' -quiet' , ' -tty' , pty, a: cmd ]
8589 echomsg ' executing "' . join (cmd) . ' "'
86- let gdbbuf = term_start (cmd, {
90+ let s: gdbbuf = term_start (cmd, {
8791 \ ' exit_cb' : function (' s:EndDebug' ),
8892 \ ' term_finish' : ' close' ,
8993 \ })
90- if gdbbuf == 0
94+ if s: gdbbuf == 0
9195 echoerr ' Failed to open the gdb terminal window'
9296 exe ' bwipe! ' . s: ptybuf
9397 exe ' bwipe! ' . s: commbuf
@@ -97,7 +101,12 @@ func s:StartDebug(cmd)
97101
98102 " Connect gdb to the communication pty, using the GDB/MI interface
99103 " If you get an error "undefined command" your GDB is too old.
100- call term_sendkeys (gdbbuf, ' new-ui mi ' . commpty . " \r " )
104+ call term_sendkeys (s: gdbbuf , ' new-ui mi ' . commpty . " \r " )
105+
106+ " Interpret commands while the target is running. This should usualy only be
107+ " exec-interrupt, since many commands don't work properly while the target is
108+ " running.
109+ call s: SendCommand (' -gdb-set mi-async on' )
101110
102111 " Sign used to highlight the line where the program has stopped.
103112 " There can be only one.
@@ -170,6 +179,9 @@ func s:InstallCommands()
170179 command Step call s: SendCommand (' -exec-step' )
171180 command Over call s: SendCommand (' -exec-next' )
172181 command Finish call s: SendCommand (' -exec-finish' )
182+ command -nargs =* Run call s: Run (<q-args> )
183+ command -nargs =* Arguments call s: SendCommand (' -exec-arguments ' . <q-args> )
184+ command Stop call s: SendCommand (' -exec-interrupt' )
173185 command Continue call s: SendCommand (' -exec-continue' )
174186 command - range -nargs =* Evaluate call s: Evaluate (<range> , <q-args> )
175187 command Gdb call win_gotoid (s: gdbwin )
@@ -183,6 +195,7 @@ func s:InstallCommands()
183195 nnoremenu WinBar.Next :Over <CR>
184196 nnoremenu WinBar.Finish :Finish <CR>
185197 nnoremenu WinBar.Cont :Continue <CR>
198+ nnoremenu WinBar.Stop :Stop <CR>
186199 nnoremenu WinBar.Eval :Evaluate <CR>
187200 endif
188201endfunc
@@ -194,6 +207,9 @@ func s:DeleteCommands()
194207 delcommand Step
195208 delcommand Over
196209 delcommand Finish
210+ delcommand Run
211+ delcommand Arguments
212+ delcommand Stop
197213 delcommand Continue
198214 delcommand Evaluate
199215 delcommand Gdb
@@ -206,6 +222,7 @@ func s:DeleteCommands()
206222 aunmenu WinBar.Next
207223 aunmenu WinBar.Finish
208224 aunmenu WinBar.Cont
225+ aunmenu WinBar.Stop
209226 aunmenu WinBar.Eval
210227 endif
211228
@@ -220,8 +237,19 @@ endfunc
220237
221238" :Break - Set a breakpoint at the cursor position.
222239func s: SetBreakpoint ()
223- call term_sendkeys (s: commbuf , ' -break-insert --source '
224- \ . fnameescape (expand (' %:p' )) . ' --line ' . line (' .' ) . " \r " )
240+ " Setting a breakpoint may not work while the program is running.
241+ " Interrupt to make it work.
242+ let do_continue = 0
243+ if ! s: stopped
244+ let do_continue = 1
245+ call s: SendCommand (' -exec-interrupt' )
246+ sleep 10 m
247+ endif
248+ call s: SendCommand (' -break-insert --source '
249+ \ . fnameescape (expand (' %:p' )) . ' --line ' . line (' .' ))
250+ if do_continue
251+ call s: SendCommand (' -exec-continue' )
252+ endif
225253endfunc
226254
227255" :Delete - Delete a breakpoint at the cursor position.
@@ -244,6 +272,13 @@ func s:SendCommand(cmd)
244272 call term_sendkeys (s: commbuf , a: cmd . " \r " )
245273endfunc
246274
275+ func s: Run (args )
276+ if a: args != ' '
277+ call s: SendCommand (' -exec-arguments ' . a: args )
278+ endif
279+ call s: SendCommand (' -exec-run' )
280+ endfunc
281+
247282" :Evaluate - evaluate what is under the cursor
248283func s: Evaluate (range , arg)
249284 if a: arg != ' '
@@ -259,7 +294,7 @@ func s:Evaluate(range, arg)
259294 else
260295 let expr = expand (' <cexpr>' )
261296 endif
262- call term_sendkeys ( s: commbuf , ' -data-evaluate-expression "' . expr . " \" \r " )
297+ call s: SendCommand ( ' -data-evaluate-expression "' . expr . ' " ' )
263298 let s: evalexpr = expr
264299endfunc
265300
@@ -286,6 +321,12 @@ endfunc
286321func s: HandleCursor (msg)
287322 let wid = win_getid (winnr ())
288323
324+ if a: msg = ~ ' ^\*stopped'
325+ let s: stopped = 1
326+ elseif a: msg = ~ ' ^\*running'
327+ let s: stopped = 0
328+ endif
329+
289330 if win_gotoid (s: startwin )
290331 let fname = substitute (a: msg , ' .*fullname="\([^"]*\)".*' , ' \1' , ' ' )
291332 if a: msg = ~ ' ^\(\*stopped\|=thread-selected\)' && filereadable (fname)
0 commit comments