Skip to content

Commit

Permalink
debug: add :GoDebugAttach
Browse files Browse the repository at this point in the history
Add :GoDebugAttach to be able to attach to a running process.

Closes #1758
  • Loading branch information
bhcleek committed Aug 29, 2020
1 parent 188884f commit c27eac1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
83 changes: 53 additions & 30 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if !exists('s:state')
\ 'functionArgs': {},
\ 'message': [],
\ 'resultHandlers': {},
\ 'kill_on_detach': v:true,
\ }

if go#util#HasDebug('debugger-state')
Expand Down Expand Up @@ -252,7 +253,7 @@ function! s:clearState() abort
endfunction

function! s:stop() abort
call s:call_jsonrpc(function('s:noop'), 'RPCServer.Detach', {'kill': v:true})
call s:call_jsonrpc(function('s:noop'), 'RPCServer.Detach', {'kill': s:state['kill_on_detach']})

if has_key(s:state, 'job')
call go#job#Wait(s:state['job'])
Expand Down Expand Up @@ -281,6 +282,7 @@ function! go#debug#Stop() abort
endfor
command! -nargs=* -complete=customlist,go#package#Complete GoDebugStart call go#debug#Start(0, <f-args>)
command! -nargs=* -complete=customlist,go#package#Complete GoDebugTest call go#debug#Start(1, <f-args>)
command! -nargs=1 GoDebugAttach call go#debug#Start(2, <f-args>)
command! -nargs=? GoDebugBreakpoint call go#debug#Breakpoint(<f-args>)

" Remove all mappings.
Expand Down Expand Up @@ -689,7 +691,7 @@ endfunction

" Start the debug mode. The first argument is the package name to compile and
" debug, anything else will be passed to the running program.
function! go#debug#Start(is_test, ...) abort
function! go#debug#Start(mode, ...) abort
call go#cmd#autowrite()

if !go#util#has_job()
Expand All @@ -702,7 +704,7 @@ function! go#debug#Start(is_test, ...) abort
return s:state['job']
endif

let s:start_args = [a:is_test] + a:000
let s:start_args = [a:mode] + a:000

if go#util#HasDebug('debugger-state')
call go#config#SetDebugDiag(s:state)
Expand All @@ -714,33 +716,19 @@ function! go#debug#Start(is_test, ...) abort
endif

try
let l:cmd = [
\ dlv,
\ (a:is_test ? 'test' : 'debug'),
\]

" append the package when it's given.
if len(a:000) > 0
let l:pkgname = a:1
if l:pkgname[0] == '.'
let l:pkgabspath = fnamemodify(l:pkgname, ':p')

let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
let l:dir = getcwd()
execute l:cd fnameescape(expand('%:p:h'))

try
let l:pkgname = go#package#FromPath(l:pkgabspath)
if type(l:pkgname) == type(0)
call go#util#EchoError('could not determine package name')
return
endif
finally
execute l:cd fnameescape(l:dir)
endtry
endif

let l:cmd += [l:pkgname]
let l:cmd = [dlv]

let s:state['kill_on_detach'] = v:true
if a:mode is 0
let l:cmd = add(dlv, 'debug')
let l:cmd = extend(l:cmd, s:package(a:000))
elseif a:mode is 1
let l:cmd = add(dlv, 'test')
let l:cmd = extend(l:cmd, s:package(a:000))
elseif a:mode is 2
let l:cmd = add(dlv, 'attach', a:1)
let s:state['kill_on_detach'] = v:false
endif

let l:cmd += [
Expand Down Expand Up @@ -782,7 +770,41 @@ function! go#debug#Start(is_test, ...) abort
return s:state['job']
endfunction

" Translate a reflect kind constant to a human string.
" s:package returns the import path of package name of a :GoDebug(Start|Test)
" call as a list so that the package can be appended to a command list using
" extend(). args is expected to be a (potentially empty_ list. The first
" element in args (if there are any) is expected to be a package path. An
" emnpty list is returned when either args is an empty list or the import path
" cannot be determined.
function! s:package(args)
if len(a:args) == 0
return []
endif

" append the package when it's given.
let l:pkgname = a:args[0]
if l:pkgname[0] == '.'
let l:pkgabspath = fnamemodify(l:pkgname, ':p')

let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
let l:dir = getcwd()
l:dir = go#util#Chdir(fnameescape(expand('%:p:h')))

try
let l:pkgname = go#package#FromPath(l:pkgabspath)
if type(l:pkgname) == type(0)
call go#util#EchoError('could not determine package name')
return []
endif
finally
call go#util#Chdir(fnameescape(l:dir))
endtry
endif

return [l:pkgname]
endfunction

" Translate a reflect kind constant to a human string.
function! s:reflect_kind(k)
" Kind constants from Go's reflect package.
return [
Expand Down Expand Up @@ -1210,6 +1232,7 @@ function! go#debug#Restart() abort
\ 'functionArgs': {},
\ 'message': [],
\ 'resultHandlers': {},
\ 'kill_on_detach': s:state['kill_on_detach'],
\ }

call call('go#debug#Start', s:start_args)
Expand Down
18 changes: 14 additions & 4 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2258,10 +2258,20 @@ the `dlv` process, or |:GoDebugRestart| to recompile the code.
*go-debug-commands*
DEBUGGER COMMANDS~

Only |:GoDebugStart|, `:GoDebugTest`, and |:GoDebugBreakpoint| are available
by default. `:GoDebugContinue` becomes available after running `:GoDebugStart`
or `:GoDebugTest`. The rest of the commands and mappings become available
after executing `:GoDebugContinue`.
Only |:GoDebugAttach|, |:GoDebugStart|, `:GoDebugTest`, and |:GoDebugBreakpoint| are available
by default. `:GoDebugContinue` becomes available after running
|`:GoDebugAttach|, `:GoDebugStart` or `:GoDebugTest`. The rest of the commands
and mappings become available after executing `:GoDebugContinue`.

*:GoDebugAttach*
:GoDebugAttach pid

Start the debug mode for pid; this does several things:

* Setup the debug windows according to |'g:go_debug_windows'|.
* Make the `:GoDebug*` commands and `(go-debug-*)` mappings available.

Use |:GoDebugStop| to stop `dlv` and exit debugging mode.

*:GoDebugStart*
:GoDebugStart [pkg] [program-args]
Expand Down
1 change: 1 addition & 0 deletions ftplugin/go/commands.vim
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ command! -nargs=0 GoFillStruct call go#fillstruct#FillStruct()
if !exists(':GoDebugStart')
command! -nargs=* -complete=customlist,go#package#Complete GoDebugStart call go#debug#Start(0, <f-args>)
command! -nargs=* -complete=customlist,go#package#Complete GoDebugTest call go#debug#Start(1, <f-args>)
command! -nargs=1 GoDebugAttach call go#debug#Start(2, <f-args>)
command! -nargs=? GoDebugBreakpoint call go#debug#Breakpoint(<f-args>)
endif

Expand Down

0 comments on commit c27eac1

Please sign in to comment.