Skip to content

Commit

Permalink
Query process state from GDB via the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sakhnik committed Aug 3, 2023
1 parent 00b0d6a commit 09c92da
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
- Port vimscript to lua: nvimgdb.vim
- Detect whether the debugged process is running using side channel: prompt -> paused, query on output feed timeout (test_interrupt, test_command)
- handle <up arrow> in pywinpty PDB
- Press any key to exit: need to unblock msvcrt.getch() somehow
- cmake test in win32?
Expand Down
8 changes: 1 addition & 7 deletions doc/nvimgdb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,7 @@ Section 7.3 PDB, BashDB *NvimgdbPDB* *NvimgdbBashDB*
==============================================================================
Section 8: Limitations *NvimgdbLimitations*

- The plugin is sensitive to the debugger settings. If prompt or frame format
is changed, random errors may occur.

- LLDB uses the same frame format in multiple different situations. The
command "thread backtrace", for instance, will cause jumps in the source
code. Workaround: execute "frame info" in the debugger or
`:GdbFrameUp` followed by `:GdbFrameDown`.
- ???

==============================================================================
Section 9: Development *NvimgdbDevelopment*
Expand Down
16 changes: 16 additions & 0 deletions lib/gdb_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def _handle_command(self, command, sock, addr):
fname = args[0]
self._send_response(self._get_breaks(os.path.normpath(fname)),
req_id, sock, addr)
elif request == "get-process-state":
self._send_response(self._get_process_state(),
req_id, sock, addr)
elif request == "get-current-frame-location":
self._send_response(self._get_current_frame_location(),
req_id, sock, addr)
Expand Down Expand Up @@ -104,6 +107,19 @@ def _send_response(self, response, req_id, sock, addr):
logger.debug("Sending response: %s", response_json)
sock.sendto(response_json, 0, addr)

def _get_process_state(self):
try:
inferior = gdb.selected_inferior()
if inferior.is_valid():
threads = inferior.threads()
is_running = any((t.is_valid() and t.is_running()
for t in threads))
state = "running" if is_running else "stopped"
return state
except gdb.error:
...
return "other"

def _get_current_frame_location(self):
try:
frame = gdb.selected_frame()
Expand Down
29 changes: 14 additions & 15 deletions lua/nvimgdb/backend/gdb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ function C.create_parser(actions, proxy)

function P:query_paused()
coroutine.resume(coroutine.create(function()
local location = proxy:query('get-current-frame-location')
log.debug({"current frame location", location})
if #location == 2 then
local fname = location[1]
local line = location[2]
self.actions:jump_to_source(fname, line)
local process_state = proxy:query('get-process-state')
log.debug({"process state", process_state})
if process_state == 'stopped' then
local location = proxy:query('get-current-frame-location')
log.debug({"current frame location", location})
if #location == 2 then
local fname = location[1]
local line = location[2]
self.actions:jump_to_source(fname, line)
end
end
end))

coroutine.resume(coroutine.create(function()
self.actions:query_breakpoints()
self.state = process_state == 'running' and self.running or self.paused
end))

return self.paused
-- Don't change the state yet
return self.state
end

local re_prompt = '%(gdb%) \x1a\x1a\x1a'
local re_prompt = '$'
self.add_trans(self.paused, '[\r\n]Continuing%.', self._paused_continue)
self.add_trans(self.paused, '[\r\n]Starting program:', self._paused_continue)
self.add_trans(self.paused, re_prompt, self.query_paused)
self.add_trans(self.running, '%sBreakpoint %d+', self.query_paused)
self.add_trans(self.running, '%sTemporary breakpoint %d+', self.query_paused)
self.add_trans(self.running, re_prompt, self.query_paused)

self.state = self.running
Expand Down Expand Up @@ -116,7 +116,6 @@ function C.get_launch_cmd(client_cmd, tmp_dir, proxy_addr)
file:write([[
set confirm off
set pagination off
python gdb.prompt_hook = lambda p: p + ("" if p.endswith("\x01\x1a\x1a\x1a\x02") else "\x01\x1a\x1a\x1a\x02")
]])
file:write("source " .. utils.get_plugin_file_path("lib", "gdb_commands.py") .. "\n")
file:write("nvim-gdb-init " .. proxy_addr .. "\n")
Expand Down

0 comments on commit 09c92da

Please sign in to comment.