neovim_gdb.vim 8.9 KB

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