@@ -98,6 +98,7 @@ func s:StartDebug_internal(dict)
9898 return
9999 endif
100100 let s: ptywin = 0
101+ let s: pid = 0
101102
102103 " Uncomment this line to write logging in "debuglog".
103104 " call ch_logfile('debuglog', 'w')
@@ -271,6 +272,8 @@ func s:StartDebug_prompt(dict)
271272 exe ' bwipe! ' . s: promptbuf
272273 return
273274 endif
275+ " Mark the buffer modified so that it's not easy to close.
276+ set modified
274277 let s: gdb_channel = job_getchannel (s: gdbjob )
275278
276279 " Interpret commands while the target is running. This should usualy only
@@ -396,10 +399,16 @@ func s:PromptCallback(text)
396399 call s: SendCommand (a: text )
397400endfunc
398401
399- " Function called when pressing CTRL-C in the prompt buffer.
402+ " Function called when pressing CTRL-C in the prompt buffer and when placing a
403+ " breakpoint.
400404func s: PromptInterrupt ()
401- call ch_log (' Interrupting gdb' )
402- call job_stop (s: gdbjob , ' int' )
405+ if s: pid == 0
406+ echoerr ' Cannot interrupt gdb, did not find a process ID'
407+ else
408+ call ch_log (' Interrupting gdb' )
409+ " Using job_stop(s:gdbjob, 'int') does not work.
410+ call debugbreak (s: pid )
411+ endif
403412endfunc
404413
405414" Function called when gdb outputs text.
@@ -430,7 +439,7 @@ func s:GdbOutCallback(channel, text)
430439
431440 " Add the output above the current prompt.
432441 call append (line (' $' ) - 1 , text)
433- set nomodified
442+ set modified
434443
435444 call win_gotoid (curwinid)
436445endfunc
@@ -509,6 +518,7 @@ endfunc
509518func s: EndPromptDebug (job, status)
510519 let curwinid = win_getid (winnr ())
511520 call win_gotoid (s: gdbwin )
521+ set nomodified
512522 close
513523 if curwinid != s: gdbwin
514524 call win_gotoid (curwinid)
@@ -535,6 +545,8 @@ func s:CommOutput(chan, msg)
535545 call s: HandleNewBreakpoint (msg)
536546 elseif msg = ~ ' ^=breakpoint-deleted,'
537547 call s: HandleBreakpointDelete (msg)
548+ elseif msg = ~ ' ^=thread-group-started'
549+ call s: HandleProgramRun (msg)
538550 elseif msg = ~ ' ^\^done,value='
539551 call s: HandleEvaluate (msg)
540552 elseif msg = ~ ' ^\^error,msg='
@@ -655,7 +667,7 @@ func s:DeleteCommands()
655667 for val in s: BreakpointSigns
656668 exe " sign undefine debugBreakpoint" . val
657669 endfor
658- unlet s: BreakpointSigns
670+ let s: BreakpointSigns = []
659671endfunc
660672
661673" :Break - Set a breakpoint at the cursor position.
@@ -666,9 +678,7 @@ func s:SetBreakpoint()
666678 if ! s: stopped
667679 let do_continue = 1
668680 if s: way == ' prompt'
669- " Need to send a signal to get the UI to listen. Strangely this is only
670- " needed once.
671- call job_stop (s: gdbjob , ' int' )
681+ call s: PromptInterrupt ()
672682 else
673683 call s: SendCommand (' -exec-interrupt' )
674684 endif
@@ -798,13 +808,13 @@ func s:HandleCursor(msg)
798808 let wid = win_getid (winnr ())
799809
800810 if a: msg = ~ ' ^\*stopped'
811+ call ch_log (' program stopped' )
801812 let s: stopped = 1
802813 elseif a: msg = ~ ' ^\*running'
814+ call ch_log (' program running' )
803815 let s: stopped = 0
804816 endif
805817
806- call s: GotoSourcewinOrCreateIt ()
807-
808818 if a: msg = ~ ' fullname='
809819 let fname = s: GetFullname (a: msg )
810820 else
@@ -813,6 +823,7 @@ func s:HandleCursor(msg)
813823 if a: msg = ~ ' ^\(\*stopped\|=thread-selected\)' && filereadable (fname)
814824 let lnum = substitute (a: msg , ' .*line="\([^"]*\)".*' , ' \1' , ' ' )
815825 if lnum = ~ ' ^[0-9]*$'
826+ call s: GotoSourcewinOrCreateIt ()
816827 if expand (' %:p' ) != fnamemodify (fname, ' :p' )
817828 if &modified
818829 " TODO: find existing window
@@ -828,7 +839,7 @@ func s:HandleCursor(msg)
828839 exe ' sign place ' . s: pc_id . ' line=' . lnum . ' name=debugPC file=' . fname
829840 setlocal signcolumn = yes
830841 endif
831- else
842+ elseif ! s: stopped || fname != ' '
832843 exe ' sign unplace ' . s: pc_id
833844 endif
834845
@@ -892,6 +903,17 @@ func s:HandleBreakpointDelete(msg)
892903 endif
893904endfunc
894905
906+ " Handle the debugged program starting to run.
907+ " Will store the process ID in s:pid
908+ func s: HandleProgramRun (msg)
909+ let nr = substitute (a: msg , ' .*pid="\([0-9]*\)\".*' , ' \1' , ' ' ) + 0
910+ if nr == 0
911+ return
912+ endif
913+ let s: pid = nr
914+ call ch_log (' Detected process ID: ' . s: pid )
915+ endfunc
916+
895917" Handle a BufRead autocommand event: place any signs.
896918func s: BufRead ()
897919 let fname = expand (' <afile>:p' )
0 commit comments