forked from aniani/vim
runtime(termdebug): Refactored StartDebug_term and EndDebug functions
- Functions are way too long. Readability and maintainability should be slightly improved. - Some variables are re-assigned to their initial value at teardown. This should not be needed since all internal variables are re-initialized at startup of every Termdebug session. closes: #15086 Signed-off-by: Ubaldo Tiberi <ubaldo.tiberi@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
parent
01a4fb104d
commit
225d4d9212
136
runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
vendored
136
runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
vendored
@ -86,7 +86,7 @@ var varbufnr: number
|
|||||||
var varbufname: string
|
var varbufname: string
|
||||||
var asmbufnr: number
|
var asmbufnr: number
|
||||||
var asmbufname: string
|
var asmbufname: string
|
||||||
var promptbuf: number
|
var promptbufnr: number
|
||||||
# This is for the "debugged-program" thing
|
# This is for the "debugged-program" thing
|
||||||
var ptybufnr: number
|
var ptybufnr: number
|
||||||
var ptybufname: string
|
var ptybufname: string
|
||||||
@ -167,7 +167,7 @@ def InitScriptVariables()
|
|||||||
varbufname = 'Termdebug-variables-listing'
|
varbufname = 'Termdebug-variables-listing'
|
||||||
asmbufnr = 0
|
asmbufnr = 0
|
||||||
asmbufname = 'Termdebug-asm-listing'
|
asmbufname = 'Termdebug-asm-listing'
|
||||||
promptbuf = 0
|
promptbufnr = 0
|
||||||
# This is for the "debugged-program" thing
|
# This is for the "debugged-program" thing
|
||||||
ptybufname = "debugged-program"
|
ptybufname = "debugged-program"
|
||||||
ptybufnr = 0
|
ptybufnr = 0
|
||||||
@ -265,7 +265,7 @@ enddef
|
|||||||
|
|
||||||
# Define the default highlighting, using the current 'background' value.
|
# Define the default highlighting, using the current 'background' value.
|
||||||
def InitHighlight()
|
def InitHighlight()
|
||||||
Highlight(1, '', &background)
|
Highlight(true, '', &background)
|
||||||
hi default debugBreakpoint term=reverse ctermbg=red guibg=red
|
hi default debugBreakpoint term=reverse ctermbg=red guibg=red
|
||||||
hi default debugBreakpointDisabled term=reverse ctermbg=gray guibg=gray
|
hi default debugBreakpointDisabled term=reverse ctermbg=gray guibg=gray
|
||||||
enddef
|
enddef
|
||||||
@ -309,9 +309,6 @@ def StartDebug_internal(dict: dict<any>)
|
|||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Uncomment this line to write logging in "debuglog".
|
|
||||||
# call ch_logfile('debuglog', 'w')
|
|
||||||
|
|
||||||
InitScriptVariables()
|
InitScriptVariables()
|
||||||
if !SanityCheck()
|
if !SanityCheck()
|
||||||
return
|
return
|
||||||
@ -321,6 +318,9 @@ def StartDebug_internal(dict: dict<any>)
|
|||||||
doauto <nomodeline> User TermdebugStartPre
|
doauto <nomodeline> User TermdebugStartPre
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# Uncomment this line to write logging in "debuglog".
|
||||||
|
# call ch_logfile('debuglog', 'w')
|
||||||
|
|
||||||
# Assume current window is the source code window
|
# Assume current window is the source code window
|
||||||
sourcewin = win_getid()
|
sourcewin = win_getid()
|
||||||
var wide = 0
|
var wide = 0
|
||||||
@ -368,38 +368,29 @@ enddef
|
|||||||
|
|
||||||
# Use when debugger didn't start or ended.
|
# Use when debugger didn't start or ended.
|
||||||
def CloseBuffers()
|
def CloseBuffers()
|
||||||
var buf_numbers = [ptybufnr, commbufnr, asmbufnr, varbufnr]
|
var buf_numbers = [promptbufnr, ptybufnr, commbufnr, asmbufnr, varbufnr]
|
||||||
for buf_nr in buf_numbers
|
for buf_nr in buf_numbers
|
||||||
if buf_nr > 0 && bufexists(buf_nr)
|
if buf_nr > 0 && bufexists(buf_nr)
|
||||||
exe $'bwipe! {buf_nr}'
|
exe $'bwipe! {buf_nr}'
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
running = false
|
|
||||||
gdbwin = 0
|
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def IsGdbStarted(): bool
|
def IsGdbStarted(): bool
|
||||||
var gdbproc_status = job_status(term_getjob(gdbbufnr))
|
var gdbproc_status = job_status(term_getjob(gdbbufnr))
|
||||||
if gdbproc_status !=# 'run'
|
if gdbproc_status !=# 'run'
|
||||||
var cmd_name = string(GetCommand()[0])
|
|
||||||
Echoerr($'{cmd_name} exited unexpectedly')
|
|
||||||
CloseBuffers()
|
|
||||||
return false
|
return false
|
||||||
endif
|
endif
|
||||||
return true
|
return true
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
# Open a terminal window without a job, to run the debugged program in.
|
def CreateProgramPty(): string
|
||||||
def StartDebug_term(dict: dict<any>)
|
|
||||||
ptybufnr = term_start('NONE', {
|
ptybufnr = term_start('NONE', {
|
||||||
term_name: ptybufname,
|
term_name: ptybufname,
|
||||||
vertical: vvertical})
|
vertical: vvertical})
|
||||||
if ptybufnr == 0
|
if ptybufnr == 0
|
||||||
Echoerr('Failed to open the program terminal window')
|
return null_string
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
var pty = job_info(term_getjob(ptybufnr))['tty_out']
|
|
||||||
ptywin = win_getid()
|
ptywin = win_getid()
|
||||||
|
|
||||||
if vvertical
|
if vvertical
|
||||||
@ -412,6 +403,10 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
return job_info(term_getjob(ptybufnr))['tty_out']
|
||||||
|
enddef
|
||||||
|
|
||||||
|
def CreateCommunicationPty(): string
|
||||||
# Create a hidden terminal window to communicate with gdb
|
# Create a hidden terminal window to communicate with gdb
|
||||||
commbufnr = term_start('NONE', {
|
commbufnr = term_start('NONE', {
|
||||||
term_name: commbufname,
|
term_name: commbufname,
|
||||||
@ -419,12 +414,12 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
hidden: 1
|
hidden: 1
|
||||||
})
|
})
|
||||||
if commbufnr == 0
|
if commbufnr == 0
|
||||||
Echoerr('Failed to open the communication terminal window')
|
return null_string
|
||||||
exe $'bwipe! {ptybufnr}'
|
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
var commpty = job_info(term_getjob(commbufnr))['tty_out']
|
return job_info(term_getjob(commbufnr))['tty_out']
|
||||||
|
enddef
|
||||||
|
|
||||||
|
def CreateGdbConsole(dict: dict<any>, pty: string, commpty: string): string
|
||||||
# Start the gdb buffer
|
# Start the gdb buffer
|
||||||
var gdb_args = get(dict, 'gdb_args', [])
|
var gdb_args = get(dict, 'gdb_args', [])
|
||||||
var proc_args = get(dict, 'proc_args', [])
|
var proc_args = get(dict, 'proc_args', [])
|
||||||
@ -464,9 +459,7 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
term_finish: 'close',
|
term_finish: 'close',
|
||||||
})
|
})
|
||||||
if gdbbufnr == 0
|
if gdbbufnr == 0
|
||||||
Echoerr('Failed to open the gdb terminal window')
|
return 'Failed to open the gdb terminal window'
|
||||||
CloseBuffers()
|
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
gdbwin = win_getid()
|
gdbwin = win_getid()
|
||||||
|
|
||||||
@ -480,8 +473,7 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
var success = false
|
var success = false
|
||||||
while !success && counter < counter_max
|
while !success && counter < counter_max
|
||||||
if !IsGdbStarted()
|
if !IsGdbStarted()
|
||||||
CloseBuffers()
|
return $'{gdbbufname} exited unexpectedly'
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
for lnum in range(1, 200)
|
for lnum in range(1, 200)
|
||||||
@ -496,9 +488,7 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
endwhile
|
endwhile
|
||||||
|
|
||||||
if !success
|
if !success
|
||||||
Echoerr('Failed to startup the gdb program.')
|
return 'Failed to startup the gdb program.'
|
||||||
CloseBuffers()
|
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# ---- gdb started. Next, let's set the MI interface. ---
|
# ---- gdb started. Next, let's set the MI interface. ---
|
||||||
@ -518,7 +508,7 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
success = false
|
success = false
|
||||||
while !success && counter < counter_max
|
while !success && counter < counter_max
|
||||||
if !IsGdbStarted()
|
if !IsGdbStarted()
|
||||||
return
|
return $'{gdbbufname} exited unexpectedly'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
var response = ''
|
var response = ''
|
||||||
@ -529,10 +519,8 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
# response can be in the same line or the next line
|
# response can be in the same line or the next line
|
||||||
response = $"{line1}{line2}"
|
response = $"{line1}{line2}"
|
||||||
if response =~ 'Undefined command'
|
if response =~ 'Undefined command'
|
||||||
Echoerr('Sorry, your gdb is too old, gdb 7.12 is required')
|
|
||||||
# CHECKME: possibly send a "server show version" here
|
# CHECKME: possibly send a "server show version" here
|
||||||
CloseBuffers()
|
return 'Sorry, your gdb is too old, gdb 7.12 is required'
|
||||||
return
|
|
||||||
endif
|
endif
|
||||||
if response =~ 'New UI allocated'
|
if response =~ 'New UI allocated'
|
||||||
# Success!
|
# Success!
|
||||||
@ -551,11 +539,37 @@ def StartDebug_term(dict: dict<any>)
|
|||||||
endwhile
|
endwhile
|
||||||
|
|
||||||
if !success
|
if !success
|
||||||
Echoerr('Cannot check if your gdb works, continuing anyway')
|
return 'Cannot check if your gdb works, continuing anyway'
|
||||||
|
endif
|
||||||
|
return ''
|
||||||
|
enddef
|
||||||
|
|
||||||
|
|
||||||
|
# Open a terminal window without a job, to run the debugged program in.
|
||||||
|
def StartDebug_term(dict: dict<any>)
|
||||||
|
|
||||||
|
var programpty = CreateProgramPty()
|
||||||
|
if programpty is null_string
|
||||||
|
Echoerr('Failed to open the program terminal window')
|
||||||
|
CloseBuffers()
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
job_setoptions(term_getjob(gdbbufnr), {exit_cb: function('EndTermDebug')})
|
var commpty = CreateCommunicationPty()
|
||||||
|
if commpty is null_string
|
||||||
|
Echoerr('Failed to open the communication terminal window')
|
||||||
|
CloseBuffers()
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
var err_message = CreateGdbConsole(dict, programpty, commpty)
|
||||||
|
if !empty(err_message)
|
||||||
|
Echoerr(err_message)
|
||||||
|
CloseBuffers()
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
job_setoptions(term_getjob(gdbbufnr), {exit_cb: function('EndDebug')})
|
||||||
|
|
||||||
# Set the filetype, this can be used to add mappings.
|
# Set the filetype, this can be used to add mappings.
|
||||||
set filetype=termdebug
|
set filetype=termdebug
|
||||||
@ -574,13 +588,13 @@ def StartDebug_prompt(dict: dict<any>)
|
|||||||
new
|
new
|
||||||
endif
|
endif
|
||||||
gdbwin = win_getid()
|
gdbwin = win_getid()
|
||||||
promptbuf = bufnr('')
|
promptbufnr = bufnr('')
|
||||||
prompt_setprompt(promptbuf, 'gdb> ')
|
prompt_setprompt(promptbufnr, 'gdb> ')
|
||||||
set buftype=prompt
|
set buftype=prompt
|
||||||
exe $"file {gdbbufname}"
|
exe $"file {gdbbufname}"
|
||||||
|
|
||||||
prompt_setcallback(promptbuf, function('PromptCallback'))
|
prompt_setcallback(promptbufnr, function('PromptCallback'))
|
||||||
prompt_setinterrupt(promptbuf, function('PromptInterrupt'))
|
prompt_setinterrupt(promptbufnr, function('PromptInterrupt'))
|
||||||
|
|
||||||
if vvertical
|
if vvertical
|
||||||
# Assuming the source code window will get a signcolumn, use two more
|
# Assuming the source code window will get a signcolumn, use two more
|
||||||
@ -607,15 +621,15 @@ def StartDebug_prompt(dict: dict<any>)
|
|||||||
|
|
||||||
ch_log($'executing "{join(gdb_cmd)}"')
|
ch_log($'executing "{join(gdb_cmd)}"')
|
||||||
gdbjob = job_start(gdb_cmd, {
|
gdbjob = job_start(gdb_cmd, {
|
||||||
exit_cb: function('EndPromptDebug'),
|
exit_cb: function('EndDebug'),
|
||||||
out_cb: function('GdbOutCallback'),
|
out_cb: function('GdbOutCallback'),
|
||||||
})
|
})
|
||||||
if job_status(gdbjob) != "run"
|
if job_status(gdbjob) != "run"
|
||||||
Echoerr('Failed to start gdb')
|
Echoerr('Failed to start gdb')
|
||||||
exe $'bwipe! {promptbuf}'
|
exe $'bwipe! {promptbufnr}'
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
exe $'au BufUnload <buffer={promptbuf}> ++once ' ..
|
exe $'au BufUnload <buffer={promptbufnr}> ++once ' ..
|
||||||
'call job_stop(gdbjob, ''kill'')'
|
'call job_stop(gdbjob, ''kill'')'
|
||||||
# Mark the buffer modified so that it's not easy to close.
|
# Mark the buffer modified so that it's not easy to close.
|
||||||
set modified
|
set modified
|
||||||
@ -852,8 +866,6 @@ def DecodeMessage(quotedText: string, literal: bool): string
|
|||||||
#\ Note: GDB docs also mention hex encodings - the translations below work
|
#\ Note: GDB docs also mention hex encodings - the translations below work
|
||||||
#\ but we keep them out for performance-reasons until we actually see
|
#\ but we keep them out for performance-reasons until we actually see
|
||||||
#\ those in mi-returns
|
#\ those in mi-returns
|
||||||
#\ \ ->substitute('\\0x\(\x\x\)', {-> eval('"\x' .. submatch(1) .. '"')}, 'g')
|
|
||||||
#\ \ ->substitute('\\0x00', NullRepl, 'g')
|
|
||||||
->substitute('\\\\', '\', 'g')
|
->substitute('\\\\', '\', 'g')
|
||||||
->substitute(NullRepl, '\\000', 'g')
|
->substitute(NullRepl, '\\000', 'g')
|
||||||
if !literal
|
if !literal
|
||||||
@ -891,28 +903,17 @@ def GetAsmAddr(msg: string): string
|
|||||||
return addr
|
return addr
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def EndDebug(job: any, status: any)
|
||||||
def EndTermDebug(job: any, status: any)
|
|
||||||
if exists('#User#TermdebugStopPre')
|
if exists('#User#TermdebugStopPre')
|
||||||
doauto <nomodeline> User TermdebugStopPre
|
doauto <nomodeline> User TermdebugStopPre
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if commbufnr > 0 && bufexists(commbufnr)
|
if way is Way.Prompt
|
||||||
exe $'bwipe! {commbufnr}'
|
ch_log("Returning from EndDebug()")
|
||||||
endif
|
endif
|
||||||
gdbwin = 0
|
|
||||||
EndDebugCommon()
|
|
||||||
enddef
|
|
||||||
|
|
||||||
def EndDebugCommon()
|
|
||||||
var curwinid = win_getid()
|
var curwinid = win_getid()
|
||||||
var buf_numbers = [ptybufnr, asmbufnr, varbufnr]
|
CloseBuffers()
|
||||||
for buf_nr in buf_numbers
|
|
||||||
if buf_nr > 0 && bufexists(buf_nr)
|
|
||||||
exe $'bwipe! {buf_nr}'
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
running = false
|
|
||||||
|
|
||||||
# Restore 'signcolumn' in all buffers for which it was set.
|
# Restore 'signcolumn' in all buffers for which it was set.
|
||||||
win_gotoid(sourcewin)
|
win_gotoid(sourcewin)
|
||||||
@ -954,21 +955,6 @@ def EndDebugCommon()
|
|||||||
g:termdebug_is_running = false
|
g:termdebug_is_running = false
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def EndPromptDebug(job: any, status: any)
|
|
||||||
if exists('#User#TermdebugStopPre')
|
|
||||||
doauto <nomodeline> User TermdebugStopPre
|
|
||||||
endif
|
|
||||||
|
|
||||||
if bufexists(promptbuf)
|
|
||||||
exe $'bwipe! {promptbuf}'
|
|
||||||
endif
|
|
||||||
|
|
||||||
EndDebugCommon()
|
|
||||||
gdbwin = 0
|
|
||||||
ch_log("Returning from EndPromptDebug()")
|
|
||||||
enddef
|
|
||||||
|
|
||||||
|
|
||||||
# Disassembly window - added by Michael Sartain
|
# Disassembly window - added by Michael Sartain
|
||||||
#
|
#
|
||||||
# - CommOutput: &"disassemble $pc\n"
|
# - CommOutput: &"disassemble $pc\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user