Skip to content

Commit

Permalink
GDB pretty printer for ARM (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
louiswilliams authored and beef9999 committed Nov 20, 2023
1 parent 0a012f6 commit 31d1ad6
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions tools/photongdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ class bcolors:
enabling = False
photon = []

def get_arch():
frame = gdb.selected_frame()
arch = frame.architecture()
return arch.name()

def get_regs(arch):
regs = {}
if arch == 'aarch64':
regs['sp'] = '$sp'
regs['bp'] = '$x29'
regs['ip'] = '$pc'
else:
regs['sp'] = '$rsp'
regs['bp'] = '$rbp'
regs['ip'] = '$rip'

return regs

def cprint(stat, *args):
print('{}{}{} {}'.format(CMAP[stat], stat, bcolors.ENDC,
Expand Down Expand Up @@ -55,11 +72,11 @@ def in_sleep(q):
return [(q['_M_impl']['_M_start'][i]) for i in range(size)]


def switch_to_ph(rsp, rbp, rip):
def switch_to_ph(regs, rsp, rbp, rip):
cprint('SWITCH', "to {} {} {}".format(hex(rsp), hex(rbp), hex(rip)))
gdb.parse_and_eval("$rsp={}".format(rsp))
gdb.parse_and_eval("$rbp={}".format(rbp))
gdb.parse_and_eval("$rip={}".format(rip))
gdb.parse_and_eval("{}={}".format(regs['sp'], rsp))
gdb.parse_and_eval("{}={}".format(regs['bp'], rbp))
gdb.parse_and_eval("{}={}".format(regs['ip'], rip))


def get_u64_ptr(p):
Expand Down Expand Up @@ -143,14 +160,20 @@ def invoke(self, arg, tty):
if i < 0 or i > len(photon):
print("No such photon thread")
return
switch_to_ph(photon[i][2], photon[i][3], photon[i][4])

arch = get_arch()
regs = get_regs(arch)
switch_to_ph(regs, photon[i][2], photon[i][3], photon[i][4])


def photon_init():
global photon
set_u64_reg('$saved_rsp', '$rsp')
set_u64_reg('$saved_rbp', '$rbp')
set_u64_reg('$saved_rip', '$rip')

arch = get_arch()
regs = get_regs(arch)
set_u64_reg('$saved_rsp', regs['sp'])
set_u64_reg('$saved_rbp', regs['bp'])
set_u64_reg('$saved_rip', regs['ip'])
load_photon_threads()
if len(photon) == 0:
return
Expand All @@ -171,9 +194,11 @@ def invoke(self, arg, tty):
def photon_restore():
if not enabling:
return
set_u64_reg('$rsp', '$saved_rsp')
set_u64_reg('$rbp', '$saved_rbp')
set_u64_reg('$rip', '$saved_rip')
arch = get_arch()
regs = get_regs(arch)
set_u64_reg(regs['sp'], '$saved_rsp')
set_u64_reg(regs['bp'], '$saved_rbp')
set_u64_reg(regs['ip'], '$saved_rip')


class PhotonRestore(gdb.Command):
Expand Down Expand Up @@ -208,4 +233,4 @@ def invoke(self, arg, tty):
PhotonLs()
PhotonFr()

cprint('INFO', 'Photon-GDB-extension loaded')
cprint('INFO', 'Photon-GDB-extension loaded')

0 comments on commit 31d1ad6

Please sign in to comment.