Skip to content

Commit

Permalink
GP-4750: from review
Browse files Browse the repository at this point in the history
GP-4750: typo
GP-4750: missed one
GP-4750: backing out gdb.execute conditions
GP-4750: unnecessary change in hooks
GP-4750: unnecessary change in arch
GP-4750: unnecessary change in util
GP-4750: testing for cmds
GP-4750: first pass
  • Loading branch information
d-millar committed Jul 8, 2024
1 parent 5ab72bf commit 25e245e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ def putreg(frame, reg_descs):
cobj.insert()
mapper = STATE.trace.register_mapper
values = []
# NB: This command will fail if the process is running
endian = arch.get_endian()
for desc in reg_descs:
v = frame.read_register(desc.name)
Expand All @@ -616,7 +617,7 @@ def ghidra_trace_putreg(group='all', *, is_mi, **kwargs):
"""

STATE.require_tx()
frame = gdb.selected_frame()
frame = util.selected_frame()
with STATE.client.batch() as b:
return putreg(frame, util.get_register_descs(frame.architecture(), group))

Expand All @@ -631,7 +632,7 @@ def ghidra_trace_delreg(group='all', *, is_mi, **kwargs):

STATE.require_tx()
inf = gdb.selected_inferior()
frame = gdb.selected_frame()
frame = util.selected_frame()
space = 'Inferiors[{}].Threads[{}].Stack[{}].Registers'.format(
inf.num, gdb.selected_thread().num, util.get_level(frame)
)
Expand Down Expand Up @@ -957,9 +958,10 @@ def activate(path=None):
if t is None:
path = INFERIOR_PATTERN.format(infnum=inf.num)
else:
frame = gdb.selected_frame()
path = FRAME_PATTERN.format(
infnum=inf.num, tnum=t.num, level=util.get_level(frame))
frame = util.selected_frame()
if frame is not None:
path = FRAME_PATTERN.format(
infnum=inf.num, tnum=t.num, level=util.get_level(frame))
trace.proxy_object_path(path).activate()


Expand Down Expand Up @@ -1329,7 +1331,7 @@ def convert_tid(t):

@contextmanager
def restore_frame():
f = gdb.selected_frame()
f = util.selected_frame()
yield
f.select()

Expand Down Expand Up @@ -1400,8 +1402,12 @@ def put_frames():
t = gdb.selected_thread()
if t is None:
return
# NB: This command will fail if the process is running
bt = gdb.execute('bt', to_string=True).strip().split('\n')
f = newest_frame(gdb.selected_frame())
f = util.selected_frame()
if f is None:
return
f = newest_frame(f)
keys = []
level = 0
while f is not None:
Expand Down
30 changes: 27 additions & 3 deletions Ghidra/Debug/Debugger-agent-gdb/src/main/py/src/ghidragdb/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ def __init__(self):


class HookState(object):
__slots__ = ('installed', 'mem_catchpoint', 'batch', 'skip_continue')
__slots__ = ('installed', 'mem_catchpoint', 'batch', 'skip_continue', 'in_break_w_cont')

def __init__(self):
self.installed = False
self.mem_catchpoint = None
self.batch = None
self.skip_continue = False
self.in_break_w_cont = False

def ensure_batch(self):
if self.batch is None:
Expand Down Expand Up @@ -86,9 +87,12 @@ def record(self, description=None):
thread = gdb.selected_thread()
if thread is not None:
if first or thread not in self.visited:
# NB: This command will fail if the process is running
commands.put_frames()
self.visited.add(thread)
frame = gdb.selected_frame()
frame = util.selected_frame()
if frame is None:
return
hashable_frame = (thread, util.get_level(frame))
if first or hashable_frame not in self.visited:
commands.putreg(
Expand All @@ -102,6 +106,7 @@ def record(self, description=None):
except MemoryError as e:
print(f"Couldn't record page with SP: {e}")
self.visited.add(hashable_frame)
# NB: These commands (put_modules/put_regions) will fail if the process is running
if first or self.regions or self.threads or self.modules:
# Sections, memory syscalls, or stack allocations
commands.put_modules()
Expand Down Expand Up @@ -238,7 +243,9 @@ def on_frame_selected():
if trace is None:
return
t = gdb.selected_thread()
f = gdb.selected_frame()
f = util.selected_frame()
if f is None:
return
HOOK_STATE.ensure_batch()
with trace.open_tx("Frame {}.{}.{} selected".format(inf.num, t.num, util.get_level(f))):
INF_STATES[inf.num].record()
Expand Down Expand Up @@ -299,11 +306,28 @@ def on_cont(event):
state.record_continued()


def check_for_continue(event):
if hasattr(event, 'breakpoints'):
if HOOK_STATE.in_break_w_cont:
return True
for brk in event.breakpoints:
if hasattr(brk, 'commands'):
for cmd in brk.commands:
if cmd == 'c' or cmd.startswith('cont'):
HOOK_STATE.in_break_w_cont = True
return True
HOOK_STATE.in_break_w_cont = False
return False


@log_errors
def on_stop(event):
if hasattr(event, 'breakpoints') and HOOK_STATE.mem_catchpoint in event.breakpoints:
HOOK_STATE.skip_continue = True
return
if check_for_continue(event):
HOOK_STATE.skip_continue = True
return
inf = gdb.selected_inferior()
if inf.num not in INF_STATES:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ def find_thread_by_stack_obj(object):
def find_frame_by_level(thread, level):
# Because threads don't have any attribute to get at frames
thread.switch()
f = gdb.selected_frame()
f = util.selected_frame()
if f is None:
return None

# Navigate up or down, because I can't just get by level
down = level - util.get_level(f)
Expand Down Expand Up @@ -340,7 +342,10 @@ def refresh_stack(node: sch.Schema('Stack')):
@REGISTRY.method(action='refresh', display='Refresh Registers')
def refresh_registers(node: sch.Schema('RegisterValueContainer')):
"""Refresh the register values for the frame."""
find_frame_by_regs_obj(node).select()
f = find_frame_by_regs_obj(node)
if f is None:
return
f.select()
# TODO: Groups?
with commands.open_tracked_tx('Refresh Registers'):
gdb.execute('ghidra trace putreg')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,10 @@ def get_register_descs(arch, group='all'):
tokens = line.strip().split()
descs.append(RegisterDesc(tokens[0]))
return descs

def selected_frame():
try:
return gdb.selected_frame()
except Exception as e:
print("No selected frame")
return None

0 comments on commit 25e245e

Please sign in to comment.