robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8ef41f5902
master
1sign defineGdbBreakpoint text= ● 2sign defineGdbCurrentLine text= ⇒ 3 4 5let s: gdb_port = 7778 6let s: run_gdb = "gdb -q -f build/bin/nvim" 7let s: breakpoints = {} 8let s: max_breakpoint_sign_id = 0 9 10 11let s: GdbServer = {} 12 13 14function s: GdbServer . new ( gdb ) 15let this = copy ( self ) 16let this . _gdb = a: gdb 17return this 18endfunction 19 20 21function s: GdbServer . on_exit () 22let self . _gdb . _server_exited = 1 23endfunction 24 25 26let s: GdbPaused = vimexpect#State ([ 27 \[ 'Continuing.' , 'continue' ], 28 \[ '\v[\o32]{2}([^:]+):(\d+):\d+' , 'jump' ], 29 \[ 'Remote communication error. Target disconnected.:' , 'retry' ], 30 \]) 31 32 33function s: GdbPaused . continue (...) 34call self . _parser . switch ( s: GdbRunning ) 35call self . update_current_line_sign ( 0 ) 36endfunction 37 38 39function s: GdbPaused . jump ( file , line , ...) 40if tabpagenr () != self . _tab 41" Don't jump if we are not in the debugger tab 42return 43endif 44let window = winnr () 45exe self . _jump_window 'wincmd w' 46let self . _current_buf = bufnr ( '%' ) 47let target_buf = bufnr ( a: file , 1 ) 48if bufnr ( '%' ) != target_buf 49exe 'buffer ' target_buf 50let self . _current_buf = target_buf 51endif 52exe ':' a: line 53let self . _current_line = a: line 54exe window 'wincmd w' 55call self . update_current_line_sign ( 1 ) 56endfunction 57 58 59function s: GdbPaused . retry (...) 60if self . _server_exited 61return 62endif 63sleep 1 64call self . attach () 65call self . send ( 'continue' ) 66endfunction 67 68 69let s: GdbRunning = vimexpect#State ([ 70 \[ '\v^Breakpoint \d+' , 'pause' ], 71 \[ '\v\[Inferior\ +.{-}\ +exited\ +normally' , 'disconnected' ], 72 \[ '(gdb)' , 'pause' ], 73 \]) 74 75 76function s: GdbRunning . pause (...) 77call self . _parser . switch ( s: GdbPaused ) 78if ! self . _initialized 79call self . send ( 'set confirm off' ) 80call self . send ( 'set pagination off' ) 81if ! empty ( self . _server_addr ) 82call self . send ( 'set remotetimeout 50' ) 83call self . attach () 84call s: RefreshBreakpoints () 85call self . send ( 'c' ) 86endif 87let self . _initialized = 1 88endif 89endfunction 90 91 92function s: GdbRunning . disconnected (...) 93if ! self . _server_exited && self . _reconnect 94" Refresh to force a delete of all watchpoints 95call s: RefreshBreakpoints () 96sleep 1 97call self . attach () 98call self . send ( 'continue' ) 99endif 100endfunction 101 102 103let s: Gdb = {} 104 105 106function s: Gdb . kill () 107tunmap <f8> 108tunmap <f10> 109tunmap <f11> 110tunmap <f12> 111call self . update_current_line_sign ( 0 ) 112exe 'bd! ' . self . _client_buf 113if self . _server_buf != -1 114exe 'bd! ' . self . _server_buf 115endif 116exe 'tabnext ' . self . _tab 117tabclose 118unlet g: gdb 119endfunction 120 121 122function ! s: Gdb . send ( data ) 123call jobsend ( self . _client_id , a: data . "\<cr>" ) 124endfunction 125 126 127function ! s: Gdb . attach () 128call self . send ( printf ( 'target remote %s' , self . _server_addr )) 129endfunction 130 131 132function ! s: Gdb . update_current_line_sign ( add ) 133" to avoid flicker when removing/adding the sign column(due to the change in 134" line width), we switch ids for the line sign and only remove the old line 135" sign after marking the new one 136let old_line_sign_id = get ( self , '_line_sign_id' , 4999 ) 137let self . _line_sign_id = old_line_sign_id == 4999 ? 4998 : 4999 138if a: add && self . _current_line != -1 && self . _current_buf != -1 139exe 'sign place ' . self . _line_sign_id . ' name=GdbCurrentLine line=' 140 \. self . _current_line . ' buffer=' . self . _current_buf 141endif 142exe 'sign unplace ' . old_line_sign_id 143endfunction 144 145 146function ! s: Spawn ( server_cmd , client_cmd , server_addr , reconnect ) 147if exists ( 'g:gdb' ) 148throw 'Gdb already running' 149endif 150let gdb = vimexpect#Parser ( s: GdbRunning , copy ( s: Gdb )) 151" gdbserver port 152let gdb . _server_addr = a: server_addr 153let gdb . _reconnect = a: reconnect 154let gdb . _initialized = 0 155" window number that will be displaying the current file 156let gdb . _jump_window = 1 157let gdb . _current_buf = -1 158let gdb . _current_line = -1 159let gdb . _has_breakpoints = 0 160let gdb . _server_exited = 0 161" Create new tab for the debugging view 162tabnew 163let gdb . _tab = tabpagenr () 164" create horizontal split to display the current file and maybe gdbserver 165sp 166let gdb . _server_buf = -1 167if type ( a: server_cmd ) == type ( '' ) 168" spawn gdbserver in a vertical split 169let server = s: GdbServer . new ( gdb ) 170server .term = v:true 171vsp |enew |let gdb . _server_id = jobstart ( a: server_cmd , server ) 172let gdb . _jump_window = 2 173let gdb . _server_buf = bufnr ( '%' ) 174endif 175" go to the bottom window and spawn gdb client 176wincmd j 177gdb .term = v:true 178enew |let gdb . _client_id = jobstart ( a: client_cmd , gdb ) 179let gdb . _client_buf = bufnr ( '%' ) 180tnoremap <silent> < f8 > < c-\ > <c-n > :GdbContinue< cr > i 181tnoremap <silent> < f10 > < c-\ > <c-n > :GdbNext< cr > i 182tnoremap <silent> < f11 > < c-\ > <c-n > :GdbStep< cr > i 183tnoremap <silent> < f12 > < c-\ > <c-n > :GdbFinish< cr > i 184" go to the window that displays the current file 185exe gdb . _jump_window 'wincmd w' 186let g: gdb = gdb 187endfunction 188 189 190function ! s: Test ( bang , filter ) 191let cmd = "GDB=1 make test" 192if a: bang == '!' 193let server_addr = '| vgdb' 194let cmd = printf ( 'VALGRIND=1 %s' , cmd ) 195else 196let server_addr = printf ( 'localhost:%d' , s: gdb_port ) 197let cmd = printf ( 'GDBSERVER_PORT=%d %s' , s: gdb_port , cmd ) 198endif 199if a: filter != '' 200let cmd = printf ( 'TEST_SCREEN_TIMEOUT=1000000 TEST_FILTER="%s" %s' , a: filter , cmd ) 201endif 202call s: Spawn ( cmd , s: run_gdb , server_addr , 1 ) 203endfunction 204 205 206function ! s: ToggleBreak () 207let file_name = bufname ( '%' ) 208let file_breakpoints = get ( s: breakpoints , file_name , {}) 209let linenr = line ( '.' ) 210if has_key ( file_breakpoints , linenr ) 211call remove ( file_breakpoints , linenr ) 212else 213let file_breakpoints [ linenr ] = 1 214endif 215let s: breakpoints [ file_name ] = file_breakpoints 216call s: RefreshBreakpointSigns () 217call s: RefreshBreakpoints () 218endfunction 219 220 221function ! s: ClearBreak () 222let s: breakpoints = {} 223call s: RefreshBreakpointSigns () 224call s: RefreshBreakpoints () 225endfunction 226 227 228function ! s: RefreshBreakpointSigns () 229let buf = bufnr ( '%' ) 230let i = 5000 231while i <= s: max_breakpoint_sign_id 232exe 'sign unplace ' . i 233let i += 1 234endwhile 235let s: max_breakpoint_sign_id = 0 236let id = 5000 237for linenr in keys ( get ( s: breakpoints , bufname ( '%' ), {})) 238exe 'sign place ' . id . ' name=GdbBreakpoint line=' . linenr . ' buffer=' . buf 239let s: max_breakpoint_sign_id = id 240let id += 1 241endfor 242endfunction 243 244 245function ! s: RefreshBreakpoints () 246if ! exists ( 'g:gdb' ) 247return 248endif 249if g: gdb . _parser . state () == s: GdbRunning 250" pause first 251call jobsend ( g: gdb . _client_id , "\<c-c>" ) 252endif 253if g: gdb . _has_breakpoints 254call g: gdb . send ( 'delete' ) 255endif 256let g: gdb . _has_breakpoints = 0 257for [ file , breakpoints ] in items ( s: breakpoints ) 258for linenr in keys ( breakpoints ) 259let g: gdb . _has_breakpoints = 1 260call g: gdb . send ( 'break ' . file . ':' . linenr ) 261endfor 262endfor 263endfunction 264 265 266function ! s: GetExpression (...) range 267let [ lnum1 , col1 ] = getpos ( "'<" )[ 1 : 2 ] 268let [ lnum2 , col2 ] = getpos ( "'>" )[ 1 : 2 ] 269let lines = getline ( lnum1 , lnum2 ) 270let lines [ -1 ] = lines [ -1 ][: col2 - 1 ] 271let lines [ 0 ] = lines [ 0 ][ col1 - 1 :] 272return join ( lines , "\n" ) 273endfunction 274 275 276function ! s: Send ( data ) 277if ! exists ( 'g:gdb' ) 278throw 'Gdb is not running' 279endif 280call g: gdb . send ( a: data ) 281endfunction 282 283 284function ! s: Eval ( expr ) 285call s: Send ( printf ( 'print %s' , a: expr )) 286endfunction 287 288 289function ! s: Watch ( expr ) 290let expr = a: expr 291if expr [ 0 ] != '&' 292let expr = '&' . expr 293endif 294 295call s: Eval ( expr ) 296call s: Send ( 'watch *$' ) 297endfunction 298 299 300function ! s: Interrupt () 301if ! exists ( 'g:gdb' ) 302throw 'Gdb is not running' 303endif 304call jobsend ( g: gdb . _client_id , "\<c-c>info line\<cr>" ) 305endfunction 306 307 308function ! s: Kill () 309if ! exists ( 'g:gdb' ) 310throw 'Gdb is not running' 311endif 312call g: gdb . kill () 313endfunction 314 315 316command ! GdbDebugNvim call s:Spawn(printf('make && gdbserver localhost:%d build/bin/nvim', s:gdb_port), s:run_gdb, printf('localhost:%d', s:gdb_port), 0) 317command ! -nargs = 1 GdbDebugServer call s:Spawn(0, s:run_gdb, 'localhost:'.<q-args>, 0) 318command ! -bang -nargs = ? GdbDebugTest call s:Test(<q-bang>, <q-args>) 319command ! -nargs = 1 -complete = file GdbInspectCore call s:Spawn(0, printf('gdb -q -f -c %s build/bin/nvim', <q-args>), 0, 0) 320command ! GdbDebugStop call s:Kill() 321command ! GdbToggleBreakpoint call s:ToggleBreak() 322command ! GdbClearBreakpoints call s:ClearBreak() 323command ! GdbContinue call s:Send("c") 324command ! GdbNext call s:Send("n") 325command ! GdbStep call s:Send("s") 326command ! GdbFinish call s:Send("finish") 327command ! GdbFrameUp call s:Send("up") 328command ! GdbFrameDown call s:Send("down") 329command ! GdbInterrupt call s:Interrupt() 330command ! GdbEvalWord call s:Eval(expand('<cword>')) 331command ! -range GdbEvalRange call s:Eval(s:GetExpression(<f-args>)) 332command ! GdbWatchWord call s:Watch(expand('<cword>') 333command ! -range GdbWatchRange call s:Watch(s:GetExpression(<f-args>)) 334 335 336nnoremap <silent> < f8 > :GdbContinue< cr > 337nnoremap <silent> < f10 > :GdbNext< cr > 338nnoremap <silent> < f11 > :GdbStep< cr > 339nnoremap <silent> < f12 > :GdbFinish< cr > 340nnoremap <silent> < c-b > :GdbToggleBreakpoint< cr > 341nnoremap <silent> < m-pageup > :GdbFrameUp< cr > 342nnoremap <silent> < m-pagedown > :GdbFrameDown< cr > 343nnoremap <silent> < f9 > :GdbEvalWord< cr > 344vnoremap <silent> < f9 > :GdbEvalRange< cr > 345nnoremap <silent> < m-f9 > :GdbWatchWord< cr > 346vnoremap <silent> < m-f9 > :GdbWatchRange< cr >