Reduce unnecessary `monitor` called if possible

pull/1445/head
lebr0nli 3 years ago committed by Disconnect3d
parent 416bd4726c
commit 1e32ff1fbb

@ -25,24 +25,35 @@ def is_debug_probe():
Returns True if the target is a debug probe for an embedded device.
Currently detects the Black Magic Probe and the SEGGER J-Link GDB Server.
"""
# See https://github.com/pwndbg/pwndbg/pull/1439#issuecomment-1348477915 for the reason why we check the ouput like this.
try:
help_output = gdb.execute("monitor help", to_string=True)
if (
"GDBserver" in help_output
and "Black Magic Probe" not in help_output
and "SEGGER J-Link GDB Server" not in help_output
):
if "Quit GDBserver\n" in help_output:
# We can't use the `monitor` command directly when using normal GDBserver because it can cause GDBserver somehow show an additional newline in the end and fail to show the context because `pwndbg.gdblib.proc.thread_is_stopped` is False when running `gdb.prompt_hook`.
# To avoid this issue, we can check the output of `monitor help` to determine if we're using GDBserver.
# The output on normal GDBserver looks like this:
# pwndbg> monitor help
# The following monitor commands are supported:
# set debug <0|1>
# Enable general debugging messages
# set debug-hw-points <0|1>
# Enable h/w breakpoint/watchpoint debugging messages
# set remote-debug <0|1>
# Enable remote protocol debugging messages
# set event-loop-debug <0|1>
# Enable event loop debugging messages
# set debug-format option1[,option2,...]
# Add additional information to debugging messages
# Options: all, none, timestamp
# exit
# Quit GDBserver
# TODO/FIXME: Investigate the cause of this problem and fix it properly.
# TODO/FIXME: Determine if this issue only occurs with normal GDBserver and find a better way to check the remote server if necessary.
return False
except gdb.error:
# Now we check if we are using Black Magic Probe or the SEGGER J-Link GDB Server
pass
try:
elif "SEGGER J-Link GDB Server" in help_output:
return True
monitor_output = gdb.execute("monitor", to_string=True)
return "Black Magic Probe" in monitor_output or "SEGGER J-Link GDB Server" in monitor_output
except gdb.error:
# the monitor command might fail, but we don't care since it doesn't fail on the devices we check for.
# SEGGER J-Link GDB Server and the Black Magic Probe should support the `monitor help` and `monitor` commands.
return False
return "Black Magic Probe" in monitor_output or "SEGGER J-Link GDB Server" in monitor_output

Loading…
Cancel
Save