Skip to content

Instantly share code, notes, and snippets.

@Mirenk
Created July 8, 2022 14:09
Show Gist options
  • Save Mirenk/3068f4787c434a9d5755a5fecea96064 to your computer and use it in GitHub Desktop.
Save Mirenk/3068f4787c434a9d5755a5fecea96064 to your computer and use it in GitHub Desktop.
gdb extention prototype
import gdb
import pprint
# 継承でコマンド作成
# invokeで呼び出し時の動作を定義
class Test(gdb.Command):
def __init__(self):
gdb.Command.__init__(self, 'test-cmd', gdb.COMMAND_USER)
def invoke(self, args, from_tty):
frame = gdb.newest_frame()
while frame:
decorator = gdb.FrameDecorator.FrameDecorator(frame)
addr_str = "0x{:016x}".format(frame.pc())
#print("Addr:", addr_str, "func:", frame.name())
frame_args = decorator.frame_args()
print('')
print("-----args")
print('')
for i in frame_args:
value = i.sym.value(frame)
print(i.sym, ' = ', value)
print(' Address: ', value.address)
frame_locals = decorator.frame_locals()
print('')
print("-----locals")
print('')
i = 1
for local_var in frame_locals:
value = local_var.sym.value(frame)
print("\033[3" + str(i) + 'm', end='')
print(local_var.sym, ' = ', value, end='')
print("\033[0m")
print(" type: ", value.type)
print(" sizeof: ", value.type.sizeof)
print(' Address: ', value.address.format_string(format='x'))
i += 1
self.print_stack(frame, frame_locals)
break
#frame = frame.older()
def read(self, addr, length):
return gdb.selected_inferior().read_memory(addr, length).tobytes()
def print_stack(self, frame, frame_locals):
def color_start(color_index):
print('\033[3' + str(local_var_index) + 'm', end='')
def color_end():
print("\033[0m", end='')
rbp_reg = int(gdb.parse_and_eval('$rbp').format_string(format='d'))
rsp_reg = int(gdb.parse_and_eval('$rsp').format_string(format='d'))
stack_size = rbp_reg - rsp_reg + 16
stack = self.read(rsp_reg, stack_size)
local_var_addr = [(int(val.sym.value(frame).address.format_string(format='d')), int(val.sym.value(frame).type.sizeof)) for val in frame_locals]
# スタック表示開始
print('')
print("-----Stack")
print('')
print("Addr | 00 01 02 03 04 05 06 07")
print("===================+========================")
color_index = 0
for i in range(stack_size//8):
if i == 0:
print("--------------------------------------------", end='')
print(" <- rsp(", '0x{:016x}'.format(rsp_reg), ')')
elif i == (rbp_reg - rsp_reg)//8:
print("--------------------------------------------", end='')
print(" <- rbp(", '0x{:016x}'.format(rbp_reg), ')')
cur_addr = rsp_reg + i*8
print("0x{:016x}".format(cur_addr), "|", end=" ")
byte_index = 0
if color_index != 0:
color_start(color_index)
for b in stack[i*8:i*8+8]:
# 色付け終了
if color_index != 0:
addr, alignof = local_var_addr[color_index-1]
if (cur_addr + byte_index) == addr + alignof:
color_end()
color_index = 0
# 色付け開始
if color_index == 0:
local_var_index = 1
for addr, _ in local_var_addr:
if (cur_addr + byte_index) == addr:
color_index = local_var_index
color_start(color_index)
break
local_var_index += 1
print("{:02x} ".format(b), end="")
byte_index += 1
if color_index != 0:
color_end()
print('')
# インスタンス作成で登録
Test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment