Skip to content

Commit 4551c0a

Browse files
committed
patch 8.1.0091: MS-Windows: Cannot interrupt gdb when program is running
Problem: MS-Windows: Cannot interrupt gdb when program is running. Solution: Add debugbreak() and use it in the terminal debugger. Respect 'modified' in a prompt buffer.
1 parent 9b0c5c2 commit 4551c0a

5 files changed

Lines changed: 77 additions & 12 deletions

File tree

runtime/doc/eval.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ cscope_connection([{num}, {dbpath} [, {prepend}]])
21082108
cursor({lnum}, {col} [, {off}])
21092109
Number move cursor to {lnum}, {col}, {off}
21102110
cursor({list}) Number move cursor to position in {list}
2111+
debugbreak({pid}) Number interrupt process being debugged
21112112
deepcopy({expr} [, {noref}]) any make a full copy of {expr}
21122113
delete({fname} [, {flags}]) Number delete the file or directory {fname}
21132114
deletebufline({expr}, {first}[, {last}])
@@ -3480,6 +3481,11 @@ cursor({list})
34803481
position within a <Tab> or after the last character.
34813482
Returns 0 when the position could be set, -1 otherwise.
34823483

3484+
debugbreak({pid}) *debugbreak()*
3485+
Specifically used to interrupt a program being debugged. It
3486+
will cause process {pid} to get a SIGTRAP. Behavior for other
3487+
processes is undefined. See |terminal-debugger|.
3488+
{only available on MS-Windows}
34833489

34843490
deepcopy({expr} [, {noref}]) *deepcopy()* *E698*
34853491
Make a copy of {expr}. For Numbers and Strings this isn't

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
397400
endfunc
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.
400404
func 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
403412
endfunc
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)
436445
endfunc
@@ -509,6 +518,7 @@ endfunc
509518
func 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 = []
659671
endfunc
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
893904
endfunc
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.
896918
func s:BufRead()
897919
let fname = expand('<afile>:p')

src/evalfunc.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ static void f_cosh(typval_T *argvars, typval_T *rettv);
123123
static void f_count(typval_T *argvars, typval_T *rettv);
124124
static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125125
static void f_cursor(typval_T *argsvars, typval_T *rettv);
126+
#ifdef WIN3264
127+
static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128+
#endif
126129
static void f_deepcopy(typval_T *argvars, typval_T *rettv);
127130
static void f_delete(typval_T *argvars, typval_T *rettv);
128131
static void f_deletebufline(typval_T *argvars, typval_T *rettv);
@@ -577,6 +580,9 @@ static struct fst
577580
{"count", 2, 4, f_count},
578581
{"cscope_connection",0,3, f_cscope_connection},
579582
{"cursor", 1, 3, f_cursor},
583+
#ifdef WIN3264
584+
{"debugbreak", 1, 1, f_debugbreak},
585+
#endif
580586
{"deepcopy", 1, 2, f_deepcopy},
581587
{"delete", 1, 2, f_delete},
582588
{"deletebufline", 2, 3, f_deletebufline},
@@ -2761,6 +2767,33 @@ f_cursor(typval_T *argvars, typval_T *rettv)
27612767
rettv->vval.v_number = 0;
27622768
}
27632769

2770+
#ifdef WIN3264
2771+
/*
2772+
* "debugbreak()" function
2773+
*/
2774+
static void
2775+
f_debugbreak(typval_T *argvars, typval_T *rettv)
2776+
{
2777+
int pid;
2778+
2779+
rettv->vval.v_number = FAIL;
2780+
pid = (int)get_tv_number(&argvars[0]);
2781+
if (pid == 0)
2782+
EMSG(_(e_invarg));
2783+
else
2784+
{
2785+
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2786+
2787+
if (hProcess != NULL)
2788+
{
2789+
DebugBreakProcess(hProcess);
2790+
CloseHandle(hProcess);
2791+
rettv->vval.v_number = OK;
2792+
}
2793+
}
2794+
}
2795+
#endif
2796+
27642797
/*
27652798
* "deepcopy()" function
27662799
*/

src/undo.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3539,7 +3539,9 @@ bufIsChanged(buf_T *buf)
35393539
int
35403540
bufIsChangedNotTerm(buf_T *buf)
35413541
{
3542-
return !bt_dontwrite(buf)
3542+
// In a "prompt" buffer we do respect 'modified', so that we can control
3543+
// closing the window by setting or resetting that option.
3544+
return (!bt_dontwrite(buf) || bt_prompt(buf))
35433545
&& (buf->b_changed || file_ff_differs(buf, TRUE));
35443546
}
35453547

src/version.c

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

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
91,
764766
/**/
765767
90,
766768
/**/

0 commit comments

Comments
 (0)